Feature: Basic notification options implementation

This commit is contained in:
Paul
2021-06-24 23:59:46 +01:00
parent 51ac1f599c
commit 21859e4c55
10 changed files with 173 additions and 25 deletions

View File

@@ -116,7 +116,7 @@ export default function Intermediate(props: Props) {
screen.id
} /** By specifying a key, we reset state whenever switching screen. */
/>
{/*<Prompt
<Prompt
when={[ 'modify_account', 'special_prompt', 'special_input', 'image_viewer', 'profile', 'channel_info', 'user_picker' ].includes(screen.id)}
message={(_, action) => {
if (action === 'POP') {
@@ -128,7 +128,7 @@ export default function Intermediate(props: Props) {
return true;
}}
/>*/}
/>
</IntermediateActionsContext.Provider>
</IntermediateContext.Provider>
);

View File

@@ -8,9 +8,11 @@ import { connectState } from "../../redux/connector";
import { Message, SYSTEM_USER_ID, User } from "revolt.js";
import { NotificationOptions } from "../../redux/reducers/settings";
import { Route, Switch, useHistory, useParams } from "react-router-dom";
import { getNotificationState, Notifications } from "../../redux/reducers/notifications";
interface Props {
options?: NotificationOptions;
notifs: Notifications;
}
const notifications: { [key: string]: Notification } = {};
@@ -24,9 +26,9 @@ async function createNotification(title: string, options: globalThis.Notificatio
}
}
function Notifier(props: Props) {
function Notifier({ options, notifs }: Props) {
const translate = useTranslation();
const showNotification = props.options?.desktopEnabled ?? false;
const showNotification = options?.desktopEnabled ?? false;
const client = useContext(AppContext);
const { guild: guild_id, channel: channel_id } = useParams<{
@@ -39,17 +41,27 @@ function Notifier(props: Props) {
async function message(msg: Message) {
if (msg.author === client.user!._id) return;
if (msg.channel === channel_id && document.hasFocus()) return;
if (client.user?.status?.presence === Users.Presence.Busy) return;
if (client.user!.status?.presence === Users.Presence.Busy) return;
const channel = client.channels.get(msg.channel);
const author = client.users.get(msg.author);
if (!channel) return;
if (author?.relationship === Users.Relationship.Blocked) return;
const notifState = getNotificationState(notifs, channel);
switch (notifState) {
case 'muted':
case 'none': return;
case 'mention': {
if (!msg.mentions?.includes(client.user!._id)) return;
}
}
playSound('message');
if (!showNotification) return;
const channel = client.channels.get(msg.channel);
const author = client.users.get(msg.author);
if (author?.relationship === Users.Relationship.Blocked) return;
let title;
switch (channel?.channel_type) {
switch (channel.channel_type) {
case "SavedMessages":
return;
case "DirectMessage":
@@ -192,7 +204,7 @@ function Notifier(props: Props) {
client.removeListener("message", message);
client.users.removeListener("mutation", relationship);
};
}, [client, playSound, guild_id, channel_id, showNotification]);
}, [client, playSound, guild_id, channel_id, showNotification, notifs]);
useEffect(() => {
function visChange() {
@@ -217,7 +229,8 @@ const NotifierComponent = connectState(
Notifier,
state => {
return {
options: state.settings.notification
options: state.settings.notification,
notifs: state.notifications
};
},
true

View File

@@ -9,6 +9,7 @@ import { useContext, useEffect } from "preact/hooks";
import { connectState } from "../../redux/connector";
import { WithDispatcher } from "../../redux/reducers";
import { Settings } from "../../redux/reducers/settings";
import { Notifications } from "../../redux/reducers/notifications";
import { AppContext, ClientStatus, StatusContext } from "./RevoltClient";
import { ClientboundNotification } from "revolt.js/dist/websocket/notifications";
import { DEFAULT_ENABLED_SYNC, SyncData, SyncKeys, SyncOptions } from "../../redux/reducers/sync";
@@ -16,7 +17,8 @@ import { DEFAULT_ENABLED_SYNC, SyncData, SyncKeys, SyncOptions } from "../../red
type Props = WithDispatcher & {
settings: Settings,
locale: Language,
sync: SyncOptions
sync: SyncOptions,
notifications: Notifications
};
var lastValues: { [key in SyncKeys]?: any } = { };
@@ -78,7 +80,7 @@ function SyncManager(props: Props) {
}
let disabled = props.sync.disabled ?? [];
for (let [key, object] of [ ['appearance', props.settings.appearance], ['theme', props.settings.theme], ['locale', props.locale] ] as [SyncKeys, any][]) {
for (let [key, object] of [ ['appearance', props.settings.appearance], ['theme', props.settings.theme], ['locale', props.locale], ['notifications', props.notifications] ] as [SyncKeys, any][]) {
useEffect(() => {
if (disabled.indexOf(key) === -1) {
if (typeof lastValues[key] !== 'undefined') {
@@ -117,7 +119,8 @@ export default connectState(
return {
settings: state.settings,
locale: state.locale,
sync: state.sync
sync: state.sync,
notifications: state.notifications
};
},
true