diff --git a/packages/core/src/bridge/actions/profile.ts b/packages/core/src/bridge/actions/profile.ts index 93cceb06..849e6fb6 100644 --- a/packages/core/src/bridge/actions/profile.ts +++ b/packages/core/src/bridge/actions/profile.ts @@ -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 { + 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( diff --git a/packages/core/src/bridge/bridge-interface.ts b/packages/core/src/bridge/bridge-interface.ts index 666c0dec..a459b8f8 100644 --- a/packages/core/src/bridge/bridge-interface.ts +++ b/packages/core/src/bridge/bridge-interface.ts @@ -125,6 +125,7 @@ export interface BridgeInterface { setSelfLongNick(longNick: string): Promise; setInputStatus(userId: number, eventType: number): Promise; setAvatar(source: string): Promise; + setGroupAvatar(groupId: number, source: string): Promise; fetchCustomFace(count?: number): Promise; getProfileLike(userId?: number, start?: number, limit?: number): Promise; getUnidirectionalFriendList(): Promise; diff --git a/packages/core/src/bridge/bridge.ts b/packages/core/src/bridge/bridge.ts index b407440a..acdbcc1f 100644 --- a/packages/core/src/bridge/bridge.ts +++ b/packages/core/src/bridge/bridge.ts @@ -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 { return setAvatar_(this, source); } + async setGroupAvatar(groupId: number, source: string): Promise { + return setGroupAvatar_(this, groupId, source); + } async fetchCustomFace(count?: number): Promise { return fetchCustomFace_(this, count); } diff --git a/packages/core/src/bridge/proto/oidb-action.ts b/packages/core/src/bridge/proto/oidb-action.ts index b75e77ff..b4b28a1a 100644 --- a/packages/core/src/bridge/proto/oidb-action.ts +++ b/packages/core/src/bridge/proto/oidb-action.ts @@ -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; \ No newline at end of file diff --git a/packages/core/src/onebot/actions/group-admin.ts b/packages/core/src/onebot/actions/group-admin.ts index 5771bfcc..d27332af 100644 --- a/packages/core/src/onebot/actions/group-admin.ts +++ b/packages/core/src/onebot/actions/group-admin.ts @@ -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)); + } }); } diff --git a/packages/core/tests/actions/profile.test.ts b/packages/core/tests/actions/profile.test.ts index 126215c3..52feb3f2 100644 --- a/packages/core/tests/actions/profile.test.ts +++ b/packages/core/tests/actions/profile.test.ts @@ -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 () => { diff --git a/packages/core/tests/onebot/extended-actions.test.ts b/packages/core/tests/onebot/extended-actions.test.ts index a016df31..95ddbb8e 100644 --- a/packages/core/tests/onebot/extended-actions.test.ts +++ b/packages/core/tests/onebot/extended-actions.test.ts @@ -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' }); + }); +});