import { Client } from "../Client" ; import { action , computed , makeAutoObservable , ObservableMap , runInAction , } from "mobx" ; import type { ChannelUnread } from "revolt-api/types/Sync" ; import { ulid } from "ulid" ; import { Channel } from "../maps/Channels" ; import { Server } from "../maps/Servers" ; export interface INotificationChecker { isMuted ( target ? ) : boolean ; } /**
 * Handles channel unreads.
 */ export default class Unreads { private client : Client ; private channels : ObservableMap < string , Omit < ChannelUnread , "_id" > > ; /**
     * Construct new Unreads store.
     */ constructor ( client ) { this . channels = new ObservableMap ( ) ; makeAutoObservable ( this ) ; this . client = client ; } /**
     * Sync unread data from the server.
     */ async sync ( ) { const unreads = await this . client . syncFetchUnreads ( ) ; runInAction ( ( ) => { for ( const unread of unreads ) { const { _id , ... data } = unread ; this . channels . set ( _id . channel , data ) ; } } ) ; } param channel_id returns @ computed getUnread ( channel_id ) { return this . channels . get ( channel_id ) ; } param channel_id param last_id @ action markUnread ( channel_id , last_id ) { this . channels . set ( channel_id , { ... this . getUnread ( channel_id ) , last_id , } ) ; } param channel_id param message_id @ action markMention ( channel_id , message_id ) { const unread = this . getUnread ( channel_id ) ; this . channels . set ( channel_id , { last_id : "0" , ... unread , mentions : [ ... ( unread ?. mentions ?? [ ] ) , message_id ] , } ) ; } param channel_id param message_id param emit param skipRateLimiter @ action markRead ( channel_id , message_id ? , emit = false , skipRateLimiter = false ) { const last_id = message_id ?? ulid ( ) ; this . channels . set ( channel_id , { last_id } ) ; if ( emit ) { this . client . channels . get ( channel_id ) ?. ack ( last_id , skipRateLimiter ) ; } } param channel_ids @ action markMultipleRead ( channel_ids ) { const last_id = ulid ( ) ; for ( const channel_id of channel_ids ) { this . channels . set ( channel_id , { last_id } ) ; } } }