atagen

malice

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