the browser-facing portion of osu!
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
2// See the LICENCE file in the repository root for full licence text.
3
4import MessageJson, { MessageType } from 'interfaces/chat/message-json';
5import { action, computed, makeObservable, observable } from 'mobx';
6import User from 'models/user';
7import * as moment from 'moment';
8import core from 'osu-core-singleton';
9import { v4 as uuidv4 } from 'uuid';
10
11export default class Message {
12 @observable channelId = -1;
13 @observable content = '';
14 @observable errored = false;
15 @observable isAction = false;
16 @observable messageId: number | string = uuidv4();
17 @observable persisted = false;
18 @observable senderId = -1;
19 @observable timestamp: string = moment().toISOString();
20 @observable type: MessageType = 'plain';
21 @observable uuid = this.messageId;
22
23 @computed
24 get sender() {
25 return core.dataStore.userStore.get(this.senderId) ?? new User(-1);
26 }
27
28 constructor() {
29 makeObservable(this);
30 }
31
32 static fromJson(this: void, json: MessageJson): Message {
33 const message = new Message();
34 message.channelId = json.channel_id;
35 message.content = json.content;
36 message.isAction = json.is_action;
37 message.messageId = json.message_id;
38 message.persisted = true;
39 message.senderId = json.sender_id;
40 message.timestamp = json.timestamp;
41 message.type = json.type;
42 message.uuid = json.uuid ?? message.uuid;
43
44 return message;
45 }
46
47 @action
48 persist(json: MessageJson) {
49 if (this.persisted) return;
50 this.content = json.content;
51 this.messageId = json.message_id;
52 this.timestamp = json.timestamp;
53 this.persisted = true;
54 }
55}