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:
1 parent
73f1cd3c80
commit
2ec72d59f9
8 files changed
+97
-80
No files matched your search
@@ -1,50 +0,0 @@
|
||||
import { protobuf_encode } from '@snowluma/proton';
|
||||
import type { Bridge } from '../bridge';
|
||||
import { makeOidbEnvelope, runOidb } from '../bridge-oidb';
|
||||
import { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
OidbDeleteFriend,
|
||||
OidbFriendRequestAction,
|
||||
OidbSetFriendRemark,
|
||||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||||
|
||||
export async function setFriendAddRequest(bridge: Bridge, uidOrFlag: string, approve: boolean): Promise<void> {
|
||||
let targetUid = uidOrFlag;
|
||||
if (/^\d+$/.test(uidOrFlag)) {
|
||||
targetUid = await bridge.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));
|
||||
}
|
||||
|
||||
export async function deleteFriend(bridge: Bridge, userId: number, block = false): Promise<void> {
|
||||
const targetUid = await bridge.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 bridge.apis.contacts.fetchFriendList(); } catch { /* ignore */ }
|
||||
}
|
||||
|
||||
export async function setFriendRemark(bridge: Bridge, userId: number, remark: string): Promise<void> {
|
||||
const uid = await bridge.resolveUserUid(userId);
|
||||
const env = makeOidbEnvelope<OidbSetFriendRemark>(0xB6E, 2, { targetUid: uid, remark });
|
||||
await runOidb(bridge, 'OidbSvcTrpcTcp.0xb6e_2', protobuf_encode<OidbBase<OidbSetFriendRemark>>(env));
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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';
|
||||
|
||||
@@ -46,10 +46,8 @@ export interface BridgeInterface {
|
||||
// ─── Group admin (moved to apis.groupAdmin) ───
|
||||
// ─── Group reaction / essence (moved to apis.interaction) ───
|
||||
|
||||
// ─── Friend ───
|
||||
setFriendAddRequest(uidOrFlag: string, approve: boolean): Promise<void>;
|
||||
deleteFriend(userId: number, block?: boolean): Promise<void>;
|
||||
setFriendRemark(userId: number, remark: string): Promise<void>;
|
||||
// ─── Friend (moved to apis.friend) ───
|
||||
// - handleRequest / delete / setRemark
|
||||
|
||||
// ─── Files (moved to apis.groupFile) ───
|
||||
// - upload / uploadPrivate / publish
|
||||
|
||||
@@ -26,11 +26,7 @@ import {
|
||||
fetchForwardNodes as fetchForwardNodes_,
|
||||
uploadForwardNodes as uploadForwardNodes_,
|
||||
} from './actions/forward';
|
||||
import {
|
||||
deleteFriend as deleteFriend_,
|
||||
setFriendAddRequest as setFriendAddRequest_,
|
||||
setFriendRemark as setFriendRemark_,
|
||||
} from './actions/friend';
|
||||
// actions/friend.ts removed — moved to apis/friend.ts::FriendApi.
|
||||
// actions/group-admin.ts removed — moved to `apis/group-admin.ts::GroupAdminApi`.
|
||||
// actions/group-album.ts removed — moved to `apis/group-album.ts::GroupAlbumApi`.
|
||||
// Group file CRUD + private c2c upload + media URL resolvers moved to
|
||||
@@ -318,8 +314,8 @@ export class Bridge implements BridgeInterface {
|
||||
// GroupAdmin methods (mute/kick/admin/card/name/title/leave/
|
||||
// add-option/search/add-request/remark/at-all-remain) moved to
|
||||
// apis.groupAdmin (apis/group-admin.ts::GroupAdminApi).
|
||||
async setFriendAddRequest(uidOrFlag: string, approve: boolean): Promise<void> { return setFriendAddRequest_(this, uidOrFlag, approve); }
|
||||
async deleteFriend(userId: number, block = false): Promise<void> { return deleteFriend_(this, userId, block); }
|
||||
// Friend methods (handleRequest/delete/setRemark) moved to
|
||||
// apis.friend.
|
||||
// GroupFile methods (upload/uploadPrivate/publish/list/getUrl/
|
||||
// getPrivateUrl/{Ptt,Video}Url/getPrivate{Ptt,Video}Url/delete/move/
|
||||
// createFolder/deleteFolder/renameFolder/getCount) moved to
|
||||
@@ -329,7 +325,6 @@ export class Bridge implements BridgeInterface {
|
||||
// Interaction methods (sendPoke/sendLike/setReaction/setEssence/
|
||||
// getEmojiLikes) moved to apis.interaction.
|
||||
// recall* / markRead* moved to apis/message.ts::MessageApi.
|
||||
async setFriendRemark(userId: number, remark: string): Promise<void> { return setFriendRemark_(this, userId, remark); }
|
||||
async getGroupHonorInfo(groupId: number, type: WebHonorType | string): Promise<any> {
|
||||
return getGroupHonorInfo_(this, groupId, type);
|
||||
}
|
||||
|
||||
@@ -540,7 +540,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
if (params.remark === undefined) {
|
||||
return failedResponse(RETCODE.BAD_REQUEST, 'remark is required (pass an empty string to clear)');
|
||||
}
|
||||
await ctx.bridge.setFriendRemark(userId, asString(params.remark));
|
||||
await ctx.bridge.apis.friend.setRemark(userId, asString(params.remark));
|
||||
return okResponse();
|
||||
});
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext {
|
||||
getPrivateFileUrl: (userId, fileId, fileHash) => bridge.apis.groupFile.getPrivateUrl(userId, fileId, fileHash),
|
||||
|
||||
// Requests — name translation.
|
||||
handleFriendRequest: (flag, approve) => bridge.setFriendAddRequest(flag, approve),
|
||||
handleFriendRequest: (flag, approve) => bridge.apis.friend.handleRequest(flag, approve),
|
||||
handleGroupRequest: (flag, _subType, approve, reason) => handleGroupAddRequest(bridge, flag, approve, reason),
|
||||
|
||||
// Pokes — flatten isGroup into the bridge signature.
|
||||
@@ -161,9 +161,9 @@ export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext {
|
||||
sendForwardMsg: (messages, groupId) => uploadForwardMessage(ref, messages, groupId),
|
||||
getForwardMsg: (resId) => getForwardMessage(ref, resId),
|
||||
forwardSingleMsg: (messageId, target) => forwardSingleMessage(ref, messageId, target),
|
||||
handleDeleteFriend: (userId, block) => bridge.deleteFriend(userId, !!block),
|
||||
handleDeleteFriend: (userId, block) => bridge.apis.friend.delete(userId, !!block),
|
||||
forceFetchClientKey: () => bridge.forceFetchClientKey(),
|
||||
setFriendRemark: (userId, remark) => bridge.setFriendRemark(userId, remark),
|
||||
setFriendRemark: (userId, remark) => bridge.apis.friend.setRemark(userId, remark),
|
||||
setGroupRemark: (groupId, remark) => bridge.apis.groupAdmin.setRemark(groupId, remark),
|
||||
setGroupAvatar: (groupId, source) => bridge.setGroupAvatar(groupId, source),
|
||||
getGroupFileCount: (groupId) => bridge.apis.groupFile.getCount(groupId),
|
||||
|
||||
Reference in new issue
Block a user