atagen

malice

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

Blame: web/src/lib/pending.ts

973f8fbdaf80atagen2026-09-28 09:10 UTC1
import { get, writable } from 'svelte/store';
973f8fbdaf80atagen2026-09-28 09:10 UTC2
import { sendMessage } from './api';
973f8fbdaf80atagen2026-09-28 09:10 UTC3
import { timelines } from './store';
cf23dbf4e77catagen2026-10-23 06:36 UTC4
import type { Mention } from './mentions';
df3c0f2b3da4atagen2026-10-22 13:07 UTC5
import type { ReplyReference, TimelineItem } from './types';
973f8fbdaf80atagen2026-09-28 09:10 UTC6
973f8fbdaf80atagen2026-09-28 09:10 UTC7
export interface PendingMessage {
973f8fbdaf80atagen2026-09-28 09:10 UTC8
  id: string;
973f8fbdaf80atagen2026-09-28 09:10 UTC9
  body: string;
df3c0f2b3da4atagen2026-10-22 13:07 UTC10
  replyTo: ReplyReference | null;
973f8fbdaf80atagen2026-09-28 09:10 UTC11
  status: 'sending' | 'failed';
973f8fbdaf80atagen2026-09-28 09:10 UTC12
  error: string | null;
973f8fbdaf80atagen2026-09-28 09:10 UTC13
  createdAt: number;
973f8fbdaf80atagen2026-09-28 09:10 UTC14
}
973f8fbdaf80atagen2026-09-28 09:10 UTC15
973f8fbdaf80atagen2026-09-28 09:10 UTC16
export const pendingByRoom = writable<Record<string, PendingMessage[]>>({});
973f8fbdaf80atagen2026-09-28 09:10 UTC17
973f8fbdaf80atagen2026-09-28 09:10 UTC18
interface PendingRecord extends PendingMessage {
973f8fbdaf80atagen2026-09-28 09:10 UTC19
  roomId: string;
cf23dbf4e77catagen2026-10-23 06:36 UTC20
  mentions: Mention[];
973f8fbdaf80atagen2026-09-28 09:10 UTC21
  knownIds: Set<string>;
973f8fbdaf80atagen2026-09-28 09:10 UTC22
}
973f8fbdaf80atagen2026-09-28 09:10 UTC23
973f8fbdaf80atagen2026-09-28 09:10 UTC24
const records = new Map<string, PendingRecord>();
973f8fbdaf80atagen2026-09-28 09:10 UTC25
const queues = new Map<string, Promise<void>>();
973f8fbdaf80atagen2026-09-28 09:10 UTC26
let nextSeq = 1;
973f8fbdaf80atagen2026-09-28 09:10 UTC27
973f8fbdaf80atagen2026-09-28 09:10 UTC28
function nextId(): string {
973f8fbdaf80atagen2026-09-28 09:10 UTC29
  return `pending-${nextSeq++}`;
973f8fbdaf80atagen2026-09-28 09:10 UTC30
}
973f8fbdaf80atagen2026-09-28 09:10 UTC31
973f8fbdaf80atagen2026-09-28 09:10 UTC32
function ownIdentities(items: TimelineItem[]): Set<string> {
973f8fbdaf80atagen2026-09-28 09:10 UTC33
  const out = new Set<string>();
973f8fbdaf80atagen2026-09-28 09:10 UTC34
  for (const item of items) {
973f8fbdaf80atagen2026-09-28 09:10 UTC35
    if (item.kind !== 'event' || !item.event.own) continue;
973f8fbdaf80atagen2026-09-28 09:10 UTC36
    const id = item.event.transaction_id ?? item.event.event_id;
973f8fbdaf80atagen2026-09-28 09:10 UTC37
    if (id) out.add(id);
973f8fbdaf80atagen2026-09-28 09:10 UTC38
  }
973f8fbdaf80atagen2026-09-28 09:10 UTC39
  return out;
973f8fbdaf80atagen2026-09-28 09:10 UTC40
}
973f8fbdaf80atagen2026-09-28 09:10 UTC41
973f8fbdaf80atagen2026-09-28 09:10 UTC42
function toPublic(record: PendingRecord): PendingMessage {
df3c0f2b3da4atagen2026-10-22 13:07 UTC43
  return { id: record.id, body: record.body, replyTo: record.replyTo, status: record.status, error: record.error, createdAt: record.createdAt };
973f8fbdaf80atagen2026-09-28 09:10 UTC44
}
973f8fbdaf80atagen2026-09-28 09:10 UTC45
973f8fbdaf80atagen2026-09-28 09:10 UTC46
function roomList(roomId: string): PendingMessage[] {
973f8fbdaf80atagen2026-09-28 09:10 UTC47
  return get(pendingByRoom)[roomId] ?? [];
973f8fbdaf80atagen2026-09-28 09:10 UTC48
}
973f8fbdaf80atagen2026-09-28 09:10 UTC49
973f8fbdaf80atagen2026-09-28 09:10 UTC50
function setRoom(roomId: string, list: PendingMessage[]): void {
973f8fbdaf80atagen2026-09-28 09:10 UTC51
  pendingByRoom.update((all) => ({ ...all, [roomId]: list }));
973f8fbdaf80atagen2026-09-28 09:10 UTC52
}
973f8fbdaf80atagen2026-09-28 09:10 UTC53
973f8fbdaf80atagen2026-09-28 09:10 UTC54
function addRecord(record: PendingRecord): void {
973f8fbdaf80atagen2026-09-28 09:10 UTC55
  records.set(record.id, record);
973f8fbdaf80atagen2026-09-28 09:10 UTC56
  setRoom(record.roomId, [...roomList(record.roomId), toPublic(record)]);
973f8fbdaf80atagen2026-09-28 09:10 UTC57
}
973f8fbdaf80atagen2026-09-28 09:10 UTC58
973f8fbdaf80atagen2026-09-28 09:10 UTC59
function updateRecord(id: string, patch: Partial<Pick<PendingRecord, 'status' | 'error'>>): void {
973f8fbdaf80atagen2026-09-28 09:10 UTC60
  const record = records.get(id);
973f8fbdaf80atagen2026-09-28 09:10 UTC61
  if (!record) return;
973f8fbdaf80atagen2026-09-28 09:10 UTC62
  Object.assign(record, patch);
973f8fbdaf80atagen2026-09-28 09:10 UTC63
  setRoom(record.roomId, roomList(record.roomId).map((p) => (p.id === id ? toPublic(record) : p)));
973f8fbdaf80atagen2026-09-28 09:10 UTC64
}
973f8fbdaf80atagen2026-09-28 09:10 UTC65
973f8fbdaf80atagen2026-09-28 09:10 UTC66
function removeRecord(id: string): void {
973f8fbdaf80atagen2026-09-28 09:10 UTC67
  const record = records.get(id);
973f8fbdaf80atagen2026-09-28 09:10 UTC68
  if (!record) return;
973f8fbdaf80atagen2026-09-28 09:10 UTC69
  records.delete(id);
973f8fbdaf80atagen2026-09-28 09:10 UTC70
  setRoom(record.roomId, roomList(record.roomId).filter((p) => p.id !== id));
973f8fbdaf80atagen2026-09-28 09:10 UTC71
}
973f8fbdaf80atagen2026-09-28 09:10 UTC72
973f8fbdaf80atagen2026-09-28 09:10 UTC73
async function attemptSend(record: PendingRecord): Promise<void> {
973f8fbdaf80atagen2026-09-28 09:10 UTC74
  if (!records.has(record.id)) return;
973f8fbdaf80atagen2026-09-28 09:10 UTC75
  updateRecord(record.id, { status: 'sending', error: null });
973f8fbdaf80atagen2026-09-28 09:10 UTC76
  try {
cf23dbf4e77catagen2026-10-23 06:36 UTC77
    await sendMessage(record.roomId, record.body, record.replyTo?.event_id ?? null, record.mentions);
973f8fbdaf80atagen2026-09-28 09:10 UTC78
  } catch (e) {
973f8fbdaf80atagen2026-09-28 09:10 UTC79
    updateRecord(record.id, { status: 'failed', error: e instanceof Error ? e.message : 'Message could not be sent' });
973f8fbdaf80atagen2026-09-28 09:10 UTC80
  }
973f8fbdaf80atagen2026-09-28 09:10 UTC81
}
973f8fbdaf80atagen2026-09-28 09:10 UTC82
973f8fbdaf80atagen2026-09-28 09:10 UTC83
function enqueue(record: PendingRecord): void {
973f8fbdaf80atagen2026-09-28 09:10 UTC84
  const previous = queues.get(record.roomId) ?? Promise.resolve();
973f8fbdaf80atagen2026-09-28 09:10 UTC85
  const next = previous.then(() => attemptSend(record));
973f8fbdaf80atagen2026-09-28 09:10 UTC86
  queues.set(record.roomId, next.catch(() => undefined));
973f8fbdaf80atagen2026-09-28 09:10 UTC87
}
973f8fbdaf80atagen2026-09-28 09:10 UTC88
cf23dbf4e77catagen2026-10-23 06:36 UTC89
export function queueSend(roomId: string, body: string, replyTo: ReplyReference | null = null, mentions: Mention[] = []): void {
973f8fbdaf80atagen2026-09-28 09:10 UTC90
  const record: PendingRecord = {
973f8fbdaf80atagen2026-09-28 09:10 UTC91
    id: nextId(),
973f8fbdaf80atagen2026-09-28 09:10 UTC92
    roomId,
973f8fbdaf80atagen2026-09-28 09:10 UTC93
    body,
df3c0f2b3da4atagen2026-10-22 13:07 UTC94
    replyTo,
cf23dbf4e77catagen2026-10-23 06:36 UTC95
    mentions,
973f8fbdaf80atagen2026-09-28 09:10 UTC96
    status: 'sending',
973f8fbdaf80atagen2026-09-28 09:10 UTC97
    error: null,
973f8fbdaf80atagen2026-09-28 09:10 UTC98
    createdAt: Date.now(),
973f8fbdaf80atagen2026-09-28 09:10 UTC99
    knownIds: ownIdentities(get(timelines)[roomId] ?? [])
973f8fbdaf80atagen2026-09-28 09:10 UTC100
  };
973f8fbdaf80atagen2026-09-28 09:10 UTC101
  addRecord(record);
973f8fbdaf80atagen2026-09-28 09:10 UTC102
  enqueue(record);
973f8fbdaf80atagen2026-09-28 09:10 UTC103
}
973f8fbdaf80atagen2026-09-28 09:10 UTC104
973f8fbdaf80atagen2026-09-28 09:10 UTC105
export function retryPending(id: string): void {
973f8fbdaf80atagen2026-09-28 09:10 UTC106
  const record = records.get(id);
973f8fbdaf80atagen2026-09-28 09:10 UTC107
  if (!record) return;
973f8fbdaf80atagen2026-09-28 09:10 UTC108
  record.knownIds = ownIdentities(get(timelines)[record.roomId] ?? []);
973f8fbdaf80atagen2026-09-28 09:10 UTC109
  enqueue(record);
973f8fbdaf80atagen2026-09-28 09:10 UTC110
}
973f8fbdaf80atagen2026-09-28 09:10 UTC111
973f8fbdaf80atagen2026-09-28 09:10 UTC112
export function discardPending(id: string): void {
973f8fbdaf80atagen2026-09-28 09:10 UTC113
  removeRecord(id);
973f8fbdaf80atagen2026-09-28 09:10 UTC114
}
973f8fbdaf80atagen2026-09-28 09:10 UTC115
973f8fbdaf80atagen2026-09-28 09:10 UTC116
export function resetPending(): void {
973f8fbdaf80atagen2026-09-28 09:10 UTC117
  records.clear();
973f8fbdaf80atagen2026-09-28 09:10 UTC118
  queues.clear();
973f8fbdaf80atagen2026-09-28 09:10 UTC119
  pendingByRoom.set({});
973f8fbdaf80atagen2026-09-28 09:10 UTC120
}
973f8fbdaf80atagen2026-09-28 09:10 UTC121
973f8fbdaf80atagen2026-09-28 09:10 UTC122
function reconcileRoom(roomId: string, items: TimelineItem[]): void {
973f8fbdaf80atagen2026-09-28 09:10 UTC123
  const list = roomList(roomId);
973f8fbdaf80atagen2026-09-28 09:10 UTC124
  if (list.length === 0) return;
973f8fbdaf80atagen2026-09-28 09:10 UTC125
  const ownItems = items.filter((item) => item.kind === 'event' && item.event.own) as Extract<
973f8fbdaf80atagen2026-09-28 09:10 UTC126
    TimelineItem,
973f8fbdaf80atagen2026-09-28 09:10 UTC127
    { kind: 'event' }
973f8fbdaf80atagen2026-09-28 09:10 UTC128
  >[];
973f8fbdaf80atagen2026-09-28 09:10 UTC129
  const claimed = new Set<string>();
973f8fbdaf80atagen2026-09-28 09:10 UTC130
  for (const pending of list) {
973f8fbdaf80atagen2026-09-28 09:10 UTC131
    const record = records.get(pending.id);
973f8fbdaf80atagen2026-09-28 09:10 UTC132
    if (!record) continue;
973f8fbdaf80atagen2026-09-28 09:10 UTC133
    const match = ownItems.find((item) => {
973f8fbdaf80atagen2026-09-28 09:10 UTC134
      if (claimed.has(item.event.item_id)) return false;
973f8fbdaf80atagen2026-09-28 09:10 UTC135
      const id = item.event.transaction_id ?? item.event.event_id;
973f8fbdaf80atagen2026-09-28 09:10 UTC136
      if (id && record.knownIds.has(id)) return false;
973f8fbdaf80atagen2026-09-28 09:10 UTC137
      return item.event.content.kind === 'Message' && item.event.content.message.body === record.body;
973f8fbdaf80atagen2026-09-28 09:10 UTC138
    });
973f8fbdaf80atagen2026-09-28 09:10 UTC139
    if (match) {
973f8fbdaf80atagen2026-09-28 09:10 UTC140
      claimed.add(match.event.item_id);
973f8fbdaf80atagen2026-09-28 09:10 UTC141
      removeRecord(record.id);
973f8fbdaf80atagen2026-09-28 09:10 UTC142
    }
973f8fbdaf80atagen2026-09-28 09:10 UTC143
  }
973f8fbdaf80atagen2026-09-28 09:10 UTC144
}
973f8fbdaf80atagen2026-09-28 09:10 UTC145
973f8fbdaf80atagen2026-09-28 09:10 UTC146
timelines.subscribe((all) => {
973f8fbdaf80atagen2026-09-28 09:10 UTC147
  for (const roomId of Object.keys(get(pendingByRoom))) {
973f8fbdaf80atagen2026-09-28 09:10 UTC148
    reconcileRoom(roomId, all[roomId] ?? []);
973f8fbdaf80atagen2026-09-28 09:10 UTC149
  }
973f8fbdaf80atagen2026-09-28 09:10 UTC150
});