迁移聊天室静音偏好工具

This commit is contained in:
2026-04-25 03:38:27 +08:00
parent 2f246f9112
commit c858f6af0c
3 changed files with 126 additions and 6 deletions
+15
View File
@@ -7,9 +7,14 @@ export {
BLOCKABLE_SYSTEM_SENDERS,
BLOCKED_SYSTEM_SENDERS_STORAGE_KEY,
CHAT_SOUND_MUTED_STORAGE_KEY,
isSoundMuted,
loadBlockedSystemSenders,
normalizeChatPreferences,
normalizeDailyStatus,
parseDailyStatusExpiry,
persistBlockedSystemSenders,
setSoundMuted,
shouldMigrateLocalChatPreferences,
} from "./chat-room/preferences-status.js";
export {
normalizeRoomStatus,
@@ -27,9 +32,14 @@ import {
BLOCKABLE_SYSTEM_SENDERS,
BLOCKED_SYSTEM_SENDERS_STORAGE_KEY,
CHAT_SOUND_MUTED_STORAGE_KEY,
isSoundMuted,
loadBlockedSystemSenders,
normalizeChatPreferences,
normalizeDailyStatus,
parseDailyStatusExpiry,
persistBlockedSystemSenders,
setSoundMuted,
shouldMigrateLocalChatPreferences,
} from "./chat-room/preferences-status.js";
import {
normalizeRoomStatus,
@@ -53,9 +63,14 @@ if (typeof window !== "undefined") {
BLOCKABLE_SYSTEM_SENDERS,
BLOCKED_SYSTEM_SENDERS_STORAGE_KEY,
CHAT_SOUND_MUTED_STORAGE_KEY,
isSoundMuted,
loadBlockedSystemSenders,
normalizeChatPreferences,
normalizeDailyStatus,
parseDailyStatusExpiry,
persistBlockedSystemSenders,
setSoundMuted,
shouldMigrateLocalChatPreferences,
normalizeRoomStatus,
renderRoomStatusRow,
renderRoomsOnlineStatus,
@@ -73,3 +73,81 @@ export function normalizeDailyStatus(raw, nowTimestamp = Date.now()) {
expires_at: parsedExpiry.toISOString(),
};
}
/**
* 从本地缓存读取已屏蔽的系统播报发送者列表。
*
* @param {string[]} blockableSystemSenders
* @returns {string[]}
*/
export function loadBlockedSystemSenders(blockableSystemSenders = BLOCKABLE_SYSTEM_SENDERS) {
try {
const saved = JSON.parse(localStorage.getItem(BLOCKED_SYSTEM_SENDERS_STORAGE_KEY) || "[]");
if (!Array.isArray(saved)) {
return [];
}
return saved.filter((sender) => blockableSystemSenders.includes(sender));
} catch (error) {
return [];
}
}
/**
* 将屏蔽的系统播报发送者列表写入本地缓存。
*
* @param {Iterable<string>} senders
* @returns {void}
*/
export function persistBlockedSystemSenders(senders) {
localStorage.setItem(BLOCKED_SYSTEM_SENDERS_STORAGE_KEY, JSON.stringify(Array.from(senders)));
}
/**
* 判断当前禁音开关是否处于打开状态。
*
* @returns {boolean}
*/
export function isSoundMuted() {
const muteCheckbox = document.getElementById("sound_muted");
if (muteCheckbox) {
return Boolean(muteCheckbox.checked);
}
return localStorage.getItem(CHAT_SOUND_MUTED_STORAGE_KEY) === "1";
}
/**
* 同步禁音复选框和本地缓存。
*
* @param {boolean} muted 是否禁音
* @returns {boolean}
*/
export function setSoundMuted(muted) {
const normalizedMuted = Boolean(muted);
localStorage.setItem(CHAT_SOUND_MUTED_STORAGE_KEY, normalizedMuted ? "1" : "0");
const muteCheckbox = document.getElementById("sound_muted");
if (muteCheckbox) {
muteCheckbox.checked = normalizedMuted;
}
return normalizedMuted;
}
/**
* 当前登录账号没有服务端偏好时,判断是否需要迁移旧本地偏好。
*
* @param {{blocked_system_senders?:string[],sound_muted?:boolean}} serverPreferences
* @param {string[]} localBlockedSenders
* @param {boolean} localMuted
* @returns {boolean}
*/
export function shouldMigrateLocalChatPreferences(serverPreferences, localBlockedSenders, localMuted) {
const hasServerPreferences = (serverPreferences?.blocked_system_senders || []).length > 0
|| Boolean(serverPreferences?.sound_muted);
return !hasServerPreferences && (localBlockedSenders.length > 0 || localMuted);
}