fix(events): keep member role cache in sync on group admin change

get_group_member_info serves straight from the in-memory member cache
on its default no_cache=false path — a deliberate choice, since clients
like OlivaDice/MaiBot query it once per inbound message and a per-call
OIDB refetch trips Tencent risk-control. But the group-admin push
decoder only emitted the notice and never updated the cached role, so a
member promoted to admin kept reading back as `member` and permission
gates rejected their commands. Patch the cached role in the decoder on
set/unset, only touching members we already know and never downgrading
the owner.

Also fix promote/demote misdetection: proton materializes an absent
embedded-message field as an empty `{}` rather than undefined, so the
old `extraEnable !== undefined` test was always true — every demote was
reported as a promotion with a fallback userUin. Key off whichever
sub-message actually carries an adminUid instead.

Fixes #93
This commit is contained in:
DrSmoothl committed 2026-06-09 16:30:10 +08:00
1 parent d508814dfa
commit 14e02bce3c
2 files changed
+103 -6

No files matched your search

@@ -7,15 +7,42 @@ import type { MsgPushDecoder } from '../registry';
export const decodeGroupAdmin: MsgPushDecoder = (ctx) => {
const admin = protobuf_decode<GroupAdmin>(ctx.content);
if (!admin?.body) return [];
const extra = admin.body.extraEnable ?? admin.body.extraDisable;
if (!extra) return [];
// proton materializes an *absent* embedded-message field as an empty
// `{}` rather than leaving it undefined, so the old
// `extraEnable !== undefined` promote/demote test was always true and
// every demote got reported as a promotion (with a fallback userUin).
// The live sub-message is the one that actually carries an `adminUid`
// — key off that instead.
const enableUid = admin.body.extraEnable?.adminUid;
const disableUid = admin.body.extraDisable?.adminUid;
const adminUid = enableUid || disableUid;
if (!adminUid) return [];
const groupId = admin.groupUin ?? 0;
const set = !!enableUid;
const userUin = resolveUidToUin(ctx.identity, groupId, adminUid, ctx.fromUin);
// Keep the in-memory member cache in step with the promotion/demotion.
// `get_group_member_info` serves straight from this cache on its
// default (no_cache=false) path — and we deliberately *don't*
// force-refresh there, because clients like OlivaDice/MaiBot query it
// once per inbound message and a per-message OIDB refetch trips
// Tencent risk-control (see the ContactsApi member-list cache). So if
// we don't patch the role here, a freshly-promoted admin keeps reading
// back as `member` until the member-list TTL lapses, and permission
// gates reject their commands (#93). Only touch a member we already
// know, and never downgrade the owner.
const cached = ctx.identity.findGroupMember(groupId, userUin);
if (cached && cached.role !== 'owner') {
ctx.identity.updateGroupMember(groupId, { ...cached, role: set ? 'admin' : 'member' });
}
const ev: GroupAdminEvent = {
kind: 'group_admin',
time: ctx.head.timestamp,
selfUin: ctx.selfUin,
groupId: admin.groupUin ?? 0,
userUin: resolveUidToUin(ctx.identity, admin.groupUin ?? 0, extra.adminUid ?? '', ctx.fromUin),
set: admin.body.extraEnable !== undefined,
groupId,
userUin,
set,
};
return [ev];
};