fix(core): persist identities from request events
This commit is contained in:
6 files changed
+135
-1
No files matched your search
@@ -360,6 +360,21 @@ export class Bridge {
|
||||
uin: event.userUin,
|
||||
});
|
||||
break;
|
||||
case 'friend_request':
|
||||
this.identity.rememberRequestIdentity({
|
||||
uid: event.fromUid,
|
||||
uin: event.fromUin,
|
||||
source: 'friend_request',
|
||||
});
|
||||
break;
|
||||
case 'group_invite':
|
||||
this.identity.rememberRequestIdentity({
|
||||
groupId: event.groupId,
|
||||
uid: event.fromUid,
|
||||
uin: event.fromUin,
|
||||
source: 'group_request',
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -144,6 +144,7 @@ export interface GroupRecallEvent extends QQEvent {
|
||||
export interface FriendRequestEvent extends QQEvent {
|
||||
kind: 'friend_request';
|
||||
fromUin: number;
|
||||
fromUid?: string;
|
||||
message: string;
|
||||
flag: string;
|
||||
}
|
||||
@@ -152,6 +153,7 @@ export interface GroupInviteEvent extends QQEvent {
|
||||
kind: 'group_invite';
|
||||
groupId: number;
|
||||
fromUin: number;
|
||||
fromUid?: string;
|
||||
subType: string;
|
||||
message: string;
|
||||
flag: string;
|
||||
|
||||
@@ -651,6 +651,7 @@ export function parseMsgPush(pkt: PacketInfo, qqInfo: QQInfo): QQEventVariant[]
|
||||
kind: 'group_invite', time: timestamp, selfUin,
|
||||
groupId: join.groupUin ?? 0,
|
||||
fromUin: resolveUidToUin(qqInfo, join.groupUin ?? 0, join.targetUid ?? '', fromUin),
|
||||
fromUid: join.targetUid ?? '',
|
||||
subType: 'add', message: '',
|
||||
flag: 'add:' + (join.groupUin ?? 0) + ':' + (join.targetUid ?? ''),
|
||||
};
|
||||
@@ -665,6 +666,7 @@ export function parseMsgPush(pkt: PacketInfo, qqInfo: QQInfo): QQEventVariant[]
|
||||
kind: 'group_invite', time: timestamp, selfUin,
|
||||
groupId: inner.groupUin ?? 0,
|
||||
fromUin: resolveUidToUin(qqInfo, inner.groupUin ?? 0, inner.invitorUid ?? '', fromUin),
|
||||
fromUid: inner.invitorUid ?? '',
|
||||
subType: 'invite', message: '',
|
||||
flag: 'invite:' + (inner.groupUin ?? 0) + ':' + (inner.invitorUid ?? ''),
|
||||
};
|
||||
@@ -678,6 +680,7 @@ export function parseMsgPush(pkt: PacketInfo, qqInfo: QQInfo): QQEventVariant[]
|
||||
kind: 'group_invite', time: timestamp, selfUin,
|
||||
groupId: invite.groupUin ?? 0,
|
||||
fromUin: resolveUidToUin(qqInfo, invite.groupUin ?? 0, invite.invitorUid ?? '', fromUin),
|
||||
fromUid: invite.invitorUid ?? '',
|
||||
subType: 'invite', message: '',
|
||||
flag: 'invite:' + (invite.groupUin ?? 0) + ':' + (invite.invitorUid ?? ''),
|
||||
};
|
||||
@@ -693,6 +696,7 @@ export function parseMsgPush(pkt: PacketInfo, qqInfo: QQInfo): QQEventVariant[]
|
||||
const ev: FriendRequestEvent = {
|
||||
kind: 'friend_request', time: timestamp, selfUin,
|
||||
fromUin: resolveUidToUin(qqInfo, 0, sourceUid, fromUin),
|
||||
fromUid: sourceUid,
|
||||
message: request.info.message ?? '', flag: sourceUid,
|
||||
};
|
||||
return [ev];
|
||||
|
||||
@@ -68,6 +68,8 @@ interface MemberRow {
|
||||
|
||||
export class IdentityService {
|
||||
private readonly db: DatabaseSync | null;
|
||||
private readonly uinByUid = new Map<string, number>();
|
||||
private readonly uidByUin = new Map<number, string>();
|
||||
private inTransaction = false;
|
||||
private writeFailed = false;
|
||||
|
||||
@@ -110,6 +112,7 @@ export class IdentityService {
|
||||
|
||||
rememberFriends(friends: FriendInfo[]): void {
|
||||
this.qqInfo.setFriends(friends);
|
||||
for (const friend of friends) this.rememberUidUin(friend.uid, friend.uin);
|
||||
this.runWrite('friends', () => this.transaction(() => {
|
||||
this.db!.prepare('UPDATE users SET is_friend = 0, updated_at = ? WHERE is_friend = 1')
|
||||
.run(nowSeconds());
|
||||
@@ -146,6 +149,7 @@ export class IdentityService {
|
||||
|
||||
rememberGroupMembers(groupId: number, members: GroupMemberInfo[]): void {
|
||||
this.qqInfo.setGroupMembers(groupId, members);
|
||||
for (const member of members) this.rememberUidUin(member.uid, member.uin);
|
||||
this.runWrite('group members', () => this.transaction(() => {
|
||||
this.upsertGroup({ groupId, memberCount: members.length });
|
||||
this.db!.prepare('UPDATE group_members SET active = 0, updated_at = ? WHERE group_id = ?')
|
||||
@@ -173,6 +177,7 @@ export class IdentityService {
|
||||
this.qqInfo.setUserProfile(info);
|
||||
const selfUin = parseInt(this.qqInfo.uin, 10) || 0;
|
||||
if (info.uin === selfUin) this.qqInfo.setSelfProfile(info);
|
||||
this.rememberUidUin(info.uid, info.uin);
|
||||
this.runWrite('user profile', () => this.transaction(() => this.upsertUser({
|
||||
uid: info.uid,
|
||||
uin: info.uin,
|
||||
@@ -183,6 +188,11 @@ export class IdentityService {
|
||||
}
|
||||
|
||||
rememberGroupRequests(requests: GroupRequestInfo[]): void {
|
||||
for (const request of requests) {
|
||||
this.rememberUidUin(request.targetUid, request.targetUin);
|
||||
this.rememberUidUin(request.invitorUid, request.invitorUin);
|
||||
this.rememberUidUin(request.operatorUid, request.operatorUin);
|
||||
}
|
||||
this.runWrite('group requests', () => this.transaction(() => {
|
||||
for (const request of requests) {
|
||||
this.upsertGroup({ groupId: request.groupId, groupName: request.groupName });
|
||||
@@ -208,10 +218,26 @@ export class IdentityService {
|
||||
}));
|
||||
}
|
||||
|
||||
rememberRequestIdentity(
|
||||
identity: { groupId?: number; uid?: string; uin?: number; nickname?: string; source?: string },
|
||||
): void {
|
||||
this.rememberUidUin(identity.uid, identity.uin);
|
||||
this.runWrite('request identity', () => this.transaction(() => {
|
||||
if (identity.groupId !== undefined) this.upsertGroup({ groupId: identity.groupId });
|
||||
this.upsertUser({
|
||||
uid: identity.uid,
|
||||
uin: identity.uin,
|
||||
nickname: identity.nickname,
|
||||
source: identity.source || 'request',
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
rememberGroupMemberIdentity(
|
||||
groupId: number,
|
||||
identity: { uid?: string; uin?: number; nickname?: string; card?: string },
|
||||
): void {
|
||||
this.rememberUidUin(identity.uid, identity.uin);
|
||||
this.runWrite('group member identity', () => this.transaction(() => this.upsertGroupMember({
|
||||
groupId,
|
||||
uid: identity.uid,
|
||||
@@ -248,6 +274,8 @@ export class IdentityService {
|
||||
}
|
||||
const globalCached = this.qqInfo.resolveUid(normalized);
|
||||
if (globalCached !== null) return globalCached;
|
||||
const mapped = this.uinByUid.get(normalized);
|
||||
if (mapped !== undefined) return mapped;
|
||||
|
||||
if (!this.db) return null;
|
||||
if (groupId !== undefined) {
|
||||
@@ -277,6 +305,8 @@ export class IdentityService {
|
||||
}
|
||||
const globalCached = this.qqInfo.findUidByUin(normalized);
|
||||
if (globalCached) return globalCached;
|
||||
const mapped = this.uidByUin.get(normalized);
|
||||
if (mapped) return mapped;
|
||||
|
||||
if (!this.db) return null;
|
||||
if (groupId !== undefined) {
|
||||
@@ -296,6 +326,14 @@ export class IdentityService {
|
||||
return normalizeUid(row?.uid) || null;
|
||||
}
|
||||
|
||||
private rememberUidUin(uid: unknown, uin: unknown): void {
|
||||
const normalizedUid = normalizeUid(uid);
|
||||
const normalizedUin = normalizeUin(uin);
|
||||
if (!normalizedUid || normalizedUin === null) return;
|
||||
this.uinByUid.set(normalizedUid, normalizedUin);
|
||||
this.uidByUin.set(normalizedUin, normalizedUid);
|
||||
}
|
||||
|
||||
private initSchema(): void {
|
||||
this.db!.exec(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
|
||||
Reference in new issue
Block a user