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 { 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);
});

@ -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) {

@ -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<User>;
@prop({
ref: () => Converse,
})
converseId: Ref<Converse>;
@prop()
converseId: string;
@prop({
ref: () => Message,
})
lastMessageId: Ref<Message>;
@prop()
lastMessageId: string;
}
export type AckDocument = DocumentType<Ack>;

@ -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;
}
/**

Loading…
Cancel
Save