Stabilise user profile timezones (#30815)
* Fix imports * lint * update test * log * Update comment
This commit is contained in:
@@ -17,6 +17,8 @@ import {
|
||||
type SyncStateData,
|
||||
SyncState,
|
||||
EventType,
|
||||
ProfileKeyTimezone,
|
||||
ProfileKeyMSC4175Timezone,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { type MatrixCall } from "matrix-js-sdk/src/webrtc/call";
|
||||
import classNames from "classnames";
|
||||
@@ -197,10 +199,12 @@ class LoggedInView extends React.Component<IProps, IState> {
|
||||
}
|
||||
|
||||
private onTimezoneUpdate = async (): Promise<void> => {
|
||||
// TODO: In a future app release, remove support for legacy key.
|
||||
if (!SettingsStore.getValue("userTimezonePublish")) {
|
||||
// Ensure it's deleted
|
||||
try {
|
||||
await this._matrixClient.deleteExtendedProfileProperty("us.cloke.msc4175.tz");
|
||||
await this._matrixClient.deleteExtendedProfileProperty(ProfileKeyMSC4175Timezone);
|
||||
await this._matrixClient.deleteExtendedProfileProperty(ProfileKeyTimezone);
|
||||
} catch (ex) {
|
||||
console.warn("Failed to delete timezone from user profile", ex);
|
||||
}
|
||||
@@ -215,7 +219,8 @@ class LoggedInView extends React.Component<IProps, IState> {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await this._matrixClient.setExtendedProfileProperty("us.cloke.msc4175.tz", currentTimezone);
|
||||
await this._matrixClient.setExtendedProfileProperty(ProfileKeyTimezone, currentTimezone);
|
||||
await this._matrixClient.setExtendedProfileProperty(ProfileKeyMSC4175Timezone, currentTimezone);
|
||||
} catch (ex) {
|
||||
console.warn("Failed to update user profile with current timezone", ex);
|
||||
}
|
||||
|
||||
@@ -5,11 +5,19 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
import { useEffect, useState } from "react";
|
||||
import { type MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix";
|
||||
import {
|
||||
type MatrixClient,
|
||||
MatrixError,
|
||||
ProfileKeyMSC4175Timezone,
|
||||
ProfileKeyTimezone,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
|
||||
import { getTwelveHourOptions } from "../DateUtils.ts";
|
||||
import { useSettingValue } from "./useSettings.ts";
|
||||
|
||||
const log = logger.getChild("useUserTimezone");
|
||||
|
||||
/**
|
||||
* Fetch a user's delclared timezone through their profile, and return
|
||||
* a friendly string of the current time for that user. This will keep
|
||||
@@ -52,11 +60,13 @@ export const useUserTimezone = (cli: MatrixClient, userId: string): { timezone:
|
||||
return;
|
||||
}
|
||||
(async () => {
|
||||
console.log("Trying to fetch TZ");
|
||||
log.debug("Trying to fetch TZ for", userId);
|
||||
try {
|
||||
const tz = await cli.getExtendedProfileProperty(userId, "us.cloke.msc4175.tz");
|
||||
const userProfile = await cli.getExtendedProfile(userId);
|
||||
// In a future spec release, remove support for legacy key.
|
||||
const tz = userProfile[ProfileKeyTimezone] ?? userProfile[ProfileKeyMSC4175Timezone];
|
||||
if (typeof tz !== "string") {
|
||||
// Err, definitely not a tz.
|
||||
// Definitely not a tz.
|
||||
throw Error("Timezone value was not a string");
|
||||
}
|
||||
// This will validate the timezone for us.
|
||||
@@ -85,7 +95,7 @@ export const useUserTimezone = (cli: MatrixClient, userId: string): { timezone:
|
||||
// No timezone set, ignore.
|
||||
return;
|
||||
}
|
||||
console.error("Could not render current timezone for user", ex);
|
||||
log.warn(`Could not render current timezone for ${userId}`, ex);
|
||||
}
|
||||
})();
|
||||
}, [supported, userId, cli, showTwelveHour]);
|
||||
|
||||
Reference in New Issue
Block a user