From b2b0db6ad5bc80d0045d35b614525def665b6242 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 14 Jul 2023 15:19:35 +0800 Subject: [PATCH] style: refactor code style and make ack mode field from ref -> string --- client/shared/redux/hooks/useConverseAck.ts | 34 +++++++++------------ client/shared/redux/hooks/useGroupAck.ts | 4 +-- server/models/chat/ack.ts | 15 +++------ server/services/core/chat/ack.service.ts | 6 ++-- 4 files changed, 24 insertions(+), 35 deletions(-) diff --git a/client/shared/redux/hooks/useConverseAck.ts b/client/shared/redux/hooks/useConverseAck.ts index 873b059f..39260dad 100644 --- a/client/shared/redux/hooks/useConverseAck.ts +++ b/client/shared/redux/hooks/useConverseAck.ts @@ -4,7 +4,7 @@ import _debounce from 'lodash/debounce'; import { isValidStr } from '../../utils/string-helper'; import { chatActions } from '../slices'; import { updateAck } from '../../model/converse'; -import { useMemoizedFn } from '../../hooks/useMemoizedFn'; +import { useEvent } from '../../hooks/useEvent'; const updateAckDebounce = _debounce( (converseId: string, lastMessageId: string) => { @@ -28,33 +28,27 @@ export function useConverseAck(converseId: string) { (state) => state.chat.ack[converseId] ?? '' ); - const setConverseAck = useMemoizedFn( - (converseId: string, lastMessageId: string) => { - if ( - isValidStr(lastMessageIdRef.current) && - lastMessageId <= lastMessageIdRef.current - ) { - // 更新的数字比较小,跳过 - return; - } - - dispatch(chatActions.setConverseAck({ converseId, lastMessageId })); - updateAckDebounce(converseId, lastMessageId); - lastMessageIdRef.current = lastMessageId; - } - ); - /** * 更新会话最新消息 */ - const updateConverseAck = useMemoizedFn((lastMessageId: string) => { - setConverseAck(converseId, lastMessageId); + const updateConverseAck = useEvent((lastMessageId: string) => { + if ( + isValidStr(lastMessageIdRef.current) && + lastMessageId <= lastMessageIdRef.current + ) { + // 更新的数字比较小,跳过 + return; + } + + dispatch(chatActions.setConverseAck({ converseId, lastMessageId })); + updateAckDebounce(converseId, lastMessageId); + lastMessageIdRef.current = lastMessageId; }); /** * 标记为会话已读 */ - const markConverseAllAck = useMemoizedFn(() => { + const markConverseAllAck = useEvent(() => { updateConverseAck(converseLastMessage); }); diff --git a/client/shared/redux/hooks/useGroupAck.ts b/client/shared/redux/hooks/useGroupAck.ts index 62981f4c..8b6f0468 100644 --- a/client/shared/redux/hooks/useGroupAck.ts +++ b/client/shared/redux/hooks/useGroupAck.ts @@ -1,4 +1,4 @@ -import { useMemoizedFn } from '../../hooks/useMemoizedFn'; +import { useEvent } from '../../hooks/useEvent'; import { updateAck } from '../../model/converse'; import { isConversePanel } from '../../utils/panel-helper'; import { chatActions } from '../slices'; @@ -13,7 +13,7 @@ export function useGroupAck(groupId: string) { const lastMessageMap = useAppSelector((state) => state.chat.lastMessageMap); const dispatch = useAppDispatch(); - const markGroupAllAck = useMemoizedFn(() => { + const markGroupAllAck = useEvent(() => { const conversePanels = (groupInfo?.panels ?? []).filter(isConversePanel); for (const converse of conversePanels) { diff --git a/server/models/chat/ack.ts b/server/models/chat/ack.ts index cf29ff57..bba2f114 100644 --- a/server/models/chat/ack.ts +++ b/server/models/chat/ack.ts @@ -3,14 +3,11 @@ import { prop, DocumentType, Ref, - ReturnModelType, index, } from '@typegoose/typegoose'; import type { Base } from '@typegoose/typegoose/lib/defaultClasses'; import type { Types } from 'mongoose'; import { User } from '../user/user'; -import { Converse } from './converse'; -import { Message } from './message'; /** * 消息已读管理 @@ -25,15 +22,11 @@ export class Ack implements Base { }) userId: Ref; - @prop({ - ref: () => Converse, - }) - converseId: Ref; + @prop() + converseId: string; - @prop({ - ref: () => Message, - }) - lastMessageId: Ref; + @prop() + lastMessageId: string; } export type AckDocument = DocumentType; diff --git a/server/services/core/chat/ack.service.ts b/server/services/core/chat/ack.service.ts index fd6cd331..bd0edc56 100644 --- a/server/services/core/chat/ack.service.ts +++ b/server/services/core/chat/ack.service.ts @@ -38,13 +38,13 @@ class AckService extends TcService { const { converseId, lastMessageId } = ctx.params; const userId = ctx.meta.userId; - await this.adapter.model.findOneAndUpdate( + await this.adapter.model.updateOne( { converseId, userId, }, { - lastMessageId: new Types.ObjectId(lastMessageId), + lastMessageId: lastMessageId, }, { upsert: true, @@ -52,6 +52,8 @@ class AckService extends TcService { ); // TODO: 如果要实现消息已读可以在此处基于会话id进行通知 + + return; } /**