atagen

malice

public
Star0
Watch0
d1818305ffd6912e4b9ac7b706c43eccd01bdd21
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/src/lib/mute.ts

00ca0561ecedatagen2026-10-26 05:50 UTC1
import { derived, get } from 'svelte/store';
00ca0561ecedatagen2026-10-26 05:50 UTC2
import { runCommand, setAccountData } from './api';
00ca0561ecedatagen2026-10-26 05:50 UTC3
import {
00ca0561ecedatagen2026-10-26 05:50 UTC4
  muteSpacePlan,
00ca0561ecedatagen2026-10-26 05:50 UTC5
  reconcilePlan,
00ca0561ecedatagen2026-10-26 05:50 UTC6
  spaceMutedRoomIds,
00ca0561ecedatagen2026-10-26 05:50 UTC7
  unmuteSpacePlan,
00ca0561ecedatagen2026-10-26 05:50 UTC8
  withSpaceMutes,
00ca0561ecedatagen2026-10-26 05:50 UTC9
  type Plan,
00ca0561ecedatagen2026-10-26 05:50 UTC10
  type RoomMode
00ca0561ecedatagen2026-10-26 05:50 UTC11
} from './mute-model';
00ca0561ecedatagen2026-10-26 05:50 UTC12
import { MUTED_SPACES_EVENT, mutedSpaces, mutedSpacesLoaded } from './muted-spaces';
00ca0561ecedatagen2026-10-26 05:50 UTC13
import { rooms } from './store';
00ca0561ecedatagen2026-10-26 05:50 UTC14
00ca0561ecedatagen2026-10-26 05:50 UTC15
export const spaceMutedRooms = derived([rooms, mutedSpaces], ([$rooms, $muted]) => spaceMutedRoomIds($rooms, $muted.spaces));
00ca0561ecedatagen2026-10-26 05:50 UTC16
00ca0561ecedatagen2026-10-26 05:50 UTC17
export const effectiveRooms = derived([rooms, spaceMutedRooms], ([$rooms, $muted]) => withSpaceMutes($rooms, $muted));
00ca0561ecedatagen2026-10-26 05:50 UTC18
00ca0561ecedatagen2026-10-26 05:50 UTC19
let queue: Promise<void> = Promise.resolve();
00ca0561ecedatagen2026-10-26 05:50 UTC20
00ca0561ecedatagen2026-10-26 05:50 UTC21
export function setRoomMode(roomId: string, mode: RoomMode | null): Promise<void> {
00ca0561ecedatagen2026-10-26 05:50 UTC22
  return runCommand({ op: 'set_room_notification_mode', room_id: roomId, mode }).then(() => undefined);
00ca0561ecedatagen2026-10-26 05:50 UTC23
}
00ca0561ecedatagen2026-10-26 05:50 UTC24
00ca0561ecedatagen2026-10-26 05:50 UTC25
async function apply(plan: Plan): Promise<void> {
00ca0561ecedatagen2026-10-26 05:50 UTC26
  await setAccountData(MUTED_SPACES_EVENT, { ...plan.state });
00ca0561ecedatagen2026-10-26 05:50 UTC27
  mutedSpaces.set(plan.state);
00ca0561ecedatagen2026-10-26 05:50 UTC28
  const results = await Promise.allSettled(plan.changes.map((c) => setRoomMode(c.roomId, c.mode)));
00ca0561ecedatagen2026-10-26 05:50 UTC29
  const failed = results.find((r): r is PromiseRejectedResult => r.status === 'rejected');
00ca0561ecedatagen2026-10-26 05:50 UTC30
  if (failed) throw failed.reason;
00ca0561ecedatagen2026-10-26 05:50 UTC31
}
00ca0561ecedatagen2026-10-26 05:50 UTC32
00ca0561ecedatagen2026-10-26 05:50 UTC33
function enqueue(job: () => Promise<void>): Promise<void> {
00ca0561ecedatagen2026-10-26 05:50 UTC34
  const run = queue.then(job);
00ca0561ecedatagen2026-10-26 05:50 UTC35
  queue = run.catch(() => undefined);
00ca0561ecedatagen2026-10-26 05:50 UTC36
  return run;
00ca0561ecedatagen2026-10-26 05:50 UTC37
}
00ca0561ecedatagen2026-10-26 05:50 UTC38
00ca0561ecedatagen2026-10-26 05:50 UTC39
export function setSpaceMuted(spaceId: string, muted: boolean): Promise<void> {
00ca0561ecedatagen2026-10-26 05:50 UTC40
  return enqueue(async () => {
00ca0561ecedatagen2026-10-26 05:50 UTC41
    const state = get(mutedSpaces);
00ca0561ecedatagen2026-10-26 05:50 UTC42
    const all = get(rooms);
00ca0561ecedatagen2026-10-26 05:50 UTC43
    await apply(muted ? muteSpacePlan(state, spaceId, all) : unmuteSpacePlan(state, spaceId, all));
00ca0561ecedatagen2026-10-26 05:50 UTC44
  });
00ca0561ecedatagen2026-10-26 05:50 UTC45
}
00ca0561ecedatagen2026-10-26 05:50 UTC46
00ca0561ecedatagen2026-10-26 05:50 UTC47
export function startMuteSync(): () => void {
00ca0561ecedatagen2026-10-26 05:50 UTC48
  const reconcile = () => {
00ca0561ecedatagen2026-10-26 05:50 UTC49
    if (!mutedSpacesLoaded() || get(mutedSpaces).spaces.length === 0) return;
00ca0561ecedatagen2026-10-26 05:50 UTC50
    void enqueue(async () => {
00ca0561ecedatagen2026-10-26 05:50 UTC51
      const plan = reconcilePlan(get(mutedSpaces), get(rooms));
00ca0561ecedatagen2026-10-26 05:50 UTC52
      if (plan.changes.length > 0) await apply(plan);
00ca0561ecedatagen2026-10-26 05:50 UTC53
    }).catch(() => undefined);
00ca0561ecedatagen2026-10-26 05:50 UTC54
  };
00ca0561ecedatagen2026-10-26 05:50 UTC55
  const stopRooms = rooms.subscribe(reconcile);
00ca0561ecedatagen2026-10-26 05:50 UTC56
  const stopMuted = mutedSpaces.subscribe(reconcile);
00ca0561ecedatagen2026-10-26 05:50 UTC57
  return () => {
00ca0561ecedatagen2026-10-26 05:50 UTC58
    stopRooms();
00ca0561ecedatagen2026-10-26 05:50 UTC59
    stopMuted();
00ca0561ecedatagen2026-10-26 05:50 UTC60
  };
00ca0561ecedatagen2026-10-26 05:50 UTC61
}