From b67f61b373bab9f599fe14cb30dc69a3e77b3ee2 Mon Sep 17 00:00:00 2001 From: Xeu Date: Thu, 11 Jul 2024 22:51:26 +0800 Subject: [PATCH] feat: add friend link apply notify --- server/src/services/friends.ts | 22 +++++++++++++++++++--- server/src/setup.ts | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/server/src/services/friends.ts b/server/src/services/friends.ts index 4217b761..0f69cfa1 100644 --- a/server/src/services/friends.ts +++ b/server/src/services/friends.ts @@ -6,11 +6,14 @@ import type { Env } from "../db/db"; import * as schema from "../db/schema"; import { friends } from "../db/schema"; import { setup } from "../setup"; -import { getDB } from "../utils/di"; import { ClientConfig, ServerConfig } from "../utils/cache"; +import { Config } from "../utils/config"; +import { getDB, getEnv } from "../utils/di"; +import { notify } from "../utils/webhook"; export function FriendService() { const db: DB = getDB(); + const env: Env = getEnv(); return new Elysia({ aot: false }) .use(setup()) .group('/friend', (group) => @@ -20,7 +23,7 @@ export function FriendService() { const apply_list = await db.query.friends.findFirst({ where: eq(friends.uid, uid_num ?? null) }); return { friend_list, apply_list }; }) - .post('/', async ({ admin, uid, set, body: { name, desc, avatar, url } }) => { + .post('/', async ({ admin, uid, username, set, body: { name, desc, avatar, url } }) => { const config = ClientConfig() const enable = await config.getOrDefault('friend_apply_enable', true) if (!enable && !admin) { @@ -58,6 +61,13 @@ export function FriendService() { uid: uid_num, accepted, }); + + if (!admin) { + const webhookUrl = await ServerConfig().get(Config.webhookUrl) || env.WEBHOOK_URL; + const content = `${env.FRONTEND_URL}/friends\n${username} 申请友链: ${name}\n${desc}\n${url}`; + // notify + await notify(webhookUrl, content); + } return 'OK'; }, { body: t.Object({ @@ -67,7 +77,7 @@ export function FriendService() { url: t.String(), }) }) - .put('/:id', async ({ admin, uid, set, params: { id }, body: { name, desc, avatar, url, accepted } }) => { + .put('/:id', async ({ admin, uid, username, set, params: { id }, body: { name, desc, avatar, url, accepted } }) => { const config = ClientConfig() const enable = await config.getOrDefault('friend_apply_enable', true) if (!enable && !admin) { @@ -102,6 +112,12 @@ export function FriendService() { url: wrap(url), accepted: accepted === undefined ? undefined : accepted, }).where(eq(friends.id, parseInt(id))); + if (!admin) { + const webhookUrl = await ServerConfig().get(Config.webhookUrl) || env.WEBHOOK_URL; + const content = `${env.FRONTEND_URL}/friends\n${username} 更新友链: ${name}\n${desc}\n${url}`; + // notify + await notify(webhookUrl, content); + } return 'OK'; }, { body: t.Object({ diff --git a/server/src/setup.ts b/server/src/setup.ts index 2c29db7b..24721944 100644 --- a/server/src/setup.ts +++ b/server/src/setup.ts @@ -66,6 +66,7 @@ export function setup() { } return { uid: user.id, + username: user.username, admin: user.permission === 1, } })