fix(friend): align remark and delete protocols

Route friend remark updates and clears through the commands used by current QQ clients. Emit the required delete flags and surface post-delete cache refresh failures.

Add byte-exact regression coverage for all affected requests.

Fixes #284.
This commit is contained in:
DrSmoothl committed 2026-07-25 15:41:02 +08:00
1 parent c64f9d3078
commit dff4b3b032
9 files changed
+192 -54

No files matched your search

+19 -4
View File
@@ -1,4 +1,6 @@
import { createLogger } from '@snowluma/common/logger';
import { ApproveDoubtBuddyReq } from '@snowluma/protocol/oidb-services/friend/approve-doubt-buddy-req';
import { ClearFriendRemark } from '@snowluma/protocol/oidb-services/friend/clear-friend-remark';
import { DeleteFriend } from '@snowluma/protocol/oidb-services/friend/delete-friend';
import { GetDoubtBuddyReq, type DoubtBuddyRequest } from '@snowluma/protocol/oidb-services/friend/get-doubt-buddy-req';
import { HandleFriendRequest } from '@snowluma/protocol/oidb-services/friend/handle-friend-request';
@@ -8,6 +10,8 @@ import type { BridgeContext } from '../bridge-context';
export type { DoubtBuddyRequest };
const log = createLogger('Bridge.Friend');
export class FriendApi {
constructor(private readonly ctx: BridgeContext) { }
@@ -22,13 +26,24 @@ export class FriendApi {
async delete(userId: number, block = false): Promise<void> {
await DeleteFriend.invoke(this.ctx, { userId, block });
// Refresh friend cache after deletion so subsequent reads don't
// surface a ghost entry. Best-effort: a transient OIDB hiccup here
// shouldn't make the delete itself look failed.
try { await this.ctx.apis.contacts.fetchFriendList(); } catch { /* ignore */ }
// The server-side delete has already completed. A refresh failure must be
// visible, but throwing here would misreport the delete itself as failed
// and encourage callers to repeat a destructive request.
try {
await this.ctx.apis.contacts.fetchFriendList();
} catch (err: unknown) {
log.warn(
'friend-list refresh failed after deleting user=%d: %s',
userId,
err instanceof Error ? err.message : String(err),
);
}
}
setRemark(userId: number, remark: string): Promise<void> {
if (remark === '') {
return ClearFriendRemark.invoke(this.ctx, { userId });
}
return SetFriendRemark.invoke(this.ctx, { userId, remark });
}
+26 -9
View File
@@ -1,10 +1,10 @@
import { describe, it, expect, vi } from 'vitest';
import { subscribeLogs, type LogEntry } from '@snowluma/common/logger';
import { protobuf_decode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type {
OidbDeleteFriend,
OidbFriendRequestAction,
OidbSetFriendRemark,
} from '@snowluma/proto-defs/oidb-actions/base';
// Post-namespace migration: FriendApi is a thin facade over the
@@ -45,19 +45,36 @@ describe('apis/friend', () => {
expect(bridge.apis.contacts.fetchFriendList).toHaveBeenCalled();
});
it('delete still succeeds when the friend-list refresh throws', async () => {
it('reports a friend-list refresh failure without misreporting the completed delete', async () => {
const bridge = mockBridge();
bridge.apis.contacts.fetchFriendList = vi.fn(async () => { throw new Error('cache miss'); });
await expect(new FriendApi(bridge as any).delete(10001))
.resolves.toBeUndefined();
const captured: LogEntry[] = [];
const unsubscribe = subscribeLogs((entry) => {
if (entry.scope === 'Bridge.Friend') captured.push(entry);
});
try {
await expect(new FriendApi(bridge as any).delete(10001))
.resolves.toBeUndefined();
} finally {
unsubscribe();
}
expect(captured.map(({ level, message }) => ({ level, message }))).toEqual([{
level: 'warn',
message: 'friend-list refresh failed after deleting user=10001: cache miss',
}]);
});
it('setRemark resolves UID and sends 0xb6e_2', async () => {
it('setRemark uses the current set command for a non-empty remark', async () => {
const bridge = mockBridge();
await new FriendApi(bridge as any).setRemark(10001, 'best-friend');
const [cmd, bytes] = bridge.sendRawPacket.mock.calls[0]!;
expect(cmd).toBe('OidbSvcTrpcTcp.0xb6e_2');
const env = protobuf_decode<OidbBase<OidbSetFriendRemark>>(bytes);
expect(env.body).toMatchObject({ targetUid: 'resolved-uid', remark: 'best-friend' });
expect(bridge.sendRawPacket.mock.calls[0]![0])
.toBe('OidbSvcTrpcTcp.0x912e_0');
});
it('setRemark uses the dedicated clear command for an empty remark', async () => {
const bridge = mockBridge();
await new FriendApi(bridge as any).setRemark(10001, '');
expect(bridge.sendRawPacket.mock.calls[0]![0])
.toBe('OidbSvcTrpcTcp.0x912f_0');
});
});