Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69e05bbf8d |
@@ -80,7 +80,7 @@ import {
|
|||||||
setRoomNotificationPreference,
|
setRoomNotificationPreference,
|
||||||
} from '../../hooks/useRoomsNotificationPreferences';
|
} from '../../hooks/useRoomsNotificationPreferences';
|
||||||
import { RoomNotificationModeSwitcher } from '../../components/RoomNotificationSwitcher';
|
import { RoomNotificationModeSwitcher } from '../../components/RoomNotificationSwitcher';
|
||||||
import { scheduleMuteTimer, unmuteRoom } from './muteTimers';
|
import { clearMuteTimer, scheduleMuteTimer } from './muteTimers';
|
||||||
import { getRoomCreatorsForRoomId, useRoomCreators } from '../../hooks/useRoomCreators';
|
import { getRoomCreatorsForRoomId, useRoomCreators } from '../../hooks/useRoomCreators';
|
||||||
import { getRoomPermissionsAPI, useRoomPermissions } from '../../hooks/useRoomPermissions';
|
import { getRoomPermissionsAPI, useRoomPermissions } from '../../hooks/useRoomPermissions';
|
||||||
import { InviteUserPrompt } from '../../components/invite-user-prompt';
|
import { InviteUserPrompt } from '../../components/invite-user-prompt';
|
||||||
@@ -381,9 +381,10 @@ const RoomNavItemMenu = forwardRef<HTMLDivElement, RoomNavItemMenuProps>(
|
|||||||
prevMode,
|
prevMode,
|
||||||
).catch(() => {});
|
).catch(() => {});
|
||||||
if (durationMs !== null) {
|
if (durationMs !== null) {
|
||||||
scheduleMuteTimer(room.roomId, durationMs, () => {
|
await scheduleMuteTimer(mx, room.roomId, durationMs).catch(() => {});
|
||||||
unmuteRoom(mx, room.roomId);
|
} else {
|
||||||
});
|
// An indefinite mute replaces any timed one.
|
||||||
|
await clearMuteTimer(mx, room.roomId);
|
||||||
}
|
}
|
||||||
requestClose();
|
requestClose();
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { test } from 'node:test';
|
import { test } from 'node:test';
|
||||||
import assert from 'node:assert/strict';
|
import assert from 'node:assert/strict';
|
||||||
import { RoomNotificationMode } from '../../hooks/useRoomsNotificationPreferences';
|
import { RoomNotificationMode } from '../../hooks/useRoomsNotificationPreferences';
|
||||||
import { shouldResetMuteOnUnmute } from './muteTimers';
|
import { muteTimersContent, parseMuteTimers, shouldResetMuteOnUnmute } from './muteTimers';
|
||||||
|
|
||||||
test('resets to Unset when the room is still Mute at expiry', () => {
|
test('resets to Unset when the room is still Mute at expiry', () => {
|
||||||
assert.equal(shouldResetMuteOnUnmute(RoomNotificationMode.Mute), true);
|
assert.equal(shouldResetMuteOnUnmute(RoomNotificationMode.Mute), true);
|
||||||
@@ -18,3 +18,16 @@ test('does not reset when the user switched to Special messages during the mute
|
|||||||
test('does not reset when the mode is already Unset', () => {
|
test('does not reset when the mode is already Unset', () => {
|
||||||
assert.equal(shouldResetMuteOnUnmute(RoomNotificationMode.Unset), false);
|
assert.equal(shouldResetMuteOnUnmute(RoomNotificationMode.Unset), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('parseMuteTimers / muteTimersContent round-trip and tolerate garbage (#333)', () => {
|
||||||
|
const entries = [
|
||||||
|
{ roomId: '!a:x', unmuteAt: 1000 },
|
||||||
|
{ roomId: '!b:x', unmuteAt: 2000 },
|
||||||
|
];
|
||||||
|
assert.deepEqual(parseMuteTimers(muteTimersContent(entries)), entries);
|
||||||
|
assert.deepEqual(parseMuteTimers({ timers: { '!a:x': 'soon', '!b:x': 5 } }), [
|
||||||
|
{ roomId: '!b:x', unmuteAt: 5 },
|
||||||
|
]);
|
||||||
|
assert.deepEqual(parseMuteTimers(undefined), []);
|
||||||
|
assert.deepEqual(parseMuteTimers({ timers: null }), []);
|
||||||
|
});
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
setRoomNotificationPreference,
|
setRoomNotificationPreference,
|
||||||
} from '../../hooks/useRoomsNotificationPreferences';
|
} from '../../hooks/useRoomsNotificationPreferences';
|
||||||
|
|
||||||
// localStorage key for timed mute timers
|
// Legacy localStorage key (before #333); migrated into account data once.
|
||||||
export const MUTE_TIMERS_KEY = 'io.lotus.mute_timers';
|
export const MUTE_TIMERS_KEY = 'io.lotus.mute_timers';
|
||||||
|
|
||||||
// setTimeout's delay is a signed 32-bit int; larger values overflow and fire
|
// setTimeout's delay is a signed 32-bit int; larger values overflow and fire
|
||||||
@@ -16,17 +16,60 @@ export const MAX_MUTE_TIMEOUT_MS = 2_147_483_647;
|
|||||||
|
|
||||||
export type MuteTimerEntry = { roomId: string; unmuteAt: number };
|
export type MuteTimerEntry = { roomId: string; unmuteAt: number };
|
||||||
|
|
||||||
export function loadMuteTimers(): MuteTimerEntry[] {
|
/** Pure: the timers in an `io.lotus.mute_timers` account data content. */
|
||||||
try {
|
export function parseMuteTimers(content: unknown): MuteTimerEntry[] {
|
||||||
const parsed = JSON.parse(localStorage.getItem(MUTE_TIMERS_KEY) ?? '[]');
|
const timers = (content as { timers?: unknown } | undefined)?.timers;
|
||||||
return Array.isArray(parsed) ? parsed : [];
|
if (!timers || typeof timers !== 'object') return [];
|
||||||
} catch {
|
return Object.entries(timers as Record<string, unknown>)
|
||||||
return [];
|
.filter((e): e is [string, number] => typeof e[1] === 'number' && Number.isFinite(e[1]))
|
||||||
}
|
.map(([roomId, unmuteAt]) => ({ roomId, unmuteAt }));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function saveMuteTimers(timers: MuteTimerEntry[]): void {
|
/** Pure: account data content for `entries`. */
|
||||||
localStorage.setItem(MUTE_TIMERS_KEY, JSON.stringify(timers));
|
export const muteTimersContent = (entries: MuteTimerEntry[]) => ({
|
||||||
|
timers: Object.fromEntries(entries.map((e) => [e.roomId, e.unmuteAt])),
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [Gitea #333] Timed mutes live in account data: the mute itself is a
|
||||||
|
* server-side push rule on every device, so the timer must be too — kept in one
|
||||||
|
* browser's localStorage, only that device ever unmuted (and never after a
|
||||||
|
* logout there).
|
||||||
|
*/
|
||||||
|
export function loadMuteTimers(mx: MatrixClient): MuteTimerEntry[] {
|
||||||
|
return parseMuteTimers(getAccountData(mx, AccountDataEvent.LotusMuteTimers)?.getContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
async function saveMuteTimers(mx: MatrixClient, entries: MuteTimerEntry[]): Promise<void> {
|
||||||
|
await mx.setAccountData(
|
||||||
|
AccountDataEvent.LotusMuteTimers as never,
|
||||||
|
muteTimersContent(entries) as never,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Move timers kept in this browser's localStorage (pre-#333) into account data. */
|
||||||
|
export async function migrateLocalMuteTimers(mx: MatrixClient): Promise<void> {
|
||||||
|
let legacy: MuteTimerEntry[] = [];
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(localStorage.getItem(MUTE_TIMERS_KEY) ?? '[]');
|
||||||
|
legacy = Array.isArray(parsed)
|
||||||
|
? parsed.filter(
|
||||||
|
(e): e is MuteTimerEntry =>
|
||||||
|
!!e && typeof e.roomId === 'string' && typeof e.unmuteAt === 'number',
|
||||||
|
)
|
||||||
|
: [];
|
||||||
|
} catch {
|
||||||
|
legacy = [];
|
||||||
|
}
|
||||||
|
if (legacy.length === 0) return;
|
||||||
|
const current = loadMuteTimers(mx);
|
||||||
|
const merged = [...current, ...legacy.filter((l) => !current.some((c) => c.roomId === l.roomId))];
|
||||||
|
await saveMuteTimers(mx, merged);
|
||||||
|
try {
|
||||||
|
localStorage.removeItem(MUTE_TIMERS_KEY);
|
||||||
|
} catch {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pure decision for the unmute guard: a timed mute should only be reset back to
|
// Pure decision for the unmute guard: a timed mute should only be reset back to
|
||||||
@@ -77,12 +120,30 @@ export async function unmuteRoom(mx: MatrixClient, roomId: string): Promise<void
|
|||||||
RoomNotificationMode.Mute,
|
RoomNotificationMode.Mute,
|
||||||
).catch(() => {});
|
).catch(() => {});
|
||||||
}
|
}
|
||||||
saveMuteTimers(loadMuteTimers().filter((e) => e.roomId !== roomId));
|
await clearMuteTimer(mx, roomId);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function scheduleMuteTimer(roomId: string, durationMs: number, onUnmute: () => void): void {
|
/** Drop a room's timer (e.g. it was muted indefinitely or unmuted by hand). */
|
||||||
const unmuteAt = Date.now() + durationMs;
|
export async function clearMuteTimer(mx: MatrixClient, roomId: string): Promise<void> {
|
||||||
const existing = loadMuteTimers().filter((e) => e.roomId !== roomId);
|
const timers = loadMuteTimers(mx);
|
||||||
saveMuteTimers([...existing, { roomId, unmuteAt }]);
|
if (!timers.some((e) => e.roomId === roomId)) return;
|
||||||
setTimeout(onUnmute, Math.min(durationMs, MAX_MUTE_TIMEOUT_MS));
|
await saveMuteTimers(
|
||||||
|
mx,
|
||||||
|
timers.filter((e) => e.roomId !== roomId),
|
||||||
|
).catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Record when a timed mute ends. MuteTimerRestore (on every Lotus device)
|
||||||
|
* schedules the unmute from account data, so whichever is online at expiry
|
||||||
|
* lifts it.
|
||||||
|
*/
|
||||||
|
export async function scheduleMuteTimer(
|
||||||
|
mx: MatrixClient,
|
||||||
|
roomId: string,
|
||||||
|
durationMs: number,
|
||||||
|
): Promise<void> {
|
||||||
|
const unmuteAt = Date.now() + durationMs;
|
||||||
|
const existing = loadMuteTimers(mx).filter((e) => e.roomId !== roomId);
|
||||||
|
await saveMuteTimers(mx, [...existing, { roomId, unmuteAt }]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,8 +58,8 @@ import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
|
|||||||
import { presenceStateFromSetting, usePresenceUpdater } from '../../hooks/usePresenceUpdater';
|
import { presenceStateFromSetting, usePresenceUpdater } from '../../hooks/usePresenceUpdater';
|
||||||
import {
|
import {
|
||||||
MAX_MUTE_TIMEOUT_MS,
|
MAX_MUTE_TIMEOUT_MS,
|
||||||
MuteTimerEntry,
|
|
||||||
loadMuteTimers,
|
loadMuteTimers,
|
||||||
|
migrateLocalMuteTimers,
|
||||||
unmuteRoom,
|
unmuteRoom,
|
||||||
} from '../../features/room-nav/muteTimers';
|
} from '../../features/room-nav/muteTimers';
|
||||||
import { STATUS_EXPIRY_KEY, STATUS_MSG_KEY } from '../../features/settings/account/Profile';
|
import { STATUS_EXPIRY_KEY, STATUS_MSG_KEY } from '../../features/settings/account/Profile';
|
||||||
@@ -91,6 +91,7 @@ import {
|
|||||||
shouldNotifyThreadReply,
|
shouldNotifyThreadReply,
|
||||||
THREAD_NOTIFICATIONS_FALLBACK_BEHAVIOR,
|
THREAD_NOTIFICATIONS_FALLBACK_BEHAVIOR,
|
||||||
} from '../../utils/threadNotifications';
|
} from '../../utils/threadNotifications';
|
||||||
|
import { AccountDataEvent } from '../../../types/matrix/accountData';
|
||||||
|
|
||||||
// Grace period after the initial sync settles before invite notifications arm, so
|
// Grace period after the initial sync settles before invite notifications arm, so
|
||||||
// the async invite-atom population lands first and isn't mistaken for new invites.
|
// the async invite-atom population lands first and isn't mistaken for new invites.
|
||||||
@@ -369,30 +370,35 @@ function PresenceUpdater() {
|
|||||||
function MuteTimerRestore() {
|
function MuteTimerRestore() {
|
||||||
const mx = useMatrixClient();
|
const mx = useMatrixClient();
|
||||||
|
|
||||||
|
// [Gitea #333] Timers are in account data, so a mute set on another device
|
||||||
|
// is lifted here too, and changes there (a new mute, a manual unmute)
|
||||||
|
// reschedule. Moves any timers this browser kept locally (pre-#333) first.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timers = loadMuteTimers();
|
migrateLocalMuteTimers(mx).catch(() => {});
|
||||||
if (timers.length === 0) return undefined;
|
}, [mx]);
|
||||||
|
|
||||||
const now = Date.now();
|
|
||||||
const pastDue: MuteTimerEntry[] = [];
|
|
||||||
const future: MuteTimerEntry[] = [];
|
|
||||||
timers.forEach((entry) => (entry.unmuteAt <= now ? pastDue : future).push(entry));
|
|
||||||
|
|
||||||
pastDue.forEach((entry) => {
|
|
||||||
unmuteRoom(mx, entry.roomId);
|
|
||||||
});
|
|
||||||
|
|
||||||
const handles = future.map((entry) =>
|
|
||||||
setTimeout(
|
|
||||||
() => {
|
|
||||||
unmuteRoom(mx, entry.roomId);
|
|
||||||
},
|
|
||||||
Math.min(entry.unmuteAt - now, MAX_MUTE_TIMEOUT_MS),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let handles: ReturnType<typeof setTimeout>[] = [];
|
||||||
|
const schedule = () => {
|
||||||
|
handles.forEach(clearTimeout);
|
||||||
|
const now = Date.now();
|
||||||
|
handles = loadMuteTimers(mx).map((entry) =>
|
||||||
|
setTimeout(
|
||||||
|
() => {
|
||||||
|
unmuteRoom(mx, entry.roomId);
|
||||||
|
},
|
||||||
|
Math.max(0, Math.min(entry.unmuteAt - now, MAX_MUTE_TIMEOUT_MS)),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
const onAccountData = (event: MatrixEvent) => {
|
||||||
|
if (event.getType() === AccountDataEvent.LotusMuteTimers) schedule();
|
||||||
|
};
|
||||||
|
schedule();
|
||||||
|
mx.on(ClientEvent.AccountData, onAccountData);
|
||||||
return () => {
|
return () => {
|
||||||
handles.forEach(clearTimeout);
|
handles.forEach(clearTimeout);
|
||||||
|
mx.removeListener(ClientEvent.AccountData, onAccountData);
|
||||||
};
|
};
|
||||||
}, [mx]);
|
}, [mx]);
|
||||||
|
|
||||||
|
|||||||
@@ -64,9 +64,10 @@ const clearMsgDrafts = (): void => {
|
|||||||
* `lotus-status-expiry-*`, used to be preserved; since [Gitea #204] they are
|
* `lotus-status-expiry-*`, used to be preserved; since [Gitea #204] they are
|
||||||
* swept with the rest — the server-side presence status survives, so a
|
* swept with the rest — the server-side presence status survives, so a
|
||||||
* re-login loses nothing)
|
* re-login loses nothing)
|
||||||
* - low-sensitivity UI/metadata residue (`io.lotus.mute_timers`, collapsed
|
* - low-sensitivity UI/metadata residue (collapsed nav/space categories,
|
||||||
* nav/space categories, `cinny_oidc_dynamic_clients`) is treated as
|
* `cinny_oidc_dynamic_clients`) is treated as preferences, not swept here.
|
||||||
* preferences, not swept here
|
* Timed mutes moved to account data in [Gitea #333]; a leftover local
|
||||||
|
* `io.lotus.mute_timers` is migrated there on the next start.
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
* [Gitea #204] The local mirror of the user's status message (+ its expiry) is
|
* [Gitea #204] The local mirror of the user's status message (+ its expiry) is
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ export enum AccountDataEvent {
|
|||||||
// [Gitea #104] Synced subset of the Lotus Settings object (utils/settingsSync).
|
// [Gitea #104] Synced subset of the Lotus Settings object (utils/settingsSync).
|
||||||
LotusSettings = 'io.lotus.settings',
|
LotusSettings = 'io.lotus.settings',
|
||||||
|
|
||||||
|
// [Gitea #333] Timed mutes: { timers: { [roomId]: unmuteAt } }, so any Lotus
|
||||||
|
// device can lift a mute set on another.
|
||||||
|
LotusMuteTimers = 'io.lotus.mute_timers',
|
||||||
|
|
||||||
SecretStorageDefaultKey = 'm.secret_storage.default_key',
|
SecretStorageDefaultKey = 'm.secret_storage.default_key',
|
||||||
|
|
||||||
CrossSigningMaster = 'm.cross_signing.master',
|
CrossSigningMaster = 'm.cross_signing.master',
|
||||||
|
|||||||
Reference in New Issue
Block a user