mirror of
https://github.com/revoltchat/revite.git
synced 2024-12-24 22:52:09 -05:00
chore(mobx): write jsdoc for notif opt.
This commit is contained in:
parent
413bf6949b
commit
ec83230c59
3 changed files with 66 additions and 16 deletions
|
@ -53,11 +53,7 @@ function Notifier({ options }: Props) {
|
||||||
|
|
||||||
const message = useCallback(
|
const message = useCallback(
|
||||||
async (msg: Message) => {
|
async (msg: Message) => {
|
||||||
if (msg.author_id === client.user!._id) return;
|
|
||||||
if (msg.channel_id === channel_id && document.hasFocus()) return;
|
if (msg.channel_id === channel_id && document.hasFocus()) return;
|
||||||
if (client.user!.status?.presence === Presence.Busy) return;
|
|
||||||
if (msg.author?.relationship === RelationshipStatus.Blocked) return;
|
|
||||||
|
|
||||||
if (!notifs.shouldNotify(msg)) return;
|
if (!notifs.shouldNotify(msg)) return;
|
||||||
|
|
||||||
playSound("message");
|
playSound("message");
|
||||||
|
|
|
@ -1,18 +1,16 @@
|
||||||
need to have a way to dump or sync to local storage
|
|
||||||
need a way to rehydrate data stores
|
|
||||||
split settings per account(?)
|
split settings per account(?)
|
||||||
multiple accounts need to be supported
|
multiple accounts need to be supported
|
||||||
oop
|
|
||||||
redux -> mobx migration (wipe existing redux data post-migration)
|
redux -> mobx migration (wipe existing redux data post-migration)
|
||||||
look into talking with other tabs to detect multiple instances
|
|
||||||
(also use this to tell the user to close all tabs before updating)
|
> look into talking with other tabs to detect multiple instances
|
||||||
|
> (also use this to tell the user to close all tabs before updating)
|
||||||
|
|
||||||
write new settings data structures for server-side
|
write new settings data structures for server-side
|
||||||
(deprecate existing API and replace with new endpoints?)
|
---- (deprecate existing API and replace with new endpoints?)
|
||||||
alternatively: keep using current system and eventually migrate
|
alternatively: keep using current system and eventually migrate
|
||||||
or: handle both incoming types of data and keep newer version
|
or: handle both incoming types of data and keep newer version (v1_prefix)
|
||||||
need to document these data structures
|
need to document these data structures
|
||||||
handle missing languages by falling back on en_GB
|
|
||||||
|
|
||||||
provide state globally? perform all authentication from inside mobx
|
provide state globally? perform all authentication from inside mobx
|
||||||
mobx parent holds client information and prepares us for first render
|
mobx parent holds client information and prepares us for first render
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { action, computed, makeAutoObservable, ObservableMap } from "mobx";
|
import { action, computed, makeAutoObservable, ObservableMap } from "mobx";
|
||||||
|
import { Presence, RelationshipStatus } from "revolt-api/types/Users";
|
||||||
import { Channel } from "revolt.js/dist/maps/Channels";
|
import { Channel } from "revolt.js/dist/maps/Channels";
|
||||||
import { Message } from "revolt.js/dist/maps/Messages";
|
import { Message } from "revolt.js/dist/maps/Messages";
|
||||||
import { Server } from "revolt.js/dist/maps/Servers";
|
import { Server } from "revolt.js/dist/maps/Servers";
|
||||||
|
@ -79,6 +80,11 @@ export default class NotificationOptions implements Store, Persistent<Data> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compute the actual notification state for a given Channel.
|
||||||
|
* @param channel Channel
|
||||||
|
* @returns Notification state
|
||||||
|
*/
|
||||||
computeForChannel(channel: Channel) {
|
computeForChannel(channel: Channel) {
|
||||||
if (this.channel.has(channel._id)) {
|
if (this.channel.has(channel._id)) {
|
||||||
return this.channel.get(channel._id);
|
return this.channel.get(channel._id);
|
||||||
|
@ -91,21 +97,46 @@ export default class NotificationOptions implements Store, Persistent<Data> {
|
||||||
return DEFAULT_STATES[channel.channel_type];
|
return DEFAULT_STATES[channel.channel_type];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether an incoming message should notify the user.
|
||||||
|
* @param message Message
|
||||||
|
* @returns Whether it should notify the user
|
||||||
|
*/
|
||||||
shouldNotify(message: Message) {
|
shouldNotify(message: Message) {
|
||||||
const state = this.computeForChannel(message.channel!);
|
// Make sure the author is not blocked.
|
||||||
|
if (message.author?.relationship === RelationshipStatus.Blocked) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
switch (state) {
|
// Check if the message was sent by us.
|
||||||
|
const user = message.client.user!;
|
||||||
|
if (message.author_id === user._id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether we are busy.
|
||||||
|
if (user.status?.presence === Presence.Busy) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (this.computeForChannel(message.channel!)) {
|
||||||
case "muted":
|
case "muted":
|
||||||
case "none":
|
case "none":
|
||||||
|
// Ignore if muted.
|
||||||
return false;
|
return false;
|
||||||
case "mention":
|
case "mention":
|
||||||
if (!message.mention_ids?.includes(message.client.user!._id))
|
// Ignore if it doesn't mention us.
|
||||||
return false;
|
if (!message.mention_ids?.includes(user._id)) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compute the notification state for a given server.
|
||||||
|
* @param server_id Server ID
|
||||||
|
* @returns Notification state
|
||||||
|
*/
|
||||||
computeForServer(server_id: string) {
|
computeForServer(server_id: string) {
|
||||||
if (this.server.has(server_id)) {
|
if (this.server.has(server_id)) {
|
||||||
return this.server.get(server_id);
|
return this.server.get(server_id);
|
||||||
|
@ -114,10 +145,20 @@ export default class NotificationOptions implements Store, Persistent<Data> {
|
||||||
return DEFAULT_SERVER_STATE;
|
return DEFAULT_SERVER_STATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the notification state of a channel.
|
||||||
|
* @param channel_id Channel ID
|
||||||
|
* @returns Notification state
|
||||||
|
*/
|
||||||
getChannelState(channel_id: string) {
|
getChannelState(channel_id: string) {
|
||||||
return this.channel.get(channel_id);
|
return this.channel.get(channel_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the notification state of a channel.
|
||||||
|
* @param channel_id Channel ID
|
||||||
|
* @param state Notification state
|
||||||
|
*/
|
||||||
setChannelState(channel_id: string, state?: NotificationState) {
|
setChannelState(channel_id: string, state?: NotificationState) {
|
||||||
if (state) {
|
if (state) {
|
||||||
this.channel.set(channel_id, state);
|
this.channel.set(channel_id, state);
|
||||||
|
@ -126,10 +167,20 @@ export default class NotificationOptions implements Store, Persistent<Data> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the notification state of a server.
|
||||||
|
* @param server_id Server ID
|
||||||
|
* @returns Notification state
|
||||||
|
*/
|
||||||
getServerState(server_id: string) {
|
getServerState(server_id: string) {
|
||||||
return this.server.get(server_id);
|
return this.server.get(server_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the notification state of a server.
|
||||||
|
* @param server_id Server ID
|
||||||
|
* @param state Notification state
|
||||||
|
*/
|
||||||
setServerState(server_id: string, state?: NotificationState) {
|
setServerState(server_id: string, state?: NotificationState) {
|
||||||
if (state) {
|
if (state) {
|
||||||
this.server.set(server_id, state);
|
this.server.set(server_id, state);
|
||||||
|
@ -138,6 +189,11 @@ export default class NotificationOptions implements Store, Persistent<Data> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether a Channel or Server is muted.
|
||||||
|
* @param target Channel or Server
|
||||||
|
* @returns Whether this object is muted
|
||||||
|
*/
|
||||||
isMuted(target?: Channel | Server) {
|
isMuted(target?: Channel | Server) {
|
||||||
if (target instanceof Channel) {
|
if (target instanceof Channel) {
|
||||||
return this.computeForChannel(target) === "muted";
|
return this.computeForChannel(target) === "muted";
|
||||||
|
|
Loading…
Reference in a new issue