Skip to content

Commit 7f6dd3b

Browse files
committed
refactor: Work on including more user info when getting the xpUser info
1 parent deb6845 commit 7f6dd3b

File tree

5 files changed

+397
-329
lines changed

5 files changed

+397
-329
lines changed

src/aux-records/AuthController.ts

Lines changed: 96 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,71 +2524,7 @@ export class AuthController {
25242524
errorMessage: 'The user was not found.',
25252525
};
25262526
}
2527-
2528-
const { hasActiveSubscription, subscriptionTier: tier } =
2529-
await this._getSubscriptionInfo(result);
2530-
2531-
let privacyFeatures: PrivacyFeatures;
2532-
let displayName: string = null;
2533-
let email: string = result.email;
2534-
let name: string = result.name;
2535-
const privoConfig = await this._config.getPrivoConfiguration();
2536-
if (privoConfig && result.privoServiceId) {
2537-
const userInfo = await this._privoClient.getUserInfo(
2538-
result.privoServiceId
2539-
);
2540-
privacyFeatures = getPrivacyFeaturesFromPermissions(
2541-
privoConfig.featureIds,
2542-
userInfo.permissions
2543-
);
2544-
displayName = userInfo.displayName;
2545-
email = userInfo.email;
2546-
name = userInfo.givenName;
2547-
2548-
if (
2549-
result.privacyFeatures?.publishData !==
2550-
privacyFeatures.publishData ||
2551-
result.privacyFeatures?.allowPublicData !==
2552-
privacyFeatures.allowPublicData ||
2553-
result.privacyFeatures?.allowAI !==
2554-
privacyFeatures.allowAI ||
2555-
result.privacyFeatures?.allowPublicInsts !==
2556-
privacyFeatures.allowPublicInsts
2557-
) {
2558-
await this._store.saveUser({
2559-
...result,
2560-
privacyFeatures: {
2561-
...privacyFeatures,
2562-
},
2563-
});
2564-
}
2565-
} else if (result.privacyFeatures) {
2566-
privacyFeatures = {
2567-
...result.privacyFeatures,
2568-
};
2569-
} else {
2570-
privacyFeatures = {
2571-
publishData: true,
2572-
allowPublicData: true,
2573-
allowAI: true,
2574-
allowPublicInsts: true,
2575-
};
2576-
}
2577-
2578-
return {
2579-
success: true,
2580-
userId: result.id,
2581-
name: name,
2582-
displayName,
2583-
email: email,
2584-
phoneNumber: result.phoneNumber,
2585-
avatarPortraitUrl: result.avatarPortraitUrl,
2586-
avatarUrl: result.avatarUrl,
2587-
hasActiveSubscription: hasActiveSubscription,
2588-
subscriptionTier: tier ?? null,
2589-
privacyFeatures: privacyFeatures,
2590-
role: result.role ?? 'none',
2591-
};
2527+
return await this.getPrivateInfoForUser(result);
25922528
} catch (err) {
25932529
const span = trace.getActiveSpan();
25942530
span?.recordException(err);
@@ -2606,6 +2542,90 @@ export class AuthController {
26062542
}
26072543
}
26082544

