fix(protocol): report incoming group invitations (#612)
Surface group invitations when the inviter is outside the friend list so clients can respond to them.
This commit is contained in:
4 files changed
+283
-7
No files matched your search
@@ -21,7 +21,9 @@ export const decodeGroupJoinRequest: MsgPushDecoder = (ctx) => {
|
|||||||
fromUid: join.targetUid ?? '',
|
fromUid: join.targetUid ?? '',
|
||||||
subType: 'add',
|
subType: 'add',
|
||||||
message: '',
|
message: '',
|
||||||
flag: '',
|
// Legacy flag: the approval path can resolve it from the request inbox
|
||||||
|
// even when the pipeline cannot find a pending request to enrich from.
|
||||||
|
flag: `add:${join.groupUin ?? 0}:${join.targetUid ?? ''}`,
|
||||||
};
|
};
|
||||||
return [ev];
|
return [ev];
|
||||||
};
|
};
|
||||||
@@ -45,7 +47,11 @@ export const decodeGroupInvitation: MsgPushDecoder = (ctx) => {
|
|||||||
invitedUid: inner.targetUid ?? '',
|
invitedUid: inner.targetUid ?? '',
|
||||||
subType: 'invite',
|
subType: 'invite',
|
||||||
message: '',
|
message: '',
|
||||||
flag: '',
|
// Self-invite notices never carry the approval msgseq, and they are
|
||||||
|
// absent from the UIN-based request inbox -- but the UID-based one used
|
||||||
|
// by `set_group_add_request` legacy parsing does return them. Ship the
|
||||||
|
// legacy flag so the event stays actionable (#125).
|
||||||
|
flag: `invite:${inner.groupUin ?? 0}:${inner.invitorUid ?? ''}`,
|
||||||
};
|
};
|
||||||
return [ev];
|
return [ev];
|
||||||
};
|
};
|
||||||
@@ -67,7 +73,8 @@ export const decodeGroupInvite: MsgPushDecoder = (ctx) => {
|
|||||||
invitedUin: ctx.selfUin,
|
invitedUin: ctx.selfUin,
|
||||||
subType: 'invite',
|
subType: 'invite',
|
||||||
message: '',
|
message: '',
|
||||||
flag: '',
|
// See decodeGroupInvitation: keep the event actionable without the card.
|
||||||
|
flag: `invite:${invite.groupUin ?? 0}:${invite.invitorUid ?? ''}`,
|
||||||
};
|
};
|
||||||
return [ev];
|
return [ev];
|
||||||
};
|
};
|
||||||
@@ -6,6 +6,14 @@ import type { QQEventVariant } from './events';
|
|||||||
import type { IdentityService } from './identity-service';
|
import type { IdentityService } from './identity-service';
|
||||||
import { formatGroupRequestFlag, type GroupRequestInfo } from './qq-info';
|
import { formatGroupRequestFlag, type GroupRequestInfo } from './qq-info';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* How long a dispatched invitation stays remembered so the MSF notice and the
|
||||||
|
* private `qun.invite` card of the SAME invitation do not both dispatch.
|
||||||
|
* Both packets come from one server-side action, so the window only bounds how
|
||||||
|
* long the marker lives -- matching itself is exact (group + inviter UID).
|
||||||
|
*/
|
||||||
|
const INVITE_PAIR_WINDOW_MS = 3_000;
|
||||||
|
|
||||||
const moduleLog = createLogger('Bridge');
|
const moduleLog = createLogger('Bridge');
|
||||||
const moduleEventLog = createLogger('Event');
|
const moduleEventLog = createLogger('Event');
|
||||||
const modulePacketLog = createLogger('Protocol.Packet');
|
const modulePacketLog = createLogger('Protocol.Packet');
|
||||||
@@ -62,6 +70,15 @@ export class IncomingPacketPipeline {
|
|||||||
private readonly log: Logger;
|
private readonly log: Logger;
|
||||||
private readonly eventLog: Logger;
|
private readonly eventLog: Logger;
|
||||||
private readonly packetLog: Logger;
|
private readonly packetLog: Logger;
|
||||||
|
/**
|
||||||
|
* One invitation can arrive as an MSF notice AND as a private card. The card
|
||||||
|
* is the richer source (it carries the approval msgseq), so either one may
|
||||||
|
* dispatch -- but never both. Two independent markers keep that mutual: a
|
||||||
|
* notice suppresses the card that follows it, a card suppresses the notice.
|
||||||
|
* Cards never suppress each other (a new msgseq is a new invitation).
|
||||||
|
*/
|
||||||
|
private readonly noticeDispatched_ = new Map<string, number>();
|
||||||
|
private readonly cardDispatched_ = new Map<string, number>();
|
||||||
|
|
||||||
constructor(private readonly deps: PacketPipelineDeps) {
|
constructor(private readonly deps: PacketPipelineDeps) {
|
||||||
// Tag every line we emit with this Bridge's UIN so per-account file
|
// Tag every line we emit with this Bridge's UIN so per-account file
|
||||||
@@ -390,6 +407,16 @@ export class IncomingPacketPipeline {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The msgseq is cached above either way. Skip the duplicate event only when
|
||||||
|
// the notice already delivered this same invitation -- a card with a new
|
||||||
|
// sequence is a genuinely new invitation and must always dispatch.
|
||||||
|
if (this.dispatchedRecently_(this.noticeDispatched_, groupId, event.senderUid ?? '')) {
|
||||||
|
this.log.debug('invitation already dispatched from the notice: group=%d', groupId);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.rememberDispatched_(this.cardDispatched_, groupId, event.senderUid ?? '');
|
||||||
|
|
||||||
this.finishDispatch({
|
this.finishDispatch({
|
||||||
kind: 'group_invite',
|
kind: 'group_invite',
|
||||||
time: event.time,
|
time: event.time,
|
||||||
@@ -405,6 +432,27 @@ export class IncomingPacketPipeline {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private dispatchedRecently_(
|
||||||
|
marker: Map<string, number>,
|
||||||
|
groupId: number,
|
||||||
|
inviterUid: string,
|
||||||
|
): boolean {
|
||||||
|
const key = `${groupId}:${inviterUid}`;
|
||||||
|
const at = marker.get(key);
|
||||||
|
if (at === undefined) return false;
|
||||||
|
if (Date.now() - at < INVITE_PAIR_WINDOW_MS) return true;
|
||||||
|
marker.delete(key);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private rememberDispatched_(
|
||||||
|
marker: Map<string, number>,
|
||||||
|
groupId: number,
|
||||||
|
inviterUid: string,
|
||||||
|
): void {
|
||||||
|
marker.set(`${groupId}:${inviterUid}`, Date.now());
|
||||||
|
}
|
||||||
|
|
||||||
private emit(event: QQEventVariant): void {
|
private emit(event: QQEventVariant): void {
|
||||||
// Fire-and-forget: errors inside subscribers are surfaced via the bus's
|
// Fire-and-forget: errors inside subscribers are surfaced via the bus's
|
||||||
// own onError hook so one bad listener never blocks the others.
|
// own onError hook so one bad listener never blocks the others.
|
||||||
@@ -502,14 +550,38 @@ export class IncomingPacketPipeline {
|
|||||||
pkt: PacketInfo,
|
pkt: PacketInfo,
|
||||||
event: Extract<QQEventVariant, { kind: 'group_invite' }>,
|
event: Extract<QQEventVariant, { kind: 'group_invite' }>,
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
// Self-invite notices omit the approval msgseq; the private card owns this event.
|
// A self-invite never appears in the UIN-based request inbox, so there is
|
||||||
|
// nothing to enrich from and querying it only wastes a round trip. The
|
||||||
|
// private card carries the approval msgseq, but it only arrives for some
|
||||||
|
// inviters -- when it does not, the notice is the only chance to surface
|
||||||
|
// the invitation at all. Dispatch it with the decoder-provided legacy flag,
|
||||||
|
// and let the card win when both arrive (see `dispatchedInvites_`).
|
||||||
if (
|
if (
|
||||||
event.subType === 'invite' &&
|
event.subType === 'invite' &&
|
||||||
this.isSelfMemberIdentity(event.invitedUin ?? 0, event.invitedUid)
|
this.isSelfMemberIdentity(event.invitedUin ?? 0, event.invitedUid)
|
||||||
) {
|
) {
|
||||||
|
if (!event.flag) {
|
||||||
|
this.log.warn('self invitation without a flag: group=%d', event.groupId);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.dispatchedRecently_(this.cardDispatched_, event.groupId, event.fromUid ?? '')) {
|
||||||
|
this.log.debug(
|
||||||
|
'self invitation already dispatched from the card: group=%d',
|
||||||
|
event.groupId,
|
||||||
|
);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.rememberDispatched_(this.noticeDispatched_, event.groupId, event.fromUid ?? '');
|
||||||
|
|
||||||
|
try {
|
||||||
|
return this.dispatchEvent(pkt, event, 'sync');
|
||||||
|
} catch (error) {
|
||||||
|
throw new EnrichedDispatchError(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const uid = event.fromUid;
|
const uid = event.fromUid;
|
||||||
|
|
||||||
if (!uid) {
|
if (!uid) {
|
||||||
|
|||||||
@@ -193,10 +193,20 @@ describe('IncomingPacketPipeline / group requests', () => {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('leaves self invitations to the card without querying or waiting', async () => {
|
it('dispatches a self invitation without querying the inbox', async () => {
|
||||||
|
// A self-invite never shows up in the UIN-based inbox, so the lookup is
|
||||||
|
// skipped and the decoder-provided legacy flag is what keeps it actionable.
|
||||||
const { pipeline, captured, resolveGroupJoinRequest, fetchProfileByUid } = makePipeline();
|
const { pipeline, captured, resolveGroupJoinRequest, fetchProfileByUid } = makePipeline();
|
||||||
await receive(pipeline, requestEvent({ subType: 'invite', invitedUin: 10001 }));
|
await receive(
|
||||||
expect(captured).toEqual([]);
|
pipeline,
|
||||||
|
requestEvent({ subType: 'invite', invitedUin: 10001, flag: 'invite:12345:u_requester' }),
|
||||||
|
);
|
||||||
|
expect(captured).toHaveLength(1);
|
||||||
|
expect(captured[0]).toMatchObject({
|
||||||
|
kind: 'group_invite',
|
||||||
|
invitedUin: 10001,
|
||||||
|
flag: 'invite:12345:u_requester',
|
||||||
|
});
|
||||||
expect(resolveGroupJoinRequest).not.toHaveBeenCalled();
|
expect(resolveGroupJoinRequest).not.toHaveBeenCalled();
|
||||||
expect(fetchProfileByUid).not.toHaveBeenCalled();
|
expect(fetchProfileByUid).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,187 @@
|
|||||||
|
import type { PacketInfo } from '@snowluma/common/protocol-types';
|
||||||
|
import { BridgeEventBus } from '@snowluma/protocol/event-bus';
|
||||||
|
import type { GroupInviteEvent, QQEventVariant } from '@snowluma/protocol/events';
|
||||||
|
import { IdentityService } from '@snowluma/protocol/identity-service';
|
||||||
|
import { IncomingPacketPipeline } from '@snowluma/protocol/packet-pipeline';
|
||||||
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
|
||||||
|
const SELF_UIN = 10001;
|
||||||
|
const GROUP_ID = 12345;
|
||||||
|
const INVITOR_UID = 'u_inviter';
|
||||||
|
const INVITOR_UIN = 20002;
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.useRealTimers();
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
function packet(): PacketInfo {
|
||||||
|
return {
|
||||||
|
pid: 42,
|
||||||
|
uin: String(SELF_UIN),
|
||||||
|
serviceCmd: 'test.self-invite',
|
||||||
|
seqId: 1,
|
||||||
|
retCode: 0,
|
||||||
|
fromClient: false,
|
||||||
|
body: Buffer.from([0x00]),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The MSF notice: no approval msgseq, only the legacy flag from the decoder. */
|
||||||
|
function selfInviteNotice(): GroupInviteEvent {
|
||||||
|
return {
|
||||||
|
kind: 'group_invite',
|
||||||
|
time: 1,
|
||||||
|
selfUin: SELF_UIN,
|
||||||
|
groupId: GROUP_ID,
|
||||||
|
fromUin: INVITOR_UIN,
|
||||||
|
fromUid: INVITOR_UID,
|
||||||
|
invitedUin: SELF_UIN,
|
||||||
|
invitedUid: undefined,
|
||||||
|
subType: 'invite',
|
||||||
|
message: '',
|
||||||
|
flag: `invite:${GROUP_ID}:${INVITOR_UID}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The private "qun.invite" card, which carries the real approval msgseq. */
|
||||||
|
function inviteCard(sequence = 777): QQEventVariant {
|
||||||
|
return {
|
||||||
|
kind: 'friend_message',
|
||||||
|
time: 1,
|
||||||
|
selfUin: SELF_UIN,
|
||||||
|
senderUin: INVITOR_UIN,
|
||||||
|
senderUid: INVITOR_UID,
|
||||||
|
senderNick: 'inviter',
|
||||||
|
msgSeq: 1,
|
||||||
|
msgId: 2,
|
||||||
|
elements: [],
|
||||||
|
inviteCardGroupUin: GROUP_ID,
|
||||||
|
inviteCardSequence: sequence,
|
||||||
|
} as QQEventVariant;
|
||||||
|
}
|
||||||
|
|
||||||
|
function makePipeline(options: { request?: 'found' | 'missing' } = {}) {
|
||||||
|
const identity = IdentityService.memory(String(SELF_UIN));
|
||||||
|
const events = new BridgeEventBus();
|
||||||
|
const received: QQEventVariant[] = [];
|
||||||
|
events.onAny((event) => {
|
||||||
|
// The card arrives as a friend_message and the pipeline derives a
|
||||||
|
// group_invite from it; only the latter is under test here.
|
||||||
|
if ((event as QQEventVariant).kind === 'group_invite') {
|
||||||
|
received.push(event as QQEventVariant);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const resolveGroupJoinRequest = vi.fn(async () =>
|
||||||
|
options.request === 'found'
|
||||||
|
? {
|
||||||
|
groupId: GROUP_ID,
|
||||||
|
groupName: '',
|
||||||
|
targetUid: 'u_target',
|
||||||
|
targetUin: 0,
|
||||||
|
targetName: '',
|
||||||
|
invitorUid: INVITOR_UID,
|
||||||
|
invitorUin: INVITOR_UIN,
|
||||||
|
invitorName: '',
|
||||||
|
operatorUid: '',
|
||||||
|
operatorUin: 0,
|
||||||
|
operatorName: '',
|
||||||
|
sequence: 42,
|
||||||
|
state: 1,
|
||||||
|
notifyType: 1,
|
||||||
|
eventType: 1,
|
||||||
|
comment: '',
|
||||||
|
filtered: false,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
);
|
||||||
|
|
||||||
|
const pipeline = new IncomingPacketPipeline({
|
||||||
|
identity,
|
||||||
|
events,
|
||||||
|
fetchGroupList: vi.fn(async () => {}),
|
||||||
|
fetchGroupMemberList: vi.fn(async () => {}),
|
||||||
|
resolveGroupJoinRequest,
|
||||||
|
rememberGroupInviteCardSequence: vi.fn(() => true),
|
||||||
|
});
|
||||||
|
|
||||||
|
return { pipeline, received, resolveGroupJoinRequest };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('self-invite delivery', () => {
|
||||||
|
it('dispatches a self-invite that the request inbox cannot resolve', async () => {
|
||||||
|
const { pipeline, received } = makePipeline({ request: 'missing' });
|
||||||
|
pipeline.registerCmd('test.self-invite', () => [selfInviteNotice()]);
|
||||||
|
|
||||||
|
await pipeline.process(packet());
|
||||||
|
|
||||||
|
expect(received).toHaveLength(1);
|
||||||
|
expect(received[0]).toMatchObject({
|
||||||
|
kind: 'group_invite',
|
||||||
|
groupId: GROUP_ID,
|
||||||
|
invitedUin: SELF_UIN,
|
||||||
|
subType: 'invite',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps the decoder-provided legacy flag so the request stays actionable', async () => {
|
||||||
|
const { pipeline, received } = makePipeline({ request: 'missing' });
|
||||||
|
pipeline.registerCmd('test.self-invite', () => [selfInviteNotice()]);
|
||||||
|
|
||||||
|
await pipeline.process(packet());
|
||||||
|
|
||||||
|
expect((received[0] as GroupInviteEvent).flag).toBe(
|
||||||
|
`invite:${GROUP_ID}:${INVITOR_UID}`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps the legacy flag even when the inbox has a matching request', async () => {
|
||||||
|
// Self-invite notices are absent from the UIN-based inbox, so the pipeline
|
||||||
|
// does not consult it at all -- the legacy flag must therefore survive.
|
||||||
|
const { pipeline, received, resolveGroupJoinRequest } = makePipeline({ request: 'found' });
|
||||||
|
pipeline.registerCmd('test.self-invite', () => [selfInviteNotice()]);
|
||||||
|
|
||||||
|
await pipeline.process(packet());
|
||||||
|
|
||||||
|
expect(resolveGroupJoinRequest).not.toHaveBeenCalled();
|
||||||
|
expect((received[0] as GroupInviteEvent).flag).toBe(
|
||||||
|
`invite:${GROUP_ID}:${INVITOR_UID}`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not query the inbox and still dispatches a self-invite', async () => {
|
||||||
|
const { pipeline, received, resolveGroupJoinRequest } = makePipeline({ request: 'missing' });
|
||||||
|
pipeline.registerCmd('test.self-invite', () => [selfInviteNotice()]);
|
||||||
|
|
||||||
|
await pipeline.process(packet());
|
||||||
|
|
||||||
|
expect(received).toHaveLength(1);
|
||||||
|
// A self-invite never shows up in the UIN-based inbox, so the lookup is skipped.
|
||||||
|
expect(resolveGroupJoinRequest).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('lets the card win when it arrives first', async () => {
|
||||||
|
const { pipeline, received } = makePipeline({ request: 'missing' });
|
||||||
|
pipeline.registerCmd('test.self-invite', () => [selfInviteNotice()]);
|
||||||
|
pipeline.registerCmd('test.card', () => [inviteCard()]);
|
||||||
|
|
||||||
|
await pipeline.process({ ...packet(), serviceCmd: 'test.card' });
|
||||||
|
await pipeline.process(packet());
|
||||||
|
|
||||||
|
expect(received).toHaveLength(1);
|
||||||
|
expect((received[0] as GroupInviteEvent).flag).toBe(`slreq:1:777:${GROUP_ID}:2:0`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('suppresses the notice when the card already delivered the invitation', async () => {
|
||||||
|
const { pipeline, received } = makePipeline({ request: 'missing' });
|
||||||
|
pipeline.registerCmd('test.self-invite', () => [selfInviteNotice()]);
|
||||||
|
pipeline.registerCmd('test.card', () => [inviteCard()]);
|
||||||
|
|
||||||
|
await pipeline.process(packet());
|
||||||
|
await pipeline.process({ ...packet(), serviceCmd: 'test.card' });
|
||||||
|
|
||||||
|
expect(received).toHaveLength(1);
|
||||||
|
expect((received[0] as GroupInviteEvent).flag).toBe(`invite:${GROUP_ID}:${INVITOR_UID}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user