From 2172c82d3bfadebe2ae6520102985aab05032b2b Mon Sep 17 00:00:00 2001 From: everpcpc Date: Sun, 15 Dec 2024 18:05:01 +0800 Subject: [PATCH] feat: add user network servies (#825) --- lib/types/convert.ts | 22 ++++++++++ lib/types/res.ts | 13 ++++++ routes/__snapshots__/index.test.ts.snap | 43 +++++++++++++++++++ .../routes/__snapshots__/user.test.ts.snap | 1 + routes/private/routes/user.ts | 8 ++++ 5 files changed, 87 insertions(+) diff --git a/lib/types/convert.ts b/lib/types/convert.ts index d275d06d..48d0e584 100644 --- a/lib/types/convert.ts +++ b/lib/types/convert.ts @@ -11,6 +11,7 @@ import { parse as parseTimelineImage } from '@app/lib/timeline/image'; import { parse as parseTimelineMemo } from '@app/lib/timeline/memo'; import type * as res from '@app/lib/types/res.ts'; import { + findNetworkService, findSubjectPlatform, findSubjectRelationType, findSubjectStaffPosition, @@ -105,6 +106,7 @@ export function toUser(user: orm.IUser, fields: orm.IUserFields): res.IUser { site: fields.site, location: fields.location, bio: fields.bio, + networkServices: [], // homepage: toUserHomepage(fields.homepage), }; } @@ -120,6 +122,26 @@ export function toSlimUser(user: orm.IUser): res.ISlimUser { }; } +export function toUserNetworkService(service: orm.IUserNetworkServices): res.IUserNetworkService { + const svc = findNetworkService(service.serviceID); + if (!svc) { + return { + title: '', + name: '', + url: '', + color: '', + account: service.account, + }; + } + return { + title: svc.title, + name: svc.name, + url: svc.url || '', + color: svc.bg_color, + account: service.account, + }; +} + export function toFriend(user: orm.IUser, friend: orm.IFriend): res.IFriend { return { user: toSlimUser(user), diff --git a/lib/types/res.ts b/lib/types/res.ts index 0b7970d7..9ed102ff 100644 --- a/lib/types/res.ts +++ b/lib/types/res.ts @@ -68,6 +68,18 @@ export const Avatar = t.Object( { $id: 'Avatar', title: 'Avatar' }, ); +export type IUserNetworkService = Static; +export const UserNetworkService = t.Object( + { + name: t.String(), + title: t.String(), + url: t.String(), + color: t.String(), + account: t.String(), + }, + { $id: 'UserNetworkService', title: 'UserNetworkService' }, +); + export type IUserHomepage = Static; export const UserHomepage = t.Object( { @@ -104,6 +116,7 @@ export const User = t.Object( site: t.String(), location: t.String(), bio: t.String(), + networkServices: t.Array(UserNetworkService), // wait for permission // homepage: t.Ref(UserHomepage), }, diff --git a/routes/__snapshots__/index.test.ts.snap b/routes/__snapshots__/index.test.ts.snap index 0e37cdb2..3b6f9af7 100644 --- a/routes/__snapshots__/index.test.ts.snap +++ b/routes/__snapshots__/index.test.ts.snap @@ -2122,6 +2122,28 @@ exports[`should build private api spec 1`] = ` type: integer location: type: string + networkServices: + items: + properties: + account: + type: string + color: + type: string + name: + type: string + title: + type: string + url: + type: string + required: + - name + - title + - url + - color + - account + title: UserNetworkService + type: object + type: array nickname: example: Sai🖖 type: string @@ -2147,6 +2169,7 @@ exports[`should build private api spec 1`] = ` - site - location - bio + - networkServices title: User type: object UserCharacterCollection: @@ -2304,6 +2327,26 @@ exports[`should build private api spec 1`] = ` - count - detail type: object + UserNetworkService: + properties: + account: + type: string + color: + type: string + name: + type: string + title: + type: string + url: + type: string + required: + - name + - title + - url + - color + - account + title: UserNetworkService + type: object UserPersonCollection: properties: createdAt: diff --git a/routes/private/routes/__snapshots__/user.test.ts.snap b/routes/private/routes/__snapshots__/user.test.ts.snap index 37b8aa54..46d40871 100644 --- a/routes/private/routes/__snapshots__/user.test.ts.snap +++ b/routes/private/routes/__snapshots__/user.test.ts.snap @@ -22,6 +22,7 @@ Object { "id": 382951, "joinedAt": 1512262276, "location": "", + "networkServices": Array [], "nickname": "树洞酱", "sign": "treeholechan@gmail.com 密码:lovemeplease", "site": "bgm.tv/character/13307", diff --git a/routes/private/routes/user.ts b/routes/private/routes/user.ts index 6a11eefe..a3a10f79 100644 --- a/routes/private/routes/user.ts +++ b/routes/private/routes/user.ts @@ -248,6 +248,14 @@ export async function setup(app: App) { throw new NotFoundError(`user ${username}`); } const user = convert.toUser(data.chii_members, data.chii_memberfields); + const svcs = await db + .select() + .from(schema.chiiUserNetworkServices) + .where(op.eq(schema.chiiUserNetworkServices.uid, user.id)) + .execute(); + for (const svc of svcs) { + user.networkServices.push(convert.toUserNetworkService(svc)); + } return user; }, );