2545+
/**
2546+
* Gets the user info for the given auth user.
2547+
*
2548+
* Not for public use.
2549+
* @param user The user to get the info for.
2550+
* @returns
2551+
*/
2552+
async getPrivateInfoForUser(user: AuthUser): Promise<GetUserInfoSuccess> {
2553+
const { hasActiveSubscription, subscriptionTier: tier } =
2554+
await this._getSubscriptionInfo(user);
2555+
const { privacyFeatures, displayName, email, name } =
2556+
await this._getUserPrivoInfo(user);
2557+
2558+
return {
2559+
success: true,
2560+
userId: user.id,
2561+
name: name,
2562+
displayName,
2563+
email: email,
2564+
phoneNumber: user.phoneNumber,
2565+
avatarPortraitUrl: user.avatarPortraitUrl,
2566+
avatarUrl: user.avatarUrl,
2567+
hasActiveSubscription: hasActiveSubscription,
2568+
subscriptionTier: tier ?? null,
2569+
privacyFeatures: privacyFeatures,
2570+
role: user.role ?? 'none',
2571+
};
2572+
}
2573+
2574+
private async _getUserPrivoInfo(user: AuthUser) {
2575+
let privacyFeatures: PrivacyFeatures;
2576+
let displayName: string = null;
2577+
let email: string = user.email;
2578+
let name: string = user.name;
2579+
const privoConfig = await this._config.getPrivoConfiguration();
2580+
if (privoConfig && user.privoServiceId) {
2581+
const userInfo = await this._privoClient.getUserInfo(
2582+
user.privoServiceId
2583+
);
2584+
privacyFeatures = getPrivacyFeaturesFromPermissions(
2585+
privoConfig.featureIds,
2586+
userInfo.permissions
2587+
);
2588+
displayName = userInfo.displayName;
2589+
email = userInfo.email;
2590+
name = userInfo.givenName;
2591+
2592+
if (
2593+
user.privacyFeatures?.publishData !==
2594+
privacyFeatures.publishData ||
2595+
user.privacyFeatures?.allowPublicData !==
2596+
privacyFeatures.allowPublicData ||
2597+
user.privacyFeatures?.allowAI !== privacyFeatures.allowAI ||
2598+
user.privacyFeatures?.allowPublicInsts !==
2599+
privacyFeatures.allowPublicInsts
2600+
) {
2601+
await this._store.saveUser({
2602+
...user,
2603+
privacyFeatures: {
2604+
...privacyFeatures,
2605+
},
2606+
});
2607+
}
2608+
} else if (user.privacyFeatures) {
2609+
privacyFeatures = {
2610+
...user.privacyFeatures,
2611+
};
2612+
} else {
2613+
privacyFeatures = {
2614+
publishData: true,
2615+
allowPublicData: true,
2616+
allowAI: true,
2617+
allowPublicInsts: true,
2618+
};
2619+
}
2620+
2621+
return {
2622+
privacyFeatures,
2623+
displayName,
2624+
email,
2625+
name,
2626+
};
2627+
}
2628+
26092629
/**
26102630
* Gets the public information for a specific user.
26112631
* @param userId The ID of the user whose information is being requested.
@@ -2716,6 +2736,17 @@ export class AuthController {
27162736
};
27172737
}
27182738

2739+
/**
2740+
* Gets the subscription information for a user.
2741+
*
2742+
* Not for public use.
2743+
* @param user The user to get the subscription information for.
2744+
* @returns
2745+
*/
2746+
getUserSubscriptionInfo(user: AuthUser) {
2747+
return this._getSubscriptionInfo(user);
2748+
}
2749+
27192750
/**
27202751
* Attempts to update a user's metadata.
27212752
* @param request The request for the operation.

src/aux-records/MemoryStore.ts

Lines changed: 75 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,13 @@ import type {
164164
RecordsNotification,
165165
} from './SystemNotificationMessenger';
166166
import type { ModerationConfiguration } from './ModerationConfiguration';
167-
import type { XpContract, XpInvoice, XpStore, XpUser } from './XpStore';
167+
import type {
168+
XpContract,
169+
XpInvoice,
170+
XpStore,
171+
XpUser,
172+
XpUserWithUserInfo,
173+
} from './XpStore';
168174

169175
export interface MemoryConfiguration {
170176
subscriptions: SubscriptionConfiguration;
@@ -234,7 +240,7 @@ export class MemoryStore
234240

235241
private _loadedPackages: Map<string, LoadedPackage> = new Map();
236242

237-
private _xpUsers: Map<XpUser['id'], XpUser> = new Map();
243+
private _xpUsers: Map<string, XpUser> = new Map();
238244
private _xpContracts: Map<XpContract['id'], XpContract> = new Map();
239245
private _xpInvoices: Map<XpInvoice['id'], XpInvoice> = new Map();
240246

@@ -384,42 +390,41 @@ export class MemoryStore
384390
this.roleAssignments = {};
385391
}
386392

387-
async batchQueryXpUsers(
388-
queryOptions:
389-
| {
390-
xpId: XpUser['id'][];
391-
authId?: AuthUser['id'][];
392-
}
393-
| {
394-
authId: AuthUser['id'][];
395-
xpId?: XpUser['id'][];
396-
}
397-
): Promise<XpUser[]> {
398-
const users = [];
399-
if ('xpId' in queryOptions) {
400-
for (const id of queryOptions.xpId) {
401-
const user = this._xpUsers.get(id);
402-
if (user) {
403-
users.push(cloneDeep(user));
404-
}
405-
}
406-
}
407-
if ('authId' in queryOptions) {
408-
const xpUsers = Array.from(this._xpUsers.values());
409-
for (const id of queryOptions.authId) {
410-
const user = xpUsers.find((u: XpUser) => u.userId === id);
411-
if (user) {
412-
users.push(cloneDeep(user));
413-
}
414-
}
415-
}
416-
return users;
417-
}
393+
// async batchQueryXpUsers(
394+
// queryOptions:
395+
// | {
396+
// xpId: XpUser['id'][];
397+
// authId?: AuthUser['id'][];
398+
// }
399+
// | {
400+
// authId: AuthUser['id'][];
401+
// xpId?: XpUser['id'][];
402+
// }
403+
// ): Promise<XpUser[]> {
404+
// const users = [];
405+
// if ('xpId' in queryOptions) {
406+
// for (const id of queryOptions.xpId) {
407+
// const user = this._xpUsers.get(id);
408+
// if (user) {
409+
// users.push(cloneDeep(user));
410+
// }
411+
// }
412+
// }
413+
// if ('authId' in queryOptions) {
414+
// const xpUsers = Array.from(this._xpUsers.values());
415+
// for (const id of queryOptions.authId) {
416+
// const user = xpUsers.find((u: XpUser) => u.userId === id);
417+
// if (user) {
418+
// users.push(cloneDeep(user));
419+
// }
420+
// }
421+
// }
422+
// return users;
423+
// }
418424

419-
async saveXpUser(id: XpUser['id'], user: XpUser) {
425+
async saveXpUser(user: XpUser) {
420426
const u = cloneDeep(user);
421-
this._xpUsers.set(id, u);
422-
return u;
427+
this._xpUsers.set(u.xpId, u);
423428
}
424429

425430
async saveXpContract(contract: XpContract) {
@@ -449,15 +454,42 @@ export class MemoryStore
449454
return i;
450455
}
451456

452-
async getXpUserByAuthId(id: AuthUser['id']): Promise<XpUser> {
453-
const user = Array.from(this._xpUsers.values()).find(
454-
(u: XpUser) => u.userId === id
455-
);
456-
return cloneDeepNull(user ?? undefined);
457+
async getXpUserByUserId(id: AuthUser['id']): Promise<XpUserWithUserInfo> {
458+
const xpUser = this._xpUsers.get(id);
459+
if (!xpUser) {
460+
return null;
461+
}
462+
463+
const authUser = this._users.find((u) => u.id === xpUser.userId);
464+
465+
if (!authUser) {
466+
console.warn('No auth user found for xp user', xpUser);
467+
return null;
468+
}
469+
470+
return {
471+
...xpUser,
472+
...authUser,
473+
};
457474
}
458475

459-
async getXpUserById(id: XpUser['id']): Promise<XpUser> {
460-
return cloneDeep(this._xpUsers.get(id) ?? undefined);
476+
async getXpUserByXpId(id: string): Promise<XpUserWithUserInfo> {
477+
const xpUser = this._xpUsers.get(id);
478+
if (!xpUser) {
479+
return null;
480+
}
481+
482+
const authUser = this._users.find((u) => u.id === xpUser.userId);
483+
484+
if (!authUser) {
485+
console.warn('No auth user found for xp user', xpUser);
486+
return null;
487+
}
488+
489+
return {
490+
...xpUser,
491+
...authUser,
492+
};
461493
}
462494

463495
async getXpContract(contractId: XpContract['id']): Promise<XpContract> {

0 commit comments

Comments
 (0)