2020-06-24 02:59:26 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2022-02-22 12:18:08 +00:00
|
|
|
import { User, UserEvent } from "matrix-js-sdk/src/models/user";
|
|
|
|
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
|
2021-08-23 17:26:57 +00:00
|
|
|
import { throttle } from "lodash";
|
2022-02-24 14:39:25 +00:00
|
|
|
import { EventType } from "matrix-js-sdk/src/@types/event";
|
2021-10-22 22:23:32 +00:00
|
|
|
|
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
|
|
|
import { AsyncStoreWithClient } from "./AsyncStoreWithClient";
|
|
|
|
import defaultDispatcher from "../dispatcher/dispatcher";
|
2020-06-24 02:59:26 +00:00
|
|
|
import { MatrixClientPeg } from "../MatrixClientPeg";
|
|
|
|
import { _t } from "../languageHandler";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { mediaFromMxc } from "../customisations/Media";
|
2020-06-24 02:59:26 +00:00
|
|
|
|
|
|
|
interface IState {
|
|
|
|
displayName?: string;
|
|
|
|
avatarUrl?: string;
|
2022-01-17 13:47:07 +00:00
|
|
|
fetchedAt?: number;
|
2020-06-24 02:59:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 19:09:43 +00:00
|
|
|
const KEY_DISPLAY_NAME = "mx_profile_displayname";
|
|
|
|
const KEY_AVATAR_URL = "mx_profile_avatar_url";
|
|
|
|
|
2020-06-24 02:59:26 +00:00
|
|
|
export class OwnProfileStore extends AsyncStoreWithClient<IState> {
|
2022-08-30 19:13:39 +00:00
|
|
|
private static readonly internalInstance = (() => {
|
|
|
|
const instance = new OwnProfileStore();
|
|
|
|
instance.start();
|
|
|
|
return instance;
|
|
|
|
})();
|
2020-06-24 02:59:26 +00:00
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
private monitoredUser: User | null;
|
2020-06-24 02:59:26 +00:00
|
|
|
|
|
|
|
private constructor() {
|
2021-03-17 19:09:43 +00:00
|
|
|
// seed from localstorage because otherwise we won't get these values until a whole network
|
|
|
|
// round-trip after the client is ready, and we often load widgets in that time, and we'd
|
|
|
|
// and up passing them an incorrect display name
|
|
|
|
super(defaultDispatcher, {
|
2023-02-03 15:27:47 +00:00
|
|
|
displayName: window.localStorage.getItem(KEY_DISPLAY_NAME) || undefined,
|
|
|
|
avatarUrl: window.localStorage.getItem(KEY_AVATAR_URL) || undefined,
|
2021-03-17 19:09:43 +00:00
|
|
|
});
|
2020-06-24 02:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static get instance(): OwnProfileStore {
|
|
|
|
return OwnProfileStore.internalInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the display name for the user, or null if not present.
|
|
|
|
*/
|
2023-02-15 13:36:22 +00:00
|
|
|
public get displayName(): string | null {
|
2020-06-24 02:59:26 +00:00
|
|
|
if (!this.matrixClient) return this.state.displayName || null;
|
|
|
|
|
|
|
|
if (this.matrixClient.isGuest()) {
|
|
|
|
return _t("Guest");
|
|
|
|
} else if (this.state.displayName) {
|
|
|
|
return this.state.displayName;
|
|
|
|
} else {
|
|
|
|
return this.matrixClient.getUserId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 13:47:07 +00:00
|
|
|
public get isProfileInfoFetched(): boolean {
|
|
|
|
return !!this.state.fetchedAt;
|
|
|
|
}
|
|
|
|
|
2020-06-24 02:59:26 +00:00
|
|
|
/**
|
|
|
|
* Gets the MXC URI of the user's avatar, or null if not present.
|
|
|
|
*/
|
2023-02-15 13:36:22 +00:00
|
|
|
public get avatarMxc(): string | null {
|
2020-06-24 02:59:26 +00:00
|
|
|
return this.state.avatarUrl || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user's avatar as an HTTP URL of the given size. If the user's
|
|
|
|
* avatar is not present, this returns null.
|
2020-09-29 20:14:51 +00:00
|
|
|
* @param size The size of the avatar. If zero, a full res copy of the avatar
|
|
|
|
* will be returned as an HTTP URL.
|
2020-06-24 02:59:26 +00:00
|
|
|
* @returns The HTTP URL of the user's avatar
|
|
|
|
*/
|
2023-02-15 13:36:22 +00:00
|
|
|
public getHttpAvatarUrl(size = 0): string | null {
|
2020-06-24 02:59:26 +00:00
|
|
|
if (!this.avatarMxc) return null;
|
2021-03-04 02:06:46 +00:00
|
|
|
const media = mediaFromMxc(this.avatarMxc);
|
|
|
|
if (!size || size <= 0) {
|
|
|
|
return media.srcHttp;
|
|
|
|
} else {
|
|
|
|
return media.getSquareThumbnailHttp(size);
|
|
|
|
}
|
2020-06-24 02:59:26 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
protected async onNotReady(): Promise<void> {
|
2020-06-24 02:59:26 +00:00
|
|
|
if (this.monitoredUser) {
|
2022-02-22 12:18:08 +00:00
|
|
|
this.monitoredUser.removeListener(UserEvent.DisplayName, this.onProfileUpdate);
|
|
|
|
this.monitoredUser.removeListener(UserEvent.AvatarUrl, this.onProfileUpdate);
|
2020-06-24 02:59:26 +00:00
|
|
|
}
|
2022-02-24 14:39:25 +00:00
|
|
|
this.matrixClient?.removeListener(RoomStateEvent.Events, this.onStateEvents);
|
2020-06-24 02:59:26 +00:00
|
|
|
await this.reset({});
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
protected async onReady(): Promise<void> {
|
2023-02-15 13:36:22 +00:00
|
|
|
const myUserId = this.matrixClient.getUserId()!;
|
2020-06-24 02:59:26 +00:00
|
|
|
this.monitoredUser = this.matrixClient.getUser(myUserId);
|
|
|
|
if (this.monitoredUser) {
|
2022-02-22 12:18:08 +00:00
|
|
|
this.monitoredUser.on(UserEvent.DisplayName, this.onProfileUpdate);
|
|
|
|
this.monitoredUser.on(UserEvent.AvatarUrl, this.onProfileUpdate);
|
2020-06-24 02:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We also have to listen for membership events for ourselves as the above User events
|
|
|
|
// are fired only with presence, which matrix.org (and many others) has disabled.
|
2022-02-22 12:18:08 +00:00
|
|
|
this.matrixClient.on(RoomStateEvent.Events, this.onStateEvents);
|
2020-06-24 02:59:26 +00:00
|
|
|
|
|
|
|
await this.onProfileUpdate(); // trigger an initial update
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
protected async onAction(payload: ActionPayload): Promise<void> {
|
2020-06-24 02:59:26 +00:00
|
|
|
// we don't actually do anything here
|
|
|
|
}
|
|
|
|
|
2022-02-24 14:39:25 +00:00
|
|
|
private onProfileUpdate = throttle(
|
2023-01-12 13:25:14 +00:00
|
|
|
async (): Promise<void> => {
|
2020-06-24 02:59:26 +00:00
|
|
|
// We specifically do not use the User object we stored for profile info as it
|
|
|
|
// could easily be wrong (such as per-room instead of global profile).
|
2023-02-15 13:36:22 +00:00
|
|
|
const profileInfo = await this.matrixClient.getProfileInfo(this.matrixClient.getUserId()!);
|
2021-03-19 11:29:14 +00:00
|
|
|
if (profileInfo.displayname) {
|
|
|
|
window.localStorage.setItem(KEY_DISPLAY_NAME, profileInfo.displayname);
|
|
|
|
} else {
|
|
|
|
window.localStorage.removeItem(KEY_DISPLAY_NAME);
|
|
|
|
}
|
|
|
|
if (profileInfo.avatar_url) {
|
|
|
|
window.localStorage.setItem(KEY_AVATAR_URL, profileInfo.avatar_url);
|
|
|
|
} else {
|
|
|
|
window.localStorage.removeItem(KEY_AVATAR_URL);
|
|
|
|
}
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2022-01-17 13:47:07 +00:00
|
|
|
await this.updateState({
|
|
|
|
displayName: profileInfo.displayname,
|
|
|
|
avatarUrl: profileInfo.avatar_url,
|
|
|
|
fetchedAt: Date.now(),
|
|
|
|
});
|
2022-02-24 14:39:25 +00:00
|
|
|
},
|
|
|
|
200,
|
|
|
|
{ trailing: true, leading: true },
|
|
|
|
);
|
2020-06-24 02:59:26 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onStateEvents = async (ev: MatrixEvent): Promise<void> => {
|
2020-06-24 02:59:26 +00:00
|
|
|
const myUserId = MatrixClientPeg.get().getUserId();
|
2022-02-24 14:39:25 +00:00
|
|
|
if (ev.getType() === EventType.RoomMember && ev.getSender() === myUserId && ev.getStateKey() === myUserId) {
|
2020-06-24 02:59:26 +00:00
|
|
|
await this.onProfileUpdate();
|
|
|
|
}
|
2022-02-24 14:39:25 +00:00
|
|
|
};
|
2020-06-24 02:59:26 +00:00
|
|
|
}
|