|
| 1 | +import { logger } from "./logger"; |
| 2 | +import { MatrixEvent } from "./matrix"; |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2021-2024 The Matrix.org Foundation C.I.C. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +*/ |
| 19 | +let serverToLocalTimeOffset: number | undefined = undefined; |
| 20 | + |
| 21 | +/** |
| 22 | + * This method should only be used after computing the server-client time offset |
| 23 | + * using: tryComputeTimeSyncWithEvent or computeTimeSyncWithRequest. |
| 24 | + */ |
| 25 | +const getServerToLocalTimeOffset = (): number => { |
| 26 | + if (!serverToLocalTimeOffset) { |
| 27 | + logger.warn("Server time offset not computed yet, using 0"); |
| 28 | + serverToLocalTimeOffset = 0; |
| 29 | + } |
| 30 | + if (!serverToLocalTimeOffset) throw new Error("Failed to compute time sync"); |
| 31 | + |
| 32 | + return serverToLocalTimeOffset; |
| 33 | +}; |
| 34 | +/** |
| 35 | + * This uses a matrix event with an age property to compute the time offset. |
| 36 | + * This also works when the event was not just now received from the homeserver |
| 37 | + * because the event tracks the time since it has been received via the localTimestamp. |
| 38 | + * @param event - The matrix event used to compute the time offset. |
| 39 | + * @returns true if the time offset computation was successful, false otherwise |
| 40 | + */ |
| 41 | +export const tryComputeTimeSyncWithEvent = (event: MatrixEvent): boolean => { |
| 42 | + const serverTimeNow = event.getTs() + event.getLocalAge(); |
| 43 | + serverToLocalTimeOffset = serverTimeNow - Date.now(); |
| 44 | + |
| 45 | + return true; |
| 46 | +}; |
| 47 | + |
| 48 | +/** |
| 49 | + * Computes the time offset between the server and the local machine by sending |
| 50 | + * a http request to the server. |
| 51 | + * (UNIMPLEMNENTED) |
| 52 | + * @returns true if the time offset computation was successful, false otherwise |
| 53 | + */ |
| 54 | +export const computeTimeSyncWithRequest = async (): Promise<boolean> => { |
| 55 | + // TODO: fetch time now from server (temporarily use offset of 0) |
| 56 | + serverToLocalTimeOffset = 0; |
| 57 | + return true; |
| 58 | + // await ... |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * This method should only be used after computing the server-client time offset |
| 63 | + * using: tryComputeTimeSyncWithEvent or computeTimeSyncWithRequest. |
| 64 | + */ |
| 65 | +export const getServerTimeNow = (): number => { |
| 66 | + return localTsToServerTs(Date.now()); |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * This method should only be used after computing the server-client time offset |
| 71 | + * using: tryComputeTimeSyncWithEvent or computeTimeSyncWithRequest. |
| 72 | + */ |
| 73 | +export const serverTsToLocalTs = (serverTs: number): number => { |
| 74 | + return serverTs + getServerToLocalTimeOffset(); |
| 75 | +}; |
| 76 | + |
| 77 | +/** |
| 78 | + * This method should only be used after computing the server-client time offset |
| 79 | + * using: tryComputeTimeSyncWithEvent or computeTimeSyncWithRequest. |
| 80 | + */ |
| 81 | +export const localTsToServerTs = (localTs: number): number => { |
| 82 | + return localTs - getServerToLocalTimeOffset(); |
| 83 | +}; |
0 commit comments