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:
5 files changed
+215
-102
No files matched your search
@@ -1,5 +1,9 @@
|
||||
import { FetchDownloadRkeys } from '@snowluma/protocol/oidb-services/contacts/fetch-download-rkeys';
|
||||
import { FetchFriendListPage } from '@snowluma/protocol/oidb-services/contacts/fetch-friend-list-page';
|
||||
import {
|
||||
FetchFriendListPage,
|
||||
type FriendListPageCategory,
|
||||
type FriendListPageEntry,
|
||||
} from '@snowluma/protocol/oidb-services/contacts/fetch-friend-list-page';
|
||||
import { FetchGroupDetail } from '@snowluma/protocol/oidb-services/contacts/fetch-group-detail';
|
||||
import { FetchGroupList } from '@snowluma/protocol/oidb-services/contacts/fetch-group-list';
|
||||
import { FetchGroupMemberListPage } from '@snowluma/protocol/oidb-services/contacts/fetch-group-member-list-page';
|
||||
@@ -41,35 +45,9 @@ function isGroupAllMuted(expireTs?: number, nowSec = Math.floor(Date.now() / 100
|
||||
return (expireTs ?? 0) > nowSec;
|
||||
}
|
||||
|
||||
// ─── Helpers (previously in bridge-contacts.ts) ───────────────────
|
||||
|
||||
type FriendPropertySource = {
|
||||
additional?: Array<{
|
||||
type?: number;
|
||||
layer1?: {
|
||||
properties?: Array<{
|
||||
code?: number;
|
||||
value?: string;
|
||||
}>;
|
||||
};
|
||||
}>;
|
||||
};
|
||||
|
||||
interface FriendRosterEntry {
|
||||
friend: FriendInfo;
|
||||
categoryId: number;
|
||||
}
|
||||
|
||||
interface FriendCategoryMeta {
|
||||
categoryId: number;
|
||||
categoryName: string;
|
||||
memberCount: number;
|
||||
sortId: number;
|
||||
}
|
||||
|
||||
interface FriendRoster {
|
||||
entries: FriendRosterEntry[];
|
||||
categories: FriendCategoryMeta[];
|
||||
entries: FriendListPageEntry[];
|
||||
categories: FriendListPageCategory[];
|
||||
}
|
||||
|
||||
export interface SetFriendCategoryParams {
|
||||
@@ -78,25 +56,6 @@ export interface SetFriendCategoryParams {
|
||||
categoryName?: string;
|
||||
}
|
||||
|
||||
export function buildFriendProperties(raw: FriendPropertySource): 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;
|
||||
}
|
||||
|
||||
export function permissionToRole(permission: number): string {
|
||||
switch (permission) {
|
||||
case 1: return 'owner';
|
||||
case 2: return 'admin';
|
||||
default: return 'member';
|
||||
}
|
||||
}
|
||||
|
||||
export function isRobotUin(uin: number, ranges: readonly RobotUinRange[]): boolean {
|
||||
return Number.isInteger(uin)
|
||||
&& uin > 0
|
||||
@@ -179,36 +138,19 @@ export class ContactsApi {
|
||||
}
|
||||
|
||||
private async fetchFriendRoster(): Promise<FriendRoster> {
|
||||
const entries: FriendRosterEntry[] = [];
|
||||
const categories = new Map<number, FriendCategoryMeta>();
|
||||
const entries: FriendListPageEntry[] = [];
|
||||
const categories = new Map<number, FriendListPageCategory>();
|
||||
const seenCookies = new Set<string>();
|
||||
let cookie: Uint8Array | undefined;
|
||||
|
||||
for (;;) {
|
||||
const resp = await FetchFriendListPage.invoke(this.ctx, { cookie });
|
||||
for (const raw of resp.friends ?? []) {
|
||||
const props = buildFriendProperties(raw);
|
||||
entries.push({
|
||||
categoryId: raw.customGroup ?? 0,
|
||||
friend: {
|
||||
uin: raw.uin ?? 0,
|
||||
uid: raw.uid ?? '',
|
||||
nickname: props.get(20002) ?? String(raw.uin ?? 0),
|
||||
remark: props.get(103) ?? '',
|
||||
},
|
||||
});
|
||||
}
|
||||
for (const raw of resp.categories ?? []) {
|
||||
const categoryId = raw.categoryId ?? 0;
|
||||
categories.set(categoryId, {
|
||||
categoryId,
|
||||
categoryName: raw.categoryName ?? '',
|
||||
memberCount: raw.memberCount ?? 0,
|
||||
sortId: raw.sortId ?? 0,
|
||||
});
|
||||
const page = await FetchFriendListPage.invoke(this.ctx, { cookie });
|
||||
entries.push(...page.entries);
|
||||
for (const category of page.categories) {
|
||||
categories.set(category.categoryId, category);
|
||||
}
|
||||
|
||||
const next = resp.cookie;
|
||||
const next = page.cookie;
|
||||
if (!next?.length) break;
|
||||
const key = toHex(next);
|
||||
if (seenCookies.has(key)) {
|
||||
@@ -370,23 +312,9 @@ export class ContactsApi {
|
||||
const members: GroupMemberInfo[] = [];
|
||||
let token = '';
|
||||
do {
|
||||
const resp = await FetchGroupMemberListPage.invoke(this.ctx, { groupId, token });
|
||||
for (const raw of resp.members ?? []) {
|
||||
members.push({
|
||||
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 = resp.token ?? '';
|
||||
const page = await FetchGroupMemberListPage.invoke(this.ctx, { groupId, token });
|
||||
members.push(...page.members);
|
||||
token = page.token;
|
||||
} while (token);
|
||||
|
||||
const robotSnapshot = await robotSnapshotPromise;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -72,11 +72,57 @@ describe('FetchFriendListPage namespace', () => {
|
||||
expect(sender.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0xfd4_1');
|
||||
});
|
||||
|
||||
it('returns the wire body verbatim (facade does the roster mapping)', async () => {
|
||||
const body = { friends: [{ uin: 10001, uid: 'u', additional: [] }] };
|
||||
it('maps a page of friends and categories into domain types', async () => {
|
||||
const body = {
|
||||
cookie: Uint8Array.from([0xAA]),
|
||||
friends: [{
|
||||
uin: 10001,
|
||||
uid: 'u1',
|
||||
customGroup: 7,
|
||||
additional: [{
|
||||
type: 1,
|
||||
layer1: {
|
||||
properties: [
|
||||
{ code: 20002, value: 'Alice' },
|
||||
{ code: 103, value: 'A' },
|
||||
],
|
||||
},
|
||||
}],
|
||||
}],
|
||||
categories: [{
|
||||
categoryId: 7,
|
||||
categoryName: 'Work',
|
||||
memberCount: 1,
|
||||
sortId: 1,
|
||||
}],
|
||||
};
|
||||
const sender = makeSender(body as any);
|
||||
const out = await FetchFriendListPage.invoke(sender, {} as any);
|
||||
expect(out.friends).toBeDefined();
|
||||
await expect(FetchFriendListPage.invoke(sender, {} as any)).resolves.toEqual({
|
||||
entries: [{
|
||||
categoryId: 7,
|
||||
friend: { uin: 10001, uid: 'u1', nickname: 'Alice', remark: 'A' },
|
||||
}],
|
||||
categories: [{
|
||||
categoryId: 7,
|
||||
categoryName: 'Work',
|
||||
memberCount: 1,
|
||||
sortId: 1,
|
||||
}],
|
||||
cookie: Uint8Array.from([0xAA]),
|
||||
});
|
||||
});
|
||||
|
||||
it('fills missing friend fields with 0 / empty string', () => {
|
||||
expect(FetchFriendListPage.deserialize({} as any, {
|
||||
friends: [{ additional: [] }],
|
||||
})).toEqual({
|
||||
entries: [{
|
||||
categoryId: 0,
|
||||
friend: { uin: 0, uid: '', nickname: '0', remark: '' },
|
||||
}],
|
||||
categories: [],
|
||||
cookie: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('encodes a follow-up cookie in the envelope body', async () => {
|
||||
|
||||
+62
-4
@@ -1,13 +1,16 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { protobuf_decode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase, OidbSvcTrpcTcp0xFE7_3Response } from '@snowluma/proto-defs/oidb';
|
||||
import type { OidbGroupMemberListRequest } from '@snowluma/proto-defs/oidb-actions/base';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
|
||||
import { FetchGroupMemberListPage } from '../../../src/oidb-services/contacts/fetch-group-member-list-page';
|
||||
|
||||
function makeSender() {
|
||||
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.alloc(0) };
|
||||
function makeSender(body?: OidbSvcTrpcTcp0xFE7_3Response) {
|
||||
const responseData = body !== undefined
|
||||
? Buffer.from(protobuf_encode<OidbBase<OidbSvcTrpcTcp0xFE7_3Response>>({ body }))
|
||||
: Buffer.alloc(0);
|
||||
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
|
||||
return { sendRawPacket: vi.fn(async () => r) };
|
||||
}
|
||||
|
||||
@@ -53,5 +56,60 @@ describe('FetchGroupMemberListPage namespace', () => {
|
||||
const env = protobuf_decode<OidbBase<OidbGroupMemberListRequest>>(bytes);
|
||||
expect(env.body?.groupUin).toBe(12345);
|
||||
});
|
||||
|
||||
it('maps members into GroupMemberInfo and leaves isRobot unset-false', async () => {
|
||||
const sender = makeSender({
|
||||
token: 'next',
|
||||
members: [{
|
||||
uin: { uid: 'u_admin', uin: 22222 },
|
||||
memberName: 'Ada',
|
||||
specialTitle: 't',
|
||||
memberCard: { memberCard: 'card' },
|
||||
level: { level: 12 },
|
||||
joinTimestamp: 1,
|
||||
lastMsgTimestamp: 2,
|
||||
shutUpTimestamp: 3,
|
||||
permission: 2,
|
||||
}],
|
||||
});
|
||||
await expect(FetchGroupMemberListPage.invoke(sender, { groupId: 42, token: '' }))
|
||||
.resolves.toEqual({
|
||||
token: 'next',
|
||||
members: [{
|
||||
uin: 22222,
|
||||
uid: 'u_admin',
|
||||
nickname: 'Ada',
|
||||
card: 'card',
|
||||
isRobot: false,
|
||||
role: 'admin',
|
||||
level: 12,
|
||||
title: 't',
|
||||
joinTime: 1,
|
||||
lastSentTime: 2,
|
||||
shutUpTime: 3,
|
||||
}],
|
||||
});
|
||||
});
|
||||
|
||||
it('maps permission 1 to owner and missing fields to 0 / empty', () => {
|
||||
expect(FetchGroupMemberListPage.deserialize({} as any, {
|
||||
members: [
|
||||
{ permission: 1 },
|
||||
{},
|
||||
],
|
||||
})).toEqual({
|
||||
token: '',
|
||||
members: [
|
||||
{
|
||||
uin: 0, uid: '', nickname: '', card: '', isRobot: false,
|
||||
role: 'owner', level: 0, title: '', joinTime: 0, lastSentTime: 0, shutUpTime: 0,
|
||||
},
|
||||
{
|
||||
uin: 0, uid: '', nickname: '', card: '', isRobot: false,
|
||||
role: 'member', level: 0, title: '', joinTime: 0, lastSentTime: 0, shutUpTime: 0,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user