diff --git a/packages/core/src/bridge/apis/friend.ts b/packages/core/src/bridge/apis/friend.ts index 5147f72c..20ae530f 100644 --- a/packages/core/src/bridge/apis/friend.ts +++ b/packages/core/src/bridge/apis/friend.ts @@ -58,26 +58,53 @@ export class FriendApi { /** List doubtful friend-add requests (可能认识的人). */ async getDoubtRequests(count: number): Promise { const list = await GetDoubtBuddyReq.invoke(this.ctx, { count }); - return list.map((item) => { + return Promise.all(list.map(async (item) => { let uid = item.uid; let userId = item.user_id; if (userId <= 0 && uid) { userId = this.ctx.identity.findUinByUid(uid) ?? 0; } + // Current Linux replies often omit the account id even though the + // official decoder still reads it as a string. The approval packet + // cannot accept a raw number, so fill from the same uin→uid path + // used by ordinary friend requests. Cache-only lookup is not + // enough: filtered applicants are not friends or group members. if (!uid && userId > 0) { - uid = this.ctx.identity.findUidByUin(userId) ?? ''; + try { + uid = await this.ctx.resolveUserUid(userId); + } catch (err: unknown) { + log.warn( + 'doubt-request uid resolve failed: user=%d err=%s', + userId, + err instanceof Error ? err.message : String(err), + ); + } } - return { ...item, uid, user_id: userId }; + return { ...item, uid: uid || '', user_id: userId }; + })); + } + + /** Approve a doubtful friend-add request. `uidOrFlag` is the list uid, or a digit-only account number. */ + async approveDoubtRequest(uidOrFlag: string): Promise { + return ApproveDoubtBuddyReq.invoke(this.ctx, { + uid: await this.resolveDoubtFlag(uidOrFlag), }); } - /** Approve a doubtful friend-add request by its uid (the list item's flag). */ - approveDoubtRequest(uid: string): Promise { - return ApproveDoubtBuddyReq.invoke(this.ctx, { uid }); + /** Reject (delete/decline) a doubtful friend-add request. `uidOrFlag` matches `approveDoubtRequest`. */ + async rejectDoubtRequest(uidOrFlag: string): Promise { + return RejectDoubtBuddyReq.invoke(this.ctx, { + uid: await this.resolveDoubtFlag(uidOrFlag), + }); } - /** Reject (delete/decline) a doubtful friend-add request by its uid. */ - rejectDoubtRequest(uid: string): Promise { - return RejectDoubtBuddyReq.invoke(this.ctx, { uid }); + /** + * Digit-only flags are account numbers and must be translated before the + * approval/reject packet is built. A raw number in that packet is rejected + * by the server. Same rule as inbound friend-request handling. + */ + private resolveDoubtFlag(uidOrFlag: string): Promise { + if (!/^\d+$/.test(uidOrFlag)) return Promise.resolve(uidOrFlag); + return this.ctx.resolveUserUid(parseInt(uidOrFlag, 10)); } } diff --git a/packages/core/tests/actions/friend.test.ts b/packages/core/tests/actions/friend.test.ts index c4c3430d..ccebec0b 100644 --- a/packages/core/tests/actions/friend.test.ts +++ b/packages/core/tests/actions/friend.test.ts @@ -7,6 +7,11 @@ import type { OidbFriendRequestAction, OidbSetFriendRemarkResponse, } from '@snowluma/proto-defs/oidb-actions/base'; +import type { + OidbDoubtApprovalReq, + OidbDoubtDelReq, + OidbDoubtGetResp, +} from '@snowluma/proto-defs/oidb-actions/doubt-buddy'; // Post-namespace migration: FriendApi is a thin facade over the // namespaces under @snowluma/protocol/oidb-services/friend. Tests assert @@ -104,4 +109,108 @@ describe('apis/friend', () => { expect(bridge.sendRawPacket.mock.calls[0]![0]) .toBe('OidbSvcTrpcTcp.0x912f_0'); }); + + it('getDoubtRequests fills a missing uid via resolveUserUid', async () => { + const bridge = mockBridge({ + sendRawPacket: vi.fn(async () => ({ + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: encodeDoubtList([{ nick: 'Alice', uin: 12345n, reqTime: 1700000000n }]), + })), + }); + const list = await new FriendApi(bridge as any).getDoubtRequests(10); + expect(bridge.resolveUserUid).toHaveBeenCalledWith(12345); + expect(list).toEqual([ + { + uid: 'resolved-uid', user_id: 12345, nick: 'Alice', source: '', + reason: '', msg: '', group_code: '', reqTime: 1700000000, + }, + ]); + }); + + it('getDoubtRequests keeps a wire uid and does not resolve', async () => { + const bridge = mockBridge({ + sendRawPacket: vi.fn(async () => ({ + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: encodeDoubtList([ + { uid: 'u_alice', nick: 'Alice', uin: 12345n, reqTime: 1700000000n }, + ]), + })), + }); + const list = await new FriendApi(bridge as any).getDoubtRequests(10); + expect(bridge.resolveUserUid).not.toHaveBeenCalled(); + expect(list[0]?.uid).toBe('u_alice'); + expect(list[0]?.user_id).toBe(12345); + }); + + it('getDoubtRequests keeps an empty uid when resolveUserUid fails', async () => { + const bridge = mockBridge({ + resolveUserUid: vi.fn(async () => { throw new Error('no mapping'); }), + sendRawPacket: vi.fn(async () => ({ + success: true, + gotResponse: true, + errorCode: 0, + errorMessage: '', + responseData: encodeDoubtList([{ nick: 'Alice', uin: 12345n }]), + })), + }); + const captured: LogEntry[] = []; + const unsubscribe = subscribeLogs((entry) => { + if (entry.scope === 'Bridge.Friend') captured.push(entry); + }); + try { + const list = await new FriendApi(bridge as any).getDoubtRequests(10); + expect(list[0]?.uid).toBe(''); + expect(list[0]?.user_id).toBe(12345); + } finally { + unsubscribe(); + } + expect(captured.map(({ level, message }) => ({ level, message }))).toEqual([{ + level: 'warn', + message: 'doubt-request uid resolve failed: user=12345 err=no mapping', + }]); + }); + + it('approveDoubtRequest resolves a digit-only flag before sending', async () => { + const bridge = mockBridge(); + await new FriendApi(bridge as any).approveDoubtRequest('12345'); + expect(bridge.resolveUserUid).toHaveBeenCalledWith(12345); + const [cmd, bytes] = bridge.sendRawPacket.mock.calls[0]!; + expect(cmd).toBe('OidbSvcTrpcTcp.0xd69_0'); + const env = protobuf_decode>(bytes); + expect(env.body).toMatchObject({ uid: 'resolved-uid', targetUid: 'resolved-uid' }); + }); + + it('approveDoubtRequest forwards a uid flag without resolving', async () => { + const bridge = mockBridge(); + await new FriendApi(bridge as any).approveDoubtRequest('u_abc'); + expect(bridge.resolveUserUid).not.toHaveBeenCalled(); + const [, bytes] = bridge.sendRawPacket.mock.calls[0]!; + const env = protobuf_decode>(bytes); + expect(env.body).toMatchObject({ uid: 'u_abc', targetUid: 'u_abc' }); + }); + + it('rejectDoubtRequest resolves a digit-only flag before sending', async () => { + const bridge = mockBridge(); + await new FriendApi(bridge as any).rejectDoubtRequest('12345'); + expect(bridge.resolveUserUid).toHaveBeenCalledWith(12345); + const [, bytes] = bridge.sendRawPacket.mock.calls[0]!; + const env = protobuf_decode>(bytes); + expect(env.body).toMatchObject({ field1: 3, inner: { uid: 'resolved-uid' } }); + }); }); + +function encodeDoubtList( + list: NonNullable['list']>, +): Buffer { + return Buffer.from(protobuf_encode>({ + command: 0xD69, + subCommand: 0, + body: { status: 1, body: { list } }, + })); +} diff --git a/packages/onebot/src/actions/extended.ts b/packages/onebot/src/actions/extended.ts index 03ccc0e5..e35a067b 100644 --- a/packages/onebot/src/actions/extended.ts +++ b/packages/onebot/src/actions/extended.ts @@ -1903,7 +1903,8 @@ export const actions = [ }, }), // set_doubt_friends_add_request — handle a 可疑好友申请 (0xd69_0). `flag` is - // the uid from the get list. approve → approvalDoubtBuddyReq; approve:false + // the uid from the get list, or the applicant's account number (resolved + // before the packet is built). approve → approvalDoubtBuddyReq; approve:false // → delDoubtBuddyReq (reject/decline). NapCat only ever approves; we add the // reject path since we RE'd delDoubtBuddyReq too. defineAction({ diff --git a/packages/protocol/tests/oidb-services/friend/get-doubt-buddy-req.test.ts b/packages/protocol/tests/oidb-services/friend/get-doubt-buddy-req.test.ts index 9028d46d..738d4c1d 100644 --- a/packages/protocol/tests/oidb-services/friend/get-doubt-buddy-req.test.ts +++ b/packages/protocol/tests/oidb-services/friend/get-doubt-buddy-req.test.ts @@ -60,6 +60,18 @@ describe('GetDoubtBuddyReq namespace', () => { expect(GetDoubtBuddyReq.deserialize({} as any, { status: 1 })).toEqual([]); }); + it('keeps an empty uid when the reply only carries the account number', () => { + expect(GetDoubtBuddyReq.deserialize({} as any, { + status: 1, + body: { list: [{ nick: 'Alice', uin: 12345n, reqTime: 1700000000n }] }, + })).toEqual([ + { + uid: '', user_id: 12345, nick: 'Alice', source: '', + reason: '', msg: '', group_code: '', reqTime: 1700000000, + }, + ]); + }); + it('keeps the uid string and does not reread it as the account number', async () => { const respEnv: OidbBase = { command: 0xD69, subCommand: 0,