perf: 优化证书申请成功通知发送方式

This commit is contained in:
xiaojunnuo
2024-11-27 12:36:28 +08:00
parent 7e5ea0cee0
commit 8002a56efc
24 changed files with 382 additions and 42 deletions
@@ -69,7 +69,7 @@ export class HandleController extends BaseController {
// }
// }
const notification = newNotification(body.typeName, input, {
const notification = await newNotification(body.typeName, input, {
http,
logger,
utils,
@@ -1,5 +1,5 @@
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { Constants, CrudController } from '@certd/lib-server';
import { Constants, CrudController, ValidateException } from '@certd/lib-server';
import { NotificationService } from '../../modules/pipeline/service/notification-service.js';
import { AuthService } from '../../modules/sys/authority/service/auth-service.js';
@@ -84,8 +84,30 @@ export class NotificationController extends CrudController<NotificationService>
@Post('/simpleInfo', { summary: Constants.per.authOnly })
async simpleInfo(@Query('id') id: number) {
if (id === 0) {
//获取默认
const res = await this.service.getDefault(this.getUserId());
if (!res) {
throw new ValidateException('默认通知配置不存在');
}
const simple = await this.service.getSimpleInfo(res.id);
return this.ok(simple);
}
await this.authService.checkEntityUserId(this.ctx, this.service, id);
const res = await this.service.getSimpleInfo(id);
return this.ok(res);
}
@Post('/getDefaultId', { summary: Constants.per.authOnly })
async getDefaultId() {
const res = await this.service.getDefault(this.getUserId());
return this.ok(res?.id);
}
@Post('/setDefault', { summary: Constants.per.authOnly })
async setDefault(@Query('id') id: number) {
await this.service.checkUserId(id, this.getUserId());
const res = await this.service.setDefault(id, this.getUserId());
return this.ok(res);
}
}
@@ -17,6 +17,9 @@ export class NotificationEntity {
@Column({ name: 'setting', comment: '通知配置', length: 10240 })
setting: string;
@Column({ name: 'is_default', comment: '是否默认' })
isDefault: boolean;
@Column({
name: 'create_time',
comment: '创建时间',
@@ -1,14 +1,20 @@
import { INotificationService } from '@certd/pipeline';
import { NotificationService } from './notification-service.js';
export class NotificationGetter implements INotificationService {
userId: number;
getter: <T>(id: any, userId?: number) => Promise<T>;
constructor(userId: number, getter: (id: any, userId: number) => Promise<any>) {
notificationService: NotificationService;
constructor(userId: number, notificationService: NotificationService) {
this.userId = userId;
this.getter = getter;
this.notificationService = notificationService;
}
async getById<T = any>(id: any) {
return await this.getter<T>(id, this.userId);
async getDefault() {
return await this.notificationService.getDefault(this.userId);
}
async getById(id: any) {
return await this.notificationService.getById(id, this.userId);
}
}
@@ -50,14 +50,60 @@ export class NotificationService extends BaseService<NotificationEntity> {
},
});
if (!res) {
throw new ValidateException('通知配置不存在');
throw new ValidateException(`通知配置不存在<${id}>`);
}
return this.buildNotificationInstanceConfig(res);
}
private buildNotificationInstanceConfig(res: NotificationEntity) {
const setting = JSON.parse(res.setting);
return {
id: res.id,
type: res.type,
name: res.name,
userId: res.userId,
setting,
};
}
async getDefault(userId: number): Promise<NotificationInstanceConfig> {
const res = await this.repository.findOne({
where: {
userId,
},
order: {
isDefault: 'DESC',
},
});
if (!res) {
throw new ValidateException('默认通知配置不存在');
}
return this.buildNotificationInstanceConfig(res);
}
async setDefault(id: number, userId: number) {
if (!id) {
throw new ValidateException('id不能为空');
}
if (!userId) {
throw new ValidateException('userId不能为空');
}
await this.repository.update(
{
userId,
},
{
isDefault: false,
}
);
await this.repository.update(
{
id,
userId,
},
{
isDefault: true,
}
);
}
}
@@ -393,7 +393,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
};
const accessGetter = new AccessGetter(userId, this.accessService.getById.bind(this.accessService));
const cnameProxyService = new CnameProxyService(userId, this.cnameRecordService.getWithAccessByDomain.bind(this.cnameRecordService));
const notificationGetter = new NotificationGetter(userId, this.notificationService.getById.bind(this.notificationService));
const notificationGetter = new NotificationGetter(userId, this.notificationService);
const executor = new Executor({
user,
pipeline,
@@ -30,6 +30,18 @@ export class BarkNotification extends BaseNotification {
helper: '你的bark服务地址+key',
})
webhook = '';
@NotificationInput({
title: '忽略证书校验',
value: false,
component: {
name: 'a-switch',
vModel: 'checked',
},
required: false,
})
skipSslVerify: boolean;
async send(body: NotificationBody) {
if (!this.webhook) {
throw new Error('服务器地址不能为空');
@@ -47,6 +59,7 @@ export class BarkNotification extends BaseNotification {
'Content-Type': 'application/json; charset=utf-8',
},
data: payload,
skipSslVerify: this.skipSslVerify,
});
}
}
@@ -43,6 +43,17 @@ export class ServerChanNotification extends BaseNotification {
})
noip: boolean;
@NotificationInput({
title: '忽略证书校验',
value: false,
component: {
name: 'a-switch',
vModel: 'checked',
},
required: false,
})
skipSslVerify: boolean;
async send(body: NotificationBody) {
if (!this.sendKey) {
throw new Error('sendKey不能为空');
@@ -54,6 +65,7 @@ export class ServerChanNotification extends BaseNotification {
text: body.title,
desp: body.content + '[查看详情](' + body.url + ')',
},
skipSslVerify: this.skipSslVerify,
});
}
}
@@ -49,6 +49,17 @@ export class VoceChatNotification extends BaseNotification {
})
targetId = '';
@NotificationInput({
title: '忽略证书校验',
value: false,
component: {
name: 'a-switch',
vModel: 'checked',
},
required: false,
})
skipSslVerify: boolean;
async send(body: NotificationBody) {
if (!this.apiKey) {
throw new Error('API Key不能为空');
@@ -68,6 +79,7 @@ export class VoceChatNotification extends BaseNotification {
'Content-Type': 'text/markdown',
},
data: `# ${body.title}\n\n${body.content}\n[查看详情](${body.url})`,
skipSslVerify: this.skipSslVerify,
});
}
}
@@ -82,6 +82,17 @@ export class WebhookNotification extends BaseNotification {
})
template = '';
@NotificationInput({
title: '忽略证书校验',
value: false,
component: {
name: 'a-switch',
vModel: 'checked',
},
required: false,
})
skipSslVerify: boolean;
replaceTemplate(target: string, body: any, urlEncode = false) {
let bodyStr = target;
const keys = Object.keys(body);
@@ -143,6 +154,7 @@ export class WebhookNotification extends BaseNotification {
...headers,
},
data: data,
skipSslVerify: this.skipSslVerify,
});
} catch (e) {
if (e.response?.data) {