fix(protocol): restore filtered friend request approvals
Correct approval handling for filtered friend requests and preserve clear failures when an account cannot be resolved or the request is rejected. Add regression coverage for separate account identities, approval errors, and existing list and rejection behavior. Fixes #460
This commit is contained in:
8 files changed
+164
-80
No files matched your search
@@ -1,28 +1,31 @@
|
||||
// 0xd69_0 — approvalDoubtBuddyReq: approve a doubtful friend-add request
|
||||
// (可能认识的人). RE'd from QQNT doubt_codec.cc EncodeRequest sub_3F3EC90:
|
||||
// {1:uid, 2:uid, [3:u32], [4:str]} — tags 3/4 emitted only when present, and
|
||||
// NapCat passes empty str1/str2, so the approve flow is just {1:uid, 2:uid}.
|
||||
// Same OIDB cmd as the getter; the server discriminates by body shape.
|
||||
// uin-form OIDB (envelope reserved=1). The reject/decline path is a separate
|
||||
// cmd (delDoubtBuddyReq) — see sibling reject-doubt-buddy-req.ts.
|
||||
// Approve an existing filtered friend request.
|
||||
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase, OidbEmpty } from '@snowluma/proto-defs/oidb';
|
||||
import type { OidbDoubtApprovalReq } from '@snowluma/proto-defs/oidb-actions/doubt-buddy';
|
||||
import type { BridgeContext } from '../../bridge-context';
|
||||
import { invokeOidb, type OidbSender } from '../../oidb-service';
|
||||
import { resolveSelfUid } from '../../self-uid';
|
||||
|
||||
export namespace ApproveDoubtBuddyReq {
|
||||
export const command = 0xD69;
|
||||
export const command = 0xD72;
|
||||
export const subCommand = 0;
|
||||
export const uinForm = true;
|
||||
export const uinForm = false;
|
||||
|
||||
export interface Params { uid: string }
|
||||
export type Deps = OidbSender;
|
||||
export type Deps = OidbSender & Pick<BridgeContext, 'identity' | 'resolveUserUid'>;
|
||||
|
||||
export const serialize = (_ctx: Deps, p: Params): OidbDoubtApprovalReq => ({
|
||||
uid: p.uid,
|
||||
targetUid: p.uid,
|
||||
});
|
||||
export const serialize = async (ctx: Deps, p: Params): Promise<OidbDoubtApprovalReq> => {
|
||||
if (!p.uid.trim()) throw new Error('cannot approve doubt request: applicant uid is unavailable');
|
||||
const selfUid = await resolveSelfUid(ctx);
|
||||
if (!selfUid.trim()) throw new Error('cannot approve doubt request: self uid is unavailable');
|
||||
return {
|
||||
selfUid,
|
||||
targetUid: p.uid,
|
||||
field3: 0,
|
||||
field4: '',
|
||||
};
|
||||
};
|
||||
|
||||
export const deserialize = (_ctx: Deps, _: OidbEmpty): void => {};
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// request. RE'd from QQNT doubt_buddy_del_worker.cc (worker ctor sub_3F3F320
|
||||
// → cmd 0xd69 sub 0; EncodeRequest sub_3F3E860 writes {1: const 3, 3:{1:uid}}).
|
||||
// The top-level tag-1 constant 3 is the op discriminator (getDoubtBuddyReq
|
||||
// uses 1). Same OIDB cmd as get/approval; the server routes by body shape.
|
||||
// uses 1). Rejection shares the listing command, not the approval command.
|
||||
// uin-form OIDB (envelope reserved=1).
|
||||
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
|
||||
@@ -3,35 +3,102 @@ import { protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase, OidbEmpty } from '@snowluma/proto-defs/oidb';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
import { ApproveDoubtBuddyReq } from '../../../src/oidb-services/friend/approve-doubt-buddy-req';
|
||||
import { env, s } from '../_pb-oracle';
|
||||
import { env, s, v } from '../_pb-oracle';
|
||||
|
||||
function makeSender() {
|
||||
const respEnv: OidbBase<OidbEmpty> = { command: 0xD69, subCommand: 0, body: {} };
|
||||
function makeSender(selfUid = 'u_self', selfUin = '10001') {
|
||||
const respEnv: OidbBase<OidbEmpty> = { command: 0xD72, subCommand: 0, body: {} };
|
||||
const r: SendPacketResult = {
|
||||
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<OidbBase<OidbEmpty>>(respEnv)),
|
||||
};
|
||||
return { sendRawPacket: vi.fn(async () => r) };
|
||||
return {
|
||||
sendRawPacket: vi.fn(async (_cmd: string, _bytes: Uint8Array) => r),
|
||||
identity: { selfUid, uin: selfUin } as ApproveDoubtBuddyReq.Deps['identity'],
|
||||
resolveUserUid: vi.fn(async () => 'u_lookup_self'),
|
||||
};
|
||||
}
|
||||
|
||||
describe('ApproveDoubtBuddyReq namespace', () => {
|
||||
it('declares command 0xD69 sub 0, uin form', () => {
|
||||
expect(ApproveDoubtBuddyReq.command).toBe(0xD69);
|
||||
expect(ApproveDoubtBuddyReq.subCommand).toBe(0);
|
||||
expect(ApproveDoubtBuddyReq.uinForm).toBe(true);
|
||||
});
|
||||
|
||||
it('serializes the uid into both tag 1 and tag 2 (str1/str2 omitted, matching NapCat approve flow)', () => {
|
||||
expect(ApproveDoubtBuddyReq.serialize({} as any, { uid: 'u_x' })).toEqual({ uid: 'u_x', targetUid: 'u_x' });
|
||||
});
|
||||
|
||||
it('byte-oracle: routes to 0xd69_0 and locks req tags {1:uid, 2:uid}', async () => {
|
||||
it('keeps the approving account separate from the applicant', async () => {
|
||||
const sender = makeSender();
|
||||
await ApproveDoubtBuddyReq.invoke(sender, { uid: 'u_abc' });
|
||||
expect(await ApproveDoubtBuddyReq.serialize(sender, { uid: 'u_applicant' })).toEqual({
|
||||
selfUid: 'u_self', targetUid: 'u_applicant', field3: 0, field4: '',
|
||||
});
|
||||
expect(sender.resolveUserUid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('matches the approval request bytes including explicit default options', async () => {
|
||||
const sender = makeSender();
|
||||
await ApproveDoubtBuddyReq.invoke(sender, { uid: 'u_applicant' });
|
||||
|
||||
expect(sender.sendRawPacket).toHaveBeenCalledOnce();
|
||||
const [cmd, bytes] = sender.sendRawPacket.mock.calls[0]!;
|
||||
expect(cmd).toBe('OidbSvcTrpcTcp.0xd69_0');
|
||||
const body = [...s(1, 'u_abc'), ...s(2, 'u_abc')];
|
||||
expect(Buffer.from(bytes).toString('hex')).toBe(env(0xD69, 0, body, true));
|
||||
expect(cmd).toBe('OidbSvcTrpcTcp.0xd72_0');
|
||||
const body = [...s(1, 'u_self'), ...s(2, 'u_applicant'), ...v(3, 0), ...s(4, '')];
|
||||
expect(Buffer.from(bytes).toString('hex')).toBe(env(0xD72, 0, body, false));
|
||||
});
|
||||
|
||||
it('resolves the current account when its self profile is not cached', async () => {
|
||||
const sender = makeSender('');
|
||||
await ApproveDoubtBuddyReq.invoke(sender, { uid: 'u_applicant' });
|
||||
expect(sender.resolveUserUid).toHaveBeenCalledWith(10001);
|
||||
const body = [...s(1, 'u_lookup_self'), ...s(2, 'u_applicant'), ...v(3, 0), ...s(4, '')];
|
||||
expect(Buffer.from(sender.sendRawPacket.mock.calls[0]![1]).toString('hex'))
|
||||
.toBe(env(0xD72, 0, body, false));
|
||||
});
|
||||
|
||||
it('does not send when the current account cannot be identified', async () => {
|
||||
const sender = makeSender('', '0');
|
||||
await expect(ApproveDoubtBuddyReq.invoke(sender, { uid: 'u_applicant' }))
|
||||
.rejects.toThrow('self uid is unavailable');
|
||||
expect(sender.sendRawPacket).not.toHaveBeenCalled();
|
||||
expect(sender.resolveUserUid).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('propagates self identity lookup errors without sending', async () => {
|
||||
const sender = makeSender('');
|
||||
sender.resolveUserUid.mockRejectedValue(new Error('profile lookup failed'));
|
||||
await expect(ApproveDoubtBuddyReq.invoke(sender, { uid: 'u_applicant' }))
|
||||
.rejects.toThrow('profile lookup failed');
|
||||
expect(sender.sendRawPacket).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects an empty resolved self identity before sending', async () => {
|
||||
const sender = makeSender('');
|
||||
sender.resolveUserUid.mockResolvedValue('');
|
||||
await expect(ApproveDoubtBuddyReq.invoke(sender, { uid: 'u_applicant' }))
|
||||
.rejects.toThrow('self uid is unavailable');
|
||||
expect(sender.sendRawPacket).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each(['', ' '])('rejects an absent applicant identity (%j)', async (uid) => {
|
||||
const sender = makeSender();
|
||||
await expect(ApproveDoubtBuddyReq.invoke(sender, { uid }))
|
||||
.rejects.toThrow('applicant uid is unavailable');
|
||||
expect(sender.sendRawPacket).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('propagates a rejected approval without retrying a different operation', async () => {
|
||||
const sender = makeSender();
|
||||
sender.sendRawPacket.mockResolvedValue({
|
||||
success: false, gotResponse: true, errorCode: 1201,
|
||||
errorMessage: 'request rejected', responseData: Buffer.alloc(0),
|
||||
});
|
||||
await expect(ApproveDoubtBuddyReq.invoke(sender, { uid: 'u_applicant' }))
|
||||
.rejects.toThrow('request rejected');
|
||||
expect(sender.sendRawPacket).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('does not mistake a successful transport for an accepted approval', async () => {
|
||||
const sender = makeSender();
|
||||
sender.sendRawPacket.mockResolvedValue({
|
||||
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<OidbBase<OidbEmpty>>({
|
||||
command: 0xD72, subCommand: 0, errorCode: 1201, errorMsg: 'approval denied', body: {},
|
||||
})),
|
||||
});
|
||||
await expect(ApproveDoubtBuddyReq.invoke(sender, { uid: 'u_applicant' }))
|
||||
.rejects.toThrow('approval denied');
|
||||
expect(sender.sendRawPacket).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user