PageSourceSearch

https://nhatrovn.vn/static/chatapp/js/services/group-notification-preferences.js

js nhatrovn.vn collected 2026-10-04 12:02:06 UTC 4,790 bytes, 168 lines download raw bytes

1/**
2 * Group Notification Preferences Service
3 * Manages per-group notification and sound preferences in localStorage
4 */
5
6const STORAGE_KEY = 'chat_group_notification_preferences';
7
8class GroupNotificationPreferences {
9    constructor() {
10        this.preferences = this.loadPreferences();
11    }
12
13    /**
14     * Load preferences from localStorage
15     * @returns {Object} - Preferences object
16     */
17    loadPreferences() {
18        try {
19            const stored = localStorage.getItem(STORAGE_KEY);
20            if (stored) {
21                return JSON.parse(stored);
22            }
23        } catch (error) {
24            console.error('Error loading group notification preferences:', error);
25        }
26        return {};
27    }
28
29    /**
30     * Save preferences to localStorage
31     */
32    savePreferences() {
33        try {
34            localStorage.setItem(STORAGE_KEY, JSON.stringify(this.preferences));
35        } catch (error) {
36            console.error('Error saving group notification preferences:', error);
37        }
38    }
39
40    /**
41     * Get preferences for a specific group
42     * @param {string} groupId - Group ID
43     * @returns {Object} - Group preferences with defaults
44     */
45    getGroupPreferences(groupId) {
46        if (!groupId) return { notifications: true, sound: true };
47
48        return this.preferences[groupId] || { notifications: true, sound: true };
49    }
50
51    /**
52     * Check if notifications are enabled for a group
53     * @param {string} groupId - Group ID
54     * @returns {boolean} - True if notifications enabled
55     */
56    isNotificationsEnabled(groupId) {
57        const prefs = this.getGroupPreferences(groupId);
58        return prefs.notifications !== false; // Default to true if not set
59    }
60
61    /**
62     * Check if sound is enabled for a group
63     * @param {string} groupId - Group ID
64     * @returns {boolean} - True if sound enabled
65     */
66    isSoundEnabled(groupId) {
67        const prefs = this.getGroupPreferences(groupId);
68        return prefs.sound !== false; // Default to true if not set
69    }
70
71    /**
72     * Toggle notifications for a group
73     * @param {string} groupId - Group ID
74     * @returns {boolean} - New notifications state
75     */
76    toggleNotifications(groupId) {
77        if (!groupId) return true;
78
79        if (!this.preferences[groupId]) {
80            this.preferences[groupId] = { notifications: true, sound: true };
81        }
82
83        this.preferences[groupId].notifications = !this.preferences[groupId].notifications;
84        this.savePreferences();
85
86        return this.preferences[groupId].notifications;
87    }
88
89    /**
90     * Toggle sound for a group
91     * @param {string} groupId - Group ID
92     * @returns {boolean} - New sound state
93     */
94    toggleSound(groupId) {
95        if (!groupId) return true;
96
97        if (!this.preferences[groupId]) {
98            this.preferences[groupId] = { notifications: true, sound: true };
99        }
100
101        this.preferences[groupId].sound = !this.preferences[groupId].sound;
102        this.savePreferences();
103
104        return this.preferences[groupId].sound;
105    }
106
107    /**
108     * Set notifications preference for a group
109     * @param {string} groupId - Group ID
110     * @param {boolean} enabled - Enable or disable
111     */
112    setNotifications(groupId, enabled) {
113        if (!groupId) return;
114
115        if (!this.preferences[groupId]) {
116            this.preferences[groupId] = { notifications: true, sound: true };
117        }
118
119        this.preferences[groupId].notifications = enabled;
120        this.savePreferences();
121    }
122
123    /**
124     * Set sound preference for a group
125     * @param {string} groupId - Group ID
126     * @param {boolean} enabled - Enable or disable
127     */
128    setSound(groupId, enabled) {
129        if (!groupId) return;
130
131        if (!this.preferences[groupId]) {
132            this.preferences[groupId] = { notifications: true, sound: true };
133        }
134
135        this.preferences[groupId].sound = enabled;
136        this.savePreferences();
137    }
138
139    /**
140     * Get all preferences
141     * @returns {Object} - All group preferences
142     */
143    getAllPreferences() {
144        return { ...this.preferences };
145    }
146
147    /**
148     * Clear all preferences
149     */
150    clearAll() {
151        this.preferences = {};
152        this.savePreferences();
153    }
154
155    /**
156     * Clear preferences for a specific group
157     * @param {string} groupId - Group ID
158     */
159    clearGroup(groupId) {
160        if (!groupId) return;
161
162        delete this.preferences[groupId];
163        this.savePreferences();
164    }
165}
166
167// Export singleton instance
168export const groupNotificationPreferences = new GroupNotificationPreferences();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.