Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 7e57394

Browse files
committed
Switch to updating presence via /sync calls instead of PUT /presence
1 parent 6e99b2f commit 7e57394

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

src/Presence.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
*/
1818

1919
import { logger } from "matrix-js-sdk/src/logger";
20+
import { SetPresence } from "matrix-js-sdk/src/sync";
2021

2122
import { MatrixClientPeg } from "./MatrixClientPeg";
2223
import dis from "./dispatcher/dispatcher";
@@ -26,16 +27,10 @@ import { ActionPayload } from "./dispatcher/payloads";
2627
// Time in ms after that a user is considered as unavailable/away
2728
const UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins
2829

29-
enum State {
30-
Online = "online",
31-
Offline = "offline",
32-
Unavailable = "unavailable",
33-
}
34-
3530
class Presence {
3631
private unavailableTimer: Timer | null = null;
3732
private dispatcherRef: string | null = null;
38-
private state: State | null = null;
33+
private state: SetPresence | null = null;
3934

4035
/**
4136
* Start listening the user activity to evaluate his presence state.
@@ -48,7 +43,7 @@ class Presence {
4843
while (this.unavailableTimer) {
4944
try {
5045
await this.unavailableTimer.finished();
51-
this.setState(State.Unavailable);
46+
this.setState(SetPresence.Unavailable);
5247
} catch (e) {
5348
/* aborted, stop got called */
5449
}
@@ -73,13 +68,13 @@ class Presence {
7368
* Get the current presence state.
7469
* @returns {string} the presence state (see PRESENCE enum)
7570
*/
76-
public getState(): State | null {
71+
public getState(): SetPresence | null {
7772
return this.state;
7873
}
7974

8075
private onAction = (payload: ActionPayload): void => {
8176
if (payload.action === "user_activity") {
82-
this.setState(State.Online);
77+
this.setState(SetPresence.Online);
8378
this.unavailableTimer?.restart();
8479
}
8580
};
@@ -89,7 +84,7 @@ class Presence {
8984
* If the state has changed, the homeserver will be notified.
9085
* @param {string} newState the new presence state (see PRESENCE enum)
9186
*/
92-
private async setState(newState: State): Promise<void> {
87+
private async setState(newState: SetPresence): Promise<void> {
9388
if (newState === this.state) {
9489
return;
9590
}
@@ -102,7 +97,7 @@ class Presence {
10297
}
10398

10499
try {
105-
await MatrixClientPeg.safeGet().setPresence({ presence: this.state });
100+
await MatrixClientPeg.safeGet().setSyncPresence(this.state);
106101
logger.info("Presence:", newState);
107102
} catch (err) {
108103
logger.error("Failed to set presence:", err);

0 commit comments

Comments
 (0)