Skip to content

Commit

Permalink
Active private receipt support
Browse files Browse the repository at this point in the history
  • Loading branch information
greentore committed Feb 6, 2023
1 parent a6fb44e commit 373d3c9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
45 changes: 44 additions & 1 deletion src/app/organisms/settings/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import settings from '../../../client/state/settings';
import navigation from '../../../client/state/navigation';
import {
toggleSystemTheme, toggleMarkdown, toggleMembershipEvents, toggleNickAvatarEvents,
toggleNotifications, toggleNotificationSounds,
toggleNotifications, toggleNotificationSounds, toggleReadReceipts,
} from '../../../client/action/settings';
import { usePermission } from '../../hooks/usePermission';

Expand Down Expand Up @@ -44,6 +44,10 @@ import CrossIC from '../../../../public/res/ic/outlined/cross.svg';
import CinnySVG from '../../../../public/res/svg/cinny.svg';
import { confirmDialog } from '../../molecules/confirm-dialog/ConfirmDialog';

let capabilities = {
privateReadReceipts: false,
};

function AppearanceSection() {
const [, updateState] = useState({});

Expand Down Expand Up @@ -188,6 +192,7 @@ function EmojiSection() {
}

function SecuritySection() {
const [, updateState] = useState({});
return (
<div className="settings-security">
<div className="settings-security__card">
Expand Down Expand Up @@ -217,6 +222,33 @@ function SecuritySection() {
)}
/>
</div>
<div className="settings-security__card">
<MenuHeader>Presence</MenuHeader>
<SettingTile
title="Send read receipts"
options={(
<Toggle
/** Always allow to switch receipts on. */
disabled={!capabilities.privateReadReceipts && settings.sendReadReceipts}
isActive={settings.sendReadReceipts}
onToggle={() => { toggleReadReceipts(); updateState({}); }}
/>
)}
content={(
<>
<Text variant="b3">
Let other people know what messages you read.
</Text>
{!capabilities.privateReadReceipts
&& (
<Text variant="b3">
Making your read receipts private requires a compatible homeserver.
</Text>
)}
</>
)}
/>
</div>
</div>
);
}
Expand Down Expand Up @@ -320,9 +352,20 @@ function useWindowToggle(setSelectedTab) {
return [isOpen, requestClose];
}

async function getCapabilities() {
const mx = initMatrix.matrixClient;
capabilities = {
privateReadReceipts: (await (Promise.all([
mx.doesServerSupportUnstableFeature('org.matrix.msc2285.stable'),
mx.isVersionSupported('v1.4')])
)).some((res) => res === true),
};
}

function Settings() {
const [selectedTab, setSelectedTab] = useState(tabItems[0]);
const [isOpen, requestClose] = useWindowToggle(setSelectedTab);
useEffect(getCapabilities, []);

const handleTabChange = (tabItem) => setSelectedTab(tabItem);
const handleLogout = async () => {
Expand Down
9 changes: 8 additions & 1 deletion src/client/action/notifications.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import initMatrix from '../initMatrix';
import settings from '../state/settings';

const ReceiptType = {
Read: 'm.read',
ReadPrivate: 'm.read.private',
};

// eslint-disable-next-line import/prefer-default-export
export async function markAsRead(roomId) {
Expand All @@ -22,5 +28,6 @@ export async function markAsRead(roomId) {
const latestEvent = getLatestValidEvent();
if (latestEvent === null) return;

await mx.sendReadReceipt(latestEvent);
const receiptType = settings.sendReadReceipts ? ReceiptType.Read : ReceiptType.ReadPrivate;
await mx.sendReadReceipt(latestEvent, receiptType);
}
6 changes: 6 additions & 0 deletions src/client/action/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ export function toggleNotificationSounds() {
type: cons.actions.settings.TOGGLE_NOTIFICATION_SOUNDS,
});
}

export function toggleReadReceipts() {
appDispatcher.dispatch({
type: cons.actions.settings.TOGGLE_READ_RECEIPTS,
});
}
2 changes: 2 additions & 0 deletions src/client/state/cons.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const cons = {
TOGGLE_NICKAVATAR_EVENT: 'TOGGLE_NICKAVATAR_EVENT',
TOGGLE_NOTIFICATIONS: 'TOGGLE_NOTIFICATIONS',
TOGGLE_NOTIFICATION_SOUNDS: 'TOGGLE_NOTIFICATION_SOUNDS',
TOGGLE_READ_RECEIPTS: 'TOGGLE_READ_RECEIPTS',
},
},
events: {
Expand Down Expand Up @@ -150,6 +151,7 @@ const cons = {
NICKAVATAR_EVENTS_TOGGLED: 'NICKAVATAR_EVENTS_TOGGLED',
NOTIFICATIONS_TOGGLED: 'NOTIFICATIONS_TOGGLED',
NOTIFICATION_SOUNDS_TOGGLED: 'NOTIFICATION_SOUNDS_TOGGLED',
READ_RECEIPTS_TOGGLED: 'READ_RECEIPTS_TOGGLED',
},
},
};
Expand Down
15 changes: 15 additions & 0 deletions src/client/state/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Settings extends EventEmitter {
this.hideNickAvatarEvents = this.getHideNickAvatarEvents();
this._showNotifications = this.getShowNotifications();
this.isNotificationSounds = this.getIsNotificationSounds();
this.sendReadReceipts = this.getSendReadReceipts();

this.isTouchScreenDevice = ('ontouchstart' in window) || (navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0);
}
Expand Down Expand Up @@ -147,6 +148,15 @@ class Settings extends EventEmitter {
return settings.isNotificationSounds;
}

getSendReadReceipts() {
if (typeof this.sendReadReceipts === 'boolean') return this.sendReadReceipts;

const settings = getSettings();
if (settings === null) return true;
if (typeof settings.sendReadReceipts === 'undefined') return true;
return settings.sendReadReceipts;
}

setter(action) {
const actions = {
[cons.actions.settings.TOGGLE_SYSTEM_THEME]: () => {
Expand Down Expand Up @@ -186,6 +196,11 @@ class Settings extends EventEmitter {
setSettings('isNotificationSounds', this.isNotificationSounds);
this.emit(cons.events.settings.NOTIFICATION_SOUNDS_TOGGLED, this.isNotificationSounds);
},
[cons.actions.settings.TOGGLE_READ_RECEIPTS]: () => {
this.sendReadReceipts = !this.sendReadReceipts;
setSettings('sendReadReceipts', this.sendReadReceipts);
this.emit(cons.events.settings.READ_RECEIPTS_TOGGLED, this.sendReadReceipts);
},
};

actions[action.type]?.();
Expand Down

0 comments on commit 373d3c9

Please sign in to comment.