Skip to content

Commit 9d8705a

Browse files
committed
refactor: extract the status visibility gate out of the presence service
1 parent c49cf89 commit 9d8705a

2 files changed

Lines changed: 38 additions & 15 deletions

File tree

ee/packages/presence/src/Presence.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { setTimeout, clearTimeout } from 'node:timers';
22

33
import type { IPresence, IBrokerNode } from '@rocket.chat/core-services';
4-
import { License, ServiceClass, Settings, StatusVisibility } from '@rocket.chat/core-services';
4+
import { License, ServiceClass, Settings } from '@rocket.chat/core-services';
55
import type { IUser } from '@rocket.chat/core-typings';
66
import { UserStatus } from '@rocket.chat/core-typings';
77
import { cronJobs } from '@rocket.chat/cron';
88
import { Logger } from '@rocket.chat/logger';
99
import { Users, UsersSessions } from '@rocket.chat/models';
1010

1111
import { PresenceReaper } from './lib/PresenceReaper';
12+
import { STATUS_VISIBILITY_SETTING_ID, StatusVisibilityGate } from './lib/StatusVisibilityGate';
1213
import { normalizeStatusText } from './lib/normalizeStatusText';
1314
import { type ClaimUpdate, processPresence } from './lib/presenceEngine';
1415

@@ -38,7 +39,7 @@ export class Presence extends ServiceClass implements IPresence {
3839

3940
private broadcastEnabled = true;
4041

41-
private statusVisibilityEnabled = false;
42+
private statusVisibility = new StatusVisibilityGate();
4243

4344
private hasPresenceLicense = false;
4445

@@ -65,8 +66,8 @@ export class Presence extends ServiceClass implements IPresence {
6566
onUpdate: (userIds) => this.handleReaperUpdates(userIds),
6667
});
6768

68-
this.onSettingChanged('Accounts_StatusVisibility_Enabled', async ({ setting }): Promise<void> => {
69-
this.statusVisibilityEnabled = setting.value === true;
69+
this.onSettingChanged(STATUS_VISIBILITY_SETTING_ID, async ({ setting }): Promise<void> => {
70+
this.statusVisibility.setEnabled(setting.value);
7071
});
7172

7273
this.onEvent('watch.instanceStatus', async ({ clientAction, id, diff }): Promise<void> => {
@@ -123,7 +124,7 @@ export class Presence extends ServiceClass implements IPresence {
123124
try {
124125
await Settings.set('Presence_broadcast_disabled', false);
125126

126-
this.statusVisibilityEnabled = (await Settings.get<boolean>('Accounts_StatusVisibility_Enabled')) === true;
127+
await this.statusVisibility.start();
127128
this.hasScalabilityLicense = await License.hasModule('scalability');
128129
this.hasPresenceLicense = await License.hasModule('unlimited-presence');
129130
this.hasLicense = this.hasPresenceLicense || this.hasScalabilityLicense;
@@ -428,16 +429,14 @@ export class Presence extends ServiceClass implements IPresence {
428429
return;
429430
}
430431

431-
if (this.statusVisibilityEnabled) {
432-
void StatusVisibility.hasRestrictions(user._id)
433-
.catch(() => true)
434-
.then((hasVisibilityRestrictions) =>
435-
this.api?.broadcast('presence.status', {
436-
user,
437-
previousStatus,
438-
hasVisibilityRestrictions,
439-
}),
440-
);
432+
if (this.statusVisibility.isEnabled()) {
433+
void this.statusVisibility.hasRestrictions(user._id).then((hasVisibilityRestrictions) =>
434+
this.api?.broadcast('presence.status', {
435+
user,
436+
previousStatus,
437+
hasVisibilityRestrictions,
438+
}),
439+
);
441440

442441
return;
443442
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Settings, StatusVisibility } from '@rocket.chat/core-services';
2+
import type { IUser } from '@rocket.chat/core-typings';
3+
4+
export const STATUS_VISIBILITY_SETTING_ID = 'Accounts_StatusVisibility_Enabled';
5+
6+
export class StatusVisibilityGate {
7+
private enabled = false;
8+
9+
async start(): Promise<void> {
10+
this.enabled = (await Settings.get<boolean>(STATUS_VISIBILITY_SETTING_ID)) === true;
11+
}
12+
13+
setEnabled(value: unknown): void {
14+
this.enabled = value === true;
15+
}
16+
17+
isEnabled(): boolean {
18+
return this.enabled;
19+
}
20+
21+
async hasRestrictions(uid: IUser['_id']): Promise<boolean> {
22+
return StatusVisibility.hasRestrictions(uid).catch(() => true);
23+
}
24+
}

0 commit comments

Comments
 (0)