feat(onebot): report the invited account on group invite events
request.group.invite already identified the inviter. The event now also reports the invited account when QQ provides it. Closes #394
This commit is contained in:
12 files changed
+181
-3
No files matched your search
@@ -16,11 +16,13 @@ export function convertFriendRequest(ctx: ConverterContext, event: FriendRequest
|
||||
}
|
||||
|
||||
export function convertGroupInvite(ctx: ConverterContext, event: GroupInvite): JsonObject {
|
||||
const invitedId = event.invitedUin ?? 0;
|
||||
return request(ctx, event, {
|
||||
request_type: 'group',
|
||||
sub_type: event.subType || 'invite',
|
||||
group_id: event.groupId,
|
||||
user_id: event.fromUin,
|
||||
...(invitedId > 0 ? { invited_id: invitedId } : {}),
|
||||
comment: event.message,
|
||||
flag: event.flag,
|
||||
});
|
||||
|
||||
@@ -180,6 +180,10 @@ export class GroupRequestPoller {
|
||||
groupId: request.groupId,
|
||||
fromUin,
|
||||
fromUid,
|
||||
...(notifyType === 1 ? {
|
||||
invitedUin: request.targetUin,
|
||||
invitedUid: request.targetUid,
|
||||
} : {}),
|
||||
subType,
|
||||
message: request.comment,
|
||||
flag: formatGroupRequestFlag(inviteCardSequence === undefined ? request : {
|
||||
|
||||
@@ -363,6 +363,23 @@ describe('convertEvent — request kinds', () => {
|
||||
} as QQEventVariant);
|
||||
expect(withSubType!.sub_type).toBe('add');
|
||||
});
|
||||
|
||||
it('group_invite: reports the invitee without replacing user_id (#394)', async () => {
|
||||
const out = await convertEvent(bareCtx(), {
|
||||
kind: 'group_invite', time: 1, selfUin: SELF_ID, groupId: GROUP_ID,
|
||||
fromUin: PEER_UIN, invitedUin: 4242, message: '', flag: 'gflag', subType: 'invite',
|
||||
} as QQEventVariant);
|
||||
expect(out!.user_id).toBe(PEER_UIN);
|
||||
expect(out!.invited_id).toBe(4242);
|
||||
});
|
||||
|
||||
it('group_invite: omits invited_id when the invitee is unknown', async () => {
|
||||
const out = await convertEvent(bareCtx(), {
|
||||
kind: 'group_invite', time: 1, selfUin: SELF_ID, groupId: GROUP_ID,
|
||||
fromUin: PEER_UIN, message: '', flag: 'gflag', subType: 'invite',
|
||||
} as QQEventVariant);
|
||||
expect(out).not.toHaveProperty('invited_id');
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertEvent — message elements', () => {
|
||||
|
||||
@@ -63,6 +63,8 @@ describe('GroupRequestPoller', () => {
|
||||
groupId: 999,
|
||||
fromUin: 456,
|
||||
fromUid: 'inviter_uid',
|
||||
invitedUin: 123,
|
||||
invitedUid: 'target_uid',
|
||||
subType: 'invite',
|
||||
message: 'please',
|
||||
flag: 'slreq:1:42:999:2:0',
|
||||
|
||||
@@ -391,6 +391,11 @@ export interface GroupInviteEvent extends QQEvent {
|
||||
groupId: number;
|
||||
fromUin: number;
|
||||
fromUid?: string;
|
||||
/** Person being invited. Present on admin-side invitation notices
|
||||
* and on the pending-request list; for a self-invite this is the
|
||||
* logged-in account. */
|
||||
invitedUin?: number;
|
||||
invitedUid?: string;
|
||||
subType: string;
|
||||
message: string;
|
||||
flag: string;
|
||||
|
||||
@@ -283,8 +283,12 @@ export function formatEvent(identity: IdentityService, event: QQEventVariant): s
|
||||
return `群戳 ${formatGroup(identity, event.groupId)} | ${formatUser(identity, event.groupId, event.userUin)} -> ${formatUser(identity, event.groupId, event.targetUin)}`;
|
||||
case 'friend_request':
|
||||
return `好友请求 ${formatUser(identity, undefined, event.fromUin)}: ${event.message}`;
|
||||
case 'group_invite':
|
||||
return `群邀请 ${formatUser(identity, undefined, event.fromUin)} -> ${formatGroup(identity, event.groupId)}`;
|
||||
case 'group_invite': {
|
||||
const invitee = event.invitedUin && event.invitedUin > 0
|
||||
? ` 邀 ${formatUser(identity, event.groupId, event.invitedUin, event.invitedUid)}`
|
||||
: '';
|
||||
return `群邀请 ${formatUser(identity, undefined, event.fromUin)}${invitee} -> ${formatGroup(identity, event.groupId)}`;
|
||||
}
|
||||
case 'group_essence':
|
||||
return `精华 ${formatGroup(identity, event.groupId)} | ${event.set ? '+' : '-'}精华`;
|
||||
case 'group_msg_emoji_like':
|
||||
|
||||
@@ -36,6 +36,8 @@ export const decodeGroupInvitation: MsgPushDecoder = (ctx) => {
|
||||
groupId: inner.groupUin ?? 0,
|
||||
fromUin: resolveUidToUin(ctx.identity, inner.groupUin ?? 0, inner.invitorUid ?? ''),
|
||||
fromUid: inner.invitorUid ?? '',
|
||||
invitedUin: resolveUidToUin(ctx.identity, inner.groupUin ?? 0, inner.targetUid ?? ''),
|
||||
invitedUid: inner.targetUid ?? '',
|
||||
subType: 'invite',
|
||||
message: '',
|
||||
flag: 'invite:' + (inner.groupUin ?? 0) + ':' + (inner.invitorUid ?? ''),
|
||||
@@ -53,6 +55,7 @@ export const decodeGroupInvite: MsgPushDecoder = (ctx) => {
|
||||
groupId: invite.groupUin ?? 0,
|
||||
fromUin: resolveUidToUin(ctx.identity, invite.groupUin ?? 0, invite.invitorUid ?? ''),
|
||||
fromUid: invite.invitorUid ?? '',
|
||||
invitedUin: ctx.selfUin,
|
||||
subType: 'invite',
|
||||
message: '',
|
||||
flag: 'invite:' + (invite.groupUin ?? 0) + ':' + (invite.invitorUid ?? ''),
|
||||
|
||||
@@ -496,6 +496,12 @@ export class IncomingPacketPipeline {
|
||||
event.message = requestR.value.comment;
|
||||
|
||||
const request = requestR.value;
|
||||
if (subType === 'invite' && request.targetUid) {
|
||||
if (!event.invitedUid) event.invitedUid = request.targetUid;
|
||||
if ((event.invitedUin ?? 0) <= 0 && request.targetUin > 0) {
|
||||
event.invitedUin = request.targetUin;
|
||||
}
|
||||
}
|
||||
const hasApprovalTuple = Number.isSafeInteger(request.sequence) && request.sequence > 0
|
||||
&& Number.isSafeInteger(request.groupId) && request.groupId > 0
|
||||
&& Number.isSafeInteger(request.eventType) && request.eventType > 0;
|
||||
@@ -534,6 +540,17 @@ export class IncomingPacketPipeline {
|
||||
}
|
||||
}
|
||||
|
||||
if ((event.invitedUin ?? 0) <= 0 && event.invitedUid) {
|
||||
try {
|
||||
const invited = await this.deps.identity.resolveUin(event.invitedUid, event.groupId);
|
||||
if (invited !== null && invited > 0) event.invitedUin = invited;
|
||||
} catch (error) {
|
||||
this.log.warn('failed to resolve invited account: groupId=%d uid=%s err=%s',
|
||||
event.groupId, event.invitedUid,
|
||||
error instanceof Error ? error.message : String(error));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return this.dispatchEvent(pkt, event, 'enriched');
|
||||
} catch (error) {
|
||||
@@ -724,6 +741,16 @@ export class IncomingPacketPipeline {
|
||||
uin: uinForCache,
|
||||
source: 'group_request',
|
||||
});
|
||||
if (event.invitedUid) {
|
||||
const invitedUin = event.invitedUin && event.invitedUin !== event.groupId
|
||||
? event.invitedUin : 0;
|
||||
this.deps.identity.rememberRequestIdentity({
|
||||
groupId: event.groupId,
|
||||
uid: event.invitedUid,
|
||||
uin: invitedUin,
|
||||
source: 'group_request',
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -613,6 +613,13 @@ describe('formatEvent', () => {
|
||||
fromUin: 30001,
|
||||
groupId: 778899,
|
||||
} as QQEventVariant)).toBe('群邀请 [惊蛰(30001)] -> [雪夜茶话(778899)]');
|
||||
|
||||
expect(formatEvent(identity, {
|
||||
kind: 'group_invite',
|
||||
fromUin: 30001,
|
||||
invitedUin: 30002,
|
||||
groupId: 778899,
|
||||
} as QQEventVariant)).toBe('群邀请 [惊蛰(30001)] 邀 [白露(30002)] -> [雪夜茶话(778899)]');
|
||||
});
|
||||
|
||||
it('renders group_essence set and unset', () => {
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import type { GroupInvitation, GroupInvite } from '@snowluma/proto-defs/notify';
|
||||
import { protobuf_encode } from '@snowluma/proton';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { IdentityService } from '../../src/identity-service';
|
||||
import type { MsgPushContext } from '../../src/msg-push/context';
|
||||
import {
|
||||
decodeGroupInvitation,
|
||||
decodeGroupInvite,
|
||||
} from '../../src/msg-push/decoders/group-join-request';
|
||||
|
||||
const SELF_UIN = 10001;
|
||||
const GROUP_ID = 12345;
|
||||
|
||||
function ctx(content: Uint8Array, identity = IdentityService.memory(String(SELF_UIN))): MsgPushContext {
|
||||
return {
|
||||
head: {
|
||||
msgType: 33, subType: 0, c2cCmd: 0, sequence: 1, ntMsgSeq: 0,
|
||||
timestamp: 1_710_000_000, msgId: 1,
|
||||
},
|
||||
fromUin: GROUP_ID,
|
||||
fromUid: '',
|
||||
selfUin: SELF_UIN,
|
||||
content,
|
||||
body: undefined,
|
||||
responseHead: undefined,
|
||||
identity,
|
||||
isHistorical: false,
|
||||
};
|
||||
}
|
||||
|
||||
describe('group invitation push carries the invitee (#394)', () => {
|
||||
it('keeps the inviter as from* and the invitee as invited*', () => {
|
||||
const identity = IdentityService.memory(String(SELF_UIN));
|
||||
identity.rememberRequestIdentity({ uid: 'u_inviter', uin: 456, source: 'group_request' });
|
||||
identity.rememberRequestIdentity({ uid: 'u_invitee', uin: 789, source: 'group_request' });
|
||||
const content = protobuf_encode<GroupInvitation>({
|
||||
cmd: 87,
|
||||
info: {
|
||||
inner: {
|
||||
groupUin: GROUP_ID,
|
||||
targetUid: 'u_invitee',
|
||||
invitorUid: 'u_inviter',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const [event] = decodeGroupInvitation(ctx(content, identity));
|
||||
expect(event).toMatchObject({
|
||||
kind: 'group_invite',
|
||||
groupId: GROUP_ID,
|
||||
fromUin: 456,
|
||||
fromUid: 'u_inviter',
|
||||
invitedUin: 789,
|
||||
invitedUid: 'u_invitee',
|
||||
subType: 'invite',
|
||||
});
|
||||
});
|
||||
|
||||
it('treats a self-invite as inviting the logged-in account', () => {
|
||||
const content = protobuf_encode<GroupInvite>({
|
||||
groupUin: GROUP_ID,
|
||||
invitorUid: 'u_inviter',
|
||||
});
|
||||
|
||||
const [event] = decodeGroupInvite(ctx(content));
|
||||
expect(event).toMatchObject({
|
||||
kind: 'group_invite',
|
||||
fromUid: 'u_inviter',
|
||||
invitedUin: SELF_UIN,
|
||||
subType: 'invite',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -259,7 +259,38 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => {
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
expect(resolveGroupInviteCardSequence).toHaveBeenCalledWith(999);
|
||||
expect(captured[0]).toMatchObject({ flag: 'slreq:1:778899:999:2:0' });
|
||||
expect(captured[0]).toMatchObject({
|
||||
flag: 'slreq:1:778899:999:2:0',
|
||||
invitedUin: 10001,
|
||||
invitedUid: 'self',
|
||||
});
|
||||
});
|
||||
|
||||
it('fills the invitee from the pending-request row (#394)', async () => {
|
||||
const resolveGroupJoinRequest = vi.fn(async () => ({
|
||||
groupId: 1, groupName: 'group',
|
||||
targetUid: 'u_invitee', targetUin: 789, targetName: 'invitee',
|
||||
invitorUid: 'u_inviter', invitorUin: 456, invitorName: 'inviter',
|
||||
operatorUid: '', operatorUin: 0, operatorName: '',
|
||||
sequence: 42, state: 1, eventType: 2,
|
||||
comment: '', filtered: false,
|
||||
}));
|
||||
const { pipeline, captured } = makePipeline({ resolveGroupJoinRequest });
|
||||
|
||||
pipeline.registerCmd('test.cmd', () => [{
|
||||
kind: 'group_invite', time: 1, selfUin: 10001,
|
||||
groupId: 1, fromUin: 456, fromUid: 'u_inviter',
|
||||
subType: 'invite', message: '', flag: 'invite:1:u_inviter',
|
||||
} as QQEventVariant]);
|
||||
|
||||
pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo);
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
|
||||
expect(captured[0]).toMatchObject({
|
||||
fromUin: 456,
|
||||
invitedUin: 789,
|
||||
invitedUid: 'u_invitee',
|
||||
});
|
||||
});
|
||||
|
||||
it('runs the profile + request lookups in parallel (independent failures)', async () => {
|
||||
|
||||
@@ -57,6 +57,8 @@ export interface OneBotRequestEvent extends OneBotBaseEvent {
|
||||
sub_type?: string;
|
||||
group_id?: number;
|
||||
user_id?: number;
|
||||
/** Invitee QQ number on `request.group` / `invite`. */
|
||||
invited_id?: number;
|
||||
comment?: string;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user