2024-11-22 17:12:39 +08:00
|
|
|
|
import { PluginRequestHandleReq } from "../plugin";
|
|
|
|
|
|
import { Registrable } from "../registry/index.js";
|
2024-11-23 23:58:31 +08:00
|
|
|
|
import { FormItemProps, HistoryResult, Pipeline } from "../dt/index.js";
|
2024-11-22 17:12:39 +08:00
|
|
|
|
import { HttpClient, ILogger, utils } from "@certd/basic";
|
|
|
|
|
|
import * as _ from "lodash-es";
|
2024-11-23 23:58:31 +08:00
|
|
|
|
import { IEmailService } from "../service/index.js";
|
2024-11-30 22:35:26 +08:00
|
|
|
|
import { isPlus } from "@certd/plus-core";
|
2024-11-22 17:12:39 +08:00
|
|
|
|
|
|
|
|
|
|
export type NotificationBody = {
|
2024-11-27 12:36:28 +08:00
|
|
|
|
userId?: number;
|
2024-11-22 17:12:39 +08:00
|
|
|
|
title: string;
|
|
|
|
|
|
content: string;
|
2024-11-27 12:36:28 +08:00
|
|
|
|
pipeline?: Pipeline;
|
|
|
|
|
|
pipelineId?: number;
|
2024-11-23 23:58:31 +08:00
|
|
|
|
result?: HistoryResult;
|
2024-11-27 12:36:28 +08:00
|
|
|
|
historyId?: number;
|
2024-11-23 23:58:31 +08:00
|
|
|
|
errorMessage?: string;
|
|
|
|
|
|
url?: string;
|
2024-11-22 17:12:39 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export type NotificationRequestHandleReqInput<T = any> = {
|
|
|
|
|
|
id?: number;
|
|
|
|
|
|
title?: string;
|
|
|
|
|
|
access: T;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export type NotificationRequestHandleReq<T = any> = PluginRequestHandleReq<NotificationRequestHandleReqInput<T>>;
|
|
|
|
|
|
|
|
|
|
|
|
export type NotificationInputDefine = FormItemProps & {
|
|
|
|
|
|
title: string;
|
|
|
|
|
|
required?: boolean;
|
|
|
|
|
|
encrypt?: boolean;
|
|
|
|
|
|
};
|
|
|
|
|
|
export type NotificationDefine = Registrable & {
|
2024-11-30 01:57:09 +08:00
|
|
|
|
needPlus?: boolean;
|
2024-11-22 17:12:39 +08:00
|
|
|
|
input?: {
|
|
|
|
|
|
[key: string]: NotificationInputDefine;
|
|
|
|
|
|
};
|
|
|
|
|
|
};
|
2024-11-23 23:58:31 +08:00
|
|
|
|
|
|
|
|
|
|
export type NotificationInstanceConfig = {
|
|
|
|
|
|
id: number;
|
|
|
|
|
|
type: string;
|
2024-11-27 12:36:28 +08:00
|
|
|
|
name: string;
|
2024-11-23 23:58:31 +08:00
|
|
|
|
userId: number;
|
|
|
|
|
|
setting: {
|
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
|
};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-12-11 11:30:32 +08:00
|
|
|
|
export type NotificationSendReq = {
|
|
|
|
|
|
id?: number;
|
|
|
|
|
|
useDefault?: boolean;
|
|
|
|
|
|
useEmail?: boolean;
|
|
|
|
|
|
emailAddress?: string;
|
2024-12-12 17:28:33 +08:00
|
|
|
|
logger: ILogger;
|
2024-12-11 11:30:32 +08:00
|
|
|
|
body: NotificationBody;
|
|
|
|
|
|
};
|
2024-11-22 17:12:39 +08:00
|
|
|
|
export interface INotificationService {
|
2024-11-23 23:58:31 +08:00
|
|
|
|
getById(id: number): Promise<NotificationInstanceConfig>;
|
2024-11-27 12:36:28 +08:00
|
|
|
|
getDefault(): Promise<NotificationInstanceConfig>;
|
2024-12-11 11:30:32 +08:00
|
|
|
|
send(req: NotificationSendReq): Promise<void>;
|
2024-11-22 17:12:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-23 23:58:31 +08:00
|
|
|
|
export interface INotification {
|
2024-11-22 17:12:39 +08:00
|
|
|
|
ctx: NotificationContext;
|
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export type NotificationContext = {
|
|
|
|
|
|
http: HttpClient;
|
|
|
|
|
|
logger: ILogger;
|
|
|
|
|
|
utils: typeof utils;
|
|
|
|
|
|
emailService: IEmailService;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export abstract class BaseNotification implements INotification {
|
2024-11-30 22:35:26 +08:00
|
|
|
|
define!: NotificationDefine;
|
2024-11-22 17:12:39 +08:00
|
|
|
|
ctx!: NotificationContext;
|
|
|
|
|
|
http!: HttpClient;
|
|
|
|
|
|
logger!: ILogger;
|
2024-11-30 22:35:26 +08:00
|
|
|
|
|
|
|
|
|
|
async doSend(body: NotificationBody) {
|
|
|
|
|
|
if (this.define.needPlus && !isPlus()) {
|
|
|
|
|
|
body.content = `${body.content}\n\n注意:此通知渠道已调整为专业版功能,后续版本将不再支持发送,请尽快修改或升级为专业版`;
|
|
|
|
|
|
}
|
|
|
|
|
|
return await this.send(body);
|
|
|
|
|
|
}
|
2024-11-22 17:12:39 +08:00
|
|
|
|
abstract send(body: NotificationBody): Promise<void>;
|
2024-11-23 23:58:31 +08:00
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
|
|
|
|
async onInstance() {}
|
2024-11-22 17:12:39 +08:00
|
|
|
|
setCtx(ctx: NotificationContext) {
|
|
|
|
|
|
this.ctx = ctx;
|
|
|
|
|
|
this.http = ctx.http;
|
|
|
|
|
|
this.logger = ctx.logger;
|
|
|
|
|
|
}
|
2024-11-30 22:35:26 +08:00
|
|
|
|
setDefine = (define: NotificationDefine) => {
|
|
|
|
|
|
this.define = define;
|
|
|
|
|
|
};
|
2024-11-22 17:12:39 +08:00
|
|
|
|
|
|
|
|
|
|
async onRequest(req: NotificationRequestHandleReq) {
|
|
|
|
|
|
if (!req.action) {
|
|
|
|
|
|
throw new Error("action is required");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let methodName = req.action;
|
|
|
|
|
|
if (!req.action.startsWith("on")) {
|
|
|
|
|
|
methodName = `on${_.upperFirst(req.action)}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
const method = this[methodName];
|
|
|
|
|
|
if (method) {
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
return await this[methodName](req.data);
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new Error(`action ${req.action} not found`);
|
|
|
|
|
|
}
|
2024-11-25 11:35:16 +08:00
|
|
|
|
|
|
|
|
|
|
async onTestRequest() {
|
2025-03-14 13:16:48 +08:00
|
|
|
|
return await this.doSend({
|
2024-11-25 11:35:16 +08:00
|
|
|
|
userId: 0,
|
2025-01-20 18:45:07 +08:00
|
|
|
|
title: "【Certd】测试通知【*.foo.com】,标题长度测试、测试、测试",
|
2025-03-17 18:20:15 +08:00
|
|
|
|
content: `测试通知,*.foo.com
|
|
|
|
|
|
换行测试
|
|
|
|
|
|
`,
|
2024-11-25 11:35:16 +08:00
|
|
|
|
pipeline: {
|
|
|
|
|
|
id: 1,
|
2025-01-20 18:45:07 +08:00
|
|
|
|
title: "证书申请成功【测试流水线】",
|
2024-11-25 11:35:16 +08:00
|
|
|
|
} as any,
|
|
|
|
|
|
pipelineId: 1,
|
|
|
|
|
|
historyId: 1,
|
2024-11-26 15:13:57 +08:00
|
|
|
|
url: "https://certd.docmirror.cn",
|
2024-11-25 11:35:16 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-11-22 17:12:39 +08:00
|
|
|
|
}
|