Check profiles before starting a DM (#10472)
This commit is contained in:
parent
78e03e0617
commit
df89d2ce28
7 changed files with 438 additions and 72 deletions
|
@ -15,7 +15,14 @@ limitations under the License.
|
|||
*/
|
||||
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
import { IMatrixProfile, MatrixClient, MatrixEvent, RoomMember, RoomMemberEvent } from "matrix-js-sdk/src/matrix";
|
||||
import {
|
||||
IMatrixProfile,
|
||||
MatrixClient,
|
||||
MatrixError,
|
||||
MatrixEvent,
|
||||
RoomMember,
|
||||
RoomMemberEvent,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import { LruCache } from "../utils/LruCache";
|
||||
|
||||
|
@ -23,12 +30,18 @@ const cacheSize = 500;
|
|||
|
||||
type StoreProfileValue = IMatrixProfile | undefined | null;
|
||||
|
||||
interface GetOptions {
|
||||
/** Whether calling the function shouuld raise an Error. */
|
||||
shouldThrow: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* This store provides cached access to user profiles.
|
||||
* Listens for membership events and invalidates the cache for a profile on update with different profile values.
|
||||
*/
|
||||
export class UserProfilesStore {
|
||||
private profiles = new LruCache<string, IMatrixProfile | null>(cacheSize);
|
||||
private profileLookupErrors = new LruCache<string, MatrixError>(cacheSize);
|
||||
private knownProfiles = new LruCache<string, IMatrixProfile | null>(cacheSize);
|
||||
|
||||
public constructor(private client: MatrixClient) {
|
||||
|
@ -48,6 +61,32 @@ export class UserProfilesStore {
|
|||
return this.profiles.get(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Async shortcut function that returns the profile from cache or
|
||||
* or fetches it on cache miss.
|
||||
*
|
||||
* @param userId - User Id of the profile to get or fetch
|
||||
* @returns The profile, if cached by the store or fetched from the API.
|
||||
* Null if the profile does not exist or an error occurred during fetch.
|
||||
*/
|
||||
public async getOrFetchProfile(userId: string, options?: GetOptions): Promise<IMatrixProfile | null> {
|
||||
const cachedProfile = this.profiles.get(userId);
|
||||
|
||||
if (cachedProfile) return cachedProfile;
|
||||
|
||||
return this.fetchProfile(userId, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a profile lookup error.
|
||||
*
|
||||
* @param userId - User Id for which to get the lookup error
|
||||
* @returns The lookup error or undefined if there was no error or the profile was not fetched.
|
||||
*/
|
||||
public getProfileLookupError(userId: string): MatrixError | undefined {
|
||||
return this.profileLookupErrors.get(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously get a profile from known users from the store cache.
|
||||
* Known user means that at least one shared room with the user exists.
|
||||
|
@ -70,8 +109,8 @@ export class UserProfilesStore {
|
|||
* @returns The profile, if found.
|
||||
* Null if the profile does not exist or there was an error fetching it.
|
||||
*/
|
||||
public async fetchProfile(userId: string): Promise<IMatrixProfile | null> {
|
||||
const profile = await this.fetchProfileFromApi(userId);
|
||||
public async fetchProfile(userId: string, options?: GetOptions): Promise<IMatrixProfile | null> {
|
||||
const profile = await this.fetchProfileFromApi(userId, options);
|
||||
this.profiles.set(userId, profile);
|
||||
return profile;
|
||||
}
|
||||
|
@ -96,17 +135,34 @@ export class UserProfilesStore {
|
|||
return profile;
|
||||
}
|
||||
|
||||
public flush(): void {
|
||||
this.profiles = new LruCache<string, IMatrixProfile | null>(cacheSize);
|
||||
this.profileLookupErrors = new LruCache<string, MatrixError>(cacheSize);
|
||||
this.knownProfiles = new LruCache<string, IMatrixProfile | null>(cacheSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up a user profile via API.
|
||||
*
|
||||
* @param userId - User Id for which the profile should be fetched for
|
||||
* @returns The profile information or null on errors
|
||||
*/
|
||||
private async fetchProfileFromApi(userId: string): Promise<IMatrixProfile | null> {
|
||||
private async fetchProfileFromApi(userId: string, options?: GetOptions): Promise<IMatrixProfile | null> {
|
||||
// invalidate cached profile errors
|
||||
this.profileLookupErrors.delete(userId);
|
||||
|
||||
try {
|
||||
return (await this.client.getProfileInfo(userId)) ?? null;
|
||||
} catch (e) {
|
||||
logger.warn(`Error retrieving profile for userId ${userId}`, e);
|
||||
|
||||
if (e instanceof MatrixError) {
|
||||
this.profileLookupErrors.set(userId, e);
|
||||
}
|
||||
|
||||
if (options?.shouldThrow) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue