refactor(oidb): migrate friend.ts to namespaces
3 OIDB cmds moved to self-contained namespaces:
- HandleFriendRequest (0xB5D_44) — digit-only flag still
auto-resolves via resolveUserUid
- DeleteFriend (0x126B_0) — verbatim magic constants in the
nested field2 sub-message
(130/109/8/8/50 — server-side
discriminator, MUST stay byte-
equal to NTQQ wire)
- SetFriendRemark (0xB6E_2)
Cache invalidation (fetchFriendList after deletion) stays in the
facade — that side-effect needs `ctx.apis.contacts`, which namespaces
deliberately can't reach.
Tests migrated to assert against bridge.sendRawPacket. 15 new
namespace-level tests covering each invoke branch + serialize edge
cases.
Verification:
- typecheck green
- @snowluma/bridge: 15 files / 133 tests
- friend.test.ts: 5/5
Wire bytes unchanged.
This commit is contained in:
9 files changed
+370
-75
No files matched your search
@@ -1,21 +1,15 @@
|
||||
// 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.
|
||||
// FriendApi — facade over friend-roster OIDB cmds. Every method is a
|
||||
// one-line forwarder to a self-contained namespace under
|
||||
// @snowluma/bridge/oidb-services/friend. The `delete()` facade method
|
||||
// adds a best-effort friend-list cache invalidation after the OIDB
|
||||
// call succeeds — that side-effect stays on the facade because it
|
||||
// reaches into `ctx.apis.contacts`, which the namespace deliberately
|
||||
// can't see.
|
||||
|
||||
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 '@snowluma/bridge/bridge-oidb';
|
||||
|
||||
function asBridge(ctx: BridgeContext): Bridge { return ctx as unknown as Bridge; }
|
||||
import { HandleFriendRequest } from '@snowluma/bridge/oidb-services/friend/handle-friend-request';
|
||||
import { DeleteFriend } from '@snowluma/bridge/oidb-services/friend/delete-friend';
|
||||
import { SetFriendRemark } from '@snowluma/bridge/oidb-services/friend/set-friend-remark';
|
||||
|
||||
export class FriendApi {
|
||||
constructor(private readonly ctx: BridgeContext) {}
|
||||
@@ -24,36 +18,12 @@ export class FriendApi {
|
||||
* 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));
|
||||
handleRequest(uidOrFlag: string, approve: boolean): Promise<void> {
|
||||
return HandleFriendRequest.invoke(this.ctx, { uidOrFlag, approve });
|
||||
}
|
||||
|
||||
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));
|
||||
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
|
||||
@@ -61,10 +31,7 @@ export class FriendApi {
|
||||
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));
|
||||
setRemark(userId: number, remark: string): Promise<void> {
|
||||
return SetFriendRemark.invoke(this.ctx, { userId, remark });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user