Skip to content

Commit

Permalink
feat: add topic delete action
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Nov 25, 2023
1 parent bf88662 commit e012c9e
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 1 deletion.
9 changes: 8 additions & 1 deletion client/web/src/plugin/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export {
showNotification,
fetchAvailableServices,
isValidStr,
useGroupInfo,
useGroupPanelInfo,
sendMessage,
showMessageTime,
Expand Down Expand Up @@ -100,7 +101,13 @@ export {
export function useCurrentUserInfo() {
const userInfo = useUserInfo();

return _pick(userInfo, ['email', 'nickname', 'discriminator', 'avatar']);
return _pick(userInfo, [
'_id',
'email',
'nickname',
'discriminator',
'avatar',
]);
}

/**
Expand Down
47 changes: 47 additions & 0 deletions server/plugins/com.msgbyte.topic/services/topic.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class GroupTopicService extends TcService {
replyCommentId: { type: 'string', optional: true },
},
});
this.registerAction('delete', this.delete, {
params: {
groupId: 'string',
panelId: 'string',
topicId: 'string',
},
});
}

protected onInited(): void {
Expand Down Expand Up @@ -199,6 +206,46 @@ class GroupTopicService extends TcService {

return true;
}

/**
* 删除话题
*/
async delete(
ctx: TcContext<{
groupId: string;
panelId: string;
topicId: string;
}>
) {
const { groupId, panelId, topicId } = ctx.params;
const userId = ctx.meta.userId;
const t = ctx.meta.t;

// 鉴权
const group = await call(ctx).getGroupInfo(groupId);
const isMember = group.members.some((member) => member.userId === userId);
if (!isMember) {
throw new Error(t('不是该群组成员'));
}

if (String(group.owner) !== userId) {
throw new Error(t('仅群组所有者有权限删除话题'));
}

const result = await this.adapter.model.deleteOne({
_id: topicId,
groupId,
panelId,
});

this.roomcastNotify(ctx, panelId, 'delete', {
groupId,
panelId,
topicId,
});

return result.deletedCount > 0;
}
}

export default GroupTopicService;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
showMessageTime,
showSuccessToasts,
useAsyncRequest,
useCurrentUserInfo,
useGroupInfo,
} from '@capital/common';
import {
IconBtn,
Expand Down Expand Up @@ -57,6 +59,11 @@ const Root = styled.div`
margin-bottom: 6px;
}
}
.footer {
display: flex;
gap: 4px;
}
}
`;

Expand All @@ -76,6 +83,9 @@ export const TopicCard: React.FC<{
const topic: Partial<GroupTopic> = props.topic ?? {};
const [showReply, toggleShowReply] = useReducer((state) => !state, false);
const [comment, setComment] = useState('');
const groupInfo = useGroupInfo(topic.groupId);
const groupOwnerId = groupInfo?.owner;
const userId = useCurrentUserInfo()._id;

const [{ loading }, handleComment] = useAsyncRequest(async () => {
await request.post('createComment', {
Expand All @@ -90,6 +100,14 @@ export const TopicCard: React.FC<{
showSuccessToasts();
}, [topic.groupId, topic.panelId, topic._id, comment]);

const [, handleDeleteTopic] = useAsyncRequest(async () => {
await request.post('delete', {
groupId: topic.groupId,
panelId: topic.panelId,
topicId: topic._id,
});
}, []);

return (
<MessageAckContainer converseId={topic.panelId} messageId={topic._id}>
<Root>
Expand Down Expand Up @@ -119,6 +137,14 @@ export const TopicCard: React.FC<{
icon="mdi:message-reply-text-outline"
onClick={toggleShowReply}
/>

{userId === groupOwnerId && (
<IconBtn
title={Translate.delete}
icon="mdi:delete-outline"
onClick={handleDeleteTopic}
/>
)}
</div>

{showReply && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const GroupTopicPanelRender: React.FC = React.memo(() => {
topicMap,
addTopicPanel,
addTopicItem,
deleteTopicItem,
updateTopicItem,
resetTopicPanel,
} = useTopicStore();
Expand Down Expand Up @@ -119,6 +120,18 @@ const GroupTopicPanelRender: React.FC = React.memo(() => {
}
);

useGlobalSocketEvent(
'plugin:com.msgbyte.topic.delete',
(info: { panelId: string; topicId: string }) => {
/**
* 仅处理当前面板的话题更新
*/
if (info.panelId === panelId) {
deleteTopicItem(panelId, info.topicId);
}
}
);

useGlobalSocketEvent(
'plugin:com.msgbyte.topic.createComment',
(topic: GroupTopic) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface TopicStoreState {
topicMap: TopicPanelMap;
addTopicPanel: (panelId: string, topicList: GroupTopic[]) => void;
addTopicItem: (panelId: string, topic: GroupTopic) => void;
deleteTopicItem: (panelId: string, topicId: string) => void;
updateTopicItem: (panelId: string, topic: GroupTopic) => void;
resetTopicPanel: (panelId: string) => void;
}
Expand All @@ -37,6 +38,15 @@ export const useTopicStore = create<
}
});
},
deleteTopicItem: (panelId, topicId) => {
set((state) => {
if (state.topicMap[panelId]) {
state.topicMap[panelId] = state.topicMap[panelId].filter(
(item) => item._id !== topicId
);
}
});
},
updateTopicItem: (panelId, topic) => {
set((state) => {
if (state.topicMap[panelId]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const Translate = {
noTopic: localTrans({ 'zh-CN': '暂无话题', 'en-US': 'No Topic' }),
createBtn: localTrans({ 'zh-CN': '创建话题', 'en-US': 'Create Topic' }),
reply: localTrans({ 'zh-CN': '回复', 'en-US': 'Reply' }),
delete: localTrans({ 'zh-CN': '删除', 'en-US': 'Delete' }),
replyThisTopic: localTrans({
'zh-CN': '回复该话题',
'en-US': 'Reply this topic',
Expand Down

0 comments on commit e012c9e

Please sign in to comment.