feat: add topic delete action

chore/upgrade-rn-0.72.7
moonrailgun 1 year ago
parent bf886625a9
commit e012c9e20b

@ -66,6 +66,7 @@ export {
showNotification, showNotification,
fetchAvailableServices, fetchAvailableServices,
isValidStr, isValidStr,
useGroupInfo,
useGroupPanelInfo, useGroupPanelInfo,
sendMessage, sendMessage,
showMessageTime, showMessageTime,
@ -100,7 +101,13 @@ export {
export function useCurrentUserInfo() { export function useCurrentUserInfo() {
const userInfo = useUserInfo(); const userInfo = useUserInfo();
return _pick(userInfo, ['email', 'nickname', 'discriminator', 'avatar']); return _pick(userInfo, [
'_id',
'email',
'nickname',
'discriminator',
'avatar',
]);
} }
/** /**

@ -41,6 +41,13 @@ class GroupTopicService extends TcService {
replyCommentId: { type: 'string', optional: true }, replyCommentId: { type: 'string', optional: true },
}, },
}); });
this.registerAction('delete', this.delete, {
params: {
groupId: 'string',
panelId: 'string',
topicId: 'string',
},
});
} }
protected onInited(): void { protected onInited(): void {
@ -199,6 +206,46 @@ class GroupTopicService extends TcService {
return true; 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; export default GroupTopicService;

@ -4,6 +4,8 @@ import {
showMessageTime, showMessageTime,
showSuccessToasts, showSuccessToasts,
useAsyncRequest, useAsyncRequest,
useCurrentUserInfo,
useGroupInfo,
} from '@capital/common'; } from '@capital/common';
import { import {
IconBtn, IconBtn,
@ -57,6 +59,11 @@ const Root = styled.div`
margin-bottom: 6px; margin-bottom: 6px;
} }
} }
.footer {
display: flex;
gap: 4px;
}
} }
`; `;
@ -76,6 +83,9 @@ export const TopicCard: React.FC<{
const topic: Partial<GroupTopic> = props.topic ?? {}; const topic: Partial<GroupTopic> = props.topic ?? {};
const [showReply, toggleShowReply] = useReducer((state) => !state, false); const [showReply, toggleShowReply] = useReducer((state) => !state, false);
const [comment, setComment] = useState(''); const [comment, setComment] = useState('');
const groupInfo = useGroupInfo(topic.groupId);
const groupOwnerId = groupInfo?.owner;
const userId = useCurrentUserInfo()._id;
const [{ loading }, handleComment] = useAsyncRequest(async () => { const [{ loading }, handleComment] = useAsyncRequest(async () => {
await request.post('createComment', { await request.post('createComment', {
@ -90,6 +100,14 @@ export const TopicCard: React.FC<{
showSuccessToasts(); showSuccessToasts();
}, [topic.groupId, topic.panelId, topic._id, comment]); }, [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 ( return (
<MessageAckContainer converseId={topic.panelId} messageId={topic._id}> <MessageAckContainer converseId={topic.panelId} messageId={topic._id}>
<Root> <Root>
@ -119,6 +137,14 @@ export const TopicCard: React.FC<{
icon="mdi:message-reply-text-outline" icon="mdi:message-reply-text-outline"
onClick={toggleShowReply} onClick={toggleShowReply}
/> />
{userId === groupOwnerId && (
<IconBtn
title={Translate.delete}
icon="mdi:delete-outline"
onClick={handleDeleteTopic}
/>
)}
</div> </div>
{showReply && ( {showReply && (

@ -58,6 +58,7 @@ const GroupTopicPanelRender: React.FC = React.memo(() => {
topicMap, topicMap,
addTopicPanel, addTopicPanel,
addTopicItem, addTopicItem,
deleteTopicItem,
updateTopicItem, updateTopicItem,
resetTopicPanel, resetTopicPanel,
} = useTopicStore(); } = useTopicStore();
@ -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( useGlobalSocketEvent(
'plugin:com.msgbyte.topic.createComment', 'plugin:com.msgbyte.topic.createComment',
(topic: GroupTopic) => { (topic: GroupTopic) => {

@ -11,6 +11,7 @@ interface TopicStoreState {
topicMap: TopicPanelMap; topicMap: TopicPanelMap;
addTopicPanel: (panelId: string, topicList: GroupTopic[]) => void; addTopicPanel: (panelId: string, topicList: GroupTopic[]) => void;
addTopicItem: (panelId: string, topic: GroupTopic) => void; addTopicItem: (panelId: string, topic: GroupTopic) => void;
deleteTopicItem: (panelId: string, topicId: string) => void;
updateTopicItem: (panelId: string, topic: GroupTopic) => void; updateTopicItem: (panelId: string, topic: GroupTopic) => void;
resetTopicPanel: (panelId: string) => void; resetTopicPanel: (panelId: string) => void;
} }
@ -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) => { updateTopicItem: (panelId, topic) => {
set((state) => { set((state) => {
if (state.topicMap[panelId]) { if (state.topicMap[panelId]) {

@ -5,6 +5,7 @@ export const Translate = {
noTopic: localTrans({ 'zh-CN': '暂无话题', 'en-US': 'No Topic' }), noTopic: localTrans({ 'zh-CN': '暂无话题', 'en-US': 'No Topic' }),
createBtn: localTrans({ 'zh-CN': '创建话题', 'en-US': 'Create Topic' }), createBtn: localTrans({ 'zh-CN': '创建话题', 'en-US': 'Create Topic' }),
reply: localTrans({ 'zh-CN': '回复', 'en-US': 'Reply' }), reply: localTrans({ 'zh-CN': '回复', 'en-US': 'Reply' }),
delete: localTrans({ 'zh-CN': '删除', 'en-US': 'Delete' }),
replyThisTopic: localTrans({ replyThisTopic: localTrans({
'zh-CN': '回复该话题', 'zh-CN': '回复该话题',
'en-US': 'Reply this topic', 'en-US': 'Reply this topic',

Loading…
Cancel
Save