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
@@ -1,92 +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 { C2CRecallRequest, GroupRecallRequest, OidbEssence, SsoReadedReportReq } from '@snowluma/proto-defs/oidb-actions/base';
|
||||
|
||||
export async function recallGroupMessage(bridge: Bridge, groupId: number, sequence: number): Promise<void> {
|
||||
const request = protobuf_encode<GroupRecallRequest>({
|
||||
type: 1,
|
||||
groupUin: groupId,
|
||||
info: { sequence, random: 0, field3: 0 },
|
||||
settings: { field1: 0 },
|
||||
});
|
||||
const result = await bridge.sendRawPacket('trpc.msg.msg_svc.MsgService.SsoGroupRecallMsg', request);
|
||||
if (!result.success) throw new Error(result.errorMessage || 'recall group message failed');
|
||||
}
|
||||
|
||||
export async function recallPrivateMessage(
|
||||
bridge: Bridge, userUin: number, clientSeq: number,
|
||||
msgSeq: number, random: number, timestamp: number,
|
||||
): Promise<void> {
|
||||
const targetUid = await bridge.resolveUserUid(userUin);
|
||||
const request = protobuf_encode<C2CRecallRequest>({
|
||||
type: 1,
|
||||
targetUid,
|
||||
info: {
|
||||
clientSequence: clientSeq,
|
||||
random,
|
||||
messageId: BigInt((0x01000000 * 0x100000000) + random),
|
||||
timestamp,
|
||||
field5: 0,
|
||||
messageSequence: msgSeq,
|
||||
},
|
||||
settings: { field1: false, field2: false },
|
||||
field6: false,
|
||||
});
|
||||
const result = await bridge.sendRawPacket('trpc.msg.msg_svc.MsgService.SsoC2CRecallMsg', request);
|
||||
if (!result.success) throw new Error(result.errorMessage || 'recall private message failed');
|
||||
}
|
||||
|
||||
export async function markPrivateMessageRead(
|
||||
bridge: Bridge,
|
||||
userId: number,
|
||||
msgSeq: number,
|
||||
timestamp: number = Math.floor(Date.now() / 1000),
|
||||
): Promise<void> {
|
||||
const uid = await bridge.resolveUserUid(userId);
|
||||
|
||||
const request = protobuf_encode<SsoReadedReportReq>({
|
||||
c2cList: [
|
||||
{
|
||||
uid,
|
||||
lastReadTime: BigInt(timestamp),
|
||||
lastReadSeq: BigInt(msgSeq),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const result = await bridge.sendRawPacket('trpc.msg.msg_svc.MsgService.SsoReadedReport', request);
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.errorMessage || 'mark private message read failed');
|
||||
}
|
||||
}
|
||||
|
||||
export async function markGroupMessageRead(
|
||||
bridge: Bridge,
|
||||
groupId: number,
|
||||
msgSeq: number,
|
||||
): Promise<void> {
|
||||
const request = protobuf_encode<SsoReadedReportReq>({
|
||||
groupList: [
|
||||
{
|
||||
groupUin: BigInt(groupId),
|
||||
lastReadSeq: BigInt(msgSeq),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const result = await bridge.sendRawPacket('trpc.msg.msg_svc.MsgService.SsoReadedReport', request);
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.errorMessage || 'mark group message read failed');
|
||||
}
|
||||
}
|
||||
|
||||
export async function setGroupEssence(bridge: Bridge, groupId: number, sequence: number, random: number, enable: boolean): Promise<void> {
|
||||
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));
|
||||
}
|
||||
@@ -1,66 +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 {
|
||||
Oidb0x9083Req,
|
||||
Oidb0x9083Resp,
|
||||
OidbGroupReaction,
|
||||
OidbLike,
|
||||
OidbPoke,
|
||||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||||
|
||||
export async function sendPoke(bridge: Bridge, isGroup: boolean, peerUin: number, targetUin?: number): Promise<void> {
|
||||
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));
|
||||
}
|
||||
|
||||
export async function sendLike(bridge: Bridge, userId: number, count: number): Promise<void> {
|
||||
const env = makeOidbEnvelope<OidbLike>(0x7E5, 104, { targetUin: userId, count });
|
||||
await runOidb(bridge, 'OidbSvcTrpcTcp.0x7e5_104', protobuf_encode<OidbBase<OidbLike>>(env));
|
||||
}
|
||||
|
||||
export async function setGroupReaction(bridge: Bridge, groupId: number, sequence: number, code: string, isSet: boolean): Promise<void> {
|
||||
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));
|
||||
}
|
||||
|
||||
export async function getEmojiLikes(
|
||||
bridge: Bridge,
|
||||
groupId: number,
|
||||
sequence: number,
|
||||
emojiId: string,
|
||||
emojiType: number = 1,
|
||||
count: number = 10,
|
||||
cookie: string = '',
|
||||
): Promise<{ users: Array<{ uin: number }>, cookie: string, isLast: boolean }> {
|
||||
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 };
|
||||
}
|
||||
@@ -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 };
|
||||
}
|
||||
}
|
||||
@@ -44,8 +44,7 @@ export interface BridgeInterface {
|
||||
// ─── Fetch (contacts / profile / system) — moved to apis.contacts ───
|
||||
|
||||
// ─── Group admin (moved to apis.groupAdmin) ───
|
||||
setGroupReaction(groupId: number, sequence: number, code: string, isSet: boolean): Promise<void>;
|
||||
setGroupEssence(groupId: number, sequence: number, random: number, enable: boolean): Promise<void>;
|
||||
// ─── Group reaction / essence (moved to apis.interaction) ───
|
||||
|
||||
// ─── Friend ───
|
||||
setFriendAddRequest(uidOrFlag: string, approve: boolean): Promise<void>;
|
||||
@@ -71,10 +70,8 @@ export interface BridgeInterface {
|
||||
|
||||
// ─── Message ops (moved to apis.message) ───
|
||||
|
||||
// ─── Interaction ───
|
||||
sendPoke(isGroup: boolean, peerUin: number, targetUin?: number): Promise<void>;
|
||||
sendLike(userId: number, count: number): Promise<void>;
|
||||
getEmojiLikes(groupId: number, sequence: number, emojiId: string, emojiType?: number, count?: number, cookie?: string): Promise<{ users: Array<{ uin: number }>; cookie: string; isLast: boolean }>;
|
||||
// ─── Interaction (moved to apis.interaction) ───
|
||||
// - sendPoke / sendLike / setReaction / setEssence / getEmojiLikes
|
||||
|
||||
// ─── Web-backed ───
|
||||
getGroupHonorInfo(groupId: number, type: WebHonorType | string): Promise<any>;
|
||||
|
||||
@@ -38,15 +38,11 @@ import {
|
||||
// result type still lives here because the OneBot side imports it
|
||||
// through bridge.ts.
|
||||
import type { GroupFilesResult } from './apis/group-file';
|
||||
import {
|
||||
setGroupEssence as setGroupEssence_,
|
||||
} from './actions/group-message';
|
||||
import {
|
||||
getEmojiLikes as getEmojiLikes_,
|
||||
sendLike as sendLike_,
|
||||
sendPoke as sendPoke_,
|
||||
setGroupReaction as setGroupReaction_,
|
||||
} from './actions/interaction';
|
||||
// actions/group-message.ts removed — `setGroupEssence` moved to
|
||||
// apis/interaction.ts::InteractionApi. The recall/markRead helpers
|
||||
// were absorbed into MessageApi back in commit 1.
|
||||
// actions/interaction.ts removed — sendPoke/sendLike/setReaction/
|
||||
// getEmojiLikes moved to apis/interaction.ts::InteractionApi.
|
||||
import {
|
||||
clickInlineKeyboardButton as clickInlineKeyboardButton_,
|
||||
getMiniAppArk as getMiniAppArk_,
|
||||
@@ -330,10 +326,8 @@ export class Bridge implements BridgeInterface {
|
||||
// apis.groupFile (apis/group-file.ts::GroupFileApi).
|
||||
async uploadForwardNodes(nodes: ForwardNodePayload[], groupId?: number, userId?: number): Promise<string> { return uploadForwardNodes_(this, nodes, groupId, userId); }
|
||||
async fetchForwardNodes(resId: string): Promise<ForwardNodePayload[]> { return fetchForwardNodes_(this, resId); }
|
||||
async sendPoke(isGroup: boolean, peerUin: number, targetUin?: number): Promise<void> { return sendPoke_(this, isGroup, peerUin, targetUin); }
|
||||
async sendLike(userId: number, count: number): Promise<void> { return sendLike_(this, userId, count); }
|
||||
async setGroupEssence(groupId: number, sequence: number, random: number, enable: boolean): Promise<void> { return setGroupEssence_(this, groupId, sequence, random, enable); }
|
||||
async setGroupReaction(groupId: number, sequence: number, code: string, isSet: boolean): Promise<void> { return setGroupReaction_(this, groupId, sequence, code, isSet); }
|
||||
// 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> {
|
||||
@@ -409,9 +403,6 @@ export class Bridge implements BridgeInterface {
|
||||
async fetchCustomFace(count?: number): Promise<string[]> {
|
||||
return fetchCustomFace_(this, count);
|
||||
}
|
||||
async getEmojiLikes(groupId: number, sequence: number, emojiId: string, emojiType?: number, count?: number, cookie?: string) {
|
||||
return getEmojiLikes_(this, groupId, sequence, emojiId, emojiType, count, cookie);
|
||||
}
|
||||
|
||||
// --- Tier-2 napcat-parity extras (group todo, stranger status, AI voice) ---
|
||||
|
||||
|
||||
Reference in new issue
Block a user