Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gruop panel ack support #105

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions client/shared/redux/hooks/useConverseAck.ts
Original file line number Diff line number Diff line change
@@ -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);
});

4 changes: 2 additions & 2 deletions client/shared/redux/hooks/useGroupAck.ts
Original file line number Diff line number Diff line change
@@ -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) {
15 changes: 4 additions & 11 deletions server/models/chat/ack.ts
Original file line number Diff line number Diff line change
@@ -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>;
6 changes: 4 additions & 2 deletions server/services/core/chat/ack.service.ts
Original file line number Diff line number Diff line change
@@ -38,20 +38,22 @@ 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,
}
);

// TODO: 如果要实现消息已读可以在此处基于会话id进行通知

return;
}

/**