feat: clear msg from channel (draft)

This commit is contained in:
Tristan Yang
2023-05-09 21:34:50 +08:00
parent cfcdbaa3cf
commit 6055a2d1e2
5 changed files with 58 additions and 5 deletions
+24 -1
View File
@@ -95,6 +95,28 @@ export const channelApi = createApi({
return `${location.origin}${invite.pathname}${invite.hash}${invite.search}`; return `${location.origin}${invite.pathname}${invite.hash}${invite.search}`;
} }
}), }),
clearChannelMessage: builder.query<void, number>({
query: (id) => ({
url: `/group/${id}/clear`,
method: "DELETE"
}),
async onQueryStarted(id, { dispatch, getState, queryFulfilled }) {
const {
channelMessage,
} = getState() as RootState;
try {
await queryFulfilled;
// 删掉该channel下的所有消息&reaction
const mids = channelMessage[id];
if (mids) {
dispatch(removeMessage(mids));
dispatch(removeReactionMessage(mids));
}
} catch {
console.error("clear channel msg error");
}
}
}),
removeChannel: builder.query<void, number>({ removeChannel: builder.query<void, number>({
query: (id) => ({ query: (id) => ({
url: `/group/${id}`, url: `/group/${id}`,
@@ -204,5 +226,6 @@ export const {
useSendChannelMsgMutation, useSendChannelMsgMutation,
useAddMembersMutation, useAddMembersMutation,
useRemoveMembersMutation, useRemoveMembersMutation,
useUpdateIconMutation useUpdateIconMutation,
useLazyClearChannelMessageQuery
} = channelApi; } = channelApi;
+7 -2
View File
@@ -13,6 +13,10 @@ const channelMsgSlice = createSlice({
resetChannelMsg() { resetChannelMsg() {
return initialState; return initialState;
}, },
clearChannelMessage(state, action: PayloadAction<number>) {
const id = action.payload;
state[id] = [];
},
fillChannelMsg(state, action: PayloadAction<State>) { fillChannelMsg(state, action: PayloadAction<State>) {
return action.payload; return action.payload;
}, },
@@ -23,7 +27,7 @@ const channelMsgSlice = createSlice({
const localMsgExisted = state[id]!.findIndex((id) => id == local_id) > -1; const localMsgExisted = state[id]!.findIndex((id) => id == local_id) > -1;
if (midExisted || localMsgExisted) return; if (midExisted || localMsgExisted) return;
// 每次入库,都排序 // 每次入库,都排序
const newArr = [...state[id], +mid].sort((a, b) => a - b); const newArr = [...state[id] as number[], +mid].sort((a, b) => a - b);
state[id] = newArr; state[id] = newArr;
} else { } else {
state[id] = [+mid]; state[id] = [+mid];
@@ -67,7 +71,8 @@ export const {
fillChannelMsg, fillChannelMsg,
addChannelMsg, addChannelMsg,
removeChannelMsg, removeChannelMsg,
replaceChannelMsg replaceChannelMsg,
clearChannelMessage
} = channelMsgSlice.actions; } = channelMsgSlice.actions;
export default channelMsgSlice.reducer; export default channelMsgSlice.reducer;
+6 -1
View File
@@ -27,7 +27,7 @@ import { ServerEvent, UsersStateEvent } from "../../../types/sse";
import { useRenewMutation } from "../../../app/services/auth"; import { useRenewMutation } from "../../../app/services/auth";
import { getLocalAuthData } from "../../utils"; import { getLocalAuthData } from "../../utils";
import { removeUserSession } from "../../../app/slices/message.user"; import { removeUserSession } from "../../../app/slices/message.user";
import { removeChannelSession } from "../../../app/slices/message.channel"; import { clearChannelMessage, removeChannelSession } from "../../../app/slices/message.channel";
const getQueryString = (params: { [key: string]: string }) => { const getQueryString = (params: { [key: string]: string }) => {
const sp = new URLSearchParams(); const sp = new URLSearchParams();
@@ -144,6 +144,11 @@ export default function useStreaming() {
keepAlive(); keepAlive();
dispatch(setReady()); dispatch(setReady());
break; break;
case "group_message_cleared": {
const { gid } = data;
dispatch(clearChannelMessage(gid));
break;
}
case "users_snapshot": { case "users_snapshot": {
const { version } = data; const { version } = data;
dispatch(updateUsersVersion(version)); dispatch(updateUsersVersion(version));
+14
View File
@@ -3,6 +3,7 @@ import toast from "react-hot-toast";
import { import {
useChangeChannelTypeMutation, useChangeChannelTypeMutation,
useGetChannelQuery, useGetChannelQuery,
// useLazyClearChannelMessageQuery,
useUpdateChannelMutation, useUpdateChannelMutation,
useUpdateIconMutation useUpdateIconMutation
} from "../../app/services/channel"; } from "../../app/services/channel";
@@ -16,6 +17,7 @@ import IconChannel from "../../assets/icons/channel.svg";
import { useAppSelector } from "../../app/store"; import { useAppSelector } from "../../app/store";
import { Channel } from "../../types/channel"; import { Channel } from "../../types/channel";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
// import StyledButton from "../../common/component/styled/Button";
export default function Overview({ id = 0 }) { export default function Overview({ id = 0 }) {
const { t } = useTranslation("setting", { keyPrefix: "channel" }); const { t } = useTranslation("setting", { keyPrefix: "channel" });
@@ -30,6 +32,7 @@ export default function Overview({ id = 0 }) {
const [changed, setChanged] = useState(false); const [changed, setChanged] = useState(false);
const [values, setValues] = useState<Channel>(); const [values, setValues] = useState<Channel>();
const [updateChannelIcon] = useUpdateIconMutation(); const [updateChannelIcon] = useUpdateIconMutation();
// const [clearMessage, { isSuccess: clearSuccess }] = useLazyClearChannelMessageQuery();
const [updateChannel, { isSuccess: updated }] = useUpdateChannelMutation(); const [updateChannel, { isSuccess: updated }] = useUpdateChannelMutation();
const [changeChannelType, { isSuccess: changeTypeSuccess }] = useChangeChannelTypeMutation(); const [changeChannelType, { isSuccess: changeTypeSuccess }] = useChangeChannelTypeMutation();
const handleUpdate = () => { const handleUpdate = () => {
@@ -51,6 +54,11 @@ export default function Overview({ id = 0 }) {
updateChannelIcon({ gid: id, image }); updateChannelIcon({ gid: id, image });
}; };
// const handleClear = () => {
// if (confirm("are you sure?")) {
// clearMessage(id);
// }
// };
const handleReset = () => { const handleReset = () => {
setValues(data); setValues(data);
}; };
@@ -60,6 +68,11 @@ export default function Overview({ id = 0 }) {
setValues(data); setValues(data);
} }
}, [data]); }, [data]);
// useEffect(() => {
// if (clearSuccess) {
// toast.success("Cleared!");
// }
// }, [clearSuccess]);
useEffect(() => { useEffect(() => {
if (data && values) { if (data && values) {
@@ -132,6 +145,7 @@ export default function Overview({ id = 0 }) {
}} }}
/> />
</div>} </div>}
{/* <StyledButton className="danger" onClick={handleClear}>Clear Message</StyledButton> */}
</div> </div>
{changed && <SaveTip saveHandler={handleUpdate} resetHandler={handleReset} />} {changed && <SaveTip saveHandler={handleUpdate} resetHandler={handleReset} />}
</div> </div>
+7 -1
View File
@@ -191,6 +191,10 @@ interface HeartbeatEvent {
type: "heartbeat"; type: "heartbeat";
time: number; time: number;
} }
interface GroupClearEvent {
type: "group_message_cleared";
gid: number;
}
export type ServerEvent = export type ServerEvent =
| ReadyEvent | ReadyEvent
@@ -209,4 +213,6 @@ export type ServerEvent =
| KickFromGroupEvent | KickFromGroupEvent
| GroupChangedEvent | GroupChangedEvent
| PinnedMessageUpdatedEvent | PinnedMessageUpdatedEvent
| HeartbeatEvent; | HeartbeatEvent
| GroupClearEvent
;