feat(protocol): write join question and answer with add option

Question-and-answer join mode now sends the question and answer;
question-only mode sends the question and clears the answer.
This commit is contained in:
DrSmoothl committed 2026-08-17 19:12:43 +08:00
1 parent b1fa11ca05
commit 3cb3024b39
3 files changed
+85 -10

No files matched your search

@@ -25,6 +25,10 @@ export interface OidbMuteAll {
}
export interface Oidb0x89a_0AddOptionSettings {
addType?: pb<16, uint_32>;
// EncodeModifyGroupDetailInfoParam: question=30/60224, answer=31/60225.
// Empty string is a real write (clears the other mode), so these are optional.
groupQuestion?: pb_optional<30, string>;
groupAnswer?: pb_optional<31, string>;
}
export interface Oidb0x89a_0AddOption {
groupUin?: pb<1, uint_64>;
@@ -4,22 +4,44 @@
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase, OidbEmpty } from '@snowluma/proto-defs/oidb';
import type { Oidb0x89a_0AddOption } from '@snowluma/proto-defs/oidb-actions/base';
import type {
Oidb0x89a_0AddOption,
Oidb0x89a_0AddOptionSettings,
} from '@snowluma/proto-defs/oidb-actions/base';
import { invokeOidb, type OidbSender } from '../../oidb-service';
/** Official join modes that carry a question. 55 is accepted as an alias of 5. */
export function isQuestionAddType(addType: number): boolean {
return addType === 4 || addType === 5 || addType === 55;
}
export function wireAddType(addType: number): number {
return addType === 55 ? 5 : addType;
}
export namespace SetAddOption {
export const command = 0x89A;
export const subCommand = 0;
export interface Params { groupId: number; addType: number; }
export interface Params {
groupId: number;
addType: number;
groupQuestion?: string;
groupAnswer?: string;
}
export type Deps = OidbSender;
export const serialize = (_ctx: Deps, p: Params): Oidb0x89a_0AddOption => ({
groupUin: BigInt(p.groupId),
settings: { addType: p.addType },
field12: 0,
});
export const serialize = (_ctx: Deps, p: Params): Oidb0x89a_0AddOption => {
const addType = wireAddType(p.addType);
const settings: Oidb0x89a_0AddOptionSettings = { addType };
if (isQuestionAddType(p.addType)) {
settings.groupQuestion = p.groupQuestion ?? '';
// Type 4 keeps the answer; type 5/55 is question-only and clears it.
settings.groupAnswer = addType === 4 ? (p.groupAnswer ?? '') : '';
}
return { groupUin: BigInt(p.groupId), settings, field12: 0 };
};
export const deserialize = (_ctx: Deps, _: OidbEmpty): void => {};
@@ -4,7 +4,12 @@ import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { Oidb0x89a_0AddOption } from '@snowluma/proto-defs/oidb-actions/base';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { SetAddOption } from '../../../src/oidb-services/group-admin/set-add-option';
import {
isQuestionAddType,
SetAddOption,
wireAddType,
} from '../../../src/oidb-services/group-admin/set-add-option';
import { env, v, s, m } from '../_pb-oracle';
function makeDeps() {
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.alloc(0) };
@@ -22,7 +27,51 @@ describe('SetAddOption namespace', () => {
await SetAddOption.invoke(deps, { groupId: 12345, addType: 2 });
const [wire, bytes] = deps.sendRawPacket.mock.calls[0]!;
expect(wire).toBe('OidbSvcTrpcTcp.0x89a_0');
const env = protobuf_decode<OidbBase<Oidb0x89a_0AddOption>>(bytes);
expect(env.body).toMatchObject({ groupUin: 12345n, settings: { addType: 2 } });
const decoded = protobuf_decode<OidbBase<Oidb0x89a_0AddOption>>(bytes);
expect(decoded.body).toMatchObject({ groupUin: 12345n, settings: { addType: 2 } });
expect(decoded.body?.settings?.groupQuestion ?? undefined).toBeUndefined();
expect(decoded.body?.settings?.groupAnswer ?? undefined).toBeUndefined();
});
it('treats 4 as question+answer and 5/55 as question-only', () => {
expect(isQuestionAddType(4)).toBe(true);
expect(isQuestionAddType(5)).toBe(true);
expect(isQuestionAddType(55)).toBe(true);
expect(isQuestionAddType(2)).toBe(false);
expect(wireAddType(55)).toBe(5);
expect(wireAddType(4)).toBe(4);
});
it('byte-oracle: type 4 writes question and answer', async () => {
const deps = makeDeps();
await SetAddOption.invoke(deps, {
groupId: 12345, addType: 4, groupQuestion: 'q', groupAnswer: 'a',
});
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const settings = [...v(16, 4), ...s(30, 'q'), ...s(31, 'a')];
const body = [...v(1, 12345), ...m(2, settings)];
expect(Buffer.from(bytes).toString('hex')).toBe(env(0x89A, 0, body, false));
});
it('byte-oracle: type 5 writes question and clears answer', async () => {
const deps = makeDeps();
await SetAddOption.invoke(deps, {
groupId: 12345, addType: 5, groupQuestion: 'why', groupAnswer: 'ignored',
});
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const settings = [...v(16, 5), ...s(30, 'why'), ...s(31, '')];
const body = [...v(1, 12345), ...m(2, settings)];
expect(Buffer.from(bytes).toString('hex')).toBe(env(0x89A, 0, body, false));
});
it('maps add_type 55 onto the official question-only mode', async () => {
const deps = makeDeps();
await SetAddOption.invoke(deps, { groupId: 1, addType: 55, groupQuestion: 'q' });
const decoded = protobuf_decode<OidbBase<Oidb0x89a_0AddOption>>(
deps.sendRawPacket.mock.calls[0]![1],
);
expect(decoded.body?.settings).toMatchObject({
addType: 5, groupQuestion: 'q', groupAnswer: '',
});
});
});