style: refactor code style and make ack mode field from ref -> string

pull/105/head
moonrailgun 2 years ago
parent b6e978e569
commit b2b0db6ad5

@ -4,7 +4,7 @@ import _debounce from 'lodash/debounce';
import { isValidStr } from '../../utils/string-helper'; import { isValidStr } from '../../utils/string-helper';
import { chatActions } from '../slices'; import { chatActions } from '../slices';
import { updateAck } from '../../model/converse'; import { updateAck } from '../../model/converse';
import { useMemoizedFn } from '../../hooks/useMemoizedFn'; import { useEvent } from '../../hooks/useEvent';
const updateAckDebounce = _debounce( const updateAckDebounce = _debounce(
(converseId: string, lastMessageId: string) => { (converseId: string, lastMessageId: string) => {
@ -28,8 +28,10 @@ export function useConverseAck(converseId: string) {
(state) => state.chat.ack[converseId] ?? '' (state) => state.chat.ack[converseId] ?? ''
); );
const setConverseAck = useMemoizedFn( /**
(converseId: string, lastMessageId: string) => { *
*/
const updateConverseAck = useEvent((lastMessageId: string) => {
if ( if (
isValidStr(lastMessageIdRef.current) && isValidStr(lastMessageIdRef.current) &&
lastMessageId <= lastMessageIdRef.current lastMessageId <= lastMessageIdRef.current
@ -41,20 +43,12 @@ export function useConverseAck(converseId: string) {
dispatch(chatActions.setConverseAck({ converseId, lastMessageId })); dispatch(chatActions.setConverseAck({ converseId, lastMessageId }));
updateAckDebounce(converseId, lastMessageId); updateAckDebounce(converseId, lastMessageId);
lastMessageIdRef.current = lastMessageId; lastMessageIdRef.current = lastMessageId;
}
);
/**
*
*/
const updateConverseAck = useMemoizedFn((lastMessageId: string) => {
setConverseAck(converseId, lastMessageId);
}); });
/** /**
* *
*/ */
const markConverseAllAck = useMemoizedFn(() => { const markConverseAllAck = useEvent(() => {
updateConverseAck(converseLastMessage); updateConverseAck(converseLastMessage);
}); });

@ -1,4 +1,4 @@
import { useMemoizedFn } from '../../hooks/useMemoizedFn'; import { useEvent } from '../../hooks/useEvent';
import { updateAck } from '../../model/converse'; import { updateAck } from '../../model/converse';
import { isConversePanel } from '../../utils/panel-helper'; import { isConversePanel } from '../../utils/panel-helper';
import { chatActions } from '../slices'; import { chatActions } from '../slices';
@ -13,7 +13,7 @@ export function useGroupAck(groupId: string) {
const lastMessageMap = useAppSelector((state) => state.chat.lastMessageMap); const lastMessageMap = useAppSelector((state) => state.chat.lastMessageMap);
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const markGroupAllAck = useMemoizedFn(() => { const markGroupAllAck = useEvent(() => {
const conversePanels = (groupInfo?.panels ?? []).filter(isConversePanel); const conversePanels = (groupInfo?.panels ?? []).filter(isConversePanel);
for (const converse of conversePanels) { for (const converse of conversePanels) {

@ -3,14 +3,11 @@ import {
prop, prop,
DocumentType, DocumentType,
Ref, Ref,
ReturnModelType,
index, index,
} from '@typegoose/typegoose'; } from '@typegoose/typegoose';
import type { Base } from '@typegoose/typegoose/lib/defaultClasses'; import type { Base } from '@typegoose/typegoose/lib/defaultClasses';
import type { Types } from 'mongoose'; import type { Types } from 'mongoose';
import { User } from '../user/user'; import { User } from '../user/user';
import { Converse } from './converse';
import { Message } from './message';
/** /**
* *
@ -25,15 +22,11 @@ export class Ack implements Base {
}) })
userId: Ref<User>; userId: Ref<User>;
@prop({ @prop()
ref: () => Converse, converseId: string;
})
converseId: Ref<Converse>;
@prop({ @prop()
ref: () => Message, lastMessageId: string;
})
lastMessageId: Ref<Message>;
} }
export type AckDocument = DocumentType<Ack>; export type AckDocument = DocumentType<Ack>;

@ -38,13 +38,13 @@ class AckService extends TcService {
const { converseId, lastMessageId } = ctx.params; const { converseId, lastMessageId } = ctx.params;
const userId = ctx.meta.userId; const userId = ctx.meta.userId;
await this.adapter.model.findOneAndUpdate( await this.adapter.model.updateOne(
{ {
converseId, converseId,
userId, userId,
}, },
{ {
lastMessageId: new Types.ObjectId(lastMessageId), lastMessageId: lastMessageId,
}, },
{ {
upsert: true, upsert: true,
@ -52,6 +52,8 @@ class AckService extends TcService {
); );
// TODO: 如果要实现消息已读可以在此处基于会话id进行通知 // TODO: 如果要实现消息已读可以在此处基于会话id进行通知
return;
} }
/** /**

Loading…
Cancel
Save