feat: add chat.inbox.batchAppend action which can batch add inbox item

pull/100/head
moonrailgun 2 years ago
parent 5ed73d7647
commit 3ad1e4410c

@ -1350,6 +1350,9 @@ importers:
oidc-provider:
specifier: ^7.10.6
version: 7.11.5
p-map:
specifier: ^4.0.0
version: 4.0.0
qs:
specifier: ^6.10.3
version: 6.11.0

@ -66,6 +66,7 @@
"nanoid": "^3.1.23",
"nodemailer": "^6.7.2",
"oidc-provider": "^7.10.6",
"p-map": "^4.0.0",
"qs": "^6.10.3",
"redlock": "^4.2.0",
"send": "^0.18.0",

@ -6,6 +6,7 @@ import {
TcPureContext,
InboxStruct,
} from 'tailchat-server-sdk';
import pMap from 'p-map';
/**
*
@ -70,6 +71,14 @@ class InboxService extends TcService {
payload: 'any',
},
});
this.registerAction('batchAppend', this.batchAppend, {
visibility: 'public',
params: {
userIds: { type: 'array', items: 'string' },
type: 'string',
payload: 'any',
},
});
this.registerAction('removeMessage', this.removeMessage, {
visibility: 'public',
params: {
@ -115,6 +124,48 @@ class InboxService extends TcService {
return true;
}
/**
* append
*/
async batchAppend(
ctx: TcContext<{
userIds: string[];
type: string;
payload: Record<string, any>;
}>
) {
const { userIds, type, payload } = ctx.params;
const docs = await this.adapter.model.create(
userIds.map((userId) => ({
userId,
type,
payload,
}))
);
const inboxItems: InboxStruct[] = await this.transformDocuments(
ctx,
{},
docs
);
pMap(
inboxItems,
async (inboxItem) => {
await Promise.all([
this.notifyUsersInboxAppend(ctx, [inboxItem.userId], inboxItem),
this.emitInboxAppendEvent(ctx, inboxItem),
]);
},
{
concurrency: 10,
}
);
return true;
}
async removeMessage(
ctx: TcContext<{
userId?: string;

Loading…
Cancel
Save