迁移聊天室静音偏好工具

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);
}
@@ -1047,6 +1047,10 @@
* @returns {string[]}
*/
function loadBlockedSystemSenders() {
if (window.ChatRoomTools?.loadBlockedSystemSenders) {
return window.ChatRoomTools.loadBlockedSystemSenders(BLOCKABLE_SYSTEM_SENDERS);
}
try {
const saved = JSON.parse(localStorage.getItem(BLOCKED_SYSTEM_SENDERS_STORAGE_KEY) || '[]');
if (!Array.isArray(saved)) {
@@ -1064,6 +1068,11 @@
* 将当前屏蔽配置持久化到 localStorage。
*/
function persistBlockedSystemSenders() {
if (window.ChatRoomTools?.persistBlockedSystemSenders) {
window.ChatRoomTools.persistBlockedSystemSenders(blockedSystemSenders);
return;
}
localStorage.setItem(
BLOCKED_SYSTEM_SENDERS_STORAGE_KEY,
JSON.stringify(Array.from(blockedSystemSenders))
@@ -1076,6 +1085,10 @@
* @returns {boolean}
*/
function isSoundMuted() {
if (window.ChatRoomTools?.isSoundMuted) {
return window.ChatRoomTools.isSoundMuted();
}
const muteCheckbox = document.getElementById('sound_muted');
if (muteCheckbox) {
@@ -1102,6 +1115,11 @@
*/
function persistChatPreferencesToLocal() {
persistBlockedSystemSenders();
if (window.ChatRoomTools?.setSoundMuted) {
window.ChatRoomTools.setSoundMuted(isSoundMuted());
return;
}
localStorage.setItem(CHAT_SOUND_MUTED_STORAGE_KEY, isSoundMuted() ? '1' : '0');
}
@@ -3264,9 +3282,10 @@
const storedBlockedSystemSenders = loadBlockedSystemSenders();
const mutedFromLocal = localStorage.getItem(CHAT_SOUND_MUTED_STORAGE_KEY) === '1';
const hasServerPreferences = initialChatPreferences.blocked_system_senders.length > 0 || initialChatPreferences.sound_muted;
const shouldMigrateLocalPreferences = !hasServerPreferences
&& (storedBlockedSystemSenders.length > 0 || mutedFromLocal);
const shouldMigrateLocalPreferences = window.ChatRoomTools?.shouldMigrateLocalChatPreferences
? window.ChatRoomTools.shouldMigrateLocalChatPreferences(initialChatPreferences, storedBlockedSystemSenders, mutedFromLocal)
: !(initialChatPreferences.blocked_system_senders.length > 0 || initialChatPreferences.sound_muted)
&& (storedBlockedSystemSenders.length > 0 || mutedFromLocal);
if (shouldMigrateLocalPreferences) {
blockedSystemSenders = new Set(storedBlockedSystemSenders);
@@ -3274,8 +3293,12 @@
// 恢复禁音复选框状态;默认一律为未禁音。
const muted = shouldMigrateLocalPreferences ? mutedFromLocal : initialChatPreferences.sound_muted;
const muteChk = document.getElementById('sound_muted');
if (muteChk) muteChk.checked = muted;
if (window.ChatRoomTools?.setSoundMuted) {
window.ChatRoomTools.setSoundMuted(muted);
} else {
const muteChk = document.getElementById('sound_muted');
if (muteChk) muteChk.checked = muted;
}
syncBlockedSystemSenderCheckboxes();
if (shouldMigrateLocalPreferences) {
@@ -3293,7 +3316,11 @@
* @param {boolean} muted true = 禁音,false = 开启声音
*/
function toggleSoundMute(muted) {
localStorage.setItem(CHAT_SOUND_MUTED_STORAGE_KEY, muted ? '1' : '0');
if (window.ChatRoomTools?.setSoundMuted) {
window.ChatRoomTools.setSoundMuted(muted);
} else {
localStorage.setItem(CHAT_SOUND_MUTED_STORAGE_KEY, muted ? '1' : '0');
}
if (muted && typeof EffectSounds !== 'undefined') {
EffectSounds.stop(); // 立即停止当前音效
}