Skip to content

Commit

Permalink
feat: add server getui.service
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Mar 25, 2023
1 parent 8b5b786 commit e6e13db
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
21 changes: 20 additions & 1 deletion server/plugins/com.msgbyte.getui/models/log.ts
Original file line number Diff line number Diff line change
@@ -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<GetuiLog>;
Expand Down
76 changes: 75 additions & 1 deletion server/plugins/com.msgbyte.getui/services/getui.service.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand All @@ -8,12 +9,85 @@ interface GetuiService
extends TcService,
TcDbService<GetuiLogDocument, GetuiLogModel> {}
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);
}
}

Expand Down

0 comments on commit e6e13db

Please sign in to comment.