refactor(api): move interaction methods to InteractionApi (#6 commit 6/13)
Folds the entire `actions/interaction.ts` (sendPoke / sendLike / setGroupReaction / getEmojiLikes) plus the lone-survivor `setGroupEssence` from `actions/group-message.ts` into `apis/interaction.ts::InteractionApi`. Both action files deleted — the recall + markRead halves of group-message were already absorbed into MessageApi back in commit 1. Skipping the planned standalone GroupMessageApi commit: with only one method left (`setGroupEssence`), giving it its own Api class would be pure overhead. Essence is conceptually an "interaction" anyway (mark a message as essence is parallel to "react with an emoji"). Method renaming (Api namespace already says "interaction"): sendPoke → sendPoke (no rename — already minimal) sendLike → sendLike setGroupReaction → setReaction setGroupEssence → setEssence getEmojiLikes → getEmojiLikes (no rename — read-side, fine) Callers updated: `instance-context.ts`, `modules/message-actions.ts`, `actions/extended.ts`. Tests `interaction.test.ts` rewritten to use `new InteractionApi(bridge as any).method(...)` and gained a `setEssence` test. `group-message.test.ts` rewritten to target MessageApi.recall* / markRead* methods that were already moved. 587/587 tests pass.
This commit is contained in:
1 parent
c92daa56df
commit
73f1cd3c80
11 files changed
+155
-221
No files matched your search
@@ -25,6 +25,7 @@ import { ContactsApi } from './contacts';
|
||||
import { GroupAdminApi } from './group-admin';
|
||||
import { GroupAlbumApi } from './group-album';
|
||||
import { GroupFileApi } from './group-file';
|
||||
import { InteractionApi } from './interaction';
|
||||
import { MessageApi } from './message';
|
||||
|
||||
export interface ApiHub {
|
||||
@@ -38,6 +39,8 @@ export interface ApiHub {
|
||||
readonly groupFile: GroupFileApi;
|
||||
/** Group photo album: list/upload/comment/like/delete + media listing. */
|
||||
readonly groupAlbum: GroupAlbumApi;
|
||||
/** Interactive engagement: poke / like / reaction / essence / emoji-like-list. */
|
||||
readonly interaction: InteractionApi;
|
||||
// Pending — added as later commits land:
|
||||
// readonly friend: FriendApi;
|
||||
// readonly interaction: InteractionApi;
|
||||
@@ -63,6 +66,7 @@ export function buildApiHub(ctx: BridgeContext): ApiHub {
|
||||
groupAdmin: new GroupAdminApi(ctx),
|
||||
groupFile: new GroupFileApi(ctx),
|
||||
groupAlbum: new GroupAlbumApi(ctx),
|
||||
interaction: new InteractionApi(ctx),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -73,4 +77,5 @@ export { ContactsApi } from './contacts';
|
||||
export { GroupAdminApi } from './group-admin';
|
||||
export { GroupAlbumApi } from './group-album';
|
||||
export { GroupFileApi } from './group-file';
|
||||
export { InteractionApi } from './interaction';
|
||||
export { MessageApi } from './message';
|
||||
@@ -0,0 +1,92 @@
|
||||
// InteractionApi — interactive engagement primitives (poke / like /
|
||||
// reaction / essence / emoji-like-list). Inlined from
|
||||
// `actions/interaction.ts` + the lone-survivor `setGroupEssence` from
|
||||
// `actions/group-message.ts` (both deleted alongside actions/* in
|
||||
// commit 13). The recall+markRead halves of `group-message.ts` were
|
||||
// already absorbed into `MessageApi` back in commit 1.
|
||||
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
Oidb0x9083Req,
|
||||
Oidb0x9083Resp,
|
||||
OidbEssence,
|
||||
OidbGroupReaction,
|
||||
OidbLike,
|
||||
OidbPoke,
|
||||
} 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 InteractionApi {
|
||||
constructor(private readonly ctx: BridgeContext) {}
|
||||
|
||||
async sendPoke(isGroup: boolean, peerUin: number, targetUin?: number): Promise<void> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const env = makeOidbEnvelope<OidbPoke>(0xED3, 1, {
|
||||
uin: targetUin ?? peerUin,
|
||||
groupUin: isGroup ? peerUin : 0,
|
||||
friendUin: isGroup ? 0 : peerUin,
|
||||
ext: 0,
|
||||
});
|
||||
await runOidb(bridge, 'OidbSvcTrpcTcp.0xed3_1', protobuf_encode<OidbBase<OidbPoke>>(env));
|
||||
}
|
||||
|
||||
async sendLike(userId: number, count: number): Promise<void> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const env = makeOidbEnvelope<OidbLike>(0x7E5, 104, { targetUin: userId, count });
|
||||
await runOidb(bridge, 'OidbSvcTrpcTcp.0x7e5_104', protobuf_encode<OidbBase<OidbLike>>(env));
|
||||
}
|
||||
|
||||
async setReaction(groupId: number, sequence: number, code: string, isSet: boolean): Promise<void> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const subCmd = isSet ? 1 : 2;
|
||||
const cmd = isSet ? 'OidbSvcTrpcTcp.0x9082_1' : 'OidbSvcTrpcTcp.0x9082_2';
|
||||
// Same heuristic NapCat uses: QQ face ids are 1–3 digits ("76"),
|
||||
// unicode codepoints are longer ("128516"). Server requires the
|
||||
// type field to pick the right resolution table; omitting it makes
|
||||
// unicode reactions silently fail.
|
||||
const type = code.length > 3 ? 2 : 1;
|
||||
const env = makeOidbEnvelope<OidbGroupReaction>(0x9082, subCmd, { groupUin: groupId, sequence, code, type });
|
||||
await runOidb(bridge, cmd, protobuf_encode<OidbBase<OidbGroupReaction>>(env));
|
||||
}
|
||||
|
||||
async setEssence(groupId: number, sequence: number, random: number, enable: boolean): Promise<void> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const subCmd = enable ? 1 : 2;
|
||||
const cmd = enable ? 'OidbSvcTrpcTcp.0xeac_1' : 'OidbSvcTrpcTcp.0xeac_2';
|
||||
const env = makeOidbEnvelope<OidbEssence>(0xEAC, subCmd, { groupUin: groupId, sequence, random });
|
||||
await runOidb(bridge, cmd, protobuf_encode<OidbBase<OidbEssence>>(env));
|
||||
}
|
||||
|
||||
async getEmojiLikes(
|
||||
groupId: number,
|
||||
sequence: number,
|
||||
emojiId: string,
|
||||
emojiType = 1,
|
||||
count = 10,
|
||||
cookie = '',
|
||||
): Promise<{ users: Array<{ uin: number }>; cookie: string; isLast: boolean }> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const req: any = {
|
||||
groupId: BigInt(groupId),
|
||||
sequence,
|
||||
emojiType,
|
||||
emojiId,
|
||||
cookie: cookie ? Buffer.from(cookie, 'base64') : new Uint8Array(0),
|
||||
field7: 0,
|
||||
count,
|
||||
field12: 1,
|
||||
};
|
||||
const env = makeOidbEnvelope<Oidb0x9083Req>(0x9083, 1, req);
|
||||
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x9083_1', protobuf_encode<OidbBase<Oidb0x9083Req>>(env));
|
||||
const resp = protobuf_decode<OidbBase<Oidb0x9083Resp>>(respBytes).body;
|
||||
const uin = resp?.inner?.userInfo?.uin;
|
||||
const users = uin ? [{ uin: Number(uin) }] : [];
|
||||
const respCookie = resp?.cookie ? Buffer.from(resp.cookie).toString('base64') : '';
|
||||
return { users, cookie: respCookie, isLast: !respCookie };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user