2021-12-10 17:00:34 +00:00
|
|
|
import { action, computed, makeAutoObservable, ObservableMap } from "mobx";
|
|
|
|
import { Channel } from "revolt-api/types/Channels";
|
|
|
|
|
2021-12-11 16:24:23 +00:00
|
|
|
import { mapToRecord } from "../../lib/conversion";
|
|
|
|
|
2021-12-11 14:36:26 +00:00
|
|
|
import Persistent from "../interfaces/Persistent";
|
2021-12-11 16:24:23 +00:00
|
|
|
import Store from "../interfaces/Store";
|
2021-12-10 17:00:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Possible notification states.
|
|
|
|
* TODO: make "muted" gray out the channel
|
|
|
|
* TODO: add server defaults
|
|
|
|
*/
|
|
|
|
export type NotificationState = "all" | "mention" | "none" | "muted";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default notification states for various types of channels.
|
|
|
|
*/
|
|
|
|
export const DEFAULT_STATES: {
|
|
|
|
[key in Channel["channel_type"]]: NotificationState;
|
|
|
|
} = {
|
|
|
|
SavedMessages: "all",
|
|
|
|
DirectMessage: "all",
|
|
|
|
Group: "all",
|
|
|
|
TextChannel: "mention",
|
|
|
|
VoiceChannel: "mention",
|
|
|
|
};
|
|
|
|
|
|
|
|
interface Data {
|
|
|
|
server?: Record<string, string>;
|
|
|
|
channel?: Record<string, string>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages the user's notification preferences.
|
|
|
|
*/
|
2021-12-11 16:24:23 +00:00
|
|
|
export default class NotificationOptions implements Store, Persistent<Data> {
|
2021-12-10 17:00:34 +00:00
|
|
|
private server: ObservableMap<string, string>;
|
|
|
|
private channel: ObservableMap<string, string>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct new Experiments store.
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
this.server = new ObservableMap();
|
|
|
|
this.channel = new ObservableMap();
|
|
|
|
makeAutoObservable(this);
|
|
|
|
}
|
|
|
|
|
2021-12-11 16:24:23 +00:00
|
|
|
get id() {
|
|
|
|
return "notifications";
|
|
|
|
}
|
|
|
|
|
2021-12-10 17:00:34 +00:00
|
|
|
toJSON() {
|
|
|
|
return {
|
2021-12-11 16:24:23 +00:00
|
|
|
server: mapToRecord(this.server),
|
|
|
|
channel: mapToRecord(this.channel),
|
2021-12-10 17:00:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@action hydrate(data: Data) {
|
|
|
|
if (data.server) {
|
|
|
|
Object.keys(data.server).forEach((key) =>
|
|
|
|
this.server.set(key, data.server![key]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.channel) {
|
|
|
|
Object.keys(data.channel).forEach((key) =>
|
|
|
|
this.channel.set(key, data.channel![key]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: implement
|
|
|
|
}
|