refactor(api): move 3 friend methods to FriendApi (#6 commit 7/13)

Inlines `actions/friend.ts` into `apis/friend.ts::FriendApi`. The
roster READ side (fetchFriendList) stays on ContactsApi where it
naturally lives next to fetchGroupList — the friend/group rosters
share the same pipeline.

Method renaming (Api namespace already says "friend"):

  setFriendAddRequest → handleRequest    (accept/reject UID-or-flag)
  deleteFriend        → delete           (with cache refresh)
  setFriendRemark     → setRemark

`delete` still does the post-OIDB best-effort
`apis.contacts.fetchFriendList()` refresh to evict the stale entry.

Callers updated: `instance-context.ts`, `actions/extended.ts`.
Tests rewritten to use `new FriendApi(bridge as any).method(...)`.

587/587 tests pass.
This commit is contained in:
DrSmoothl committed 2026-05-20 23:20:33 +08:00
1 parent 73f1cd3c80
commit 2ec72d59f9
8 files changed
+97 -80

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
// FriendApi — friend roster mutations (handle add request /
// delete / set remark). Inlined from `actions/friend.ts` (deleted
// alongside actions/* in commit 13). The READ side of friends
// (fetchFriendList) lives on ContactsApi because friends/groups
// share the same roster pipeline.
import { protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type {
OidbDeleteFriend,
OidbFriendRequestAction,
OidbSetFriendRemark,
} from '@snowluma/proto-defs/oidb-actions/base';
import type { BridgeContext } from '../bridge-context';
import type { Bridge } from '../bridge';
import { makeOidbEnvelope, runOidb } from '../bridge-oidb';
function asBridge(ctx: BridgeContext): Bridge { return ctx as unknown as Bridge; }
export class FriendApi {
constructor(private readonly ctx: BridgeContext) {}
/**
* Accept or reject an inbound friend request. `uidOrFlag` is either a
* pre-resolved UID string or a numeric uin (then resolved on the fly).
*/
async handleRequest(uidOrFlag: string, approve: boolean): Promise<void> {
const bridge = asBridge(this.ctx);
let targetUid = uidOrFlag;
if (/^\d+$/.test(uidOrFlag)) {
targetUid = await this.ctx.resolveUserUid(parseInt(uidOrFlag, 10));
}
const env = makeOidbEnvelope<OidbFriendRequestAction>(0xB5D, 44, { accept: approve ? 3 : 5, targetUid });
await runOidb(bridge, 'OidbSvcTrpcTcp.0xb5d_44', protobuf_encode<OidbBase<OidbFriendRequestAction>>(env));
}
async delete(userId: number, block = false): Promise<void> {
const bridge = asBridge(this.ctx);
const targetUid = await this.ctx.resolveUserUid(userId);
const env = makeOidbEnvelope<OidbDeleteFriend>(0x126B, 0, {
field1: {
targetUid,
field2: {
field1: 130,
field2: 109,
field3: {
field1: 8,
field2: 8,
field3: 50,
},
},
block,
field4: false,
},
});
await runOidb(bridge, 'OidbSvcTrpcTcp.0x126b_0', protobuf_encode<OidbBase<OidbDeleteFriend>>(env));
// 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 */ }
}
async setRemark(userId: number, remark: string): Promise<void> {
const bridge = asBridge(this.ctx);
const uid = await this.ctx.resolveUserUid(userId);
const env = makeOidbEnvelope<OidbSetFriendRemark>(0xB6E, 2, { targetUid: uid, remark });
await runOidb(bridge, 'OidbSvcTrpcTcp.0xb6e_2', protobuf_encode<OidbBase<OidbSetFriendRemark>>(env));
}
}
+5 -1
View File
@@ -22,6 +22,7 @@
import type { BridgeContext } from '../bridge-context';
import { ContactsApi } from './contacts';
import { FriendApi } from './friend';
import { GroupAdminApi } from './group-admin';
import { GroupAlbumApi } from './group-album';
import { GroupFileApi } from './group-file';
@@ -41,8 +42,9 @@ export interface ApiHub {
readonly groupAlbum: GroupAlbumApi;
/** Interactive engagement: poke / like / reaction / essence / emoji-like-list. */
readonly interaction: InteractionApi;
/** Friend roster mutations: handleRequest / delete / setRemark. */
readonly friend: FriendApi;
// Pending — added as later commits land:
// readonly friend: FriendApi;
// readonly interaction: InteractionApi;
// readonly profile: ProfileApi;
// readonly forward: ForwardApi;
@@ -67,6 +69,7 @@ export function buildApiHub(ctx: BridgeContext): ApiHub {
groupFile: new GroupFileApi(ctx),
groupAlbum: new GroupAlbumApi(ctx),
interaction: new InteractionApi(ctx),
friend: new FriendApi(ctx),
};
}
@@ -74,6 +77,7 @@ export function buildApiHub(ctx: BridgeContext): ApiHub {
// `import type { MessageApi } from '@snowluma/core/.../apis'` for
// signature use. Concrete instances always come from `bridge.apis.*`.
export { ContactsApi } from './contacts';
export { FriendApi } from './friend';
export { GroupAdminApi } from './group-admin';
export { GroupAlbumApi } from './group-album';
export { GroupFileApi } from './group-file';