summaryrefslogtreecommitdiff
path: root/ui/lib/store/messages.svelte.js
blob: dadade61bc45c19a820d72170303de6389bbfcbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { DateTime } from 'luxon';
import { marked } from 'marked';
import DOMPurify from 'dompurify';

const RUN_COALESCE_MAX_INTERVAL = 10 /* min */ * 60 /* sec */ * 1000; /* ms */

export class Messages {
  channels = $state({}); // Mapping<ChannelId, Message>

  inChannel(channel) {
    return this.channels[channel] || [];
  }

  addMessage(channel, id, { at, sender, body }) {
    let parsedAt = DateTime.fromISO(at);
    let renderedBody = DOMPurify.sanitize(marked.parse(body, { breaks: true }));
    const message = { id, at: parsedAt, body, renderedBody };

    // You might be thinking, can't this be
    //
    //     let runs = (this.channels[channel] ||= []);
    //
    // Let me tell you, I thought that too. Javascript's semantics allow it. It
    // didn't work - the first message in each channel was getting lost as the
    // update to `this.channels` wasn't actually happening. I suspect this is
    // due to the implementation of Svelte's `$state` rune, but I don't know it
    // for sure.
    //
    // In any case, splitting the read and write up like this has the same
    // semantics, and _works_. (This time, for sure!)
    let runs = this.channels[channel] || [];

    let currentRun = runs.slice(-1)[0];
    if (currentRun === undefined) {
      currentRun = { sender, messages: [message] };
      runs.push(currentRun);
    } else {
      let lastMessage = currentRun.messages.slice(-1)[0];
      let newRun =
        currentRun.sender !== sender || parsedAt - lastMessage.at > RUN_COALESCE_MAX_INTERVAL;

      if (newRun) {
        currentRun = { sender, messages: [message] };
        runs.push(currentRun);
      } else {
        currentRun.messages.push(message);
      }
    }

    this.channels[channel] = runs;

    return this;
  }

  setMessages(messages) {
    this.channels = {};
    for (let { channel, id, at, sender, body } of messages) {
      this.addMessage(channel, id, { at, sender, body });
    }
    return this;
  }

  deleteMessage(messageId) {
    for (let channel in this.channels) {
      this.channels[channel] = this.channels[channel]
        .map(({ sender, messages }) => ({
          sender,
          messages: messages.filter(({ id }) => id != messageId)
        }))
        .filter(({ messages }) => messages.length > 0);
    }
    return this;
  }

  deleteChannel(id) {
    delete this.channels[id];
    return this;
  }
}