fix(group): self-heal member group card from message field 4

A member's cached group card could stay stale for the lifetime of a long-running
bot: nothing refreshes it on a card change (no push), and the member-list refetch
only fires on join/leave/admin events (+60s TTL), never periodically — so a quiet
group freezes at warmup. get_group_msg_history / live events then showed an empty
or outdated sender.card even though the current card was in the message.

Every group message carries the sender's current display name in grp.memberCard
(field 4 = group card if set, else base nickname). The group decoder now prefers
it over the possibly-stale cache when it differs from the base nickname (a real
group card); and packet-pipeline self-heals it back into the in-memory member
cache, gated so it writes only on an actual change. This keeps active members'
cards fresh for all cache consumers off message traffic alone — no extra fetch,
no rate-limit exposure. Verified on-target: a member's card change now reflects
in the next message without a member-list refetch.

Known, accepted edge: a member changing only their base nickname (no card) is
mislabeled as a card until the next member-list refetch corrects it — field 4
alone can't distinguish the two, and the mislabeled value is still the member's
own current name.
This commit is contained in:
DrSmoothl committed 2026-07-05 14:20:36 +08:00
1 parent 8dde7ef8e2
commit 007dfaf6e2
4 files changed
+104

No files matched your search

@@ -67,6 +67,35 @@ describe('fetchGroupMessageRange / SsoGetGroupMsg', () => {
expect(out[1]).toMatchObject({ msgSeq: 120, senderUin: 222, senderNick: 'Bob' });
});
it('[#1] prefers the fresher group card in grp.memberCard (field 4) over a stale cache card', async () => {
const stale = { uin: 222, uid: '', nickname: 'BaseNick', card: 'StaleCard', role: 'member', level: 0, title: '', joinTime: 0, lastSentTime: 0, shutUpTime: 0 };
const idWithMember = { findGroupMember: (_g: number, u: number) => (u === 222 ? stale : undefined) } as unknown as IdentityService;
const resp = protobuf_encode<SsoGetGroupMsgResponse>({
body: { groupUin: 9999, messages: [
{ responseHead: { fromUin: 222, grp: { groupUin: 9999, memberCard: 'FreshCard' } }, // field2 empty, field4 = fresh card
contentHead: { msgType: 82, sequence: 130, timestamp: 1700000130, msgId: 5130 },
body: { richText: { elems: [{ text: { str: 'hi' } }] } } },
] },
});
const out = await fetchGroupMessageRange({ sendRawPacket: async () => okResult(resp) }, idWithMember, 10001, 9999, 100, 140);
// Base nickname from cache; card overridden by the fresher field-4 value.
expect(out[0]).toMatchObject({ senderUin: 222, senderNick: 'BaseNick', senderCard: 'FreshCard' });
});
it('[#1] keeps the cache card when field 4 equals the base nickname (no active group card)', async () => {
const m = { uin: 222, uid: '', nickname: 'BaseNick', card: 'CachedCard', role: 'member', level: 0, title: '', joinTime: 0, lastSentTime: 0, shutUpTime: 0 };
const id = { findGroupMember: () => m } as unknown as IdentityService;
const resp = protobuf_encode<SsoGetGroupMsgResponse>({
body: { groupUin: 9999, messages: [
{ responseHead: { fromUin: 222, grp: { groupUin: 9999, memberCard: 'BaseNick' } }, // field4 == base nick → not a card
contentHead: { msgType: 82, sequence: 130, timestamp: 1700000130, msgId: 5130 },
body: { richText: { elems: [{ text: { str: 'hi' } }] } } },
] },
});
const out = await fetchGroupMessageRange({ sendRawPacket: async () => okResult(resp) }, id, 10001, 9999, 100, 140);
expect(out[0]).toMatchObject({ senderNick: 'BaseNick', senderCard: 'CachedCard' });
});
it('drops content-less blank messages, keeps real ones (#102 parity)', async () => {
const resp = protobuf_encode<SsoGetGroupMsgResponse>({
body: {