add way to add/remove moderators from the message click handler

pull/1445/head
Audric Ackermann 4 years ago
parent 5c3cb0a165
commit ce8bc84de0
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -2221,5 +2221,11 @@
},
"userRemovedFromModerators": {
"message": "User removed from moderator list"
},
"errorHappenedWhileRemovingModerator": {
"message": "An error happened"
},
"errorHappenedWhileRemovingModeratorDesc": {
"message": "An error happened while removing this user from the moderator list."
}
}

@ -1560,9 +1560,11 @@
const profileName = this.getProfileName();
const number = this.getNumber();
let name;
if (window.shortenPubkey) {
if (window.libsession) {
name = profileName
? `${profileName} (${window.shortenPubkey(number)})`
? `${profileName} (${window.libsession.Types.PubKey.shorten(
number
)})`
: number;
} else {
name = profileName ? `${profileName} (${number})` : number;
@ -1589,7 +1591,7 @@
if (pubkey === textsecure.storage.user.getNumber()) {
return i18n('you');
}
return profileName || window.shortenPubkey(pubkey);
return profileName || window.libsession.Types.PubKey.shorten(pubkey);
},
/**

@ -440,14 +440,6 @@ if (process.env.USE_STUBBED_NETWORK) {
window.SwarmPolling = new SwarmPolling();
}
window.shortenPubkey = pubkey => {
const pk = pubkey.key ? pubkey.key : pubkey;
return `(...${pk.substring(pk.length - 6)})`;
};
window.pubkeyPattern = /@[a-fA-F0-9]{64,66}\b/g;
window.lokiFeatureFlags = {
multiDeviceUnpairing: true,
privateGroupChats: true,

@ -66,7 +66,7 @@ export class AddMentions extends React.Component<Props> {
public render() {
const { text, renderOther, convoId } = this.props;
const results: Array<any> = [];
const FIND_MENTIONS = window.pubkeyPattern;
const FIND_MENTIONS = new RegExp(`@${PubKey.regexForPubkeys}`, 'g');
// We have to do this, because renderNonNewLine is not required in our Props object,
// but it is always provided via defaultProps.

@ -39,6 +39,8 @@ import { withTheme } from 'styled-components';
import { MessageMetadata } from './message/MessageMetadata';
import { MessageRegularProps } from '../../../js/models/messages';
import { PubKey } from '../../session/types';
import { ToastUtils } from '../../session/utils';
import { ConversationController } from '../../session/conversations';
// Same as MIN_WIDTH in ImageGrid.tsx
const MINIMUM_LINK_PREVIEW_IMAGE_WIDTH = 200;
@ -65,6 +67,8 @@ class MessageInner extends React.PureComponent<MessageRegularProps, State> {
this.handleImageErrorBound = this.handleImageError.bind(this);
this.onReplyPrivate = this.onReplyPrivate.bind(this);
this.handleContextMenu = this.handleContextMenu.bind(this);
this.onAddModerator = this.onAddModerator.bind(this);
this.onRemoveFromModerator = this.onRemoveFromModerator.bind(this);
this.state = {
expiring: false,
@ -688,12 +692,16 @@ class MessageInner extends React.PureComponent<MessageRegularProps, State> {
{weAreAdmin && isPublic ? (
<Item onClick={onBanUser}>{window.i18n('banUser')}</Item>
) : null}
{/* {weAreAdmin && isPublic && !isAdmin ? (
<Item onClick={onRemoveFromModerator}>{window.i18n('addAsModerator')}</Item>
{weAreAdmin && isPublic && !isAdmin ? (
<Item onClick={this.onAddModerator}>
{window.i18n('addAsModerator')}
</Item>
) : null}
{weAreAdmin && isPublic && isAdmin ? (
<Item onClick={onAddModerator}>{window.i18n('removeFromModerators')}</Item>
) : null} */}
<Item onClick={this.onRemoveFromModerator}>
{window.i18n('removeFromModerators')}
</Item>
) : null}
</Menu>
);
}
@ -788,7 +796,8 @@ class MessageInner extends React.PureComponent<MessageRegularProps, State> {
// We parse the message later, but we still need to do an early check
// to see if the message mentions us, so we can display the entire
// message differently
const mentions = text ? text.match(window.pubkeyPattern) : [];
const regex = new RegExp(`@${PubKey.regexForPubkeys}`, 'g');
const mentions = text ? text.match(regex) : [];
const mentionMe =
mentions &&
mentions.some(m => m.slice(1) === window.lokiPublicChatAPI.ourKey);
@ -969,6 +978,48 @@ class MessageInner extends React.PureComponent<MessageRegularProps, State> {
this.props.onReply(this.props.timestamp);
}
}
private async onAddModerator() {
const { authorPhoneNumber: pubkey, convoId } = this.props;
try {
const convo = ConversationController.getInstance().getOrThrow(convoId);
const channelAPI = await convo.getPublicSendData();
const res = await channelAPI.serverAPI.addModerator([pubkey]);
if (!res) {
window.log.warn('failed to add moderators:', res);
ToastUtils.pushUserNeedsToHaveJoined();
} else {
window.log.info(`${pubkey} added as moderator...`);
ToastUtils.pushUserAddedToModerators();
}
} catch (e) {
window.log.error('Got error while adding moderator:', e);
}
}
private async onRemoveFromModerator() {
const { authorPhoneNumber: pubkey, convoId } = this.props;
try {
const convo = ConversationController.getInstance().getOrThrow(convoId);
const channelAPI = await convo.getPublicSendData();
const res = await channelAPI.serverAPI.removeModerators([pubkey]);
if (!res) {
window.log.warn('failed to remove moderators:', res);
ToastUtils.pushErrorHappenedWhileRemovingModerator();
} else {
// refresh the moderator list. Will trigger a refresh
const modPubKeys = (await channelAPI.getModerators()) as Array<string>;
convo.updateGroupAdmins(modPubKeys);
window.log.info(`${pubkey} removed from moderators...`);
ToastUtils.pushUserRemovedToModerators();
}
} catch (e) {
window.log.error('Got error while removing moderator:', e);
}
}
}
export const Message = withTheme(MessageInner);

@ -246,3 +246,11 @@ export function pushUserRemovedToModerators() {
export function pushInvalidPubKey() {
pushToastSuccess('invalidPubKey', window.i18n('invalidPubkeyFormat'));
}
export function pushErrorHappenedWhileRemovingModerator() {
pushToastError(
'errorHappenedWhileRemovingModerator',
window.i18n('errorHappenedWhileRemovingModerator'),
window.i18n('errorHappenedWhileRemovingModeratorDesc')
);
}

1
ts/window.d.ts vendored

@ -91,7 +91,6 @@ declare global {
userConfig: any;
versionInfo: any;
getStoragePubKey: any;
pubkeyPattern: any;
getConversations: () => ConversationCollection;
getGuid: any;
ContactBuffer: any;

Loading…
Cancel
Save