diff --git a/packages/core/src/bridge/actions/group-admin.ts b/packages/core/src/bridge/actions/group-admin.ts deleted file mode 100644 index 59886231..00000000 --- a/packages/core/src/bridge/actions/group-admin.ts +++ /dev/null @@ -1,162 +0,0 @@ -import { protobuf_decode, 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 { - Oidb0x89a_0AddOption, - Oidb0x89a_0Search, - Oidb0x8a0Req, - Oidb0x8a7Req, - Oidb0x8a7Resp, - Oidb0xf16Req, - OidbGroupRequestAction, - OidbKickMember, - OidbLeaveGroup, - OidbMuteAll, - OidbMuteMember, - OidbRenameGroup, - OidbRenameMember, - OidbSetAdmin, - OidbSpecialTitle, -} from '@snowluma/proto-defs/oidb-actions/base'; - -// ─────────────── mute / un-mute ─────────────── - -export async function muteGroupMember(bridge: Bridge, groupId: number, userId: number, duration: number): Promise { - const uid = await bridge.resolveUserUid(userId, groupId); - const env = makeOidbEnvelope(0x1253, 1, { - groupUin: groupId, type: 1, body: { targetUid: uid, duration }, - }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0x1253_1', protobuf_encode>(env)); -} - -export async function muteGroupAll(bridge: Bridge, groupId: number, enable: boolean): Promise { - const env = makeOidbEnvelope(0x89A, 0, { - groupUin: groupId, muteState: { state: enable ? 0xFFFFFFFF : 0 }, - }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0x89a_0', protobuf_encode>(env)); -} - -// ─────────────── join-policy ─────────────── - -export async function setGroupAddOption(bridge: Bridge, groupId: number, addType: number): Promise { - const env = makeOidbEnvelope(0x89A, 0, { - groupUin: BigInt(groupId), settings: { addType }, field12: 0, - }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0x89a_0', protobuf_encode>(env)); -} - -export async function setGroupSearch(bridge: Bridge, groupId: number): Promise { - const env = makeOidbEnvelope(0x89A, 0, { - groupUin: BigInt(groupId), settings: new Uint8Array(0), field12: 0, - }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0x89a_0', protobuf_encode>(env)); -} - -export async function setGroupAddRequest( - bridge: Bridge, groupId: number, sequence: number, eventType: number, - approve: boolean, reason = '', filtered = false, -): Promise { - const subCmd = filtered ? 2 : 1; - const cmd = filtered ? 'OidbSvcTrpcTcp.0x10c8_2' : 'OidbSvcTrpcTcp.0x10c8_1'; - const env = makeOidbEnvelope( - 0x10C8, subCmd, - { accept: approve ? 1 : 2, body: { sequence: BigInt(sequence), eventType, groupUin: groupId, message: reason } }, - true, - ); - await runOidb(bridge, cmd, protobuf_encode>(env)); -} - -// ─────────────── kick / leave ─────────────── - -export async function kickGroupMember(bridge: Bridge, groupId: number, userId: number, reject: boolean, reason = ''): Promise { - const uid = await bridge.resolveUserUid(userId, groupId); - const env = makeOidbEnvelope(0x8A0, 1, { - groupUin: groupId, targetUid: uid, rejectAddRequest: reject, reason, - }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0x8a0_1', protobuf_encode>(env)); -} - -export async function kickGroupMembers(bridge: Bridge, groupId: number, userIds: number[], reject: boolean): Promise { - const targetUids = await Promise.all(userIds.map(userId => bridge.resolveUserUid(userId, groupId))); - const env = makeOidbEnvelope(0x8A0, 1, { - groupId: BigInt(groupId), targetUids, rejectAddRequest: reject ? 1 : 0, kickReason: new Uint8Array(0), field12: 0, - }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0x8a0_1', protobuf_encode>(env)); -} - -export async function leaveGroup(bridge: Bridge, groupId: number): Promise { - const env = makeOidbEnvelope(0x1097, 1, { groupUin: groupId }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0x1097_1', protobuf_encode>(env)); -} - -// ─────────────── role / display name ─────────────── - -export async function setGroupAdmin(bridge: Bridge, groupId: number, userId: number, enable: boolean): Promise { - const uid = await bridge.resolveUserUid(userId, groupId); - const env = makeOidbEnvelope(0x1096, 1, { groupUin: groupId, uid, isAdmin: enable }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0x1096_1', protobuf_encode>(env)); -} - -export async function setGroupCard(bridge: Bridge, groupId: number, userId: number, card: string): Promise { - const uid = await bridge.resolveUserUid(userId, groupId); - const env = makeOidbEnvelope(0x8FC, 3, { - groupUin: groupId, body: { targetUid: uid, targetName: card }, - }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0x8fc_3', protobuf_encode>(env)); -} - -export async function setGroupName(bridge: Bridge, groupId: number, name: string): Promise { - const env = makeOidbEnvelope(0x89A, 15, { groupUin: groupId, body: { targetName: name } }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0x89a_15', protobuf_encode>(env)); -} - -export async function setGroupSpecialTitle(bridge: Bridge, groupId: number, userId: number, title: string): Promise { - const uid = await bridge.resolveUserUid(userId, groupId); - const env = makeOidbEnvelope(0x8FC, 2, { - groupUin: groupId, body: { targetUid: uid, specialTitle: title, expireTime: -1 }, - }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0x8fc_2', protobuf_encode>(env)); -} - -// ─────────────── personal-side metadata ─────────────── - -// setGroupRemark is the bot's local-only label for the group; it lives -// here rather than in friend.ts because the semantic is "operate on a -// group" rather than "operate on a contact list". -export async function setGroupRemark(bridge: Bridge, groupId: number, remark: string): Promise { - const env = makeOidbEnvelope(0xF16, 1, { inner: { groupId: BigInt(groupId), remark }, field12: 0 }); - await runOidb(bridge, 'OidbSvcTrpcTcp.0xf16_1', protobuf_encode>(env)); -} - -// ─────────────── group-level quota query ─────────────── - -export async function getGroupAtAllRemain( - bridge: Bridge, - groupId: number, -) { - const req = { - basic1: 1, - basic2: 2, - basic3: 1, - uin: BigInt(bridge.identity.uin), - groupId: BigInt(groupId), - type: 0, - }; - - const env = makeOidbEnvelope(0x8A7, 0, req); - const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x8a7_0', protobuf_encode>(env)); - const result = protobuf_decode>(respBytes).body; - - if (!result) { - throw new Error('get group at all remain result empty'); - } - - // Cast numbers to plain Number: the OIDB layer may surface uint32 as - // BigInt and the WebUI JSON serializer chokes on those. - return { - can_at_all: !!result.canAtAll, - remain_at_all_count_for_group: Number(result.groupRemain || 0), - remain_at_all_count_for_uin: Number(result.uinRemain || 0), - }; -} diff --git a/packages/core/src/bridge/apis/group-admin.ts b/packages/core/src/bridge/apis/group-admin.ts new file mode 100644 index 00000000..693a2a36 --- /dev/null +++ b/packages/core/src/bridge/apis/group-admin.ts @@ -0,0 +1,175 @@ +// GroupAdminApi — mute/kick/admin/card/name/title/leave + join-policy +// + add-request handling. Inlined from the previous +// `actions/group-admin.ts` (deleted alongside the rest of the +// actions/* facade in commit 13). + +import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; +import type { OidbBase } from '@snowluma/proto-defs/oidb'; +import type { + Oidb0x89a_0AddOption, + Oidb0x89a_0Search, + Oidb0x8a0Req, + Oidb0x8a7Req, + Oidb0x8a7Resp, + Oidb0xf16Req, + OidbGroupRequestAction, + OidbKickMember, + OidbLeaveGroup, + OidbMuteAll, + OidbMuteMember, + OidbRenameGroup, + OidbRenameMember, + OidbSetAdmin, + OidbSpecialTitle, +} 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 GroupAdminApi { + constructor(private readonly ctx: BridgeContext) {} + + async muteMember(groupId: number, userId: number, duration: number): Promise { + const bridge = asBridge(this.ctx); + const uid = await this.ctx.resolveUserUid(userId, groupId); + const env = makeOidbEnvelope(0x1253, 1, { + groupUin: groupId, type: 1, body: { targetUid: uid, duration }, + }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0x1253_1', protobuf_encode>(env)); + } + + async muteAll(groupId: number, enable: boolean): Promise { + const bridge = asBridge(this.ctx); + const env = makeOidbEnvelope(0x89A, 0, { + groupUin: groupId, muteState: { state: enable ? 0xFFFFFFFF : 0 }, + }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0x89a_0', protobuf_encode>(env)); + } + + async setAddOption(groupId: number, addType: number): Promise { + const bridge = asBridge(this.ctx); + const env = makeOidbEnvelope(0x89A, 0, { + groupUin: BigInt(groupId), settings: { addType }, field12: 0, + }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0x89a_0', protobuf_encode>(env)); + } + + async setSearch(groupId: number): Promise { + const bridge = asBridge(this.ctx); + const env = makeOidbEnvelope(0x89A, 0, { + groupUin: BigInt(groupId), settings: new Uint8Array(0), field12: 0, + }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0x89a_0', protobuf_encode>(env)); + } + + async setAddRequest( + groupId: number, sequence: number, eventType: number, + approve: boolean, reason = '', filtered = false, + ): Promise { + const bridge = asBridge(this.ctx); + const subCmd = filtered ? 2 : 1; + const cmd = filtered ? 'OidbSvcTrpcTcp.0x10c8_2' : 'OidbSvcTrpcTcp.0x10c8_1'; + const env = makeOidbEnvelope( + 0x10C8, subCmd, + { accept: approve ? 1 : 2, body: { sequence: BigInt(sequence), eventType, groupUin: groupId, message: reason } }, + true, + ); + await runOidb(bridge, cmd, protobuf_encode>(env)); + } + + async kickMember(groupId: number, userId: number, reject: boolean, reason = ''): Promise { + const bridge = asBridge(this.ctx); + const uid = await this.ctx.resolveUserUid(userId, groupId); + const env = makeOidbEnvelope(0x8A0, 1, { + groupUin: groupId, targetUid: uid, rejectAddRequest: reject, reason, + }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0x8a0_1', protobuf_encode>(env)); + } + + async kickMembers(groupId: number, userIds: number[], reject: boolean): Promise { + const bridge = asBridge(this.ctx); + const targetUids = await Promise.all(userIds.map(userId => this.ctx.resolveUserUid(userId, groupId))); + const env = makeOidbEnvelope(0x8A0, 1, { + groupId: BigInt(groupId), targetUids, rejectAddRequest: reject ? 1 : 0, kickReason: new Uint8Array(0), field12: 0, + }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0x8a0_1', protobuf_encode>(env)); + } + + async leave(groupId: number): Promise { + const bridge = asBridge(this.ctx); + const env = makeOidbEnvelope(0x1097, 1, { groupUin: groupId }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0x1097_1', protobuf_encode>(env)); + } + + async setAdmin(groupId: number, userId: number, enable: boolean): Promise { + const bridge = asBridge(this.ctx); + const uid = await this.ctx.resolveUserUid(userId, groupId); + const env = makeOidbEnvelope(0x1096, 1, { groupUin: groupId, uid, isAdmin: enable }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0x1096_1', protobuf_encode>(env)); + } + + async setCard(groupId: number, userId: number, card: string): Promise { + const bridge = asBridge(this.ctx); + const uid = await this.ctx.resolveUserUid(userId, groupId); + const env = makeOidbEnvelope(0x8FC, 3, { + groupUin: groupId, body: { targetUid: uid, targetName: card }, + }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0x8fc_3', protobuf_encode>(env)); + } + + async setName(groupId: number, name: string): Promise { + const bridge = asBridge(this.ctx); + const env = makeOidbEnvelope(0x89A, 15, { groupUin: groupId, body: { targetName: name } }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0x89a_15', protobuf_encode>(env)); + } + + async setSpecialTitle(groupId: number, userId: number, title: string): Promise { + const bridge = asBridge(this.ctx); + const uid = await this.ctx.resolveUserUid(userId, groupId); + const env = makeOidbEnvelope(0x8FC, 2, { + groupUin: groupId, body: { targetUid: uid, specialTitle: title, expireTime: -1 }, + }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0x8fc_2', protobuf_encode>(env)); + } + + /** + * The bot's local-only label for a group. Lives here rather than + * `FriendApi` because the semantic is "operate on a group" rather + * than "operate on a contact list". + */ + async setRemark(groupId: number, remark: string): Promise { + const bridge = asBridge(this.ctx); + const env = makeOidbEnvelope(0xF16, 1, { inner: { groupId: BigInt(groupId), remark }, field12: 0 }); + await runOidb(bridge, 'OidbSvcTrpcTcp.0xf16_1', protobuf_encode>(env)); + } + + async getAtAllRemain(groupId: number): Promise<{ + can_at_all: boolean; + remain_at_all_count_for_group: number; + remain_at_all_count_for_uin: number; + }> { + const bridge = asBridge(this.ctx); + const req = { + basic1: 1, + basic2: 2, + basic3: 1, + uin: BigInt(this.ctx.identity.uin), + groupId: BigInt(groupId), + type: 0, + }; + const env = makeOidbEnvelope(0x8A7, 0, req); + const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x8a7_0', protobuf_encode>(env)); + const result = protobuf_decode>(respBytes).body; + if (!result) throw new Error('get group at all remain result empty'); + + // Cast numbers to plain Number: the OIDB layer may surface uint32 + // as BigInt and the WebUI JSON serializer chokes on those. + return { + can_at_all: !!result.canAtAll, + remain_at_all_count_for_group: Number(result.groupRemain || 0), + remain_at_all_count_for_uin: Number(result.uinRemain || 0), + }; + } +} diff --git a/packages/core/src/bridge/apis/index.ts b/packages/core/src/bridge/apis/index.ts index 2f383ae6..729fdba5 100644 --- a/packages/core/src/bridge/apis/index.ts +++ b/packages/core/src/bridge/apis/index.ts @@ -22,6 +22,7 @@ import type { BridgeContext } from '../bridge-context'; import { ContactsApi } from './contacts'; +import { GroupAdminApi } from './group-admin'; import { MessageApi } from './message'; export interface ApiHub { @@ -29,8 +30,9 @@ export interface ApiHub { readonly message: MessageApi; /** Friend / group / member roster + user-profile + group-request-list + download-rkey. */ readonly contacts: ContactsApi; + /** Group moderation: mute/kick/admin/card/name/title/leave + join-policy. */ + readonly groupAdmin: GroupAdminApi; // Pending — added as later commits land: - // readonly groupAdmin: GroupAdminApi; // readonly groupFile: GroupFileApi; // readonly groupAlbum: GroupAlbumApi; // readonly friend: FriendApi; @@ -54,6 +56,7 @@ export function buildApiHub(ctx: BridgeContext): ApiHub { return { message: new MessageApi(ctx), contacts: new ContactsApi(ctx), + groupAdmin: new GroupAdminApi(ctx), }; } @@ -61,4 +64,5 @@ 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 { GroupAdminApi } from './group-admin'; export { MessageApi } from './message'; diff --git a/packages/core/src/bridge/bridge-interface.ts b/packages/core/src/bridge/bridge-interface.ts index 66e4d462..dac9178f 100644 --- a/packages/core/src/bridge/bridge-interface.ts +++ b/packages/core/src/bridge/bridge-interface.ts @@ -44,20 +44,7 @@ export interface BridgeInterface { // ─── Fetch (contacts / profile / system) — moved to apis.contacts ─── - // ─── Group admin ─── - muteGroupMember(groupId: number, userId: number, duration: number): Promise; - muteGroupAll(groupId: number, enable: boolean): Promise; - setGroupAddOption(groupId: number, addType: number): Promise; - setGroupSearch(groupId: number): Promise; - setGroupAddRequest(groupId: number, sequence: number, eventType: number, approve: boolean, reason?: string, filtered?: boolean): Promise; - kickGroupMember(groupId: number, userId: number, reject: boolean, reason?: string): Promise; - kickGroupMembers(groupId: number, userIds: number[], reject: boolean): Promise; - leaveGroup(groupId: number): Promise; - setGroupAdmin(groupId: number, userId: number, enable: boolean): Promise; - setGroupCard(groupId: number, userId: number, card: string): Promise; - setGroupName(groupId: number, name: string): Promise; - setGroupSpecialTitle(groupId: number, userId: number, title: string): Promise; - setGroupRemark(groupId: number, remark: string): Promise; + // ─── Group admin (moved to apis.groupAdmin) ─── setGroupReaction(groupId: number, sequence: number, code: string, isSet: boolean): Promise; setGroupEssence(groupId: number, sequence: number, random: number, enable: boolean): Promise; @@ -135,7 +122,6 @@ export interface BridgeInterface { fetchCustomFace(count?: number): Promise; getProfileLike(userId?: number, start?: number, limit?: number): Promise; getUnidirectionalFriendList(): Promise; - getGroupAtAllRemain(groupId: number): Promise; // ─── Misc ─── translateEn2Zh(words: string[]): Promise; diff --git a/packages/core/src/bridge/bridge.ts b/packages/core/src/bridge/bridge.ts index fe3e183d..9a54e5c3 100644 --- a/packages/core/src/bridge/bridge.ts +++ b/packages/core/src/bridge/bridge.ts @@ -31,22 +31,7 @@ import { setFriendAddRequest as setFriendAddRequest_, setFriendRemark as setFriendRemark_, } from './actions/friend'; -import { - getGroupAtAllRemain as getGroupAtAllRemain_, - kickGroupMember as kickGroupMember_, - kickGroupMembers as kickGroupMembers_, - leaveGroup as leaveGroup_, - muteGroupAll as muteGroupAll_, - muteGroupMember as muteGroupMember_, - setGroupAddOption as setGroupAddOption_, - setGroupAddRequest as setGroupAddRequest_, - setGroupAdmin as setGroupAdmin_, - setGroupCard as setGroupCard_, - setGroupName as setGroupName_, - setGroupRemark as setGroupRemark_, - setGroupSearch as setGroupSearch_, - setGroupSpecialTitle as setGroupSpecialTitle_, -} from './actions/group-admin'; +// actions/group-admin.ts removed — moved to `apis/group-admin.ts::GroupAdminApi`. import { commentGroupAlbumMedia as commentGroupAlbumMedia_, deleteGroupAlbumMedia as deleteGroupAlbumMedia_, @@ -354,19 +339,11 @@ export class Bridge implements BridgeInterface { // fetchUserProfile / fetchGroupRequests / fetchDownloadRKeys // moved to apis.contacts (see apis/contacts.ts::ContactsApi). - // --- Delegated: Admin / action methods --- - - async muteGroupMember(groupId: number, userId: number, duration: number): Promise { return muteGroupMember_(this, groupId, userId, duration); } - async muteGroupAll(groupId: number, enable: boolean): Promise { return muteGroupAll_(this, groupId, enable); } - async setGroupAddOption(groupId: number, addType: number): Promise { return setGroupAddOption_(this, groupId, addType); } - async setGroupSearch(groupId: number): Promise { return setGroupSearch_(this, groupId); } - async kickGroupMember(groupId: number, userId: number, reject: boolean, reason = ''): Promise { return kickGroupMember_(this, groupId, userId, reject, reason); } - async kickGroupMembers(groupId: number, userIds: number[], reject: boolean): Promise { return kickGroupMembers_(this, groupId, userIds, reject); } - async leaveGroup(groupId: number): Promise { return leaveGroup_(this, groupId); } - async setGroupAdmin(groupId: number, userId: number, enable: boolean): Promise { return setGroupAdmin_(this, groupId, userId, enable); } - async setGroupCard(groupId: number, userId: number, card: string): Promise { return setGroupCard_(this, groupId, userId, card); } - async setGroupName(groupId: number, name: string): Promise { return setGroupName_(this, groupId, name); } - async setGroupSpecialTitle(groupId: number, userId: number, title: string): Promise { return setGroupSpecialTitle_(this, groupId, userId, title); } + // --- Delegated: action methods --- + // + // 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 { return setFriendAddRequest_(this, uidOrFlag, approve); } async deleteFriend(userId: number, block = false): Promise { return deleteFriend_(this, userId, block); } async uploadGroupFile(groupId: number, file: string, name = '', folderId = '/', uploadFile = true): Promise<{ fileId: string | null }> { @@ -392,14 +369,12 @@ export class Bridge implements BridgeInterface { async createGroupFileFolder(groupId: number, name: string, parentId = '/'): Promise { return createGroupFileFolder_(this, groupId, name, parentId); } async deleteGroupFileFolder(groupId: number, folderId: string): Promise { return deleteGroupFileFolder_(this, groupId, folderId); } async renameGroupFileFolder(groupId: number, folderId: string, newFolderName: string): Promise { return renameGroupFileFolder_(this, groupId, folderId, newFolderName); } - async setGroupAddRequest(groupId: number, sequence: number, eventType: number, approve: boolean, reason = '', filtered = false): Promise { return setGroupAddRequest_(this, groupId, sequence, eventType, approve, reason, filtered); } async sendPoke(isGroup: boolean, peerUin: number, targetUin?: number): Promise { return sendPoke_(this, isGroup, peerUin, targetUin); } async sendLike(userId: number, count: number): Promise { return sendLike_(this, userId, count); } async setGroupEssence(groupId: number, sequence: number, random: number, enable: boolean): Promise { return setGroupEssence_(this, groupId, sequence, random, enable); } async setGroupReaction(groupId: number, sequence: number, code: string, isSet: boolean): Promise { return setGroupReaction_(this, groupId, sequence, code, isSet); } // recall* / markRead* moved to apis/message.ts::MessageApi. async setFriendRemark(userId: number, remark: string): Promise { return setFriendRemark_(this, userId, remark); } - async setGroupRemark(groupId: number, remark: string): Promise { return setGroupRemark_(this, groupId, remark); } async getGroupHonorInfo(groupId: number, type: WebHonorType | string): Promise { return getGroupHonorInfo_(this, groupId, type); } @@ -450,9 +425,6 @@ export class Bridge implements BridgeInterface { async fetchGroupFileCount(groupId: number): Promise<{ fileCount: number; maxCount: number }> { return fetchGroupFileCount_(this, groupId); } - async getGroupAtAllRemain(groupId: number) { - return getGroupAtAllRemain_(this, groupId); - } // extend async setOnlineStatus(status: number, extStatus: number = 0, batteryStatus: number = 100): Promise { return setOnlineStatus_(this, status, extStatus, batteryStatus); diff --git a/packages/core/src/onebot/actions/extended.ts b/packages/core/src/onebot/actions/extended.ts index 3e328801..0d461c27 100644 --- a/packages/core/src/onebot/actions/extended.ts +++ b/packages/core/src/onebot/actions/extended.ts @@ -550,7 +550,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.setGroupRemark(groupId, asString(params.remark)); + await ctx.bridge.apis.groupAdmin.setRemark(groupId, asString(params.remark)); return okResponse(); }); @@ -888,7 +888,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { try { - const data = await ctx.bridge.getGroupAtAllRemain(groupId); + const data = await ctx.bridge.apis.groupAdmin.getAtAllRemain(groupId); return okResponse(data); } catch (e) { return failedResponse(RETCODE.ACTION_FAILED, String(e)); diff --git a/packages/core/src/onebot/actions/group-admin.ts b/packages/core/src/onebot/actions/group-admin.ts index 404b017a..c5bb2a74 100644 --- a/packages/core/src/onebot/actions/group-admin.ts +++ b/packages/core/src/onebot/actions/group-admin.ts @@ -42,14 +42,14 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { const groupId = asNumber(params.group_id); const addType = asNumber(params.add_type); if (!groupId || addType === undefined) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and add_type are required'); - await ctx.bridge.setGroupAddOption(groupId, addType); + await ctx.bridge.apis.groupAdmin.setAddOption(groupId, addType); return okResponse(); }); h.registerAction('set_group_search', async (params) => { const groupId = asNumber(params.group_id); if (!groupId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id is required'); - await ctx.bridge.setGroupSearch(groupId); + await ctx.bridge.apis.groupAdmin.setSearch(groupId); return okResponse(); }); @@ -58,7 +58,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { const userId = asNumber(params.user_id); const enable = asBoolean(params.enable, true); if (!groupId || !userId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and user_id are required'); - await ctx.bridge.setGroupAdmin(groupId, userId, enable); + await ctx.bridge.apis.groupAdmin.setAdmin(groupId, userId, enable); return okResponse(); }); @@ -67,7 +67,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { const userId = asNumber(params.user_id); const card = asString(params.card); if (!groupId || !userId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and user_id are required'); - await ctx.bridge.setGroupCard(groupId, userId, card); + await ctx.bridge.apis.groupAdmin.setCard(groupId, userId, card); return okResponse(); }); @@ -75,7 +75,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { const groupId = asNumber(params.group_id); const name = asString(params.group_name); if (!groupId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id is required'); - await ctx.bridge.setGroupName(groupId, name); + await ctx.bridge.apis.groupAdmin.setName(groupId, name); return okResponse(); }); @@ -91,7 +91,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { const userId = asNumber(params.user_id); const title = asString(params.special_title); if (!groupId || !userId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and user_id are required'); - await ctx.bridge.setGroupSpecialTitle(groupId, userId, title); + await ctx.bridge.apis.groupAdmin.setSpecialTitle(groupId, userId, title); return okResponse(); }); diff --git a/packages/core/src/onebot/instance-context.ts b/packages/core/src/onebot/instance-context.ts index 721c7b3d..a78d407f 100644 --- a/packages/core/src/onebot/instance-context.ts +++ b/packages/core/src/onebot/instance-context.ts @@ -112,11 +112,11 @@ export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext { getStrangerInfo: (userId) => getStrangerInfo(bridge, userId), // Group admin — names translate to OneBot vocabulary. - setGroupKick: (groupId, userId, reject) => bridge.kickGroupMember(groupId, userId, reject), - setGroupKickMembers: (groupId, userIds, reject) => bridge.kickGroupMembers(groupId, userIds, reject), - setGroupBan: (groupId, userId, duration) => bridge.muteGroupMember(groupId, userId, duration), - setGroupWholeBan: (groupId, enable) => bridge.muteGroupAll(groupId, enable), - setGroupLeave: (groupId) => bridge.leaveGroup(groupId), + setGroupKick: (groupId, userId, reject) => bridge.apis.groupAdmin.kickMember(groupId, userId, reject), + setGroupKickMembers: (groupId, userIds, reject) => bridge.apis.groupAdmin.kickMembers(groupId, userIds, reject), + setGroupBan: (groupId, userId, duration) => bridge.apis.groupAdmin.muteMember(groupId, userId, duration), + setGroupWholeBan: (groupId, enable) => bridge.apis.groupAdmin.muteAll(groupId, enable), + setGroupLeave: (groupId) => bridge.apis.groupAdmin.leave(groupId), // Group file — fileId field extraction + defaults. uploadGroupFile: async (groupId, file, name, folderId, uploadFile) => { @@ -164,7 +164,7 @@ export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext { handleDeleteFriend: (userId, block) => bridge.deleteFriend(userId, !!block), forceFetchClientKey: () => bridge.forceFetchClientKey(), setFriendRemark: (userId, remark) => bridge.setFriendRemark(userId, remark), - setGroupRemark: (groupId, remark) => bridge.setGroupRemark(groupId, remark), + setGroupRemark: (groupId, remark) => bridge.apis.groupAdmin.setRemark(groupId, remark), setGroupAvatar: (groupId, source) => bridge.setGroupAvatar(groupId, source), getGroupFileCount: (groupId) => bridge.fetchGroupFileCount(groupId), diff --git a/packages/core/src/onebot/modules/request-actions.ts b/packages/core/src/onebot/modules/request-actions.ts index 5b437a00..a29aa974 100644 --- a/packages/core/src/onebot/modules/request-actions.ts +++ b/packages/core/src/onebot/modules/request-actions.ts @@ -27,7 +27,7 @@ export async function handleGroupAddRequest( throw new Error('matching group request not found'); } - await bridge.setGroupAddRequest( + await bridge.apis.groupAdmin.setAddRequest( groupId, matching.sequence, matching.eventType, diff --git a/packages/core/tests/actions/group-admin.test.ts b/packages/core/tests/actions/group-admin.test.ts index 28b1ce7f..b2a1c7d6 100644 --- a/packages/core/tests/actions/group-admin.test.ts +++ b/packages/core/tests/actions/group-admin.test.ts @@ -21,7 +21,7 @@ vi.mock('../../src/bridge/bridge-oidb', async () => { }); import * as oidb from '../../src/bridge/bridge-oidb'; -import * as admin from '../../src/bridge/actions/group-admin'; +import { GroupAdminApi } from '../../src/bridge/apis/group-admin'; import { mockBridge } from './_helpers'; describe('actions/group-admin', () => { @@ -33,7 +33,7 @@ describe('actions/group-admin', () => { it('muteGroupMember resolves UID and dispatches 0x1253_1', async () => { const bridge = mockBridge(); - await admin.muteGroupMember(bridge as any, 12345, 67890, 600); + await new GroupAdminApi(bridge as any).muteMember(12345, 67890, 600); expect(bridge.resolveUserUid).toHaveBeenCalledWith(67890, 12345); expect(oidb.runOidb).toHaveBeenCalledOnce(); const [, cmd] = vi.mocked(oidb.runOidb).mock.calls[0]!; @@ -50,13 +50,13 @@ describe('actions/group-admin', () => { it('muteGroupAll flips the muteState flag based on enable', async () => { const bridge = mockBridge(); - await admin.muteGroupAll(bridge as any, 12345, true); + await new GroupAdminApi(bridge as any).muteAll(12345, true); expect(vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2]).toMatchObject({ groupUin: 12345, muteState: { state: 0xFFFFFFFF }, }); - await admin.muteGroupAll(bridge as any, 12345, false); + await new GroupAdminApi(bridge as any).muteAll(12345, false); expect(vi.mocked(oidb.makeOidbEnvelope).mock.calls[1]![2]).toMatchObject({ muteState: { state: 0 }, }); @@ -64,7 +64,7 @@ describe('actions/group-admin', () => { it('kickGroupMember resolves UID per-group and forwards reject + reason', async () => { const bridge = mockBridge(); - await admin.kickGroupMember(bridge as any, 12345, 67890, true, 'bad behaviour'); + await new GroupAdminApi(bridge as any).kickMember(12345, 67890, true, 'bad behaviour'); expect(bridge.resolveUserUid).toHaveBeenCalledWith(67890, 12345); expect(vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2]).toMatchObject({ groupUin: 12345, @@ -80,7 +80,7 @@ describe('actions/group-admin', () => { .mockResolvedValueOnce('uid-a') .mockResolvedValueOnce('uid-b'); - await admin.kickGroupMembers(bridge as any, 12345, [11, 22], false); + await new GroupAdminApi(bridge as any).kickMembers(12345, [11, 22], false); expect(bridge.resolveUserUid).toHaveBeenCalledTimes(2); const payload: any = vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2]; expect(payload.targetUids).toEqual(['uid-a', 'uid-b']); @@ -89,7 +89,7 @@ describe('actions/group-admin', () => { it('leaveGroup sends 0x1097_1 with the groupUin', async () => { const bridge = mockBridge(); - await admin.leaveGroup(bridge as any, 12345); + await new GroupAdminApi(bridge as any).leave(12345); const [, cmd] = vi.mocked(oidb.runOidb).mock.calls[0]!; expect(cmd).toBe('OidbSvcTrpcTcp.0x1097_1'); expect(vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2]).toMatchObject({ groupUin: 12345 }); @@ -97,7 +97,7 @@ describe('actions/group-admin', () => { it('setGroupAdmin resolves UID and sends 0x1096_1', async () => { const bridge = mockBridge(); - await admin.setGroupAdmin(bridge as any, 12345, 67890, true); + await new GroupAdminApi(bridge as any).setAdmin(12345, 67890, true); expect(vi.mocked(oidb.makeOidbEnvelope).mock.calls[0]![2]).toMatchObject({ groupUin: 12345, uid: 'resolved-uid', isAdmin: true, }); @@ -105,12 +105,12 @@ describe('actions/group-admin', () => { it('setGroupCard / setGroupName / setGroupSpecialTitle / setGroupRemark / setGroupAddOption / setGroupSearch dispatch the right command', async () => { const bridge = mockBridge(); - await admin.setGroupCard(bridge as any, 1, 2, 'newCard'); - await admin.setGroupName(bridge as any, 1, 'newName'); - await admin.setGroupSpecialTitle(bridge as any, 1, 2, 'newTitle'); - await admin.setGroupRemark(bridge as any, 1, 'newRemark'); - await admin.setGroupAddOption(bridge as any, 1, 2); - await admin.setGroupSearch(bridge as any, 1); + await new GroupAdminApi(bridge as any).setCard(1, 2, 'newCard'); + await new GroupAdminApi(bridge as any).setName(1, 'newName'); + await new GroupAdminApi(bridge as any).setSpecialTitle(1, 2, 'newTitle'); + await new GroupAdminApi(bridge as any).setRemark(1, 'newRemark'); + await new GroupAdminApi(bridge as any).setAddOption(1, 2); + await new GroupAdminApi(bridge as any).setSearch(1); const cmds = vi.mocked(oidb.runOidb).mock.calls.map(call => call[1]); expect(cmds).toEqual([ @@ -125,8 +125,8 @@ describe('actions/group-admin', () => { it('setGroupAddRequest picks _1 / _2 based on filtered flag', async () => { const bridge = mockBridge(); - await admin.setGroupAddRequest(bridge as any, 12345, 5, 1, true, 'ok', false); - await admin.setGroupAddRequest(bridge as any, 12345, 5, 1, false, 'no', true); + await new GroupAdminApi(bridge as any).setAddRequest(12345, 5, 1, true, 'ok', false); + await new GroupAdminApi(bridge as any).setAddRequest(12345, 5, 1, false, 'no', true); const cmds = vi.mocked(oidb.runOidb).mock.calls.map(call => call[1]); expect(cmds).toEqual([ @@ -149,7 +149,7 @@ describe('actions/group-admin', () => { body: { canAtAll: true, groupRemain: 12, uinRemain: 5 } as any, }), ); - const out = await admin.getGroupAtAllRemain(bridge as any, 12345); + const out = await new GroupAdminApi(bridge as any).getAtAllRemain(12345); expect(out).toEqual({ can_at_all: true, remain_at_all_count_for_group: 12, @@ -164,6 +164,6 @@ describe('actions/group-admin', () => { vi.mocked(oidb.runOidb).mockResolvedValueOnce( protobuf_encode>({}), ); - await expect(admin.getGroupAtAllRemain(bridge as any, 12345)).rejects.toThrow(/empty/); + await expect(new GroupAdminApi(bridge as any).getAtAllRemain(12345)).rejects.toThrow(/empty/); }); }); diff --git a/packages/core/tests/onebot/request-actions.test.ts b/packages/core/tests/onebot/request-actions.test.ts index 044bd724..6b9b7d90 100644 --- a/packages/core/tests/onebot/request-actions.test.ts +++ b/packages/core/tests/onebot/request-actions.test.ts @@ -52,32 +52,32 @@ function fakeRequest(overrides: Partial = {}): GroupRequestInf describe('onebot/modules/request-actions / handleGroupAddRequest', () => { it('matches add requests by groupId and targetUid', async () => { - const setGroupAddRequest = vi.fn(async () => {}); + const setAddRequest = vi.fn(async () => {}); const bridge = fakeBridge({ fetchGroupRequests: vi.fn(async () => [ fakeRequest({ groupId: 999, targetUid: 'u_t', sequence: 42, eventType: 7, filtered: false }), ]), - setGroupAddRequest: setGroupAddRequest as any, + apis: { groupAdmin: { setAddRequest } } as any, }); await handleGroupAddRequest(bridge, 'add:999:u_t', true, 'ok'); - expect(setGroupAddRequest).toHaveBeenCalledOnce(); - expect(setGroupAddRequest).toHaveBeenCalledWith(999, 42, 7, true, 'ok', false); + expect(setAddRequest).toHaveBeenCalledOnce(); + expect(setAddRequest).toHaveBeenCalledWith(999, 42, 7, true, 'ok', false); }); it('matches invite requests by groupId and invitorUid', async () => { - const setGroupAddRequest = vi.fn(async () => {}); + const setAddRequest = vi.fn(async () => {}); const bridge = fakeBridge({ fetchGroupRequests: vi.fn(async () => [ fakeRequest({ groupId: 999, invitorUid: 'u_i', sequence: 97, eventType: 8, filtered: false }), ]), - setGroupAddRequest: setGroupAddRequest as any, + apis: { groupAdmin: { setAddRequest } } as any, }); await handleGroupAddRequest(bridge, 'invite:999:u_i', false, 'no'); - expect(setGroupAddRequest).toHaveBeenCalledOnce(); - expect(setGroupAddRequest).toHaveBeenCalledWith(999, 97, 8, false, 'no', false); + expect(setAddRequest).toHaveBeenCalledOnce(); + expect(setAddRequest).toHaveBeenCalledWith(999, 97, 8, false, 'no', false); }); });