mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.7 KiB
TypeScript
103 lines
2.7 KiB
TypeScript
import { RequestError } from 'got';
|
|
import { TcService, TcDbService, InboxStruct, call } from 'tailchat-server-sdk';
|
|
import { GetuiClient } from '../lib/GetuiClient';
|
|
import type { GetuiLogDocument, GetuiLogModel } from '../models/log';
|
|
|
|
/**
|
|
* Support Getui Notify in chinese mainland
|
|
*/
|
|
interface GetuiService
|
|
extends TcService,
|
|
TcDbService<GetuiLogDocument, GetuiLogModel> {}
|
|
class GetuiService extends TcService {
|
|
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() {
|
|
this.registerAvailableAction(() => this.getuiAvailable);
|
|
|
|
if (!this.getuiAvailable) {
|
|
console.warn(
|
|
'[plugin:com.msgbyte.getui] require env: GETUI_APPID, GETUI_APPKEY, GETUI_MASTERSECRET'
|
|
);
|
|
return;
|
|
}
|
|
|
|
const { appId, appKey, masterSecert } = this.getuiInfo;
|
|
const client = new GetuiClient(appId, appKey, masterSecert);
|
|
|
|
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.payload;
|
|
|
|
let title = 'new';
|
|
if (message.groupId) {
|
|
const groupInfo = await call(ctx).getGroupInfo(message.groupId);
|
|
title = groupInfo.name;
|
|
}
|
|
const content = message.messagePlainContent ?? message.messageSnippet;
|
|
const payload = {
|
|
converseId: message.converseId,
|
|
groupId: message.groupId,
|
|
};
|
|
|
|
try {
|
|
const { requestId } = await client.singlePush(
|
|
userId,
|
|
title,
|
|
content,
|
|
payload
|
|
);
|
|
await this.adapter.model.create({
|
|
requestId,
|
|
userId,
|
|
title,
|
|
content,
|
|
payload,
|
|
success: true,
|
|
});
|
|
} catch (err) {
|
|
let errorMsg = String(err);
|
|
|
|
if (err instanceof RequestError && err.response) {
|
|
errorMsg = String(err.response.body);
|
|
}
|
|
await this.adapter.model.create({
|
|
userId,
|
|
title,
|
|
content,
|
|
payload,
|
|
success: false,
|
|
errorMsg,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
export default GetuiService;
|