add the ban/unban logic for opengroupv2

pull/1576/head
Audric Ackermann 4 years ago
parent 4aeec224b4
commit 5916ce5cbe
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -1683,6 +1683,22 @@
"description": "Shown in the settings page as the heading for the blocked user settings",
"androidKey": "preferences_app_protection__blocked_contacts"
},
"unbanUser": {
"message": "Unban User",
"description": "Unban user from open group by public key."
},
"unbanUserConfirm": {
"message": "Are you sure you want to unban user?",
"description": "Message shown when confirming user unban."
},
"userUnbanned": {
"message": "User unbanned successfully",
"description": "Toast on succesful user unban."
},
"userUnbanFailed": {
"message": "Unban failed!",
"description": "Toast on unsuccesful user unban."
},
"banUser": {
"message": "Ban User",
"description": "Ban user from open group by public key."

@ -559,9 +559,11 @@ class MessageInner extends React.PureComponent<MessageRegularProps, State> {
onRetrySend,
onShowDetail,
isPublic,
isOpenGroupV2,
weAreAdmin,
isAdmin,
onBanUser,
onUnbanUser,
} = this.props;
const showRetry = status === 'error' && direction === 'outgoing';
@ -625,6 +627,9 @@ class MessageInner extends React.PureComponent<MessageRegularProps, State> {
</>
) : null}
{weAreAdmin && isPublic ? <Item onClick={onBanUser}>{window.i18n('banUser')}</Item> : null}
{weAreAdmin && isOpenGroupV2 ? (
<Item onClick={onUnbanUser}>{window.i18n('unbanUser')}</Item>
) : null}
{weAreAdmin && isPublic && !isAdmin ? (
<Item onClick={this.onAddModerator}>{window.i18n('addAsModerator')}</Item>
) : null}

