Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support admin delete message completely #449

Merged
merged 1 commit into from Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/server/src/routes/message.ts
Expand Up @@ -55,10 +55,8 @@ async function pushNotification(
try {
const results = await expo.sendPushNotificationsAsync(chunk);
results.forEach((result) => {
const {
status,
message: errMessage,
} = result as ExpoPushErrorTicket;
const { status, message: errMessage } =
result as ExpoPushErrorTicket;
if (status === 'error') {
logger.warn('[Notification]', errMessage);
}
Expand Down Expand Up @@ -187,7 +185,7 @@ export async function sendMessage(ctx: Context<SendMessageData>) {
if (notificationTokens.length) {
pushNotification(
notificationTokens,
(messageData as unknown) as MessageDocument,
messageData as unknown as MessageDocument,
toGroup.name,
);
}
Expand All @@ -209,7 +207,7 @@ export async function sendMessage(ctx: Context<SendMessageData>) {
if (notificationTokens.length) {
pushNotification(
notificationTokens.map(({ token }) => token),
(messageData as unknown) as MessageDocument,
messageData as unknown as MessageDocument,
);
}
}
Expand Down Expand Up @@ -394,7 +392,7 @@ export async function deleteMessage(ctx: Context<{ messageId: string }>) {
!client.disableDeleteMessage || ctx.socket.isAdmin,
'已禁止撤回消息',
);

const { messageId } = ctx.data;
assert(messageId, 'messageId不能为空');

Expand All @@ -408,8 +406,12 @@ export async function deleteMessage(ctx: Context<{ messageId: string }>) {
'只能撤回本人的消息',
);

message.deleted = true;
await message.save();
if (ctx.socket.isAdmin) {
await Message.deleteOne({ _id: messageId });
} else {
message.deleted = true;
await message.save();
}

/**
* 广播删除消息通知, 区分群消息和私聊消息
Expand All @@ -418,6 +420,7 @@ export async function deleteMessage(ctx: Context<{ messageId: string }>) {
const messageData = {
linkmanId: message.to.toString(),
messageId,
isAdmin: ctx.socket.isAdmin,
};
if (isValid(message.to)) {
// 群消息
Expand Down
4 changes: 3 additions & 1 deletion packages/web/src/modules/Chat/Message/Message.tsx
Expand Up @@ -93,13 +93,14 @@ class Message extends Component<MessageProps, MessageState> {
* 管理员撤回消息
*/
handleDeleteMessage = async () => {
const { id, linkmanId, loading } = this.props;
const { id, linkmanId, loading, isAdmin } = this.props;
if (loading) {
dispatch({
type: ActionTypes.DeleteMessage,
payload: {
linkmanId,
messageId: id,
isAdmin,
} as DeleteMessagePayload,
});
return;
Expand All @@ -112,6 +113,7 @@ class Message extends Component<MessageProps, MessageState> {
payload: {
linkmanId,
messageId: id,
isAdmin
} as DeleteMessagePayload,
});
this.setState({ showButtonList: false });
Expand Down
13 changes: 11 additions & 2 deletions packages/web/src/socket.ts
Expand Up @@ -158,7 +158,7 @@ socket.on('message', async (message: any) => {
dispatch({
type: ActionTypes.AddLinkman,
payload: {
linkman: (newLinkman as unknown) as Linkman,
linkman: newLinkman as unknown as Linkman,
focus: false,
},
});
Expand Down Expand Up @@ -258,12 +258,21 @@ socket.on('changeTag', (tag: string) => {

socket.on(
'deleteMessage',
({ linkmanId, messageId }: { linkmanId: string; messageId: string }) => {
({
linkmanId,
messageId,
isAdmin,
}: {
linkmanId: string;
messageId: string;
isAdmin: boolean;
}) => {
dispatch({
type: ActionTypes.DeleteMessage,
payload: {
linkmanId,
messageId,
isAdmin,
} as DeleteMessagePayload,
});
},
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/state/action.ts
Expand Up @@ -102,6 +102,7 @@ export interface UpdateMessagePayload {
export interface DeleteMessagePayload {
linkmanId: string;
messageId: string;
isAdmin: boolean;
}

export interface Action {
Expand Down
22 changes: 12 additions & 10 deletions packages/web/src/state/reducer.ts
Expand Up @@ -523,7 +523,7 @@ function reducer(state: State = initialState, action: Action): State {
}

case ActionTypes.DeleteMessage: {
const { linkmanId, messageId } =
const { linkmanId, messageId, isAdmin } =
action.payload as DeleteMessagePayload;
if (!state.linkmans[linkmanId]) {
/* istanbul ignore next */
Expand All @@ -535,21 +535,23 @@ function reducer(state: State = initialState, action: Action): State {
return state;
}

const newMessages = isAdmin
? deleteObjectKey(state.linkmans[linkmanId].messages, messageId)
: {
...state.linkmans[linkmanId].messages,
[messageId]: convertMessage({
...state.linkmans[linkmanId].messages[messageId],
deleted: true,
}),
};

return {
...state,
linkmans: {
...state.linkmans,
[linkmanId]: {
...state.linkmans[linkmanId],
messages: {
...state.linkmans[linkmanId].messages,
[messageId]: convertMessage({
...state.linkmans[linkmanId].messages[
messageId
],
deleted: true,
}),
},
messages: newMessages,
},
},
};
Expand Down