feat(onebot): fetch custom face details
Add packet-backed custom face detail retrieval and expose the result through the OneBot action. Fixes #333
This commit is contained in:
8 files changed
+363
-7
No files matched your search
@@ -7,6 +7,7 @@ import type {
|
||||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||||
import { AddCustomFace } from '@snowluma/protocol/oidb-services/custom-face/add-custom-face';
|
||||
import { DeleteCustomFace } from '@snowluma/protocol/oidb-services/custom-face/delete-custom-face';
|
||||
import { FetchCustomFaceDetail } from '@snowluma/protocol/oidb-services/custom-face/fetch-custom-face-detail';
|
||||
import { ModifyCustomFace } from '@snowluma/protocol/oidb-services/custom-face/modify-custom-face';
|
||||
import { MoveCustomFace } from '@snowluma/protocol/oidb-services/custom-face/move-custom-face';
|
||||
import { OrderCustomFace } from '@snowluma/protocol/oidb-services/custom-face/order-custom-face';
|
||||
@@ -23,6 +24,13 @@ import type { BridgeContext } from '../bridge-context';
|
||||
|
||||
function asBridge(ctx: BridgeContext): Bridge { return ctx as unknown as Bridge; }
|
||||
|
||||
export interface CustomFaceDetail {
|
||||
emojiId: string;
|
||||
url: string;
|
||||
md5: string;
|
||||
desc: string;
|
||||
}
|
||||
|
||||
export class ProfileApi {
|
||||
constructor(private readonly ctx: BridgeContext) { }
|
||||
|
||||
@@ -134,6 +142,36 @@ export class ProfileApi {
|
||||
}
|
||||
|
||||
async fetchCustomFace(count = 10): Promise<string[]> {
|
||||
const faceIds = await this.fetchCustomFaceIds(count);
|
||||
return faceIds.map((id) => this.customFaceUrl(id));
|
||||
}
|
||||
|
||||
async fetchCustomFaceDetails(count = 10): Promise<CustomFaceDetail[]> {
|
||||
const faceIds = await this.fetchCustomFaceIds(count);
|
||||
if (faceIds.length === 0) return [];
|
||||
|
||||
const emojis = faceIds.map((emojiId) => ({
|
||||
emojiId,
|
||||
md5: md5FromEmojiId(emojiId),
|
||||
}));
|
||||
const details = await FetchCustomFaceDetail.invoke(this.ctx, { emojis });
|
||||
const descriptions = new Map(details.map((detail) => [detail.emojiId, detail.desc]));
|
||||
|
||||
return emojis.map(({ emojiId, md5 }) => ({
|
||||
emojiId,
|
||||
url: this.customFaceUrl(emojiId),
|
||||
md5,
|
||||
// The detail response may omit entries which have never had a remark.
|
||||
desc: descriptions.get(emojiId) ?? '',
|
||||
}));
|
||||
}
|
||||
|
||||
private async fetchCustomFaceIds(count: number): Promise<string[]> {
|
||||
if (!Number.isInteger(count) || count < 0) {
|
||||
throw new Error('custom face count must be a non-negative integer');
|
||||
}
|
||||
if (count === 0) return [];
|
||||
|
||||
const req = {
|
||||
inner: { field1: 1, osVersion: '10.0.26200', qqVersion: '9.9.28-46928' },
|
||||
uin: BigInt(this.ctx.identity.uin),
|
||||
@@ -146,11 +184,15 @@ export class ProfileApi {
|
||||
throw new Error(result.errorMessage || 'fetch custom face failed');
|
||||
}
|
||||
const resp = protobuf_decode<FaceroamOpResp>(result.responseData);
|
||||
if (!resp || resp.retCode !== 0) {
|
||||
if (!resp || (resp.retCode ?? 0) !== 0) {
|
||||
throw new Error(`fetch custom face error: ${resp?.message || 'unknown'}`);
|
||||
}
|
||||
const faceIds = resp.item?.faceIds || [];
|
||||
return faceIds.slice(0, count).map((id: string) => `https://p.qpic.cn/qq_expression/${this.ctx.identity.uin}/${id}/0`);
|
||||
return faceIds.slice(0, count);
|
||||
}
|
||||
|
||||
private customFaceUrl(emojiId: string): string {
|
||||
return `https://p.qpic.cn/qq_expression/${this.ctx.identity.uin}/${emojiId}/0`;
|
||||
}
|
||||
|
||||
/** 删除一个收藏表情(custom face)。emoji_id 来自 fetchCustomFace 返回的 URL 路径段。 */
|
||||
|
||||
@@ -2,6 +2,9 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
CustomFaceModifyResp,
|
||||
CustomFaceMoveBody,
|
||||
FaceroamOpResp,
|
||||
GroupAvatarExtra,
|
||||
Oidb0x7edResp,
|
||||
Oidb0xe17Resp,
|
||||
@@ -208,4 +211,67 @@ describe('apis/profile', () => {
|
||||
const out = await new ProfileApi(bridge as any).getUnidirectionalFriendList();
|
||||
expect(out).toEqual([{ uin: 10001 }, { uin: 10002 }]);
|
||||
});
|
||||
|
||||
it('fetchCustomFaceDetails supplements the Faceroam ids with server descriptions (#333)', async () => {
|
||||
const bridge = mockBridge();
|
||||
const emojiA = '10001_0_0_0_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_0_0';
|
||||
const emojiB = '10001_0_0_0_BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB_0_0';
|
||||
bridge.sendRawPacket
|
||||
.mockResolvedValueOnce({
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<FaceroamOpResp>({
|
||||
retCode: 0,
|
||||
item: { faceIds: [emojiA, emojiB] },
|
||||
})),
|
||||
} as any)
|
||||
.mockResolvedValueOnce({
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<OidbBase<CustomFaceModifyResp>>({
|
||||
body: {
|
||||
retCode: 0,
|
||||
entries: [{ emoji: { emojiId: emojiB }, desc: '第二个表情' }],
|
||||
},
|
||||
})),
|
||||
} as any);
|
||||
|
||||
const out = await new ProfileApi(bridge as any).fetchCustomFaceDetails(2);
|
||||
|
||||
expect(bridge.sendRawPacket.mock.calls.map((call) => call[0])).toEqual([
|
||||
'Faceroam.OpReq',
|
||||
'OidbSvcTrpcTcp.0x902e_1',
|
||||
]);
|
||||
const detailRequest = protobuf_decode<OidbBase<CustomFaceMoveBody>>(
|
||||
bridge.sendRawPacket.mock.calls[1]![1],
|
||||
);
|
||||
expect(detailRequest.body?.emojis).toEqual([
|
||||
{ emojiId: emojiA, md5: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' },
|
||||
{ emojiId: emojiB, md5: 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB' },
|
||||
]);
|
||||
expect(out).toEqual([
|
||||
{
|
||||
emojiId: emojiA,
|
||||
url: `https://p.qpic.cn/qq_expression/10001/${emojiA}/0`,
|
||||
md5: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
|
||||
desc: '',
|
||||
},
|
||||
{
|
||||
emojiId: emojiB,
|
||||
url: `https://p.qpic.cn/qq_expression/10001/${emojiB}/0`,
|
||||
md5: 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB',
|
||||
desc: '第二个表情',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('fetchCustomFaceDetails returns immediately for count=0', async () => {
|
||||
const bridge = mockBridge();
|
||||
await expect(new ProfileApi(bridge as any).fetchCustomFaceDetails(0)).resolves.toEqual([]);
|
||||
expect(bridge.sendRawPacket).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1219,6 +1219,40 @@ export const actions = [
|
||||
},
|
||||
}),
|
||||
|
||||
defineAction({
|
||||
name: 'fetch_custom_face_detail',
|
||||
summary: '获取自定义表情详情',
|
||||
readOnly: true,
|
||||
returns: '自定义表情详情数组,包含资源标识、图片地址、摘要与描述。',
|
||||
returnsSchema: {
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
emoji_id: { type: 'string', description: '可传给 SnowLuma 表情管理接口的资源标识' },
|
||||
resId: { type: 'string', description: '兼容 NapCat 的资源标识' },
|
||||
url: { type: 'string', description: '表情图片地址' },
|
||||
md5: { type: 'string', description: '表情内容摘要' },
|
||||
desc: { type: 'string', description: '自定义表情描述;未设置时为空字符串' },
|
||||
},
|
||||
required: ['emoji_id', 'resId', 'url', 'md5', 'desc'],
|
||||
},
|
||||
},
|
||||
params: {
|
||||
count: f.int({ min: 0 }).default(48),
|
||||
},
|
||||
run: async (p, ctx) => {
|
||||
const details = await ctx.bridge.apis.profile.fetchCustomFaceDetails(p.count);
|
||||
return okResponse(details.map((detail) => ({
|
||||
emoji_id: detail.emojiId,
|
||||
resId: detail.emojiId,
|
||||
url: detail.url,
|
||||
md5: detail.md5,
|
||||
desc: detail.desc,
|
||||
})));
|
||||
},
|
||||
}),
|
||||
|
||||
defineAction({
|
||||
name: 'get_emoji_likes',
|
||||
summary: '获取表情回应用户',
|
||||
|
||||
@@ -90,9 +90,9 @@ describe('compileActionRegistry namespace conflicts', () => {
|
||||
});
|
||||
|
||||
describe('compiled production Action registry', () => {
|
||||
it('preserves all 185 canonical Action docs', () => {
|
||||
expect(ACTION_REGISTRY.actions).toHaveLength(185);
|
||||
expect(collectActionDocs()).toHaveLength(185);
|
||||
it('preserves all 186 canonical Action docs', () => {
|
||||
expect(ACTION_REGISTRY.actions).toHaveLength(186);
|
||||
expect(collectActionDocs()).toHaveLength(186);
|
||||
});
|
||||
|
||||
it('resolves every executable name to exactly one canonical doc', () => {
|
||||
|
||||
@@ -61,6 +61,7 @@ const APIS_ROUTING: Record<string, [string, string]> = {
|
||||
setAvatar: ['profile', 'setAvatar'],
|
||||
setGroupAvatar: ['profile', 'setGroupAvatar'],
|
||||
fetchCustomFace: ['profile', 'fetchCustomFace'],
|
||||
fetchCustomFaceDetails: ['profile', 'fetchCustomFaceDetails'],
|
||||
getProfileLike: ['profile', 'getLike'],
|
||||
getUnidirectionalFriendList: ['profile', 'getUnidirectionalFriendList'],
|
||||
// FriendApi: handleRequest/delete/setRemark.
|
||||
@@ -1407,6 +1408,51 @@ describe('extended-actions / set_group_portrait', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('extended-actions / fetch_custom_face_detail', () => {
|
||||
it('returns real packet-backed fields with SnowLuma and NapCat resource aliases', async () => {
|
||||
const fetchCustomFaceDetails = vi.fn(async () => [{
|
||||
emojiId: '10001_0_0_0_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_0_0',
|
||||
url: 'https://p.qpic.cn/qq_expression/10001/id/0',
|
||||
md5: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
|
||||
desc: '开心',
|
||||
}]);
|
||||
const bridge = fakeBridge({ fetchCustomFaceDetails });
|
||||
|
||||
const response = await makeHandler(fakeCtx(bridge)).handle('fetch_custom_face_detail', {
|
||||
count: '1',
|
||||
});
|
||||
|
||||
expect(fetchCustomFaceDetails).toHaveBeenCalledWith(1);
|
||||
expect(response).toMatchObject({
|
||||
status: 'ok',
|
||||
data: [{
|
||||
emoji_id: '10001_0_0_0_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_0_0',
|
||||
resId: '10001_0_0_0_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_0_0',
|
||||
url: 'https://p.qpic.cn/qq_expression/10001/id/0',
|
||||
md5: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
|
||||
desc: '开心',
|
||||
}],
|
||||
});
|
||||
});
|
||||
|
||||
it('defaults to the compatibility page size of 48', async () => {
|
||||
const fetchCustomFaceDetails = vi.fn(async () => []);
|
||||
const bridge = fakeBridge({ fetchCustomFaceDetails });
|
||||
await makeHandler(fakeCtx(bridge)).handle('fetch_custom_face_detail', {});
|
||||
expect(fetchCustomFaceDetails).toHaveBeenCalledWith(48);
|
||||
});
|
||||
|
||||
it('rejects a negative count before calling the bridge', async () => {
|
||||
const fetchCustomFaceDetails = vi.fn();
|
||||
const bridge = fakeBridge({ fetchCustomFaceDetails });
|
||||
const response = await makeHandler(fakeCtx(bridge)).handle('fetch_custom_face_detail', {
|
||||
count: -1,
|
||||
});
|
||||
expect(response).toMatchObject({ status: 'failed', retcode: 1400 });
|
||||
expect(fetchCustomFaceDetails).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Wave 1: get_group_shut_list ───
|
||||
|
||||
describe('extended-actions / get_group_shut_list', () => {
|
||||
|
||||
@@ -666,8 +666,10 @@ export interface CustomFaceModifyBody {
|
||||
// 0x902e modify 响应:f1=retcode, f2=errmsg, f3=opType, f4 repeated 受影响条目
|
||||
// {f1:{emojiId,md5}, f3:desc}(含改后 desc)。
|
||||
export interface CustomFaceModifyRespEntry {
|
||||
emoji?: pb<1, CustomFaceOpEmojiEntry>;
|
||||
desc?: pb<3, string>;
|
||||
emoji?: pb<1, CustomFaceOpEmojiEntry>;
|
||||
/** Older QQ builds return the description in tag 2. */
|
||||
legacyDesc?: pb<2, string>;
|
||||
desc?: pb<3, string>;
|
||||
}
|
||||
export interface CustomFaceModifyResp {
|
||||
retCode?: pb<1, uint_32>;
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
// 0x902e_1 opType=2 — supplement the Faceroam custom-face id list with the
|
||||
// server-side descriptions. QQ first fetches ids through Faceroam.OpReq, then
|
||||
// sends every {emojiId, md5} here. The response returns repeated entries in f4.
|
||||
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type {
|
||||
CustomFaceModifyResp,
|
||||
CustomFaceMoveBody,
|
||||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import { invokeOidb, type OidbSender } from '../../oidb-service';
|
||||
import { CLIENT_VERSION } from './shared';
|
||||
|
||||
export namespace FetchCustomFaceDetail {
|
||||
export const command = 0x902e;
|
||||
export const subCommand = 1;
|
||||
export const uinForm = true;
|
||||
|
||||
export interface EmojiItem {
|
||||
emojiId: string;
|
||||
md5: string;
|
||||
}
|
||||
|
||||
export interface Params {
|
||||
emojis: EmojiItem[];
|
||||
}
|
||||
|
||||
export interface Detail {
|
||||
emojiId: string;
|
||||
desc: string;
|
||||
}
|
||||
|
||||
export type Deps = OidbSender;
|
||||
|
||||
export const serialize = (_ctx: Deps, p: Params): CustomFaceMoveBody => ({
|
||||
field1: 1,
|
||||
osVersion: CLIENT_VERSION,
|
||||
opType: 2,
|
||||
emojis: p.emojis.map((emoji) => ({
|
||||
emojiId: emoji.emojiId,
|
||||
md5: emoji.md5,
|
||||
})),
|
||||
});
|
||||
|
||||
export const deserialize = (_ctx: Deps, body: CustomFaceModifyResp): Detail[] => {
|
||||
if (body.retCode && body.retCode !== 0) {
|
||||
throw new Error(`fetch custom face detail error: ${body.errMsg || body.retCode}`);
|
||||
}
|
||||
|
||||
const seen = new Set<string>();
|
||||
return (body.entries ?? []).map((entry, index) => {
|
||||
const emojiId = entry.emoji?.emojiId;
|
||||
if (!emojiId) {
|
||||
throw new Error(`fetch custom face detail response entry ${index} has no emoji id`);
|
||||
}
|
||||
if (seen.has(emojiId)) {
|
||||
throw new Error(`fetch custom face detail response has duplicate emoji id: ${emojiId}`);
|
||||
}
|
||||
seen.add(emojiId);
|
||||
return {
|
||||
emojiId,
|
||||
// Current builds use tag 3. Older builds use tag 2 when tag 3 is empty.
|
||||
desc: entry.desc || entry.legacyDesc || '',
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export const encode = (env: OidbBase<CustomFaceMoveBody>): Uint8Array =>
|
||||
protobuf_encode<OidbBase<CustomFaceMoveBody>>(env);
|
||||
|
||||
export const decode = (bytes: Uint8Array): OidbBase<CustomFaceModifyResp> =>
|
||||
protobuf_decode<OidbBase<CustomFaceModifyResp>>(bytes);
|
||||
|
||||
export const invoke = (deps: Deps, params: Params): Promise<Detail[]> =>
|
||||
invokeOidb(deps, FetchCustomFaceDetail, params);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
CustomFaceModifyResp,
|
||||
CustomFaceMoveBody,
|
||||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||||
|
||||
import { FetchCustomFaceDetail } from '../../../src/oidb-services/custom-face/fetch-custom-face-detail';
|
||||
|
||||
const EMOJI_A_ID = '2550419068_0_0_0_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_0_0';
|
||||
const EMOJI_A_MD5 = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
|
||||
const EMOJI_B_ID = '2550419068_0_0_0_BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB_0_0';
|
||||
const EMOJI_B_MD5 = 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB';
|
||||
|
||||
function makeSender(body: CustomFaceModifyResp) {
|
||||
const r: SendPacketResult = {
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<OidbBase<CustomFaceModifyResp>>({ body })),
|
||||
};
|
||||
return { sendRawPacket: vi.fn(async () => r) };
|
||||
}
|
||||
|
||||
describe('FetchCustomFaceDetail namespace', () => {
|
||||
it('sends the IDA-confirmed 0x902e_1 opType=2 request with every id and md5', async () => {
|
||||
const sender = makeSender({ retCode: 0, entries: [] });
|
||||
await FetchCustomFaceDetail.invoke(sender, {
|
||||
emojis: [
|
||||
{ emojiId: EMOJI_A_ID, md5: EMOJI_A_MD5 },
|
||||
{ emojiId: EMOJI_B_ID, md5: EMOJI_B_MD5 },
|
||||
],
|
||||
});
|
||||
|
||||
const [cmd, bytes] = sender.sendRawPacket.mock.calls[0]!;
|
||||
expect(cmd).toBe('OidbSvcTrpcTcp.0x902e_1');
|
||||
const env = protobuf_decode<OidbBase<CustomFaceMoveBody>>(bytes);
|
||||
expect(env.command).toBe(0x902e);
|
||||
expect(env.subCommand).toBe(1);
|
||||
expect(env.reserved).toBe(1);
|
||||
expect(env.body).toMatchObject({
|
||||
field1: 1,
|
||||
opType: 2,
|
||||
emojis: [
|
||||
{ emojiId: EMOJI_A_ID, md5: EMOJI_A_MD5 },
|
||||
{ emojiId: EMOJI_B_ID, md5: EMOJI_B_MD5 },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('returns tag 3 descriptions and falls back to legacy tag 2', async () => {
|
||||
const sender = makeSender({
|
||||
retCode: 0,
|
||||
entries: [
|
||||
{ emoji: { emojiId: EMOJI_A_ID }, legacyDesc: '旧版描述' },
|
||||
{ emoji: { emojiId: EMOJI_B_ID }, legacyDesc: '旧值', desc: '新版描述' },
|
||||
],
|
||||
});
|
||||
|
||||
await expect(FetchCustomFaceDetail.invoke(sender, {
|
||||
emojis: [{ emojiId: EMOJI_A_ID, md5: EMOJI_A_MD5 }],
|
||||
})).resolves.toEqual([
|
||||
{ emojiId: EMOJI_A_ID, desc: '旧版描述' },
|
||||
{ emojiId: EMOJI_B_ID, desc: '新版描述' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('does not silently accept malformed or duplicate response entries', () => {
|
||||
expect(() => FetchCustomFaceDetail.deserialize({} as any, {
|
||||
entries: [{ desc: 'missing id' }],
|
||||
})).toThrow(/has no emoji id/);
|
||||
|
||||
expect(() => FetchCustomFaceDetail.deserialize({} as any, {
|
||||
entries: [
|
||||
{ emoji: { emojiId: EMOJI_A_ID } },
|
||||
{ emoji: { emojiId: EMOJI_A_ID } },
|
||||
],
|
||||
})).toThrow(/duplicate emoji id/);
|
||||
});
|
||||
|
||||
it('surfaces a non-zero business result', () => {
|
||||
expect(() => FetchCustomFaceDetail.deserialize({} as any, {
|
||||
retCode: 5,
|
||||
errMsg: 'denied',
|
||||
})).toThrow(/denied/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user