@ -36,7 +36,9 @@ export class AddModeratorsDialog extends React.Component<Props, State> {
}
public async componentDidMount() {
this.channelAPI = await this.props.convo.getPublicSendData();
if (this.props.convo.isOpenGroupV1()) {
this.channelAPI = await this.props.convo.getPublicSendData();
}
this.setState({ firstLoading: false });
}
@ -56,9 +58,16 @@ export class AddModeratorsDialog extends React.Component<Props, State> {
this.setState({
addingInProgress: true,
});
const res = await this.channelAPI.serverAPI.addModerator([pubkey.key]);
if (!res) {
window.log.warn('failed to add moderators:', res);
let isAdded: any;
if (this.props.convo.isOpenGroupV1()) {
isAdded = await this.channelAPI.serverAPI.addModerator([pubkey.key]);
} else {
// this is a v2 opengroup
// FIXME audric addModerators
throw new Error('TODO');
}
if (!isAdded) {
window.log.warn('failed to add moderators:', isAdded);
ToastUtils.pushUserNeedsToHaveJoined();
} else {

@ -29,7 +29,7 @@ export function banUser(userToBan: string, conversation?: ConversationModel) {
if (!roomInfos) {
window.log.warn('banUser room not found');
} else {
await ApiV2.banUser(pubKeyToBan, _.pick(roomInfos, 'serverUrl', 'roomId'));
success = await ApiV2.banUser(pubKeyToBan, _.pick(roomInfos, 'serverUrl', 'roomId'));
}
} else {
const channelAPI = await conversation.getPublicSendData();
@ -48,6 +48,51 @@ export function banUser(userToBan: string, conversation?: ConversationModel) {
});
}
/**
* There is no way to unban on an opengroupv1 server.
* This function only works for opengroupv2 server
*/
export function unbanUser(userToUnBan: string, conversation?: ConversationModel) {
let pubKeyToUnban: PubKey;
try {
pubKeyToUnban = PubKey.cast(userToUnBan);
} catch (e) {
window.log.warn(e);
ToastUtils.pushUserBanFailure();
return;
}
if (!isOpenGroupV2(conversation?.id || '')) {
window.log.warn('no way to unban on a opengroupv1');
ToastUtils.pushUserBanFailure();
return;
}
window.confirmationDialog({
title: window.i18n('unbanUser'),
message: window.i18n('unbanUserConfirm'),
resolve: async () => {
if (!conversation) {
// double check here. the convo might have been removed since the dialog was opened
window.log.info('cannot unban user, the corresponding conversation was not found.');
return;
}
let success = false;
if (isOpenGroupV2(conversation.id)) {
const roomInfos = await getV2OpenGroupRoom(conversation.id);
if (!roomInfos) {
window.log.warn('unbanUser room not found');
} else {
success = await ApiV2.unbanUser(pubKeyToUnban, _.pick(roomInfos, 'serverUrl', 'roomId'));
}
}
if (success) {
ToastUtils.pushUserUnbanSuccess();
} else {
ToastUtils.pushUserUnbanFailure();
}
},
});
}
export function copyBodyToClipboard(body?: string) {
window.clipboard.writeText(body);

@ -375,7 +375,11 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
}
public getGroupAdmins() {
return this.get('groupAdmins') || this.get('moderators');
const groupAdmins = this.get('groupAdmins');
if (groupAdmins?.length) {
return groupAdmins;
}
return this.get('moderators');
}
public getProps(): ReduxConversationType {
const groupAdmins = this.getGroupAdmins();

@ -3,13 +3,13 @@ import Backbone from 'backbone';
import filesize from 'filesize';
import _ from 'lodash';
import { SignalService } from '../../ts/protobuf';
import { getMessageQueue, Types, Utils } from '../../ts/session';
import { getMessageQueue, Utils } from '../../ts/session';
import { ConversationController } from '../../ts/session/conversations';
import { MessageController } from '../../ts/session/messages';
import { DataMessage, OpenGroupMessage } from '../../ts/session/messages/outgoing';
import { ClosedGroupVisibleMessage } from '../session/messages/outgoing/visibleMessage/ClosedGroupVisibleMessage';
import { PubKey } from '../../ts/session/types';
import { ToastUtils, UserUtils } from '../../ts/session/utils';
import { UserUtils } from '../../ts/session/utils';
import {
fillMessageAttributesWithDefaults,
MessageAttributes,
@ -24,8 +24,6 @@ import { actions as conversationActions } from '../state/ducks/conversations';
import { VisibleMessage } from '../session/messages/outgoing/visibleMessage/VisibleMessage';
import { buildSyncMessage } from '../session/utils/syncUtils';
import { isOpenGroupV2 } from '../opengroup/utils/OpenGroupUtils';
import { banUser } from '../opengroup/opengroupV2/OpenGroupAPIV2';
import { getV2OpenGroupRoom } from '../data/opengroups';
import { MessageInteraction } from '../interactions';
import {
uploadAttachmentsV2,
@ -468,6 +466,7 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
const convoId = conversation ? conversation.id : undefined;
const isGroup = !!conversation && !conversation.isPrivate();
const isPublic = !!this.get('isPublic');
const isPublicOpenGroupV2 = isOpenGroupV2(this.getConversation()?.id || '');
const attachments = this.get('attachments') || [];
@ -493,11 +492,13 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
expirationLength,
expirationTimestamp,
isPublic,
isOpenGroupV2: isPublicOpenGroupV2,
isKickedFromGroup: conversation && conversation.get('isKickedFromGroup'),
onCopyText: this.copyText,
onCopyPubKey: this.copyPubKey,
onBanUser: this.banUser,
onUnbanUser: this.unbanUser,
onRetrySend: this.retrySend,
markRead: this.markRead,
@ -709,6 +710,9 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
public banUser() {
MessageInteraction.banUser(this.get('source'), this.getConversation());
}
public unbanUser() {
MessageInteraction.unbanUser(this.get('source'), this.getConversation());
}
public copyText() {
MessageInteraction.copyBodyToClipboard(this.get('body'));

@ -207,6 +207,7 @@ export interface MessageRegularProps {
expirationTimestamp?: number;
convoId: string;
isPublic?: boolean;
isOpenGroupV2?: boolean;
selected: boolean;
isKickedFromGroup: boolean;
// whether or not to show check boxes
@ -225,6 +226,8 @@ export interface MessageRegularProps {
onDeleteMessage: (messageId: string) => void;
onCopyPubKey?: () => void;
onBanUser?: () => void;
onUnbanUser?: () => void;
onShowDetail: () => void;
onShowUserDetails: (userPubKey: string) => void;
markRead: (readAt: number) => Promise<void>;

@ -417,7 +417,7 @@ export const isUserModerator = (
export const banUser = async (
userToBan: PubKey,
roomInfos: OpenGroupRequestCommonType
): Promise<void> => {
): Promise<boolean> => {
const queryParams = { public_key: userToBan.key };
const request: OpenGroupV2Request = {
method: 'POST',
@ -428,13 +428,15 @@ export const banUser = async (
endpoint: 'block_list',
};
const banResult = await sendOpenGroupV2Request(request);
const isOk = parseStatusCodeFromOnionRequest(banResult) === 200;
console.warn('banResult', banResult);
return isOk;
};
export const unbanUser = async (
userToBan: PubKey,
roomInfos: OpenGroupRequestCommonType
): Promise<void> => {
): Promise<boolean> => {
const request: OpenGroupV2Request = {
method: 'DELETE',
room: roomInfos.roomId,
@ -442,7 +444,10 @@ export const unbanUser = async (
isAuthRequired: true,
endpoint: `block_list/${userToBan.key}`,
};
await sendOpenGroupV2Request(request);
const unbanResult = await sendOpenGroupV2Request(request);
const isOk = parseStatusCodeFromOnionRequest(unbanResult) === 200;
console.warn('unbanResult', unbanResult);
return isOk;
};
export const getAllRoomInfos = async (roomInfos: OpenGroupRequestCommonType) => {

@ -10,7 +10,7 @@ import { getV2OpenGroupRoom, saveV2OpenGroupRoom } from '../../data/opengroups';
import { OpenGroupMessageV2 } from './OpenGroupMessageV2';
import { handleOpenGroupV2Message } from '../../receiver/receiver';
const pollForEverythingInterval = 4 * 1000;
const pollForEverythingInterval = 6 * 1000;
/**
* An OpenGroupServerPollerV2 polls for everything for a particular server. We should
@ -256,8 +256,7 @@ const handleCompactPollResults = async (
}
const existingModerators = convo.get('moderators') || [];
let changeOnConvo = false;
// res.moderators is already sorted
if (!_.isEqual(existingModerators.sort(), res.moderators)) {
if (!_.isEqual(existingModerators.sort(), res.moderators.sort())) {
convo.set({ moderators: res.moderators });
changeOnConvo = true;
}

@ -341,11 +341,7 @@ const processOnionResponse = async (
return jsonRes;
} catch (e) {
log.error(
`(${reqIdx}) [path] lokiRpc::processOnionResponse - parse error outer json`,
e.code,
e.message,
'json:',
plaintext
`(${reqIdx}) [path] lokiRpc::processOnionResponse - parse error outer json ${e.code} ${e.message} json: '${plaintext}'`
);
return RequestError.OTHER;
}

@ -118,6 +118,14 @@ export function pushUserBanFailure() {
pushToastError('userBanFailed', window.i18n('userBanFailed'));
}
export function pushUserUnbanSuccess() {
pushToastSuccess('userUnbanned', window.i18n('userUnbanned'));
}
export function pushUserUnbanFailure() {
pushToastError('userUnbanFailed', window.i18n('userUnbanFailed'));
}
export function pushMessageDeleteForbidden() {
pushToastError('messageDeletionForbidden', window.i18n('messageDeletionForbidden'));
}

Loading…
Cancel
Save