From e6e13db307774d121c4f7b2564c6c49f1a881843 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 25 Mar 2023 14:29:41 +0800 Subject: [PATCH] feat: add server getui.service --- .../plugins/com.msgbyte.getui/models/log.ts | 21 ++++- .../services/getui.service.ts | 76 ++++++++++++++++++- 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/server/plugins/com.msgbyte.getui/models/log.ts b/server/plugins/com.msgbyte.getui/models/log.ts index 100d21a1ac1..fd120e6704a 100644 --- a/server/plugins/com.msgbyte.getui/models/log.ts +++ b/server/plugins/com.msgbyte.getui/models/log.ts @@ -1,14 +1,33 @@ import { db } from 'tailchat-server-sdk'; -const { getModelForClass, prop, modelOptions, TimeStamps } = db; +const { getModelForClass, prop, modelOptions, TimeStamps, Severity } = db; @modelOptions({ options: { customName: 'p_getui_log', + allowMixed: Severity.ALLOW, }, }) export class GetuiLog extends TimeStamps implements db.Base { _id: db.Types.ObjectId; id: string; + + @prop() + userId: string; + + @prop() + title: string; + + @prop() + content: string; + + @prop() + payload: object; + + @prop() + success: boolean; + + @prop() + errorMsg?: string; } export type GetuiLogDocument = db.DocumentType; diff --git a/server/plugins/com.msgbyte.getui/services/getui.service.ts b/server/plugins/com.msgbyte.getui/services/getui.service.ts index 6f8592d4ac3..099e4c11741 100644 --- a/server/plugins/com.msgbyte.getui/services/getui.service.ts +++ b/server/plugins/com.msgbyte.getui/services/getui.service.ts @@ -1,4 +1,5 @@ -import { TcService, TcDbService } from 'tailchat-server-sdk'; +import { TcService, TcDbService, InboxStruct, call } from 'tailchat-server-sdk'; +import { GetuiClient } from '../lib/GetuiClient'; import type { GetuiLogDocument, GetuiLogModel } from '../models/log'; /** @@ -8,12 +9,85 @@ interface GetuiService extends TcService, TcDbService {} class GetuiService extends TcService { + client: GetuiClient = null; + get serviceName() { return 'plugin:com.msgbyte.getui'; } + get getuiInfo() { + return { + appId: process.env.GETUI_APPID, + appKey: process.env.GETUI_APPKEY, + masterSecert: process.env.GETUI_MASTERSECRET, + }; + } + + get getuiAvailable(): boolean { + const { appId, appKey, masterSecert } = this.getuiInfo; + if (appId && appKey && masterSecert) { + return true; + } + + return false; + } + onInit() { + if (!this.getuiAvailable) { + console.warn( + '[plugin:com.msgbyte.getui] require env: GETUI_APPID, GETUI_APPKEY, GETUI_MASTERSECRET' + ); + return; + } + + this.initClient(); + this.registerLocalDb(require('../models/log').default); + this.registerEventListener( + 'chat.inbox.append', + async (inboxItem: InboxStruct, ctx) => { + if (inboxItem.type === 'message') { + const userId = inboxItem.userId; + const message = inboxItem.message; + + let title = 'new'; + if (message.groupId) { + const groupInfo = await call(ctx).getGroupInfo(message.groupId); + title = groupInfo.name; + } + const content = message.messageSnippet; + const payload = { + converseId: message.converseId, + groupId: message.groupId, + }; + + try { + await this.client.singlePush(userId, title, content, payload); + await this.adapter.model.create({ + userId, + title, + content, + payload, + success: true, + }); + } catch (err) { + await this.adapter.model.create({ + userId, + title, + content, + payload, + success: false, + errorMsg: String(err), + }); + } + } + } + ); + } + + initClient() { + const { appId, appKey, masterSecert } = this.getuiInfo; + this.client = new GetuiClient(appId, appKey, masterSecert); } }