feat(mobx): server notification options + data store

This commit is contained in:
Paul
2021-12-11 23:34:46 +00:00
parent f8b8d96d3d
commit 413bf6949b
10 changed files with 237 additions and 102 deletions

View File

@@ -10,6 +10,7 @@ import Draft from "./stores/Draft";
import Experiments from "./stores/Experiments";
import Layout from "./stores/Layout";
import LocaleOptions from "./stores/LocaleOptions";
import NotificationOptions from "./stores/NotificationOptions";
import ServerConfig from "./stores/ServerConfig";
/**
@@ -22,6 +23,7 @@ export default class State {
experiments: Experiments;
layout: Layout;
config: ServerConfig;
notifications: NotificationOptions;
private persistent: [string, Persistent<unknown>][] = [];
@@ -35,6 +37,7 @@ export default class State {
this.experiments = new Experiments();
this.layout = new Layout();
this.config = new ServerConfig();
this.notifications = new NotificationOptions();
makeAutoObservable(this);
this.registerListeners = this.registerListeners.bind(this);

View File

@@ -49,9 +49,7 @@ export function findLanguage(lang?: string): Language {
}
/**
* Keeps track of the last open channels, tabs, etc.
* Handles providing good UX experience on navigating
* back and forth between different parts of the app.
* Keeps track of user's language settings.
*/
export default class LocaleOptions implements Store, Persistent<Data> {
private lang: Language;

View File

@@ -1,5 +1,7 @@
import { action, computed, makeAutoObservable, ObservableMap } from "mobx";
import { Channel } from "revolt-api/types/Channels";
import { Channel } from "revolt.js/dist/maps/Channels";
import { Message } from "revolt.js/dist/maps/Messages";
import { Server } from "revolt.js/dist/maps/Servers";
import { mapToRecord } from "../../lib/conversion";
@@ -22,21 +24,26 @@ export const DEFAULT_STATES: {
SavedMessages: "all",
DirectMessage: "all",
Group: "all",
TextChannel: "mention",
VoiceChannel: "mention",
TextChannel: undefined!,
VoiceChannel: undefined!,
};
/**
* Default state for servers.
*/
export const DEFAULT_SERVER_STATE: NotificationState = "mention";
interface Data {
server?: Record<string, string>;
channel?: Record<string, string>;
server?: Record<string, NotificationState>;
channel?: Record<string, NotificationState>;
}
/**
* Manages the user's notification preferences.
*/
export default class NotificationOptions implements Store, Persistent<Data> {
private server: ObservableMap<string, string>;
private channel: ObservableMap<string, string>;
private server: ObservableMap<string, NotificationState>;
private channel: ObservableMap<string, NotificationState>;
/**
* Construct new Experiments store.
@@ -72,5 +79,72 @@ export default class NotificationOptions implements Store, Persistent<Data> {
}
}
// TODO: implement
computeForChannel(channel: Channel) {
if (this.channel.has(channel._id)) {
return this.channel.get(channel._id);
}
if (channel.server_id) {
return this.computeForServer(channel.server_id);
}
return DEFAULT_STATES[channel.channel_type];
}
shouldNotify(message: Message) {
const state = this.computeForChannel(message.channel!);
switch (state) {
case "muted":
case "none":
return false;
case "mention":
if (!message.mention_ids?.includes(message.client.user!._id))
return false;
}
return true;
}
computeForServer(server_id: string) {
if (this.server.has(server_id)) {
return this.server.get(server_id);
}
return DEFAULT_SERVER_STATE;
}
getChannelState(channel_id: string) {
return this.channel.get(channel_id);
}
setChannelState(channel_id: string, state?: NotificationState) {
if (state) {
this.channel.set(channel_id, state);
} else {
this.channel.delete(channel_id);
}
}
getServerState(server_id: string) {
return this.server.get(server_id);
}
setServerState(server_id: string, state?: NotificationState) {
if (state) {
this.server.set(server_id, state);
} else {
this.server.delete(server_id);
}
}
isMuted(target?: Channel | Server) {
if (target instanceof Channel) {
return this.computeForChannel(target) === "muted";
} else if (target instanceof Server) {
return this.computeForServer(target._id) === "muted";
} else {
return false;
}
}
}