feat(onebot): implement set_group_portrait via highway cmdId 3000
Port the Lagrange.Core protocol path for group avatar upload —
napcat's NTQQ GroupService.setHeader has no oidb wrapper, but
Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs
spells out the wire shape: same highway HTTP endpoint as personal
avatar, but with cmdId=3000 and a GroupAvatarExtra proto carried as
the `extend` blob (type=101, groupUin, field3={field1=1}, field5=3,
field6=1 — all four are protocol-prescribed constants).
Adds:
- GroupAvatarExtraSchema in proto/oidb-action.ts
- bridge.setGroupAvatar(groupId, source) wrapping the highway upload
- BridgeInterface.setGroupAvatar
- OneBot set_group_portrait action (was a stub returning
'not yet implemented'; accepts the same file-source variants as
set_qq_avatar)
5 new tests: 3 bridge-action (cmdId, extra-blob round-trip, empty-file
guard) + 2 OneBot (forwarding, param validation) — also tightens the
existing setAvatar test to assert the extend blob stays empty.
This commit is contained in:
7 files changed
+134
-3
No files matched your search
@@ -10,6 +10,7 @@ import { computeHashes, loadBinarySource } from '../highway/utils';
|
||||
import {
|
||||
FaceroamOpReqSchema,
|
||||
FaceroamOpRespSchema,
|
||||
GroupAvatarExtraSchema,
|
||||
Oidb0x112aReqSchema,
|
||||
Oidb0x112aRespSchema,
|
||||
Oidb0x7edReqSchema,
|
||||
@@ -175,6 +176,36 @@ export async function setAvatar(
|
||||
await uploadHighwayHttp(bridge, session, 90, loaded.bytes, hashes.md5, new Uint8Array(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set group avatar. Mirrors Lagrange.Core's GroupSetAvatar:
|
||||
* - same highway HTTP upload as personal avatar
|
||||
* - cmdId 3000 (instead of 90)
|
||||
* - GroupAvatarExtra proto carried as the `extend` blob, with the
|
||||
* four protocol-prescribed constants (type=101, field5=3, field6=1,
|
||||
* field3.field1=1) and the target groupUin.
|
||||
*
|
||||
* Source ref: Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs#GroupSetAvatar.
|
||||
*/
|
||||
export async function setGroupAvatar(
|
||||
bridge: Bridge,
|
||||
groupId: number,
|
||||
source: string,
|
||||
): Promise<void> {
|
||||
const loaded = await loadBinarySource(source, 'group-avatar');
|
||||
if (!loaded.bytes.length) throw new Error('group avatar file is empty');
|
||||
|
||||
const hashes = computeHashes(loaded.bytes);
|
||||
const session = await fetchHighwaySession(bridge);
|
||||
const extra = protoEncode({
|
||||
type: 101,
|
||||
groupUin: groupId,
|
||||
field3: { field1: 1 },
|
||||
field5: 3,
|
||||
field6: 1,
|
||||
}, GroupAvatarExtraSchema);
|
||||
await uploadHighwayHttp(bridge, session, 3000, loaded.bytes, hashes.md5, extra);
|
||||
}
|
||||
|
||||
// ─────────────── queries on me / my contacts ───────────────
|
||||
|
||||
export async function getProfileLike(
|
||||
|
||||
@@ -125,6 +125,7 @@ export interface BridgeInterface {
|
||||
setSelfLongNick(longNick: string): Promise<any>;
|
||||
setInputStatus(userId: number, eventType: number): Promise<any>;
|
||||
setAvatar(source: string): Promise<void>;
|
||||
setGroupAvatar(groupId: number, source: string): Promise<void>;
|
||||
fetchCustomFace(count?: number): Promise<string[]>;
|
||||
getProfileLike(userId?: number, start?: number, limit?: number): Promise<any>;
|
||||
getUnidirectionalFriendList(): Promise<any>;
|
||||
|
||||
@@ -90,6 +90,7 @@ import {
|
||||
setSelfLongNick as setSelfLongNick_,
|
||||
setInputStatus as setInputStatus_,
|
||||
setAvatar as setAvatar_,
|
||||
setGroupAvatar as setGroupAvatar_,
|
||||
fetchCustomFace as fetchCustomFace_,
|
||||
getProfileLike as getProfileLike_,
|
||||
getUnidirectionalFriendList as getUnidirectionalFriendList_,
|
||||
@@ -540,6 +541,9 @@ export class Bridge implements BridgeInterface {
|
||||
async setAvatar(source: string): Promise<void> {
|
||||
return setAvatar_(this, source);
|
||||
}
|
||||
async setGroupAvatar(groupId: number, source: string): Promise<void> {
|
||||
return setGroupAvatar_(this, groupId, source);
|
||||
}
|
||||
async fetchCustomFace(count?: number): Promise<string[]> {
|
||||
return fetchCustomFace_(this, count);
|
||||
}
|
||||
|
||||
@@ -1257,4 +1257,20 @@ export const OidbAiVoiceRespSchema = {
|
||||
field2: { field: 2, type: 'uint32' as const },
|
||||
field3: { field: 3, type: 'uint32' as const },
|
||||
msgInfo: { field: 4, type: 'message' as const, schema: OidbAiVoiceMsgInfoSchema },
|
||||
} satisfies ProtoSchema;
|
||||
|
||||
// --- Highway cmdId 3000: GroupAvatarExtra ---
|
||||
// Ported from Lagrange.Core/Internal/Packets/Service/Highway/GroupAvatarExtra.cs.
|
||||
// Field values are protocol-prescribed constants (Lagrange comments on each).
|
||||
|
||||
export const GroupAvatarExtraField3Schema = {
|
||||
field1: { field: 1, type: 'uint32' as const }, // observed value: 1
|
||||
} satisfies ProtoSchema;
|
||||
|
||||
export const GroupAvatarExtraSchema = {
|
||||
type: { field: 1, type: 'uint32' as const }, // observed value: 101
|
||||
groupUin: { field: 2, type: 'uint32' as const },
|
||||
field3: { field: 3, type: 'message' as const, schema: GroupAvatarExtraField3Schema },
|
||||
field5: { field: 5, type: 'uint32' as const }, // observed value: 3
|
||||
field6: { field: 6, type: 'uint32' as const }, // observed value: 1
|
||||
} satisfies ProtoSchema;
|
||||
@@ -103,7 +103,20 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
return okResponse();
|
||||
});
|
||||
|
||||
h.registerAction('set_group_portrait', async () => {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, 'not yet implemented');
|
||||
// Highway upload with cmdId 3000 + GroupAvatarExtra (Lagrange protocol).
|
||||
// `file` accepts everything bridge.setGroupAvatar's underlying
|
||||
// loadBinarySource handles: local paths, file:// URLs, http(s) URLs,
|
||||
// and base64:// data URIs — same surface as set_qq_avatar.
|
||||
h.registerAction('set_group_portrait', async (params) => {
|
||||
const groupId = asNumber(params.group_id);
|
||||
const file = asString(params.file);
|
||||
if (!groupId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id is required');
|
||||
if (!file) return failedResponse(RETCODE.BAD_REQUEST, 'file is required');
|
||||
try {
|
||||
await ctx.bridge.setGroupAvatar(groupId, file);
|
||||
return okResponse();
|
||||
} catch (err) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -117,8 +117,40 @@ describe('actions/profile', () => {
|
||||
await profile.setAvatar(bridge as any, '/some/avatar.png');
|
||||
expect(highwayClient.fetchHighwaySession).toHaveBeenCalledOnce();
|
||||
expect(highwayClient.uploadHighwayHttp).toHaveBeenCalledOnce();
|
||||
const [, , cmdId] = vi.mocked(highwayClient.uploadHighwayHttp).mock.calls[0]!;
|
||||
const [, , cmdId, , , extend] = vi.mocked(highwayClient.uploadHighwayHttp).mock.calls[0]!;
|
||||
expect(cmdId).toBe(90);
|
||||
expect((extend as Uint8Array).length).toBe(0); // personal avatar has no extra payload
|
||||
});
|
||||
|
||||
it('setGroupAvatar uses cmdId 3000 and packs the Lagrange GroupAvatarExtra constants', async () => {
|
||||
const bridge = mockBridge();
|
||||
await profile.setGroupAvatar(bridge as any, 12345, '/some/group-avatar.png');
|
||||
expect(highwayClient.fetchHighwaySession).toHaveBeenCalledOnce();
|
||||
expect(highwayClient.uploadHighwayHttp).toHaveBeenCalledOnce();
|
||||
const [, , cmdId, , , extend] = vi.mocked(highwayClient.uploadHighwayHttp).mock.calls[0]!;
|
||||
expect(cmdId).toBe(3000);
|
||||
// Decode the extra blob back through the schema and assert every
|
||||
// protocol-prescribed constant lands where it should.
|
||||
const { protoDecode } = await import('../../src/protobuf/decode');
|
||||
const { GroupAvatarExtraSchema } = await import('../../src/bridge/proto/oidb-action');
|
||||
const decoded = protoDecode(extend as Uint8Array, GroupAvatarExtraSchema);
|
||||
expect(decoded).toEqual({
|
||||
type: 101,
|
||||
groupUin: 12345,
|
||||
field3: { field1: 1 },
|
||||
field5: 3,
|
||||
field6: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it('setGroupAvatar rejects an empty file before hitting highway', async () => {
|
||||
const bridge = mockBridge();
|
||||
const { loadBinarySource } = await import('../../src/bridge/highway/utils');
|
||||
vi.mocked(loadBinarySource).mockResolvedValueOnce({
|
||||
bytes: new Uint8Array(0), fileName: 'empty.png',
|
||||
} as any);
|
||||
await expect(profile.setGroupAvatar(bridge as any, 1, 'empty.png')).rejects.toThrow(/empty/);
|
||||
expect(highwayClient.fetchHighwaySession).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('getProfileLike (self): resolves self UID, returns formatted favorite + vote info', async () => {
|
||||
|
||||
@@ -497,3 +497,37 @@ describe('extended-actions / set_diy_online_status', () => {
|
||||
expect(res).toMatchObject({ status: 'failed', retcode: 100, wording: 'denied' });
|
||||
});
|
||||
});
|
||||
|
||||
// ─── set_group_portrait (Lagrange-protocol highway upload, cmdId 3000) ───
|
||||
|
||||
describe('extended-actions / set_group_portrait', () => {
|
||||
it('forwards group_id + file to bridge.setGroupAvatar', async () => {
|
||||
const setGroupAvatar = vi.fn(async () => {});
|
||||
const bridge = fakeBridge({ setGroupAvatar: setGroupAvatar as any });
|
||||
const res = await makeHandler(fakeCtx(bridge)).handle('set_group_portrait', {
|
||||
group_id: 12345, file: '/tmp/avatar.png',
|
||||
});
|
||||
expect(res.status).toBe('ok');
|
||||
expect(setGroupAvatar).toHaveBeenCalledWith(12345, '/tmp/avatar.png');
|
||||
});
|
||||
|
||||
it('rejects missing group_id or file', async () => {
|
||||
const setGroupAvatar = vi.fn();
|
||||
const bridge = fakeBridge({ setGroupAvatar: setGroupAvatar as any });
|
||||
const r1 = await makeHandler(fakeCtx(bridge)).handle('set_group_portrait', { file: 'x' });
|
||||
const r2 = await makeHandler(fakeCtx(bridge)).handle('set_group_portrait', { group_id: 1 });
|
||||
expect(r1).toMatchObject({ status: 'failed', retcode: 1400 });
|
||||
expect(r2).toMatchObject({ status: 'failed', retcode: 1400 });
|
||||
expect(setGroupAvatar).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('surfaces highway / decode errors as action_failed', async () => {
|
||||
const bridge = fakeBridge({
|
||||
setGroupAvatar: (async () => { throw new Error('highway 500'); }) as any,
|
||||
});
|
||||
const res = await makeHandler(fakeCtx(bridge)).handle('set_group_portrait', {
|
||||
group_id: 1, file: 'x.png',
|
||||
});
|
||||
expect(res).toMatchObject({ status: 'failed', retcode: 100, wording: 'highway 500' });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user