Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix push notifications for rooms and direct messages not saving #2074

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 55 additions & 39 deletions src/app/molecules/global-notification/GlobalNotification.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';

import { openReusableContextMenu } from '../../../client/action/navigation';
import { getEventCords } from '../../../util/common';
Expand Down Expand Up @@ -55,12 +55,13 @@ function useGlobalNotif() {
const mx = useMatrixClient();
const pushRules = useAccountData('m.push_rules')?.getContent();
const underride = pushRules?.global?.underride ?? [];
const rulesToType = {

const [rulesToType, setRulesToType] = useState({
[DM]: notifType.ON,
[ENC_DM]: notifType.ON,
[ROOM]: notifType.NOISY,
[ENC_ROOM]: notifType.NOISY,
};
});

const getRuleCondition = (rule) => {
const condition = [];
Expand All @@ -75,10 +76,10 @@ function useGlobalNotif() {
return condition;
};

const setRule = (rule, type) => {
const setRule = async (rule, type) => {
const content = pushRules ?? {};
if (!content.global) content.global = {};
if (!content.global.underride) content.global.underride = [];
content.global = content.global ?? {};
content.global.underride = content.global.underride ?? [];
const ur = content.global.underride;
let ruleContent = ur.find((action) => action?.rule_id === rule);
if (!ruleContent) {
Expand All @@ -93,18 +94,33 @@ function useGlobalNotif() {
}
ruleContent.actions = getTypeActions(type);

mx.setAccountData('m.push_rules', content);
setRulesToType((prevRules) => ({
...prevRules,
[rule]: type,
}));

await mx.setAccountData('mx.push_rules', content);
};

const dmRule = underride.find((rule) => rule.rule_id === DM);
const encDmRule = underride.find((rule) => rule.rule_id === ENC_DM);
const roomRule = underride.find((rule) => rule.rule_id === ROOM);
const encRoomRule = underride.find((rule) => rule.rule_id === ENC_ROOM);
const updateRulesToType = () => {
const updatedRules = { ...rulesToType };

const dmRule = underride.find((rule) => rule.rule_id === DM);
const encDmRule = underride.find((rule) => rule.rule_id === ENC_DM);
const roomRule = underride.find((rule) => rule.rule_id === ROOM);
const encRoomRule = underride.find((rule) => rule.rule_id === ENC_ROOM);

if (dmRule) rulesToType[DM] = getActionType(dmRule);
if (encDmRule) rulesToType[ENC_DM] = getActionType(encDmRule);
if (roomRule) rulesToType[ROOM] = getActionType(roomRule);
if (encRoomRule) rulesToType[ENC_ROOM] = getActionType(encRoomRule);
if (dmRule) updatedRules[DM] = getActionType(dmRule);
if (encDmRule) updatedRules[ENC_DM] = getActionType(encDmRule);
if (roomRule) updatedRules[ROOM] = getActionType(roomRule);
if (encRoomRule) updatedRules[ENC_ROOM] = getActionType(encRoomRule);

setRulesToType(updatedRules);
};

React.useEffect(() => {
updateRulesToType();
}, [underride]);

return [rulesToType, setRule];
}
Expand All @@ -113,59 +129,59 @@ function GlobalNotification() {
const [rulesToType, setRule] = useGlobalNotif();

const onSelect = (evt, rule) => {
openReusableContextMenu(
'bottom',
getEventCords(evt, '.btn-surface'),
(requestClose) => (
<NotificationSelector
value={rulesToType[rule]}
onSelect={(value) => {
if (rulesToType[rule] !== value) setRule(rule, value);
requestClose();
}}
/>
),
);
openReusableContextMenu('bottom', getEventCords(evt, '.btn-surface'), (requestClose) => (
<NotificationSelector
value={rulesToType[rule]}
onSelect={(value) => {
if (rulesToType[rule] !== value) setRule(rule, value);
requestClose();
}}
/>
));
};

return (
<div className="global-notification">
<MenuHeader>Global Notifications</MenuHeader>
<SettingTile
title="Direct messages"
options={(
options={
<Button onClick={(evt) => onSelect(evt, DM)} iconSrc={ChevronBottomIC}>
{ typeToLabel[rulesToType[DM]] }
{typeToLabel[rulesToType[DM]]}
</Button>
)}
}
content={<Text variant="b3">Default notification settings for all direct message.</Text>}
/>
<SettingTile
title="Encrypted direct messages"
options={(
options={
<Button onClick={(evt) => onSelect(evt, ENC_DM)} iconSrc={ChevronBottomIC}>
{typeToLabel[rulesToType[ENC_DM]]}
</Button>
)}
content={<Text variant="b3">Default notification settings for all encrypted direct message.</Text>}
}
content={
<Text variant="b3">Default notification settings for all encrypted direct message.</Text>
}
/>
<SettingTile
title="Rooms messages"
options={(
options={
<Button onClick={(evt) => onSelect(evt, ROOM)} iconSrc={ChevronBottomIC}>
{typeToLabel[rulesToType[ROOM]]}
</Button>
)}
}
content={<Text variant="b3">Default notification settings for all room message.</Text>}
/>
<SettingTile
title="Encrypted rooms messages"
options={(
options={
<Button onClick={(evt) => onSelect(evt, ENC_ROOM)} iconSrc={ChevronBottomIC}>
{typeToLabel[rulesToType[ENC_ROOM]]}
</Button>
)}
content={<Text variant="b3">Default notification settings for all encrypted room message.</Text>}
}
content={
<Text variant="b3">Default notification settings for all encrypted room message.</Text>
}
/>
</div>
);
Expand Down
Loading