diff --git a/packages/core/src/bridge/apis/contacts.ts b/packages/core/src/bridge/apis/contacts.ts index 6ae4f411..ba80c18e 100644 --- a/packages/core/src/bridge/apis/contacts.ts +++ b/packages/core/src/bridge/apis/contacts.ts @@ -1,5 +1,6 @@ import { FetchDownloadRkeys } from '@snowluma/protocol/oidb-services/contacts/fetch-download-rkeys'; import { FetchFriendListPage } 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'; import { FetchGroupRequests } from '@snowluma/protocol/oidb-services/contacts/fetch-group-requests'; @@ -104,6 +105,28 @@ export class ContactsApi { return groups; } + /** + * Fetch a single group's public detail by id via `0x88D_0` — works even for a + * group the bot hasn't joined (used to resolve a group-invite's name). Returns + * null when the server has no such group / denies the lookup. Deliberately + * does NOT `rememberGroups` it — a non-member group must not pollute the + * joined-groups roster / get_group_list. + */ + async fetchGroupDetail(groupId: number): Promise { + if (!(groupId > 0)) return null; + const resp = await FetchGroupDetail.invoke(this.ctx, { groupUin: groupId }); + const r = resp.groupInfo?.results; + if (!r) return null; + return { + groupId, + groupName: r.name ?? '', + remark: '', + memberCount: Number(r.memberCount ?? 0n), + memberMax: Number(r.maxMemberCount ?? 0n), + members: new Map(), + }; + } + async fetchGroupMemberList( groupId: number, options: { force?: boolean } = {}, diff --git a/packages/onebot/src/modules/contact-actions.ts b/packages/onebot/src/modules/contact-actions.ts index 62e44a67..8392378a 100644 --- a/packages/onebot/src/modules/contact-actions.ts +++ b/packages/onebot/src/modules/contact-actions.ts @@ -53,6 +53,13 @@ export async function getGroupList( })); } +// Short-TTL cache for the non-member group lookup (0x88D_0). Joined groups come +// from the identity roster (kept fresh by fetchGroupList) and are NOT cached +// here; this only memoizes the per-id server query so a burst of invites for the +// same group doesn't hammer 0x88D_0 (which would risk a rate-limit / kick). +const NON_MEMBER_GROUP_TTL_MS = 5 * 60 * 1000; +const nonMemberGroupCache = new Map(); + export async function getGroupInfo( bridge: BridgeInterface, groupId: number, @@ -66,13 +73,37 @@ export async function getGroupInfo( } } const g = bridge.identity.findGroup(groupId); - if (!g) return null; - return { - group_id: g.groupId, - group_name: g.groupName, - member_count: g.memberCount, - max_member_count: g.memberMax, - }; + if (g) { + return { + group_id: g.groupId, + group_name: g.groupName, + member_count: g.memberCount, + max_member_count: g.memberMax, + }; + } + + // Not a joined group — fall back to the by-id server lookup so a group invite + // can still resolve its name. Cached with a short TTL (skipped when noCache). + if (!noCache) { + const cached = nonMemberGroupCache.get(groupId); + if (cached && Date.now() - cached.at < NON_MEMBER_GROUP_TTL_MS) return { ...cached.info }; + } + try { + const detail = await bridge.apis.contacts.fetchGroupDetail(groupId); + if (detail) { + const info: JsonObject = { + group_id: detail.groupId, + group_name: detail.groupName, + member_count: detail.memberCount, + max_member_count: detail.memberMax, + }; + nonMemberGroupCache.set(groupId, { info, at: Date.now() }); + return { ...info }; + } + } catch { + // Server lookup failed (no such group / denied) — fall through to null. + } + return null; } export async function getGroupMemberList( diff --git a/packages/onebot/tests/contact-actions.test.ts b/packages/onebot/tests/contact-actions.test.ts index 4864d1ed..a03a8c44 100644 --- a/packages/onebot/tests/contact-actions.test.ts +++ b/packages/onebot/tests/contact-actions.test.ts @@ -34,6 +34,7 @@ const APIS_ROUTING: Record = { fetchFriendList: 'contacts', fetchGroupList: 'contacts', fetchGroupMemberList: 'contacts', fetchUserProfile: 'contacts', fetchGroupRequests: 'contacts', fetchDownloadRKeys: 'contacts', + fetchGroupDetail: 'contacts', }; function fakeBridge(overrides: Record = {}): BridgeInterface { @@ -211,13 +212,52 @@ describe('onebot/contact-actions / getGroupInfo', () => { expect(out).toMatchObject({ group_id: 600, group_name: 'Group F' }); }); - it('returns null when the group remains unknown after fetch', async () => { + it('returns null when the group remains unknown after fetch and the server has no such group', async () => { const bridge = fakeBridge({ fetchGroupList: vi.fn(async () => []), + fetchGroupDetail: vi.fn(async () => null), identity: fakeIdentity({ findGroup: () => null }), }); expect(await getGroupInfo(bridge, 700)).toBeNull(); }); + + it('resolves a non-member group via the by-id server lookup (e.g. a group invite name)', async () => { + const fetchGroupDetail = vi.fn(async () => makeGroup(7100, '邀请来的群')); + const bridge = fakeBridge({ + fetchGroupList: vi.fn(async () => []), + fetchGroupDetail, + identity: fakeIdentity({ findGroup: () => null }), + }); + const out = await getGroupInfo(bridge, 7100); + expect(fetchGroupDetail).toHaveBeenCalledWith(7100); + expect(out).toMatchObject({ group_id: 7100, group_name: '邀请来的群' }); + }); + + it('caches the non-member lookup — a second call within TTL does not re-fetch', async () => { + const fetchGroupDetail = vi.fn(async () => makeGroup(7200, 'Cached Invite Group')); + const bridge = fakeBridge({ + fetchGroupList: vi.fn(async () => []), + fetchGroupDetail, + identity: fakeIdentity({ findGroup: () => null }), + }); + const a = await getGroupInfo(bridge, 7200); + const b = await getGroupInfo(bridge, 7200); + expect(a).toMatchObject({ group_name: 'Cached Invite Group' }); + expect(b).toMatchObject({ group_name: 'Cached Invite Group' }); + expect(fetchGroupDetail).toHaveBeenCalledTimes(1); + }); + + it('noCache bypasses the non-member cache', async () => { + const fetchGroupDetail = vi.fn(async () => makeGroup(7300, 'NoCache Group')); + const bridge = fakeBridge({ + fetchGroupList: vi.fn(async () => []), + fetchGroupDetail, + identity: fakeIdentity({ findGroup: () => null }), + }); + await getGroupInfo(bridge, 7300); + await getGroupInfo(bridge, 7300, true); + expect(fetchGroupDetail).toHaveBeenCalledTimes(2); + }); }); describe('onebot/contact-actions / getGroupMemberList', () => { diff --git a/packages/proto-defs/src/oidb-actions/base.ts b/packages/proto-defs/src/oidb-actions/base.ts index 9c919be0..ef50b9db 100644 --- a/packages/proto-defs/src/oidb-actions/base.ts +++ b/packages/proto-defs/src/oidb-actions/base.ts @@ -257,6 +257,35 @@ export interface OidbGroupListConfig { export interface OidbGroupListRequest { config?: pb<1, OidbGroupListConfig>; } + +// 0x88D_0 — fetch a single group's detail by group uin (works for groups the +// bot has NOT joined, unlike the 0xFE5_2 joined-list query). `flags` is a +// request mask: a present bool(true)/string("") asks the server to return that +// field; the tags mirror the response `Results`. Cross-checked against +// dev/Lagrange.Core/.../Request/OidbSvcTrpcTcp0x88D_0.cs. +export interface OidbGroupDetailFlags { + ownerUid?: pb<1, bool>; + createTime?: pb<2, bool>; + maxMemberCount?: pb<5, bool>; + memberCount?: pb<6, bool>; + level?: pb<10, bool>; + name?: pb<15, string>; + noticePreview?: pb<16, string>; + uin?: pb<21, bool>; + lastSequence?: pb<22, bool>; + lastMessageTime?: pb<23, bool>; + question?: pb<24, bool>; + answer?: pb<25, string>; + maxAdminCount?: pb<29, string>; +} +export interface OidbGroupDetailConfig { + uin?: pb<1, uint_64>; + flags?: pb<2, OidbGroupDetailFlags>; +} +export interface OidbGroupDetailRequest { + field1?: pb<1, uint_32>; + config?: pb<2, OidbGroupDetailConfig>; +} export interface OidbGroupMemberListBody { memberName?: pb<10, bool>; memberCard?: pb<11, bool>; diff --git a/packages/proto-defs/src/oidb.ts b/packages/proto-defs/src/oidb.ts index b636b441..475f59bb 100644 --- a/packages/proto-defs/src/oidb.ts +++ b/packages/proto-defs/src/oidb.ts @@ -94,6 +94,32 @@ export interface OidbSvcTrpcTcp0xFE5_2Response { groups?: pb_repeated<2, OidbSvcTrpcTcp0xFE5_2Group>; } +// Oidb.0x88D_0 — single group detail by uin. `results` tags mirror the request +// flags. Cross-checked against +// dev/Lagrange.Core/.../Response/OidbSvcTrpcTcp0x88D_0Response.cs. +export interface OidbSvcTrpcTcp0x88D_0Results { + ownerUid?: pb<1, string>; + createTime?: pb<2, uint_64>; + maxMemberCount?: pb<5, uint_64>; + memberCount?: pb<6, uint_64>; + level?: pb<10, uint_64>; + name?: pb<15, string>; + noticePreview?: pb<16, string>; + uin?: pb<21, uint_64>; + lastSequence?: pb<22, uint_64>; + lastMessageTime?: pb<23, uint_64>; + question?: pb<24, string>; + answer?: pb<25, string>; + maxAdminCount?: pb<29, uint_64>; +} +export interface OidbSvcTrpcTcp0x88D_0ResponseGroupInfo { + uin?: pb<1, uint_64>; + results?: pb<3, OidbSvcTrpcTcp0x88D_0Results>; +} +export interface OidbSvcTrpcTcp0x88D_0Response { + groupInfo?: pb<1, OidbSvcTrpcTcp0x88D_0ResponseGroupInfo>; +} + // Oidb.0xFE7_3 Group member list export interface OidbSvcTrpcTcp0xFE7_3Uin { uid?: pb<2, string>; diff --git a/packages/protocol/package.json b/packages/protocol/package.json index c3f02cc3..e111136d 100644 --- a/packages/protocol/package.json +++ b/packages/protocol/package.json @@ -139,6 +139,10 @@ "types": "./src/oidb-services/contacts/fetch-friend-list-page.ts", "default": "./src/oidb-services/contacts/fetch-friend-list-page.ts" }, + "./oidb-services/contacts/fetch-group-detail": { + "types": "./src/oidb-services/contacts/fetch-group-detail.ts", + "default": "./src/oidb-services/contacts/fetch-group-detail.ts" + }, "./oidb-services/contacts/fetch-group-list": { "types": "./src/oidb-services/contacts/fetch-group-list.ts", "default": "./src/oidb-services/contacts/fetch-group-list.ts" diff --git a/packages/protocol/src/oidb-services/contacts/fetch-group-detail.ts b/packages/protocol/src/oidb-services/contacts/fetch-group-detail.ts new file mode 100644 index 00000000..74d4b622 --- /dev/null +++ b/packages/protocol/src/oidb-services/contacts/fetch-group-detail.ts @@ -0,0 +1,50 @@ +// 0x88D_0 — fetch a single group's detail by group uin. +// +// Unlike 0xFE5_2 (the bot's joined-groups list), this resolves ANY group by id, +// including ones the bot hasn't joined — used to put a name on a group invite +// (where the invite push itself carries no group name). The `flags` block is a +// request mask: a present bool(true)/string("") asks the server to include that +// field in the response. Cross-checked against +// dev/Lagrange.Core/.../GetGroupInfoService.cs — note it builds the envelope +// with isUid=false, i.e. reserved=0 (NO uinForm), unlike the 0xFE5_2 list query. + +import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; +import type { OidbBase, OidbSvcTrpcTcp0x88D_0Response } from '@snowluma/proto-defs/oidb'; +import type { OidbGroupDetailRequest } from '@snowluma/proto-defs/oidb-actions/base'; +import { invokeOidb, type OidbSender } from '../../oidb-service'; + +export namespace FetchGroupDetail { + export const command = 0x88D; + export const subCommand = 0; + // NOTE: deliberately no `uinForm` — Lagrange sends this with reserved=0. + + export interface Params { groupUin: number; } + + export type Deps = OidbSender; + + const on = true; + + export const serialize = (_ctx: Deps, p: Params): OidbGroupDetailRequest => ({ + field1: 537099973, + config: { + uin: BigInt(p.groupUin), + // Mirror Lagrange's flag set so the server returns the full detail block. + flags: { + ownerUid: on, createTime: on, maxMemberCount: on, memberCount: on, + level: on, name: '', noticePreview: '', uin: on, lastSequence: on, + lastMessageTime: on, question: on, answer: '', maxAdminCount: '', + }, + }, + }); + + export const deserialize = (_ctx: Deps, body: OidbSvcTrpcTcp0x88D_0Response): OidbSvcTrpcTcp0x88D_0Response => body; + + export const encode = (env: OidbBase): Uint8Array => + protobuf_encode>(env); + + export const decode = (bytes: Uint8Array): OidbBase => + protobuf_decode>(bytes); + + export const invoke = (deps: Deps, p: Params): Promise => + invokeOidb(deps, FetchGroupDetail, p); +} diff --git a/packages/protocol/tests/oidb-services/contacts/fetch-group-detail.test.ts b/packages/protocol/tests/oidb-services/contacts/fetch-group-detail.test.ts new file mode 100644 index 00000000..603fa99a --- /dev/null +++ b/packages/protocol/tests/oidb-services/contacts/fetch-group-detail.test.ts @@ -0,0 +1,70 @@ +// 0x88D_0 — fetch a single group's detail by uin (resolves non-member groups, +// e.g. to name a group invite). Verifies the request mask + envelope (reserved=0, +// matching Lagrange's GetGroupInfoService) and the response decode. + +import { describe, expect, it, vi } from 'vitest'; +import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; +import type { OidbBase, OidbSvcTrpcTcp0x88D_0Response } from '@snowluma/proto-defs/oidb'; +import type { OidbGroupDetailRequest } from '@snowluma/proto-defs/oidb-actions/base'; +import type { SendPacketResult } from '@snowluma/common/packet-sender'; + +import { FetchGroupDetail } from '../../../src/oidb-services/contacts/fetch-group-detail'; + +function makeSender(body?: OidbSvcTrpcTcp0x88D_0Response) { + const responseData = body !== undefined + ? Buffer.from(protobuf_encode>({ body })) + : Buffer.alloc(0); + const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData }; + return { sendRawPacket: vi.fn(async () => r) }; +} + +describe('FetchGroupDetail namespace', () => { + it('declares 0x88D_0 with no uinForm (reserved stays 0, per Lagrange)', () => { + expect(FetchGroupDetail.command).toBe(0x88D); + expect(FetchGroupDetail.subCommand).toBe(0); + expect((FetchGroupDetail as { uinForm?: boolean }).uinForm).toBeUndefined(); + }); + + describe('serialize', () => { + it('builds the field1 + uin + request-mask flags', () => { + const out = FetchGroupDetail.serialize({} as any, { groupUin: 601692726 }); + expect(out.field1).toBe(537099973); + expect(out.config?.uin).toBe(BigInt(601692726)); + expect(out.config?.flags?.name).toBe(''); // string mask → request the name + expect(out.config?.flags?.memberCount).toBe(true); + expect(out.config?.flags?.maxMemberCount).toBe(true); + }); + }); + + describe('deserialize', () => { + it('passes the response body through', () => { + const body = { groupInfo: { uin: 1n, results: { name: 'X' } } } as any; + expect(FetchGroupDetail.deserialize({} as any, body)).toBe(body); + }); + }); + + describe('invoke (e2e)', () => { + it('routes to OidbSvcTrpcTcp.0x88d_0 and round-trips the uin in the request', async () => { + const sender = makeSender({ groupInfo: { uin: 601692726n, results: { name: 'g' } } }); + await FetchGroupDetail.invoke(sender, { groupUin: 601692726 }); + + const [cmd, bytes] = sender.sendRawPacket.mock.calls[0]!; + expect(cmd).toBe('OidbSvcTrpcTcp.0x88d_0'); + const env = protobuf_decode>(bytes); + expect(env.command).toBe(0x88D); + expect(env.subCommand ?? 0).toBe(0); // proto3 omits the 0 default on the wire + expect(env.reserved ?? 0).toBe(0); // NOT uin-form + expect(env.body?.config?.uin).toBe(BigInt(601692726)); + }); + + it('returns the decoded group detail (name + counts)', async () => { + const sender = makeSender({ + groupInfo: { uin: 601692726n, results: { name: '测试群', memberCount: 42n, maxMemberCount: 500n } }, + }); + const out = await FetchGroupDetail.invoke(sender, { groupUin: 601692726 }); + expect(out.groupInfo?.results?.name).toBe('测试群'); + expect(out.groupInfo?.results?.memberCount).toBe(42n); + expect(out.groupInfo?.results?.maxMemberCount).toBe(500n); + }); + }); +});