refactor(protocol): map friend and member pages to domain types

FetchFriendListPage and FetchGroupMemberListPage deserialized as
identity functions and leaked Oidb property codes into ContactsApi.

Each page now returns a domain page. Contacts keeps pagination,
category assembly, member cache, and robot classification.
This commit is contained in:
DrSmoothl committed 2026-08-16 21:12:59 +08:00
1 parent 23352dcad4
commit eee3c49f2d
5 files changed
+215 -102

No files matched your search

@@ -5,11 +5,54 @@
// while-loop until the server stops emitting a cookie and
// concatenates results into the public FriendInfo[] shape.
import type { OidbBase, OidbSvcTrpcTcp0xFD4_1Response } from '@snowluma/proto-defs/oidb';
import type { OidbBase, OidbFriend, OidbSvcTrpcTcp0xFD4_1Response } from '@snowluma/proto-defs/oidb';
import type { OidbFriendListRequest } from '@snowluma/proto-defs/oidb-actions/base';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { FriendInfo } from '../../qq-info';
import { invokeOidb, type OidbSender } from '../../oidb-service';
export interface FriendListPageEntry {
friend: FriendInfo;
categoryId: number;
}
export interface FriendListPageCategory {
categoryId: number;
categoryName: string;
memberCount: number;
sortId: number;
}
export interface FriendListPage {
entries: FriendListPageEntry[];
categories: FriendListPageCategory[];
cookie?: Uint8Array;
}
function friendProperties(raw: OidbFriend): Map<number, string> {
const props = new Map<number, string>();
for (const additional of raw.additional ?? []) {
if ((additional.type ?? 0) !== 1 || !additional.layer1) continue;
for (const property of additional.layer1.properties ?? []) {
props.set(property.code ?? 0, property.value ?? '');
}
}
return props;
}
function mapFriend(raw: OidbFriend): FriendListPageEntry {
const props = friendProperties(raw);
return {
categoryId: raw.customGroup ?? 0,
friend: {
uin: raw.uin ?? 0,
uid: raw.uid ?? '',
nickname: props.get(20002) ?? String(raw.uin ?? 0),
remark: props.get(103) ?? '',
},
};
}
export namespace FetchFriendListPage {
export const command = 0xFD4;
export const subCommand = 1;
@@ -40,7 +83,16 @@ export namespace FetchFriendListPage {
return body;
};
export const deserialize = (_ctx: Deps, body: OidbSvcTrpcTcp0xFD4_1Response): OidbSvcTrpcTcp0xFD4_1Response => body;
export const deserialize = (_ctx: Deps, body: OidbSvcTrpcTcp0xFD4_1Response): FriendListPage => ({
entries: (body.friends ?? []).map(mapFriend),
categories: (body.categories ?? []).map((raw) => ({
categoryId: raw.categoryId ?? 0,
categoryName: raw.categoryName ?? '',
memberCount: raw.memberCount ?? 0,
sortId: raw.sortId ?? 0,
})),
cookie: body.cookie?.length ? Uint8Array.from(body.cookie) : undefined,
});
export const encode = (env: OidbBase<OidbFriendListRequest>): Uint8Array =>
protobuf_encode<OidbBase<OidbFriendListRequest>>(env);
@@ -48,6 +100,6 @@ export namespace FetchFriendListPage {
export const decode = (bytes: Uint8Array): OidbBase<OidbSvcTrpcTcp0xFD4_1Response> =>
protobuf_decode<OidbBase<OidbSvcTrpcTcp0xFD4_1Response>>(bytes);
export const invoke = (deps: Deps, params: Params): Promise<OidbSvcTrpcTcp0xFD4_1Response> =>
export const invoke = (deps: Deps, params: Params): Promise<FriendListPage> =>
invokeOidb(deps, FetchFriendListPage, params);
}
@@ -9,8 +9,22 @@
import type { OidbBase, OidbSvcTrpcTcp0xFE7_3Response } from '@snowluma/proto-defs/oidb';
import type { OidbGroupMemberListRequest } from '@snowluma/proto-defs/oidb-actions/base';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { GroupMemberInfo } from '../../qq-info';
import { invokeOidb, type OidbSender } from '../../oidb-service';
export interface GroupMemberListPage {
members: GroupMemberInfo[];
token: string;
}
function permissionToRole(permission: number): string {
switch (permission) {
case 1: return 'owner';
case 2: return 'admin';
default: return 'member';
}
}
export namespace FetchGroupMemberListPage {
export const command = 0xFE7;
export const subCommand = 3;
@@ -41,7 +55,22 @@ export namespace FetchGroupMemberListPage {
return body;
};
export const deserialize = (_ctx: Deps, body: OidbSvcTrpcTcp0xFE7_3Response): OidbSvcTrpcTcp0xFE7_3Response => body;
export const deserialize = (_ctx: Deps, body: OidbSvcTrpcTcp0xFE7_3Response): GroupMemberListPage => ({
members: (body.members ?? []).map((raw) => ({
uin: raw.uin?.uin ?? 0,
uid: raw.uin?.uid ?? '',
nickname: raw.memberName ?? '',
card: raw.memberCard?.memberCard ?? '',
isRobot: false,
role: permissionToRole(raw.permission ?? 0),
level: raw.level?.level ?? 0,
title: raw.specialTitle ?? '',
joinTime: raw.joinTimestamp ?? 0,
lastSentTime: raw.lastMsgTimestamp ?? 0,
shutUpTime: raw.shutUpTimestamp ?? 0,
})),
token: body.token ?? '',
});
export const encode = (env: OidbBase<OidbGroupMemberListRequest>): Uint8Array =>
protobuf_encode<OidbBase<OidbGroupMemberListRequest>>(env);
@@ -49,6 +78,6 @@ export namespace FetchGroupMemberListPage {
export const decode = (bytes: Uint8Array): OidbBase<OidbSvcTrpcTcp0xFE7_3Response> =>
protobuf_decode<OidbBase<OidbSvcTrpcTcp0xFE7_3Response>>(bytes);
export const invoke = (deps: Deps, params: Params): Promise<OidbSvcTrpcTcp0xFE7_3Response> =>
export const invoke = (deps: Deps, params: Params): Promise<GroupMemberListPage> =>
invokeOidb(deps, FetchGroupMemberListPage, params);
}