mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-01-19 03:56:05 +01:00
add special messages notification settings
This commit is contained in:
parent
a5042bba4a
commit
cb7db3c13f
5 changed files with 261 additions and 9 deletions
|
@ -50,13 +50,14 @@ type PushRulesProps = {
|
||||||
encrypted?: boolean;
|
encrypted?: boolean;
|
||||||
oneToOne?: boolean;
|
oneToOne?: boolean;
|
||||||
};
|
};
|
||||||
function AllMessagesModeSwitcher({ ruleId, pushRules, encrypted, oneToOne }: PushRulesProps) {
|
function AllMessagesModeSwitcher({
|
||||||
const mx = useMatrixClient();
|
|
||||||
const defaultPushRuleData = getAllMessageDefaultRule(
|
|
||||||
ruleId,
|
ruleId,
|
||||||
encrypted ?? false,
|
pushRules,
|
||||||
oneToOne ?? false
|
encrypted = false,
|
||||||
);
|
oneToOne = false,
|
||||||
|
}: PushRulesProps) {
|
||||||
|
const mx = useMatrixClient();
|
||||||
|
const defaultPushRuleData = getAllMessageDefaultRule(ruleId, encrypted, oneToOne);
|
||||||
const { kind, pushRule } = usePushRule(pushRules, ruleId) ?? defaultPushRuleData;
|
const { kind, pushRule } = usePushRule(pushRules, ruleId) ?? defaultPushRuleData;
|
||||||
const getModeActions = useNotificationModeActions();
|
const getModeActions = useNotificationModeActions();
|
||||||
|
|
||||||
|
@ -83,7 +84,7 @@ export function AllMessagesNotifications() {
|
||||||
<Box alignItems="Center" justifyContent="SpaceBetween" gap="200">
|
<Box alignItems="Center" justifyContent="SpaceBetween" gap="200">
|
||||||
<Text size="L400">All Messages</Text>
|
<Text size="L400">All Messages</Text>
|
||||||
<Box gap="100">
|
<Box gap="100">
|
||||||
<Text size="T200">Hint: </Text>
|
<Text size="T200">Badge: </Text>
|
||||||
<Badge radii="300" variant="Secondary" fill="Solid">
|
<Badge radii="300" variant="Secondary" fill="Solid">
|
||||||
<Text size="L400">1</Text>
|
<Text size="L400">1</Text>
|
||||||
</Badge>
|
</Badge>
|
||||||
|
|
|
@ -8,6 +8,7 @@ import { useSetting } from '../../../state/hooks/settings';
|
||||||
import { settingsAtom } from '../../../state/settings';
|
import { settingsAtom } from '../../../state/settings';
|
||||||
import { usePermissionState } from '../../../hooks/usePermission';
|
import { usePermissionState } from '../../../hooks/usePermission';
|
||||||
import { AllMessagesNotifications } from './AllMessages';
|
import { AllMessagesNotifications } from './AllMessages';
|
||||||
|
import { SpecialMessagesNotifications } from './SpecialMessages';
|
||||||
|
|
||||||
function SystemNotification() {
|
function SystemNotification() {
|
||||||
const notifPermission = usePermissionState(
|
const notifPermission = usePermissionState(
|
||||||
|
@ -102,6 +103,7 @@ export function Notifications({ requestClose }: NotificationsProps) {
|
||||||
<Box direction="Column" gap="700">
|
<Box direction="Column" gap="700">
|
||||||
<SystemNotification />
|
<SystemNotification />
|
||||||
<AllMessagesNotifications />
|
<AllMessagesNotifications />
|
||||||
|
<SpecialMessagesNotifications />
|
||||||
</Box>
|
</Box>
|
||||||
</PageContent>
|
</PageContent>
|
||||||
</Scroll>
|
</Scroll>
|
||||||
|
|
222
src/app/features/settings/notifications/SpecialMessages.tsx
Normal file
222
src/app/features/settings/notifications/SpecialMessages.tsx
Normal file
|
@ -0,0 +1,222 @@
|
||||||
|
import React, { useCallback, useMemo } from 'react';
|
||||||
|
import { ConditionKind, IPushRules, PushRuleKind, RuleId } from 'matrix-js-sdk';
|
||||||
|
import { Box, Text, Badge } from 'folds';
|
||||||
|
import { useAccountData } from '../../../hooks/useAccountData';
|
||||||
|
import { AccountDataEvent } from '../../../../types/matrix/accountData';
|
||||||
|
import { SequenceCard } from '../../../components/sequence-card';
|
||||||
|
import { SequenceCardStyle } from '../styles.css';
|
||||||
|
import { SettingTile } from '../../../components/setting-tile';
|
||||||
|
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
||||||
|
import { useUserProfile } from '../../../hooks/useUserProfile';
|
||||||
|
import { getMxIdLocalPart } from '../../../utils/matrix';
|
||||||
|
import { makePushRuleData, PushRuleData, usePushRule } from '../../../hooks/usePushRule';
|
||||||
|
import {
|
||||||
|
getNotificationModeActions,
|
||||||
|
NotificationMode,
|
||||||
|
NotificationModeOptions,
|
||||||
|
useNotificationModeActions,
|
||||||
|
} from '../../../hooks/useNotificationMode';
|
||||||
|
import { NotificationModeSwitcher } from './NotificationModeSwitcher';
|
||||||
|
|
||||||
|
const NOTIFY_MODE_OPS: NotificationModeOptions = {
|
||||||
|
highlight: true,
|
||||||
|
};
|
||||||
|
const getDefaultIsUserMention = (userId: string): PushRuleData =>
|
||||||
|
makePushRuleData(
|
||||||
|
PushRuleKind.Override,
|
||||||
|
RuleId.IsUserMention,
|
||||||
|
getNotificationModeActions(NotificationMode.NotifyLoud, { highlight: true }),
|
||||||
|
[
|
||||||
|
{
|
||||||
|
kind: ConditionKind.EventPropertyContains,
|
||||||
|
key: 'content.m\\.mentions.user_ids',
|
||||||
|
value: userId,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
const DefaultContainsDisplayName = makePushRuleData(
|
||||||
|
PushRuleKind.Override,
|
||||||
|
RuleId.ContainsDisplayName,
|
||||||
|
getNotificationModeActions(NotificationMode.NotifyLoud, { highlight: true }),
|
||||||
|
[
|
||||||
|
{
|
||||||
|
kind: ConditionKind.ContainsDisplayName,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
const getDefaultContainsUsername = (username: string) =>
|
||||||
|
makePushRuleData(
|
||||||
|
PushRuleKind.ContentSpecific,
|
||||||
|
RuleId.ContainsUserName,
|
||||||
|
getNotificationModeActions(NotificationMode.NotifyLoud, { highlight: true }),
|
||||||
|
undefined,
|
||||||
|
username
|
||||||
|
);
|
||||||
|
|
||||||
|
const DefaultIsRoomMention = makePushRuleData(
|
||||||
|
PushRuleKind.Override,
|
||||||
|
RuleId.IsRoomMention,
|
||||||
|
getNotificationModeActions(NotificationMode.Notify, { highlight: true }),
|
||||||
|
[
|
||||||
|
{
|
||||||
|
kind: ConditionKind.EventPropertyIs,
|
||||||
|
key: 'content.m\\.mentions.room',
|
||||||
|
value: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: ConditionKind.SenderNotificationPermission,
|
||||||
|
key: 'room',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
const DefaultAtRoomNotification = makePushRuleData(
|
||||||
|
PushRuleKind.Override,
|
||||||
|
RuleId.AtRoomNotification,
|
||||||
|
getNotificationModeActions(NotificationMode.Notify, { highlight: true }),
|
||||||
|
[
|
||||||
|
{
|
||||||
|
kind: ConditionKind.EventMatch,
|
||||||
|
key: 'content.body',
|
||||||
|
pattern: '@room',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: ConditionKind.SenderNotificationPermission,
|
||||||
|
key: 'room',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
type PushRulesProps = {
|
||||||
|
ruleId: RuleId;
|
||||||
|
pushRules: IPushRules;
|
||||||
|
defaultPushRuleData: PushRuleData;
|
||||||
|
};
|
||||||
|
function MentionModeSwitcher({ ruleId, pushRules, defaultPushRuleData }: PushRulesProps) {
|
||||||
|
const mx = useMatrixClient();
|
||||||
|
|
||||||
|
const { kind, pushRule } = usePushRule(pushRules, ruleId) ?? defaultPushRuleData;
|
||||||
|
const getModeActions = useNotificationModeActions(NOTIFY_MODE_OPS);
|
||||||
|
|
||||||
|
const handleChange = useCallback(
|
||||||
|
async (mode: NotificationMode) => {
|
||||||
|
const actions = getModeActions(mode);
|
||||||
|
await mx.setPushRuleActions('global', kind, ruleId, actions);
|
||||||
|
},
|
||||||
|
[mx, getModeActions, kind, ruleId]
|
||||||
|
);
|
||||||
|
|
||||||
|
return <NotificationModeSwitcher pushRule={pushRule} onChange={handleChange} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SpecialMessagesNotifications() {
|
||||||
|
const mx = useMatrixClient();
|
||||||
|
const userId = mx.getUserId()!;
|
||||||
|
const { displayName } = useUserProfile(userId);
|
||||||
|
const pushRulesEvt = useAccountData(AccountDataEvent.PushRules);
|
||||||
|
const pushRules = useMemo(
|
||||||
|
() => pushRulesEvt?.getContent<IPushRules>() ?? { global: {} },
|
||||||
|
[pushRulesEvt]
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box direction="Column" gap="100">
|
||||||
|
<Box alignItems="Center" justifyContent="SpaceBetween" gap="200">
|
||||||
|
<Text size="L400">Special Messages</Text>
|
||||||
|
<Box gap="100">
|
||||||
|
<Text size="T200">Badge: </Text>
|
||||||
|
<Badge radii="300" variant="Success" fill="Solid">
|
||||||
|
<Text size="L400">1</Text>
|
||||||
|
</Badge>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
<SequenceCard
|
||||||
|
className={SequenceCardStyle}
|
||||||
|
variant="SurfaceVariant"
|
||||||
|
direction="Column"
|
||||||
|
gap="400"
|
||||||
|
>
|
||||||
|
<SettingTile
|
||||||
|
title={`Mention User ID ("${userId}")`}
|
||||||
|
after={
|
||||||
|
<MentionModeSwitcher
|
||||||
|
pushRules={pushRules}
|
||||||
|
ruleId={RuleId.IsUserMention}
|
||||||
|
defaultPushRuleData={getDefaultIsUserMention(userId)}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</SequenceCard>
|
||||||
|
<SequenceCard
|
||||||
|
className={SequenceCardStyle}
|
||||||
|
variant="SurfaceVariant"
|
||||||
|
direction="Column"
|
||||||
|
gap="400"
|
||||||
|
>
|
||||||
|
<SettingTile
|
||||||
|
title={`Contains Displayname ${displayName ? `("${displayName}")` : ''}`}
|
||||||
|
after={
|
||||||
|
<MentionModeSwitcher
|
||||||
|
pushRules={pushRules}
|
||||||
|
ruleId={RuleId.ContainsDisplayName}
|
||||||
|
defaultPushRuleData={DefaultContainsDisplayName}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</SequenceCard>
|
||||||
|
<SequenceCard
|
||||||
|
className={SequenceCardStyle}
|
||||||
|
variant="SurfaceVariant"
|
||||||
|
direction="Column"
|
||||||
|
gap="400"
|
||||||
|
>
|
||||||
|
<SettingTile
|
||||||
|
title={`Contains Username ("${getMxIdLocalPart(userId)}")`}
|
||||||
|
after={
|
||||||
|
<MentionModeSwitcher
|
||||||
|
pushRules={pushRules}
|
||||||
|
ruleId={RuleId.ContainsUserName}
|
||||||
|
defaultPushRuleData={getDefaultContainsUsername(getMxIdLocalPart(userId) ?? userId)}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</SequenceCard>
|
||||||
|
<SequenceCard
|
||||||
|
className={SequenceCardStyle}
|
||||||
|
variant="SurfaceVariant"
|
||||||
|
direction="Column"
|
||||||
|
gap="400"
|
||||||
|
>
|
||||||
|
<SettingTile
|
||||||
|
title="Mention @room"
|
||||||
|
after={
|
||||||
|
<MentionModeSwitcher
|
||||||
|
pushRules={pushRules}
|
||||||
|
ruleId={RuleId.IsRoomMention}
|
||||||
|
defaultPushRuleData={DefaultIsRoomMention}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</SequenceCard>
|
||||||
|
<SequenceCard
|
||||||
|
className={SequenceCardStyle}
|
||||||
|
variant="SurfaceVariant"
|
||||||
|
direction="Column"
|
||||||
|
gap="400"
|
||||||
|
>
|
||||||
|
<SettingTile
|
||||||
|
title="Contains @room"
|
||||||
|
after={
|
||||||
|
<MentionModeSwitcher
|
||||||
|
pushRules={pushRules}
|
||||||
|
ruleId={RuleId.AtRoomNotification}
|
||||||
|
defaultPushRuleData={DefaultAtRoomNotification}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</SequenceCard>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ export enum NotificationMode {
|
||||||
NotifyLoud = 'NotifyLoud',
|
NotifyLoud = 'NotifyLoud',
|
||||||
}
|
}
|
||||||
|
|
||||||
type NotificationModeOptions = {
|
export type NotificationModeOptions = {
|
||||||
soundValue?: string;
|
soundValue?: string;
|
||||||
highlight?: boolean;
|
highlight?: boolean;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
import { IPushRule, IPushRules, PushRuleKind, RuleId } from 'matrix-js-sdk';
|
import {
|
||||||
|
IPushRule,
|
||||||
|
IPushRules,
|
||||||
|
PushRuleAction,
|
||||||
|
PushRuleCondition,
|
||||||
|
PushRuleKind,
|
||||||
|
RuleId,
|
||||||
|
} from 'matrix-js-sdk';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
export type PushRuleData = {
|
export type PushRuleData = {
|
||||||
|
@ -6,6 +13,26 @@ export type PushRuleData = {
|
||||||
pushRule: IPushRule;
|
pushRule: IPushRule;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const makePushRuleData = (
|
||||||
|
kind: PushRuleKind,
|
||||||
|
ruleId: RuleId,
|
||||||
|
actions: PushRuleAction[],
|
||||||
|
conditions?: PushRuleCondition[],
|
||||||
|
pattern?: string,
|
||||||
|
enabled?: boolean,
|
||||||
|
_default?: boolean
|
||||||
|
): PushRuleData => ({
|
||||||
|
kind,
|
||||||
|
pushRule: {
|
||||||
|
rule_id: ruleId,
|
||||||
|
default: _default ?? true,
|
||||||
|
enabled: enabled ?? true,
|
||||||
|
pattern,
|
||||||
|
conditions,
|
||||||
|
actions,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export const orderedPushRuleKinds: PushRuleKind[] = [
|
export const orderedPushRuleKinds: PushRuleKind[] = [
|
||||||
PushRuleKind.Override,
|
PushRuleKind.Override,
|
||||||
PushRuleKind.ContentSpecific,
|
PushRuleKind.ContentSpecific,
|
||||||
|
|
Loading…
Reference in a new issue