|
| 1 | +/* |
| 2 | +Copyright 2023 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { logger } from "matrix-js-sdk/src/logger"; |
| 18 | +import { IMatrixProfile, MatrixClient, MatrixEvent, RoomMember, RoomMemberEvent } from "matrix-js-sdk/src/matrix"; |
| 19 | + |
| 20 | +import { LruCache } from "../utils/LruCache"; |
| 21 | + |
| 22 | +const cacheSize = 500; |
| 23 | + |
| 24 | +type StoreProfileValue = IMatrixProfile | undefined | null; |
| 25 | + |
| 26 | +/** |
| 27 | + * This store provides cached access to user profiles. |
| 28 | + * Listens for membership events and invalidates the cache for a profile on update with different profile values. |
| 29 | + */ |
| 30 | +export class UserProfilesStore { |
| 31 | + private profiles = new LruCache<string, IMatrixProfile | null>(cacheSize); |
| 32 | + private knownProfiles = new LruCache<string, IMatrixProfile | null>(cacheSize); |
| 33 | + |
| 34 | + public constructor(private client: MatrixClient) { |
| 35 | + client.on(RoomMemberEvent.Membership, this.onRoomMembershipEvent); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Synchronously get a profile from the store cache. |
| 40 | + * |
| 41 | + * @param userId - User Id of the profile to fetch |
| 42 | + * @returns The profile, if cached by the store. |
| 43 | + * Null if the profile does not exist. |
| 44 | + * Undefined if the profile is not cached by the store. |
| 45 | + * In this case a profile can be fetched from the API via {@link fetchProfile}. |
| 46 | + */ |
| 47 | + public getProfile(userId: string): StoreProfileValue { |
| 48 | + return this.profiles.get(userId); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Synchronously get a profile from known users from the store cache. |
| 53 | + * Known user means that at least one shared room with the user exists. |
| 54 | + * |
| 55 | + * @param userId - User Id of the profile to fetch |
| 56 | + * @returns The profile, if cached by the store. |
| 57 | + * Null if the profile does not exist. |
| 58 | + * Undefined if the profile is not cached by the store. |
| 59 | + * In this case a profile can be fetched from the API via {@link fetchOnlyKnownProfile}. |
| 60 | + */ |
| 61 | + public getOnlyKnownProfile(userId: string): StoreProfileValue { |
| 62 | + return this.knownProfiles.get(userId); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Asynchronousely fetches a profile from the API. |
| 67 | + * Stores the result in the cache, so that next time {@link getProfile} returns this value. |
| 68 | + * |
| 69 | + * @param userId - User Id for which the profile should be fetched for |
| 70 | + * @returns The profile, if found. |
| 71 | + * Null if the profile does not exist or there was an error fetching it. |
| 72 | + */ |
| 73 | + public async fetchProfile(userId: string): Promise<IMatrixProfile | null> { |
| 74 | + const profile = await this.fetchProfileFromApi(userId); |
| 75 | + this.profiles.set(userId, profile); |
| 76 | + return profile; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Asynchronousely fetches a profile from a known user from the API. |
| 81 | + * Known user means that at least one shared room with the user exists. |
| 82 | + * Stores the result in the cache, so that next time {@link getOnlyKnownProfile} returns this value. |
| 83 | + * |
| 84 | + * @param userId - User Id for which the profile should be fetched for |
| 85 | + * @returns The profile, if found. |
| 86 | + * Undefined if the user is unknown. |
| 87 | + * Null if the profile does not exist or there was an error fetching it. |
| 88 | + */ |
| 89 | + public async fetchOnlyKnownProfile(userId: string): Promise<StoreProfileValue> { |
| 90 | + // Do not look up unknown users. The test for existence in knownProfiles is a performance optimisation. |
| 91 | + // If the user Id exists in knownProfiles we know them. |
| 92 | + if (!this.knownProfiles.has(userId) && !this.isUserIdKnown(userId)) return undefined; |
| 93 | + |
| 94 | + const profile = await this.fetchProfileFromApi(userId); |
| 95 | + this.knownProfiles.set(userId, profile); |
| 96 | + return profile; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Looks up a user profile via API. |
| 101 | + * |
| 102 | + * @param userId - User Id for which the profile should be fetched for |
| 103 | + * @returns The profile information or null on errors |
| 104 | + */ |
| 105 | + private async fetchProfileFromApi(userId: string): Promise<IMatrixProfile | null> { |
| 106 | + try { |
| 107 | + return (await this.client.getProfileInfo(userId)) ?? null; |
| 108 | + } catch (e) { |
| 109 | + logger.warn(`Error retrieving profile for userId ${userId}`, e); |
| 110 | + } |
| 111 | + |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Whether at least one shared room with the userId exists. |
| 117 | + * |
| 118 | + * @param userId |
| 119 | + * @returns true: at least one room shared with user identified by its Id, else false. |
| 120 | + */ |
| 121 | + private isUserIdKnown(userId: string): boolean { |
| 122 | + return this.client.getRooms().some((room) => { |
| 123 | + return !!room.getMember(userId); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Simple cache invalidation if a room membership event is received and |
| 129 | + * at least one profile value differs from the cached one. |
| 130 | + */ |
| 131 | + private onRoomMembershipEvent = (event: MatrixEvent, member: RoomMember): void => { |
| 132 | + const profile = this.profiles.get(member.userId); |
| 133 | + |
| 134 | + if ( |
| 135 | + profile && |
| 136 | + (profile.displayname !== member.rawDisplayName || profile.avatar_url !== member.getMxcAvatarUrl()) |
| 137 | + ) { |
| 138 | + this.profiles.delete(member.userId); |
| 139 | + } |
| 140 | + |
| 141 | + const knownProfile = this.knownProfiles.get(member.userId); |
| 142 | + |
| 143 | + if ( |
| 144 | + knownProfile && |
| 145 | + (knownProfile.displayname !== member.rawDisplayName || knownProfile.avatar_url !== member.getMxcAvatarUrl()) |
| 146 | + ) { |
| 147 | + this.knownProfiles.delete(member.userId); |
| 148 | + } |
| 149 | + }; |
| 150 | +} |
0 commit comments