refactor(identity): consolidate UID resolution behind IdentityService
Pull the cache→DB→network-fetch fallback cascade for UIN→UID translation into a single async `resolveUid` on IdentityService, and drop the duplicated `resolveUserUid` helper that used to live in bridge-oidb. Bridge injects itself as the IdentityFetcher post-construction so the network fallback runs through the same OIDB profile fetch the helper used. Each action module's `resolveUserUid(bridge, uin, groupId?)` call becomes `bridge.resolveUserUid(uin, groupId?)` — same signature on Bridge, but internally it now goes through `identity.resolveUid` so future changes (retry, coalescing, alternative resolvers) only need to land in one place. This is part 1 of the IdentityService deepening (RoadMap item 1). The QQInfo bypass in msg-push-handler and the broader QQInfo absorption land in the follow-up commit.
This commit is contained in:
20 files changed
+157
-75
No files matched your search
@@ -3,7 +3,7 @@
|
|||||||
// protocol uses opaque UIDs rather than QQ uin numbers.
|
// protocol uses opaque UIDs rather than QQ uin numbers.
|
||||||
|
|
||||||
import type { Bridge } from '../bridge';
|
import type { Bridge } from '../bridge';
|
||||||
import { sendOidbAndCheck, resolveUserUid } from '../bridge-oidb';
|
import { sendOidbAndCheck } from '../bridge-oidb';
|
||||||
import {
|
import {
|
||||||
OidbDeleteFriendSchema,
|
OidbDeleteFriendSchema,
|
||||||
OidbFriendRequestActionSchema,
|
OidbFriendRequestActionSchema,
|
||||||
@@ -13,14 +13,14 @@ import {
|
|||||||
export async function setFriendAddRequest(bridge: Bridge, uidOrFlag: string, approve: boolean): Promise<void> {
|
export async function setFriendAddRequest(bridge: Bridge, uidOrFlag: string, approve: boolean): Promise<void> {
|
||||||
let targetUid = uidOrFlag;
|
let targetUid = uidOrFlag;
|
||||||
if (/^\d+$/.test(uidOrFlag)) {
|
if (/^\d+$/.test(uidOrFlag)) {
|
||||||
targetUid = await resolveUserUid(bridge, parseInt(uidOrFlag, 10));
|
targetUid = await bridge.resolveUserUid(parseInt(uidOrFlag, 10));
|
||||||
}
|
}
|
||||||
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0xb5d_44', 0xB5D, 44,
|
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0xb5d_44', 0xB5D, 44,
|
||||||
{ accept: approve ? 3 : 5, targetUid }, OidbFriendRequestActionSchema);
|
{ accept: approve ? 3 : 5, targetUid }, OidbFriendRequestActionSchema);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteFriend(bridge: Bridge, userId: number, block = false): Promise<void> {
|
export async function deleteFriend(bridge: Bridge, userId: number, block = false): Promise<void> {
|
||||||
const targetUid = await resolveUserUid(bridge, userId);
|
const targetUid = await bridge.resolveUserUid(userId);
|
||||||
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x126b_0', 0x126B, 0,
|
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x126b_0', 0x126B, 0,
|
||||||
{
|
{
|
||||||
field1: {
|
field1: {
|
||||||
@@ -47,7 +47,7 @@ export async function deleteFriend(bridge: Bridge, userId: number, block = false
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function setFriendRemark(bridge: Bridge, userId: number, remark: string): Promise<void> {
|
export async function setFriendRemark(bridge: Bridge, userId: number, remark: string): Promise<void> {
|
||||||
const uid = await resolveUserUid(bridge, userId);
|
const uid = await bridge.resolveUserUid(userId);
|
||||||
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0xb6e_2', 0xB6E, 2,
|
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0xb6e_2', 0xB6E, 2,
|
||||||
{ targetUid: uid, remark }, OidbSetFriendRemarkSchema);
|
{ targetUid: uid, remark }, OidbSetFriendRemarkSchema);
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
// so callers can grep across the two layers.
|
// so callers can grep across the two layers.
|
||||||
|
|
||||||
import type { Bridge } from '../bridge';
|
import type { Bridge } from '../bridge';
|
||||||
import { sendOidbAndCheck, sendOidbAndDecode, resolveUserUid } from '../bridge-oidb';
|
import { sendOidbAndCheck, sendOidbAndDecode } from '../bridge-oidb';
|
||||||
import {
|
import {
|
||||||
Oidb0x89a_0AddOptionSchema,
|
Oidb0x89a_0AddOptionSchema,
|
||||||
Oidb0x89a_0SearchSchema,
|
Oidb0x89a_0SearchSchema,
|
||||||
@@ -26,7 +26,7 @@ import {
|
|||||||
// ─────────────── mute / un-mute ───────────────
|
// ─────────────── mute / un-mute ───────────────
|
||||||
|
|
||||||
export async function muteGroupMember(bridge: Bridge, groupId: number, userId: number, duration: number): Promise<void> {
|
export async function muteGroupMember(bridge: Bridge, groupId: number, userId: number, duration: number): Promise<void> {
|
||||||
const uid = await resolveUserUid(bridge, userId, groupId);
|
const uid = await bridge.resolveUserUid(userId, groupId);
|
||||||
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x1253_1', 0x1253, 1,
|
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x1253_1', 0x1253, 1,
|
||||||
{ groupUin: groupId, type: 1, body: { targetUid: uid, duration } }, OidbMuteMemberSchema);
|
{ groupUin: groupId, type: 1, body: { targetUid: uid, duration } }, OidbMuteMemberSchema);
|
||||||
}
|
}
|
||||||
@@ -62,13 +62,13 @@ export async function setGroupAddRequest(
|
|||||||
// ─────────────── kick / leave ───────────────
|
// ─────────────── kick / leave ───────────────
|
||||||
|
|
||||||
export async function kickGroupMember(bridge: Bridge, groupId: number, userId: number, reject: boolean, reason = ''): Promise<void> {
|
export async function kickGroupMember(bridge: Bridge, groupId: number, userId: number, reject: boolean, reason = ''): Promise<void> {
|
||||||
const uid = await resolveUserUid(bridge, userId, groupId);
|
const uid = await bridge.resolveUserUid(userId, groupId);
|
||||||
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x8a0_1', 0x8A0, 1,
|
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x8a0_1', 0x8A0, 1,
|
||||||
{ groupUin: groupId, targetUid: uid, rejectAddRequest: reject, reason }, OidbKickMemberSchema);
|
{ groupUin: groupId, targetUid: uid, rejectAddRequest: reject, reason }, OidbKickMemberSchema);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function kickGroupMembers(bridge: Bridge, groupId: number, userIds: number[], reject: boolean): Promise<void> {
|
export async function kickGroupMembers(bridge: Bridge, groupId: number, userIds: number[], reject: boolean): Promise<void> {
|
||||||
const targetUids = await Promise.all(userIds.map(userId => resolveUserUid(bridge, userId, groupId)));
|
const targetUids = await Promise.all(userIds.map(userId => bridge.resolveUserUid(userId, groupId)));
|
||||||
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x8a0_1', 0x8A0, 1,
|
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x8a0_1', 0x8A0, 1,
|
||||||
{ groupId: BigInt(groupId), targetUids, rejectAddRequest: reject ? 1 : 0, kickReason: new Uint8Array(0), field12: 0 }, Oidb0x8a0ReqSchema);
|
{ groupId: BigInt(groupId), targetUids, rejectAddRequest: reject ? 1 : 0, kickReason: new Uint8Array(0), field12: 0 }, Oidb0x8a0ReqSchema);
|
||||||
}
|
}
|
||||||
@@ -81,13 +81,13 @@ export async function leaveGroup(bridge: Bridge, groupId: number): Promise<void>
|
|||||||
// ─────────────── role / display name ───────────────
|
// ─────────────── role / display name ───────────────
|
||||||
|
|
||||||
export async function setGroupAdmin(bridge: Bridge, groupId: number, userId: number, enable: boolean): Promise<void> {
|
export async function setGroupAdmin(bridge: Bridge, groupId: number, userId: number, enable: boolean): Promise<void> {
|
||||||
const uid = await resolveUserUid(bridge, userId, groupId);
|
const uid = await bridge.resolveUserUid(userId, groupId);
|
||||||
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x1096_1', 0x1096, 1,
|
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x1096_1', 0x1096, 1,
|
||||||
{ groupUin: groupId, uid, isAdmin: enable }, OidbSetAdminSchema);
|
{ groupUin: groupId, uid, isAdmin: enable }, OidbSetAdminSchema);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setGroupCard(bridge: Bridge, groupId: number, userId: number, card: string): Promise<void> {
|
export async function setGroupCard(bridge: Bridge, groupId: number, userId: number, card: string): Promise<void> {
|
||||||
const uid = await resolveUserUid(bridge, userId, groupId);
|
const uid = await bridge.resolveUserUid(userId, groupId);
|
||||||
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x8fc_3', 0x8FC, 3,
|
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x8fc_3', 0x8FC, 3,
|
||||||
{ groupUin: groupId, body: { targetUid: uid, targetName: card } }, OidbRenameMemberSchema);
|
{ groupUin: groupId, body: { targetUid: uid, targetName: card } }, OidbRenameMemberSchema);
|
||||||
}
|
}
|
||||||
@@ -98,7 +98,7 @@ export async function setGroupName(bridge: Bridge, groupId: number, name: string
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function setGroupSpecialTitle(bridge: Bridge, groupId: number, userId: number, title: string): Promise<void> {
|
export async function setGroupSpecialTitle(bridge: Bridge, groupId: number, userId: number, title: string): Promise<void> {
|
||||||
const uid = await resolveUserUid(bridge, userId, groupId);
|
const uid = await bridge.resolveUserUid(userId, groupId);
|
||||||
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x8fc_2', 0x8FC, 2,
|
await sendOidbAndCheck(bridge, 'OidbSvcTrpcTcp.0x8fc_2', 0x8FC, 2,
|
||||||
{ groupUin: groupId, body: { targetUid: uid, specialTitle: title, expireTime: -1 } }, OidbSpecialTitleSchema);
|
{ groupUin: groupId, body: { targetUid: uid, specialTitle: title, expireTime: -1 } }, OidbSpecialTitleSchema);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
import type { Bridge } from '../bridge';
|
import type { Bridge } from '../bridge';
|
||||||
import { protoEncode } from '../../protobuf/decode';
|
import { protoEncode } from '../../protobuf/decode';
|
||||||
import { sendOidbAndDecode, resolveUserUid } from '../bridge-oidb';
|
import { sendOidbAndDecode } from '../bridge-oidb';
|
||||||
import { fetchHighwaySession, uploadHighwayHttp } from '../highway/highway-client';
|
import { fetchHighwaySession, uploadHighwayHttp } from '../highway/highway-client';
|
||||||
import { computeHashes, computeMd5, loadBinarySource } from '../highway/utils';
|
import { computeHashes, computeMd5, loadBinarySource } from '../highway/utils';
|
||||||
import {
|
import {
|
||||||
@@ -368,12 +368,12 @@ export async function uploadPrivateFile(
|
|||||||
const loaded = await loadBinarySource(source, 'file');
|
const loaded = await loadBinarySource(source, 'file');
|
||||||
if (!loaded.bytes.length) throw new Error('private file is empty');
|
if (!loaded.bytes.length) throw new Error('private file is empty');
|
||||||
|
|
||||||
const targetUid = await resolveUserUid(bridge, userId);
|
const targetUid = await bridge.resolveUserUid(userId);
|
||||||
let selfUid = bridge.qqInfo.selfUid;
|
let selfUid = bridge.qqInfo.selfUid;
|
||||||
if (!selfUid) {
|
if (!selfUid) {
|
||||||
const selfUin = toInt(bridge.qqInfo.uin);
|
const selfUin = toInt(bridge.qqInfo.uin);
|
||||||
if (selfUin > 0) {
|
if (selfUin > 0) {
|
||||||
selfUid = await resolveUserUid(bridge, selfUin);
|
selfUid = await bridge.resolveUserUid(selfUin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!selfUid) throw new Error('self uid is unavailable');
|
if (!selfUid) throw new Error('self uid is unavailable');
|
||||||
@@ -568,7 +568,7 @@ export async function fetchGroupFileUrl(bridge: Bridge, groupId: number, fileId:
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchPrivateFileUrl(bridge: Bridge, userId: number, fileId: string, fileHash: string): Promise<string> {
|
export async function fetchPrivateFileUrl(bridge: Bridge, userId: number, fileId: string, fileHash: string): Promise<string> {
|
||||||
const uid = await resolveUserUid(bridge, userId);
|
const uid = await bridge.resolveUserUid(userId);
|
||||||
const resp = await sendOidbAndDecode<any>(
|
const resp = await sendOidbAndDecode<any>(
|
||||||
bridge,
|
bridge,
|
||||||
'OidbSvcTrpcTcp.0xe37_1200',
|
'OidbSvcTrpcTcp.0xe37_1200',
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
import type { Bridge } from '../bridge';
|
import type { Bridge } from '../bridge';
|
||||||
import { protoEncode } from '../../protobuf/decode';
|
import { protoEncode } from '../../protobuf/decode';
|
||||||
import { sendOidbAndCheck, resolveUserUid } from '../bridge-oidb';
|
import { sendOidbAndCheck } from '../bridge-oidb';
|
||||||
import {
|
import {
|
||||||
C2CRecallRequestSchema,
|
C2CRecallRequestSchema,
|
||||||
GroupRecallRequestSchema,
|
GroupRecallRequestSchema,
|
||||||
@@ -28,7 +28,7 @@ export async function recallPrivateMessage(
|
|||||||
bridge: Bridge, userUin: number, clientSeq: number,
|
bridge: Bridge, userUin: number, clientSeq: number,
|
||||||
msgSeq: number, random: number, timestamp: number,
|
msgSeq: number, random: number, timestamp: number,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const targetUid = await resolveUserUid(bridge, userUin);
|
const targetUid = await bridge.resolveUserUid(userUin);
|
||||||
const request = protoEncode({
|
const request = protoEncode({
|
||||||
type: 1,
|
type: 1,
|
||||||
targetUid,
|
targetUid,
|
||||||
@@ -53,7 +53,7 @@ export async function markPrivateMessageRead(
|
|||||||
msgSeq: number,
|
msgSeq: number,
|
||||||
timestamp: number = Math.floor(Date.now() / 1000),
|
timestamp: number = Math.floor(Date.now() / 1000),
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const uid = await resolveUserUid(bridge, userId);
|
const uid = await bridge.resolveUserUid(userId);
|
||||||
|
|
||||||
const request = protoEncode({
|
const request = protoEncode({
|
||||||
c2cList: [
|
c2cList: [
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
import type { Bridge } from '../bridge';
|
import type { Bridge } from '../bridge';
|
||||||
import { protoDecode, protoEncode } from '../../protobuf/decode';
|
import { protoDecode, protoEncode } from '../../protobuf/decode';
|
||||||
import { sendOidbAndCheck, sendOidbAndDecode, resolveUserUid } from '../bridge-oidb';
|
import { sendOidbAndCheck, sendOidbAndDecode } from '../bridge-oidb';
|
||||||
import { fetchHighwaySession, uploadHighwayHttp } from '../highway/highway-client';
|
import { fetchHighwaySession, uploadHighwayHttp } from '../highway/highway-client';
|
||||||
import { computeHashes, loadBinarySource } from '../highway/utils';
|
import { computeHashes, loadBinarySource } from '../highway/utils';
|
||||||
import {
|
import {
|
||||||
@@ -119,7 +119,7 @@ export async function setInputStatus(
|
|||||||
userId: number,
|
userId: number,
|
||||||
eventType: number,
|
eventType: number,
|
||||||
) {
|
) {
|
||||||
const targetUid = await resolveUserUid(bridge, userId);
|
const targetUid = await bridge.resolveUserUid(userId);
|
||||||
|
|
||||||
if (!targetUid) {
|
if (!targetUid) {
|
||||||
throw new Error('target uid not found');
|
throw new Error('target uid not found');
|
||||||
@@ -167,7 +167,7 @@ export async function getProfileLike(
|
|||||||
const isSelf = !userId;
|
const isSelf = !userId;
|
||||||
const targetUid = isSelf
|
const targetUid = isSelf
|
||||||
? await resolveSelfUid(bridge)
|
? await resolveSelfUid(bridge)
|
||||||
: await resolveUserUid(bridge, userId);
|
: await bridge.resolveUserUid(userId);
|
||||||
|
|
||||||
if (!targetUid) {
|
if (!targetUid) {
|
||||||
throw new Error('target uid not found');
|
throw new Error('target uid not found');
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
// same five-line utility from every themed file.
|
// same five-line utility from every themed file.
|
||||||
|
|
||||||
import type { Bridge } from '../bridge';
|
import type { Bridge } from '../bridge';
|
||||||
import { resolveUserUid } from '../bridge-oidb';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Coerce numbers, strings, and bigints to a plain integer.
|
* Coerce numbers, strings, and bigints to a plain integer.
|
||||||
@@ -48,7 +47,7 @@ export async function resolveSelfUid(bridge: Bridge): Promise<string> {
|
|||||||
if (selfUin <= 0) {
|
if (selfUin <= 0) {
|
||||||
throw new Error('self uid is unavailable');
|
throw new Error('self uid is unavailable');
|
||||||
}
|
}
|
||||||
selfUid = await resolveUserUid(bridge, selfUin);
|
selfUid = await bridge.resolveUserUid(selfUin);
|
||||||
return selfUid;
|
return selfUid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// OIDB protocol helpers -- encode requests, send+check, send+decode, UID resolution.
|
// OIDB protocol helpers -- encode requests, send+check, send+decode.
|
||||||
|
|
||||||
import type { Bridge } from './bridge';
|
import type { Bridge } from './bridge';
|
||||||
import type { ProtoSchema } from '../protobuf/decode';
|
import type { ProtoSchema } from '../protobuf/decode';
|
||||||
@@ -67,25 +67,3 @@ export async function sendOidbAndDecode<T>(
|
|||||||
return (resp as any).body as T;
|
return (resp as any).body as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve a UIN to a UID string, using cache or fetching as needed.
|
|
||||||
* Mirrors C++ resolve_user_uid_for_action.
|
|
||||||
*/
|
|
||||||
export async function resolveUserUid(bridge: Bridge, uin: number, groupId?: number): Promise<string> {
|
|
||||||
// Try group member first
|
|
||||||
if (groupId !== undefined) {
|
|
||||||
const uid = bridge.identity.findUidByUin(uin, groupId);
|
|
||||||
if (uid) return uid;
|
|
||||||
// Try fetching member list
|
|
||||||
try { await bridge.fetchGroupMemberList(groupId); } catch { /* ignore */ }
|
|
||||||
const uid2 = bridge.identity.findUidByUin(uin, groupId);
|
|
||||||
if (uid2) return uid2;
|
|
||||||
}
|
|
||||||
// Try cached
|
|
||||||
const uid = bridge.identity.findUidByUin(uin);
|
|
||||||
if (uid) return uid;
|
|
||||||
// Fetch profile
|
|
||||||
const profile = await bridge.fetchUserProfile(uin);
|
|
||||||
if (profile.uid) return profile.uid;
|
|
||||||
throw new Error(`failed to resolve UID for UIN ${uin}`);
|
|
||||||
}
|
|
||||||
@@ -18,7 +18,6 @@ import {
|
|||||||
} from './proto/action';
|
} from './proto/action';
|
||||||
|
|
||||||
// Delegated modules
|
// Delegated modules
|
||||||
import { resolveUserUid as resolveUserUid_ } from './bridge-oidb';
|
|
||||||
import {
|
import {
|
||||||
fetchFriendList as fetchFriendList_,
|
fetchFriendList as fetchFriendList_,
|
||||||
fetchGroupList as fetchGroupList_,
|
fetchGroupList as fetchGroupList_,
|
||||||
@@ -176,6 +175,10 @@ export class Bridge {
|
|||||||
constructor(qqInfo: QQInfo, identity = IdentityService.memory(qqInfo)) {
|
constructor(qqInfo: QQInfo, identity = IdentityService.memory(qqInfo)) {
|
||||||
this.qqInfo_ = qqInfo;
|
this.qqInfo_ = qqInfo;
|
||||||
this.identity = identity;
|
this.identity = identity;
|
||||||
|
this.identity.setFetcher({
|
||||||
|
fetchProfile: (uin) => this.fetchUserProfile(uin),
|
||||||
|
fetchGroupMemberList: (gid) => this.fetchGroupMemberList(gid),
|
||||||
|
});
|
||||||
this.registerDefaultHandlers();
|
this.registerDefaultHandlers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -553,7 +556,7 @@ export class Bridge {
|
|||||||
// --- Delegated: OIDB helpers ---
|
// --- Delegated: OIDB helpers ---
|
||||||
|
|
||||||
async resolveUserUid(uin: number, groupId?: number): Promise<string> {
|
async resolveUserUid(uin: number, groupId?: number): Promise<string> {
|
||||||
return resolveUserUid_(this, uin, groupId);
|
return this.identity.resolveUid(uin, groupId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Delegated: Contact / info queries ---
|
// --- Delegated: Contact / info queries ---
|
||||||
|
|||||||
@@ -8,6 +8,17 @@ import { createLogger } from '../utils/logger';
|
|||||||
|
|
||||||
const log = createLogger('Identity');
|
const log = createLogger('Identity');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hooks Identity uses when its read methods miss every cache layer. Wired in
|
||||||
|
* after construction (Bridge owns these methods, but Bridge can't exist
|
||||||
|
* before Identity does). Each call is expected to invoke the matching
|
||||||
|
* remember* observation on this service as a side effect.
|
||||||
|
*/
|
||||||
|
export interface IdentityFetcher {
|
||||||
|
fetchProfile(uin: number): Promise<UserProfileInfo>;
|
||||||
|
fetchGroupMemberList?(groupId: number): Promise<unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
interface UserInput {
|
interface UserInput {
|
||||||
uid?: string;
|
uid?: string;
|
||||||
uin?: number;
|
uin?: number;
|
||||||
@@ -71,6 +82,7 @@ export class IdentityService {
|
|||||||
private readonly uinByUid = new Map<string, number>();
|
private readonly uinByUid = new Map<string, number>();
|
||||||
private readonly uidByUin = new Map<number, string>();
|
private readonly uidByUin = new Map<number, string>();
|
||||||
private inTransaction = false;
|
private inTransaction = false;
|
||||||
|
private fetcher: IdentityFetcher | null = null;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly qqInfo: QQInfo,
|
private readonly qqInfo: QQInfo,
|
||||||
@@ -105,6 +117,38 @@ export class IdentityService {
|
|||||||
return this.db !== null;
|
return this.db !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setFetcher(fetcher: IdentityFetcher): void {
|
||||||
|
this.fetcher = fetcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UIN → UID with network fallback. Returns cached value on hit; otherwise
|
||||||
|
* invokes the registered fetcher (typically `bridge.fetchUserProfile`),
|
||||||
|
* which is expected to write the result back through `rememberUserProfile`
|
||||||
|
* before this method re-queries the cache. Throws if no UID can be
|
||||||
|
* obtained from any layer.
|
||||||
|
*/
|
||||||
|
async resolveUid(uin: number, groupId?: number): Promise<string> {
|
||||||
|
const normalized = normalizeUin(uin);
|
||||||
|
if (normalized === null) throw new Error(`invalid uin: ${uin}`);
|
||||||
|
|
||||||
|
const cached = this.findUidByUin(normalized, groupId);
|
||||||
|
if (cached) return cached;
|
||||||
|
|
||||||
|
if (groupId !== undefined && this.fetcher?.fetchGroupMemberList) {
|
||||||
|
try { await this.fetcher.fetchGroupMemberList(groupId); } catch { /* ignore */ }
|
||||||
|
const afterMembers = this.findUidByUin(normalized, groupId);
|
||||||
|
if (afterMembers) return afterMembers;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.fetcher) throw new Error(`failed to resolve UID for UIN ${normalized}: no fetcher`);
|
||||||
|
|
||||||
|
const profile = await this.fetcher.fetchProfile(normalized);
|
||||||
|
if (profile.uid) return profile.uid;
|
||||||
|
|
||||||
|
throw new Error(`failed to resolve UID for UIN ${normalized}`);
|
||||||
|
}
|
||||||
|
|
||||||
rememberFriends(friends: FriendInfo[]): void {
|
rememberFriends(friends: FriendInfo[]): void {
|
||||||
this.qqInfo.setFriends(friends);
|
this.qqInfo.setFriends(friends);
|
||||||
for (const friend of friends) this.rememberUidUin(friend.uid, friend.uin);
|
for (const friend of friends) this.rememberUidUin(friend.uid, friend.uin);
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ export interface MockBridge {
|
|||||||
fetchFriendList: ReturnType<typeof vi.fn>;
|
fetchFriendList: ReturnType<typeof vi.fn>;
|
||||||
fetchGroupMemberList: ReturnType<typeof vi.fn>;
|
fetchGroupMemberList: ReturnType<typeof vi.fn>;
|
||||||
fetchUserProfile: ReturnType<typeof vi.fn>;
|
fetchUserProfile: ReturnType<typeof vi.fn>;
|
||||||
|
resolveUserUid: ReturnType<typeof vi.fn>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mockBridge(overrides: Partial<MockBridge> = {}): MockBridge {
|
export function mockBridge(overrides: Partial<MockBridge> = {}): MockBridge {
|
||||||
@@ -50,6 +51,7 @@ export function mockBridge(overrides: Partial<MockBridge> = {}): MockBridge {
|
|||||||
fetchFriendList: vi.fn(async () => []),
|
fetchFriendList: vi.fn(async () => []),
|
||||||
fetchGroupMemberList: vi.fn(async () => []),
|
fetchGroupMemberList: vi.fn(async () => []),
|
||||||
fetchUserProfile: vi.fn(async () => ({ uid: 'profile-uid' })),
|
fetchUserProfile: vi.fn(async () => ({ uid: 'profile-uid' })),
|
||||||
|
resolveUserUid: vi.fn(async () => 'resolved-uid'),
|
||||||
...overrides,
|
...overrides,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,6 @@ import { describe, it, expect, vi } from 'vitest';
|
|||||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||||
sendOidbAndCheck: vi.fn(async () => undefined),
|
sendOidbAndCheck: vi.fn(async () => undefined),
|
||||||
sendOidbAndDecode: vi.fn(async () => ({})),
|
sendOidbAndDecode: vi.fn(async () => ({})),
|
||||||
resolveUserUid: vi.fn(async () => 'resolved-uid'),
|
|
||||||
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|||||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||||
sendOidbAndCheck: vi.fn(async () => undefined),
|
sendOidbAndCheck: vi.fn(async () => undefined),
|
||||||
sendOidbAndDecode: vi.fn(async () => ({})),
|
sendOidbAndDecode: vi.fn(async () => ({})),
|
||||||
resolveUserUid: vi.fn(async () => 'resolved-uid'),
|
|
||||||
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -14,13 +13,12 @@ import { mockBridge } from './_helpers';
|
|||||||
describe('actions/friend', () => {
|
describe('actions/friend', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
||||||
vi.mocked(oidb.resolveUserUid).mockClear();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('setFriendAddRequest: numeric input is treated as UIN and resolved', async () => {
|
it('setFriendAddRequest: numeric input is treated as UIN and resolved', async () => {
|
||||||
const bridge = mockBridge();
|
const bridge = mockBridge();
|
||||||
await friend.setFriendAddRequest(bridge as any, '10001', true);
|
await friend.setFriendAddRequest(bridge as any, '10001', true);
|
||||||
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 10001);
|
expect(bridge.resolveUserUid).toHaveBeenCalledWith(10001);
|
||||||
const payload: any = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4];
|
const payload: any = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4];
|
||||||
expect(payload).toMatchObject({ accept: 3, targetUid: 'resolved-uid' });
|
expect(payload).toMatchObject({ accept: 3, targetUid: 'resolved-uid' });
|
||||||
});
|
});
|
||||||
@@ -28,7 +26,7 @@ describe('actions/friend', () => {
|
|||||||
it('setFriendAddRequest: non-numeric flag is forwarded as-is', async () => {
|
it('setFriendAddRequest: non-numeric flag is forwarded as-is', async () => {
|
||||||
const bridge = mockBridge();
|
const bridge = mockBridge();
|
||||||
await friend.setFriendAddRequest(bridge as any, 'flag-abc', false);
|
await friend.setFriendAddRequest(bridge as any, 'flag-abc', false);
|
||||||
expect(oidb.resolveUserUid).not.toHaveBeenCalled();
|
expect(bridge.resolveUserUid).not.toHaveBeenCalled();
|
||||||
const payload: any = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4];
|
const payload: any = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4];
|
||||||
expect(payload).toMatchObject({ accept: 5, targetUid: 'flag-abc' });
|
expect(payload).toMatchObject({ accept: 5, targetUid: 'flag-abc' });
|
||||||
});
|
});
|
||||||
@@ -36,7 +34,7 @@ describe('actions/friend', () => {
|
|||||||
it('deleteFriend resolves UID, calls 0x126b_0, and triggers a friend-list refresh', async () => {
|
it('deleteFriend resolves UID, calls 0x126b_0, and triggers a friend-list refresh', async () => {
|
||||||
const bridge = mockBridge();
|
const bridge = mockBridge();
|
||||||
await friend.deleteFriend(bridge as any, 10001, true);
|
await friend.deleteFriend(bridge as any, 10001, true);
|
||||||
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 10001);
|
expect(bridge.resolveUserUid).toHaveBeenCalledWith(10001);
|
||||||
const call = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]!;
|
const call = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]!;
|
||||||
expect(call[1]).toBe('OidbSvcTrpcTcp.0x126b_0');
|
expect(call[1]).toBe('OidbSvcTrpcTcp.0x126b_0');
|
||||||
expect((call[4] as any).field1.block).toBe(true);
|
expect((call[4] as any).field1.block).toBe(true);
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|||||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||||
sendOidbAndCheck: vi.fn(async () => undefined),
|
sendOidbAndCheck: vi.fn(async () => undefined),
|
||||||
sendOidbAndDecode: vi.fn(async () => ({})),
|
sendOidbAndDecode: vi.fn(async () => ({})),
|
||||||
resolveUserUid: vi.fn(async () => 'resolved-uid'),
|
|
||||||
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -17,13 +16,12 @@ describe('actions/group-admin', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
||||||
vi.mocked(oidb.sendOidbAndDecode).mockClear();
|
vi.mocked(oidb.sendOidbAndDecode).mockClear();
|
||||||
vi.mocked(oidb.resolveUserUid).mockClear();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('muteGroupMember resolves UID and dispatches 0x1253_1', async () => {
|
it('muteGroupMember resolves UID and dispatches 0x1253_1', async () => {
|
||||||
const bridge = mockBridge();
|
const bridge = mockBridge();
|
||||||
await admin.muteGroupMember(bridge as any, 12345, 67890, 600);
|
await admin.muteGroupMember(bridge as any, 12345, 67890, 600);
|
||||||
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 67890, 12345);
|
expect(bridge.resolveUserUid).toHaveBeenCalledWith(67890, 12345);
|
||||||
expect(oidb.sendOidbAndCheck).toHaveBeenCalledOnce();
|
expect(oidb.sendOidbAndCheck).toHaveBeenCalledOnce();
|
||||||
const args = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]!;
|
const args = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]!;
|
||||||
expect(args[1]).toBe('OidbSvcTrpcTcp.0x1253_1');
|
expect(args[1]).toBe('OidbSvcTrpcTcp.0x1253_1');
|
||||||
@@ -53,7 +51,7 @@ describe('actions/group-admin', () => {
|
|||||||
it('kickGroupMember resolves UID per-group and forwards reject + reason', async () => {
|
it('kickGroupMember resolves UID per-group and forwards reject + reason', async () => {
|
||||||
const bridge = mockBridge();
|
const bridge = mockBridge();
|
||||||
await admin.kickGroupMember(bridge as any, 12345, 67890, true, 'bad behaviour');
|
await admin.kickGroupMember(bridge as any, 12345, 67890, true, 'bad behaviour');
|
||||||
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 67890, 12345);
|
expect(bridge.resolveUserUid).toHaveBeenCalledWith(67890, 12345);
|
||||||
expect(vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4]).toMatchObject({
|
expect(vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4]).toMatchObject({
|
||||||
groupUin: 12345,
|
groupUin: 12345,
|
||||||
targetUid: 'resolved-uid',
|
targetUid: 'resolved-uid',
|
||||||
@@ -64,12 +62,12 @@ describe('actions/group-admin', () => {
|
|||||||
|
|
||||||
it('kickGroupMembers resolves each UID in parallel', async () => {
|
it('kickGroupMembers resolves each UID in parallel', async () => {
|
||||||
const bridge = mockBridge();
|
const bridge = mockBridge();
|
||||||
vi.mocked(oidb.resolveUserUid)
|
vi.mocked(bridge.resolveUserUid)
|
||||||
.mockResolvedValueOnce('uid-a')
|
.mockResolvedValueOnce('uid-a')
|
||||||
.mockResolvedValueOnce('uid-b');
|
.mockResolvedValueOnce('uid-b');
|
||||||
|
|
||||||
await admin.kickGroupMembers(bridge as any, 12345, [11, 22], false);
|
await admin.kickGroupMembers(bridge as any, 12345, [11, 22], false);
|
||||||
expect(oidb.resolveUserUid).toHaveBeenCalledTimes(2);
|
expect(bridge.resolveUserUid).toHaveBeenCalledTimes(2);
|
||||||
const payload: any = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4];
|
const payload: any = vi.mocked(oidb.sendOidbAndCheck).mock.calls[0]![4];
|
||||||
expect(payload.targetUids).toEqual(['uid-a', 'uid-b']);
|
expect(payload.targetUids).toEqual(['uid-a', 'uid-b']);
|
||||||
expect(payload.rejectAddRequest).toBe(0);
|
expect(payload.rejectAddRequest).toBe(0);
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|||||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||||
sendOidbAndCheck: vi.fn(async () => undefined),
|
sendOidbAndCheck: vi.fn(async () => undefined),
|
||||||
sendOidbAndDecode: vi.fn(async () => ({})),
|
sendOidbAndDecode: vi.fn(async () => ({})),
|
||||||
resolveUserUid: vi.fn(async () => 'resolved-uid'),
|
|
||||||
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -30,7 +29,6 @@ describe('actions/group-file', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
||||||
vi.mocked(oidb.sendOidbAndDecode).mockClear();
|
vi.mocked(oidb.sendOidbAndDecode).mockClear();
|
||||||
vi.mocked(oidb.resolveUserUid).mockClear();
|
|
||||||
vi.mocked(highwayClient.fetchHighwaySession).mockClear();
|
vi.mocked(highwayClient.fetchHighwaySession).mockClear();
|
||||||
vi.mocked(highwayClient.uploadHighwayHttp).mockClear();
|
vi.mocked(highwayClient.uploadHighwayHttp).mockClear();
|
||||||
});
|
});
|
||||||
@@ -104,7 +102,7 @@ describe('actions/group-file', () => {
|
|||||||
const bridge = mockBridge({
|
const bridge = mockBridge({
|
||||||
qqInfo: { uin: '10001', selfUid: '', findGroupMember: vi.fn(() => null) },
|
qqInfo: { uin: '10001', selfUid: '', findGroupMember: vi.fn(() => null) },
|
||||||
});
|
});
|
||||||
vi.mocked(oidb.resolveUserUid)
|
vi.mocked(bridge.resolveUserUid)
|
||||||
.mockResolvedValueOnce('target-uid') // target user
|
.mockResolvedValueOnce('target-uid') // target user
|
||||||
.mockResolvedValueOnce('self-uid-resolved'); // self fallback
|
.mockResolvedValueOnce('self-uid-resolved'); // self fallback
|
||||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
||||||
@@ -112,7 +110,7 @@ describe('actions/group-file', () => {
|
|||||||
});
|
});
|
||||||
const out = await groupFile.uploadPrivateFile(bridge as any, 67890, '/path/file');
|
const out = await groupFile.uploadPrivateFile(bridge as any, 67890, '/path/file');
|
||||||
expect(out).toEqual({ fileId: 'fid', fileHash: 'hash' });
|
expect(out).toEqual({ fileId: 'fid', fileHash: 'hash' });
|
||||||
expect(oidb.resolveUserUid).toHaveBeenCalledTimes(2);
|
expect(bridge.resolveUserUid).toHaveBeenCalledTimes(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fetchGroupFiles paginates files + folders out of OIDB items', async () => {
|
it('fetchGroupFiles paginates files + folders out of OIDB items', async () => {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|||||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||||
sendOidbAndCheck: vi.fn(async () => undefined),
|
sendOidbAndCheck: vi.fn(async () => undefined),
|
||||||
sendOidbAndDecode: vi.fn(async () => ({})),
|
sendOidbAndDecode: vi.fn(async () => ({})),
|
||||||
resolveUserUid: vi.fn(async () => 'resolved-uid'),
|
|
||||||
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -14,7 +13,6 @@ import { mockBridge } from './_helpers';
|
|||||||
describe('actions/group-message', () => {
|
describe('actions/group-message', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
||||||
vi.mocked(oidb.resolveUserUid).mockClear();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('recallGroupMessage sends to SsoGroupRecallMsg with the group sequence', async () => {
|
it('recallGroupMessage sends to SsoGroupRecallMsg with the group sequence', async () => {
|
||||||
@@ -42,7 +40,7 @@ describe('actions/group-message', () => {
|
|||||||
it('recallPrivateMessage resolves the target UID before dispatch', async () => {
|
it('recallPrivateMessage resolves the target UID before dispatch', async () => {
|
||||||
const bridge = mockBridge();
|
const bridge = mockBridge();
|
||||||
await msg.recallPrivateMessage(bridge as any, 10001, 100, 200, 123, 1700000000);
|
await msg.recallPrivateMessage(bridge as any, 10001, 100, 200, 123, 1700000000);
|
||||||
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 10001);
|
expect(bridge.resolveUserUid).toHaveBeenCalledWith(10001);
|
||||||
const [serviceCmd] = bridge.sendRawPacket.mock.calls[0]!;
|
const [serviceCmd] = bridge.sendRawPacket.mock.calls[0]!;
|
||||||
expect(serviceCmd).toBe('trpc.msg.msg_svc.MsgService.SsoC2CRecallMsg');
|
expect(serviceCmd).toBe('trpc.msg.msg_svc.MsgService.SsoC2CRecallMsg');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|||||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||||
sendOidbAndCheck: vi.fn(async () => undefined),
|
sendOidbAndCheck: vi.fn(async () => undefined),
|
||||||
sendOidbAndDecode: vi.fn(async () => ({})),
|
sendOidbAndDecode: vi.fn(async () => ({})),
|
||||||
resolveUserUid: vi.fn(async () => 'resolved-uid'),
|
|
||||||
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|||||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||||
sendOidbAndCheck: vi.fn(async () => undefined),
|
sendOidbAndCheck: vi.fn(async () => undefined),
|
||||||
sendOidbAndDecode: vi.fn(async () => ({})),
|
sendOidbAndDecode: vi.fn(async () => ({})),
|
||||||
resolveUserUid: vi.fn(async () => 'resolved-uid'),
|
|
||||||
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|||||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||||
sendOidbAndCheck: vi.fn(async () => undefined),
|
sendOidbAndCheck: vi.fn(async () => undefined),
|
||||||
sendOidbAndDecode: vi.fn(async () => ({})),
|
sendOidbAndDecode: vi.fn(async () => ({})),
|
||||||
resolveUserUid: vi.fn(async () => 'resolved-uid'),
|
|
||||||
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -27,7 +26,6 @@ describe('actions/profile', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
||||||
vi.mocked(oidb.sendOidbAndDecode).mockClear();
|
vi.mocked(oidb.sendOidbAndDecode).mockClear();
|
||||||
vi.mocked(oidb.resolveUserUid).mockClear();
|
|
||||||
vi.mocked(highwayClient.fetchHighwaySession).mockClear();
|
vi.mocked(highwayClient.fetchHighwaySession).mockClear();
|
||||||
vi.mocked(highwayClient.uploadHighwayHttp).mockClear();
|
vi.mocked(highwayClient.uploadHighwayHttp).mockClear();
|
||||||
});
|
});
|
||||||
@@ -63,7 +61,7 @@ describe('actions/profile', () => {
|
|||||||
it('setInputStatus resolves UID first and sends 0xcd4_1', async () => {
|
it('setInputStatus resolves UID first and sends 0xcd4_1', async () => {
|
||||||
const bridge = mockBridge();
|
const bridge = mockBridge();
|
||||||
await profile.setInputStatus(bridge as any, 10001, 1);
|
await profile.setInputStatus(bridge as any, 10001, 1);
|
||||||
expect(oidb.resolveUserUid).toHaveBeenCalledWith(bridge, 10001);
|
expect(bridge.resolveUserUid).toHaveBeenCalledWith(10001);
|
||||||
const call = vi.mocked(oidb.sendOidbAndDecode).mock.calls[0]!;
|
const call = vi.mocked(oidb.sendOidbAndDecode).mock.calls[0]!;
|
||||||
expect(call[1]).toBe('OidbSvcTrpcTcp.0xcd4_1');
|
expect(call[1]).toBe('OidbSvcTrpcTcp.0xcd4_1');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { afterEach, describe, expect, it } from 'vitest';
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { IdentityService } from '../src/bridge/identity-service';
|
import { IdentityService } from '../src/bridge/identity-service';
|
||||||
import { QQInfo, type GroupMemberInfo, type QQGroupInfo } from '../src/bridge/qq-info';
|
import { QQInfo, type GroupMemberInfo, type QQGroupInfo, type UserProfileInfo } from '../src/bridge/qq-info';
|
||||||
|
|
||||||
const SELF_UIN = '10001';
|
const SELF_UIN = '10001';
|
||||||
const GROUP_ID = 123456789;
|
const GROUP_ID = 123456789;
|
||||||
@@ -195,3 +195,73 @@ describe('IdentityService', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('IdentityService.resolveUid', () => {
|
||||||
|
function makeProfile(uin: number, uid: string): UserProfileInfo {
|
||||||
|
return { uin, uid, nickname: '', remark: '', qid: '', sex: 'unknown', age: 0, sign: '', avatar: '' };
|
||||||
|
}
|
||||||
|
|
||||||
|
it('returns the cached uid without invoking the fetcher', async () => {
|
||||||
|
const qqInfo = new QQInfo(SELF_UIN);
|
||||||
|
const identity = IdentityService.memory(qqInfo);
|
||||||
|
identity.rememberRequestIdentity({ uid: 'u_known', uin: 12345 });
|
||||||
|
const fetchProfile = vi.fn(async (uin: number) => makeProfile(uin, 'should-not-be-used'));
|
||||||
|
identity.setFetcher({ fetchProfile });
|
||||||
|
|
||||||
|
await expect(identity.resolveUid(12345)).resolves.toBe('u_known');
|
||||||
|
expect(fetchProfile).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to fetchProfile on miss and returns the resolved uid', async () => {
|
||||||
|
const qqInfo = new QQInfo(SELF_UIN);
|
||||||
|
const identity = IdentityService.memory(qqInfo);
|
||||||
|
const fetchProfile = vi.fn(async (uin: number) => {
|
||||||
|
// Simulate the bridge writing the result back via rememberUserProfile.
|
||||||
|
identity.rememberUserProfile(makeProfile(uin, 'u_fetched'));
|
||||||
|
return makeProfile(uin, 'u_fetched');
|
||||||
|
});
|
||||||
|
identity.setFetcher({ fetchProfile });
|
||||||
|
|
||||||
|
await expect(identity.resolveUid(99999)).resolves.toBe('u_fetched');
|
||||||
|
expect(fetchProfile).toHaveBeenCalledWith(99999);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws when neither cache nor fetcher can produce a uid', async () => {
|
||||||
|
const qqInfo = new QQInfo(SELF_UIN);
|
||||||
|
const identity = IdentityService.memory(qqInfo);
|
||||||
|
identity.setFetcher({ fetchProfile: async (uin) => makeProfile(uin, '') });
|
||||||
|
|
||||||
|
await expect(identity.resolveUid(99999)).rejects.toThrow(/failed to resolve UID/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('tries fetchGroupMemberList before fetchProfile when groupId is provided', async () => {
|
||||||
|
const qqInfo = new QQInfo(SELF_UIN);
|
||||||
|
const identity = IdentityService.memory(qqInfo);
|
||||||
|
identity.rememberGroups([{
|
||||||
|
groupId: GROUP_ID, groupName: '', remark: '',
|
||||||
|
memberCount: 0, memberMax: 0, members: new Map(),
|
||||||
|
}]);
|
||||||
|
|
||||||
|
const fetchProfile = vi.fn(async (uin: number) => makeProfile(uin, 'u_via_profile'));
|
||||||
|
const fetchGroupMemberList = vi.fn(async (groupId: number) => {
|
||||||
|
// Roster fetch should populate the cache.
|
||||||
|
identity.rememberGroupMembers(groupId, [makeMember(77777, 'u_via_roster')]);
|
||||||
|
return [];
|
||||||
|
});
|
||||||
|
identity.setFetcher({ fetchProfile, fetchGroupMemberList });
|
||||||
|
|
||||||
|
await expect(identity.resolveUid(77777, GROUP_ID)).resolves.toBe('u_via_roster');
|
||||||
|
expect(fetchGroupMemberList).toHaveBeenCalledWith(GROUP_ID);
|
||||||
|
expect(fetchProfile).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws on invalid (zero / negative) uin without calling fetcher', async () => {
|
||||||
|
const qqInfo = new QQInfo(SELF_UIN);
|
||||||
|
const identity = IdentityService.memory(qqInfo);
|
||||||
|
const fetchProfile = vi.fn();
|
||||||
|
identity.setFetcher({ fetchProfile });
|
||||||
|
|
||||||
|
await expect(identity.resolveUid(0)).rejects.toThrow(/invalid uin/);
|
||||||
|
expect(fetchProfile).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -5,7 +5,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|||||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||||
sendOidbAndDecode: vi.fn(async () => ({})),
|
sendOidbAndDecode: vi.fn(async () => ({})),
|
||||||
sendOidbAndCheck: vi.fn(async () => undefined),
|
sendOidbAndCheck: vi.fn(async () => undefined),
|
||||||
resolveUserUid: vi.fn(async () => 'uid'),
|
|
||||||
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user