Skip to content

Commit

Permalink
feat: add friend link apply notify
Browse files Browse the repository at this point in the history
  • Loading branch information
OXeu committed Jul 11, 2024
1 parent 200631c commit b67f61b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
22 changes: 19 additions & 3 deletions server/src/services/friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -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) {
Expand Down Expand Up @@ -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({
Expand All @@ -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) {
Expand Down Expand Up @@ -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({
Expand Down
1 change: 1 addition & 0 deletions server/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function setup() {
}
return {
uid: user.id,
username: user.username,
admin: user.permission === 1,
}
})
Expand Down

0 comments on commit b67f61b

Please sign in to comment.