feat: 收件箱后端实现

监听新的提及信息
pull/49/head
moonrailgun 2 years ago
parent 9a0649049c
commit 5dcfabe014

@ -10,13 +10,16 @@ import type { Types } from 'mongoose';
import { User } from '../user/user';
import { Message } from './message';
/**
*
*/
@index({ userId: 1 })
export class Inbox extends TimeStamps implements Base {
_id: Types.ObjectId;
id: string;
class InboxMessage {
/**
* Id
*/
groupId?: string;
/**
* Id
*/
converseId: string;
@prop({
ref: () => Message,
@ -28,7 +31,15 @@ export class Inbox extends TimeStamps implements Base {
*/
@prop()
messageSnippet: string;
}
/**
*
*/
@index({ userId: 1 })
export class Inbox extends TimeStamps implements Base {
_id: Types.ObjectId;
id: string;
/**
* id
*/
@ -36,6 +47,16 @@ export class Inbox extends TimeStamps implements Base {
ref: () => User,
})
userId: Ref<User>;
@prop({
type: () => String,
})
type: 'message';
@prop({
type: () => InboxMessage,
})
message?: InboxMessage;
}
export type InboxDocument = DocumentType<Inbox>;

@ -315,7 +315,7 @@ export abstract class TcService extends Service {
* socket
*/
unicastNotify(
ctx: TcContext,
ctx: TcPureContext,
userId: string,
eventName: string,
eventData: unknown
@ -332,7 +332,7 @@ export abstract class TcService extends Service {
* socket
*/
listcastNotify(
ctx: TcContext,
ctx: TcPureContext,
userIds: string[],
eventName: string,
eventData: unknown
@ -349,7 +349,7 @@ export abstract class TcService extends Service {
* socket
*/
roomcastNotify(
ctx: TcContext,
ctx: TcPureContext,
roomId: string,
eventName: string,
eventData: unknown
@ -365,7 +365,7 @@ export abstract class TcService extends Service {
* socket
*/
broadcastNotify(
ctx: TcContext,
ctx: TcPureContext,
eventName: string,
eventData: unknown
): Promise<void> {

@ -8,12 +8,17 @@ export interface BuiltinEventMap {
'chat.message.updateMessage':
| {
type: 'add';
groupId?: string;
converseId: string;
messageId: string;
content: string;
meta: MessageMetaStruct;
}
| {
type: 'recall' | 'delete';
groupId?: string;
converseId: string;
messageId: string;
meta: MessageMetaStruct;
};
}

@ -1,6 +1,10 @@
import { Types } from 'mongoose';
import type { InboxDocument, InboxModel } from '../../../models/chat/inbox';
import { TcService, TcContext, TcDbService } from 'tailchat-server-sdk';
import {
TcService,
TcContext,
TcDbService,
TcPureContext,
} from 'tailchat-server-sdk';
/**
*
@ -16,34 +20,120 @@ class InboxService extends TcService {
onInit(): void {
this.registerLocalDb(require('../../../models/chat/inbox').default);
this.registerEventListener('chat.message.updateMessage', (payload) => {
// TODO
});
this.registerEventListener(
'chat.message.updateMessage',
async (payload, ctx) => {
if (
Array.isArray(payload.meta.mentions) &&
payload.meta.mentions.length > 0
) {
const mentions = payload.meta.mentions;
if (payload.type === 'add') {
await Promise.all(
mentions.map((userId) => {
return ctx.call('chat.inbox.appendMessage', {
userId,
groupId: payload.groupId,
converseId: payload.converseId,
messageId: payload.messageId,
messageSnippet: payload.content,
});
})
);
} else if (payload.type === 'delete') {
await Promise.all(
mentions.map((userId) => {
return ctx.call('chat.inbox.removeMessage', {
userId,
groupId: payload.groupId,
converseId: payload.converseId,
messageId: payload.messageId,
});
})
);
}
this.notifyUsersInboxUpdate(ctx, mentions);
}
}
);
this.registerAction('append', this.append, {
this.registerAction('appendMessage', this.appendMessage, {
visibility: 'public',
params: {
userId: { type: 'string', optional: true },
groupId: { type: 'string', optional: true },
converseId: 'string',
messageId: 'string',
messageSnippet: 'string',
},
});
this.registerAction('removeMessage', this.removeMessage, {
visibility: 'public',
params: {
userId: { type: 'string', optional: true },
groupId: { type: 'string', optional: true },
converseId: 'string',
messageId: 'string',
},
});
this.registerAction('all', this.all);
}
async append(
async appendMessage(
ctx: TcContext<{
userId?: string;
groupId?: string;
converseId: string;
messageId: string;
messageSnippet: string;
}>
) {
const { userId = ctx.meta.userId, messageId, messageSnippet } = ctx.params;
const {
userId = ctx.meta.userId,
groupId,
converseId,
messageId,
messageSnippet,
} = ctx.params;
await this.adapter.model.create({
userId,
type: 'message',
message: {
groupId,
converseId,
messageId,
messageSnippet,
},
});
return true;
}
async removeMessage(
ctx: TcContext<{
userId?: string;
groupId?: string;
converseId: string;
messageId: string;
}>
) {
const {
userId = ctx.meta.userId,
groupId,
converseId,
messageId,
messageSnippet,
} = ctx.params;
await this.adapter.model.remove({
userId,
type: 'message',
message: {
groupId,
converseId,
messageId,
},
});
return true;
@ -61,6 +151,18 @@ class InboxService extends TcService {
return await this.transformDocuments(ctx, {}, list);
}
/**
*
*
*
*/
private async notifyUsersInboxUpdate(
ctx: TcPureContext,
userIds: string[]
): Promise<void> {
await this.listcastNotify(ctx, userIds, 'updated', {});
}
}
export default InboxService;

@ -139,6 +139,8 @@ class MessageService extends TcService {
ctx.emit('chat.message.updateMessage', {
type: 'add',
groupId: String(groupId),
converseId: String(converseId),
messageId: String(message._id),
content,
meta,
@ -204,7 +206,10 @@ class MessageService extends TcService {
this.roomcastNotify(ctx, converseId, 'update', json);
ctx.emit('chat.message.updateMessage', {
type: 'recall',
groupId: String(groupId),
converseId: String(converseId),
messageId: String(message._id),
meta: message.meta,
});
return json;
@ -241,7 +246,10 @@ class MessageService extends TcService {
this.roomcastNotify(ctx, converseId, 'delete', { converseId, messageId });
ctx.emit('chat.message.updateMessage', {
type: 'delete',
groupId: String(groupId),
converseId: String(converseId),
messageId: String(message._id),
meta: message.meta,
});
return true;

Loading…
Cancel
Save