Skip to content

Commit bc369dc

Browse files
author
Evie Gauthier
committed
feat(sliding-sync): tie presence extension into sendPresence setting
1 parent 9ad69ac commit bc369dc

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

src/app/features/settings/general/General.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ function Editor({ isMobile }: { isMobile: boolean }) {
410410
const [isMarkdown, setIsMarkdown] = useSetting(settingsAtom, 'isMarkdown');
411411
const [hideActivity, setHideActivity] = useSetting(settingsAtom, 'hideActivity');
412412
const [hideReads, setHideReads] = useSetting(settingsAtom, 'hideReads');
413+
const [sendPresence, setSendPresence] = useSetting(settingsAtom, 'sendPresence');
413414

414415
return (
415416
<Box direction="Column" gap="100">
@@ -453,6 +454,13 @@ function Editor({ isMobile }: { isMobile: boolean }) {
453454
after={<Switch variant="Primary" value={hideReads} onChange={setHideReads} />}
454455
/>
455456
</SequenceCard>
457+
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
458+
<SettingTile
459+
title="Presence Status"
460+
description="Show and receive online status from other users."
461+
after={<Switch variant="Primary" value={sendPresence} onChange={setSendPresence} />}
462+
/>
463+
</SequenceCard>
456464
</Box>
457465
);
458466
}

src/app/pages/client/ClientNonUIFeatures.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
} from '$utils/notificationStyle';
3939
import { mobileOrTablet } from '$utils/user-agent';
4040
import { useSlidingSyncActiveRoom } from '$hooks/useSlidingSyncActiveRoom';
41+
import { getSlidingSyncManager } from '$client/initMatrix';
4142
import { getInboxInvitesPath } from '../pathUtils';
4243
import { BackgroundNotifications } from './BackgroundNotifications';
4344

@@ -580,6 +581,17 @@ function SlidingSyncActiveRoomSubscriber() {
580581
return null;
581582
}
582583

584+
function PresenceFeature() {
585+
const mx = useMatrixClient();
586+
const [sendPresence] = useSetting(settingsAtom, 'sendPresence');
587+
588+
useEffect(() => {
589+
getSlidingSyncManager(mx)?.setPresenceEnabled(sendPresence);
590+
}, [mx, sendPresence]);
591+
592+
return null;
593+
}
594+
583595
export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) {
584596
return (
585597
<>
@@ -592,6 +604,7 @@ export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) {
592604
<BackgroundNotifications />
593605
<SyncNotificationSettingsWithServiceWorker />
594606
<SlidingSyncActiveRoomSubscriber />
607+
<PresenceFeature />
595608
{children}
596609
</>
597610
);

src/app/state/settings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export interface Settings {
7878
captionPosition: CaptionPosition;
7979

8080
// Sable features!
81+
sendPresence: boolean;
8182
mobileGestures: boolean;
8283
rightSwipeAction: RightSwipeAction;
8384
hideMembershipInReadOnly: boolean;
@@ -153,6 +154,7 @@ const defaultSettings: Settings = {
153154
captionPosition: CaptionPosition.Below,
154155

155156
// Sable features!
157+
sendPresence: true,
156158
mobileGestures: true,
157159
rightSwipeAction: RightSwipeAction.Reply,
158160
hideMembershipInReadOnly: true,

src/client/slidingSync.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,14 @@ const getListEndIndex = (list: MSC3575List | null): number => {
241241
// poll and feeds received `m.presence` events into the SDK's User objects so that
242242
// components using `useUserPresence` see live updates (same path as regular /sync).
243243
class ExtensionPresence implements Extension<{ enabled: boolean }, { events?: object[] }> {
244+
private enabled = true;
245+
244246
public constructor(private readonly mx: MatrixClient) {}
245247

248+
public setEnabled(value: boolean): void {
249+
this.enabled = value;
250+
}
251+
246252
public name(): string {
247253
return 'presence';
248254
}
@@ -253,7 +259,7 @@ class ExtensionPresence implements Extension<{ enabled: boolean }, { events?: ob
253259
}
254260

255261
public async onRequest(_isInitial: boolean): Promise<{ enabled: boolean }> {
256-
return { enabled: true };
262+
return { enabled: this.enabled };
257263
}
258264

259265
public async onResponse(data: { events?: object[] }): Promise<void> {
@@ -297,6 +303,8 @@ export class SlidingSyncManager {
297303

298304
private readonly onLifecycle: (state: SlidingSyncState, resp: unknown, err?: Error) => void;
299305

306+
private presenceExtension!: ExtensionPresence;
307+
300308
public readonly slidingSync: SlidingSync;
301309

302310
public readonly probeTimeoutMs: number;
@@ -329,7 +337,8 @@ export class SlidingSyncManager {
329337

330338
// Register the presence extension so m.presence events from the server are fed
331339
// into the SDK's User objects, keeping useUserPresence accurate during sliding sync.
332-
this.slidingSync.registerExtension(new ExtensionPresence(mx));
340+
this.presenceExtension = new ExtensionPresence(mx);
341+
this.slidingSync.registerExtension(this.presenceExtension);
333342

334343
// Register a custom subscription for unencrypted active rooms; encrypted rooms use
335344
// the default subscription (which already has [*,*]).
@@ -406,6 +415,10 @@ export class SlidingSyncManager {
406415
);
407416
}
408417

418+
public setPresenceEnabled(enabled: boolean): void {
419+
this.presenceExtension.setEnabled(enabled);
420+
}
421+
409422
public getDiagnostics(): SlidingSyncDiagnostics {
410423
return {
411424
proxyBaseUrl: this.proxyBaseUrl,

0 commit comments

Comments
 (0)