-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use server time for calling (WPB-10979) (#3017)
* fix: use server time for calling * chore: address comments * fix: get serverTime after websocket is connected * chore: cleanup * chore: broken unit test
- Loading branch information
1 parent
e1b3f2a
commit 4357e06
Showing
13 changed files
with
173 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
logic/src/commonMain/kotlin/com/wire/kalium/logic/util/ServerTimeHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.util | ||
|
||
import com.wire.kalium.logic.kaliumLogger | ||
import kotlinx.datetime.Clock | ||
|
||
internal interface ServerTimeHandler { | ||
fun computeTimeOffset(serverTime: Long) | ||
fun toServerTimestamp(localTimestamp: Long = Clock.System.now().epochSeconds): Long | ||
} | ||
|
||
internal class ServerTimeHandlerImpl : ServerTimeHandler { | ||
|
||
/** | ||
* Used to store the difference (offset) between the server time and the local client time. | ||
* And it will be used to adjust timestamps between server and client times. | ||
*/ | ||
private var timeOffset: Long = 0 | ||
|
||
/** | ||
* Compute the time offset between the server and the client | ||
* @param serverTime the server time to compute the offset | ||
*/ | ||
override fun computeTimeOffset(serverTime: Long) { | ||
kaliumLogger.i("ServerTimeHandler: computing time offset between server and client..") | ||
val offset = Clock.System.now().epochSeconds - serverTime | ||
timeOffset = offset | ||
} | ||
|
||
/** | ||
* Convert local timestamp to server timestamp | ||
* @param localTimestamp timestamp from client to convert | ||
* @return the timestamp adjusted with the client/server time shift | ||
*/ | ||
override fun toServerTimestamp(localTimestamp: Long): Long { | ||
return localTimestamp - timeOffset | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.