summaryrefslogtreecommitdiff
path: root/ui/lib/state/remote/messages.svelte.js
blob: 10de27a6f6c046620a78d71f0e6874e7ec783dab (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
import { DateTime } from 'luxon';

class Message {
  static boot({ id, at, conversation, sender, body }) {
    return new Message({
      id,
      at: DateTime.fromISO(at),
      conversation,
      sender,
      body,
    });
  }

  constructor({ id, at, conversation, sender, body }) {
    this.id = id;
    this.at = at;
    this.conversation = conversation;
    this.sender = sender;
    this.body = body;
  }
}

export class Messages {
  all = $state([]);

  add({ id, at, conversation, sender, body }) {
    const message = Message.boot({ id, at, conversation, sender, body });
    this.all.push(message);
  }

  remove(id) {
    const index = this.all.findIndex((message) => message.id === id);
    this.all.splice(index, 1);
  }
}