Debian Codegraph @

event/ephemeral.go

raw
1 // Copyright (c) 2020 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package event
8
9 import (
10 "encoding/json"
11
12 "maunium.net/go/mautrix/id"
13 )
14
15 // TypingEventContent represents the content of a m.typing ephemeral event.
16 // https://spec.matrix.org/v1.2/client-server-api/#mtyping
17 type TypingEventContent struct {
18 UserIDs []id.UserID `json:"user_ids"`
19 }
20
21 // ReceiptEventContent represents the content of a m.receipt ephemeral event.
22 // https://spec.matrix.org/v1.2/client-server-api/#mreceipt
23 type ReceiptEventContent map[id.EventID]Receipts
24
25 type Receipts struct {
26 Read map[id.UserID]ReadReceipt `json:"m.read"`
27 }
28
29 type ReadReceipt struct {
30 Timestamp int64 `json:"ts"`
31
32 // Extra contains any unknown fields in the read receipt event.
33 // Most servers don't allow clients to set them, so this will be empty in most cases.
34 Extra map[string]interface{} `json:"-"`
35 }
36
37 func (rr *ReadReceipt) UnmarshalJSON(data []byte) error {
38 // Hacky compatibility hack against crappy clients that send double-encoded read receipts.
39 // TODO is this actually needed? clients can't currently set custom content in receipts 🤔
40 if data[0] == '"' && data[len(data)-1] == '"' {
41 var strData string
42 err := json.Unmarshal(data, &strData)
43 if err != nil {
44 return err
45 }
46 data = []byte(strData)
47 }
48
49 var parsed map[string]interface{}
50 err := json.Unmarshal(data, &parsed)
51 if err != nil {
52 return err
53 }
54 ts, _ := parsed["ts"].(float64)
55 delete(parsed, "ts")
56 *rr = ReadReceipt{
57 Timestamp: int64(ts),
58 Extra: parsed,
59 }
60 return nil
61 }
62
63 type Presence string
64
65 const (
66 PresenceOnline Presence = "online"
67 PresenceOffline Presence = "offline"
68 PresenceUnavailable Presence = "unavailable"
69 )
70
71 // PresenceEventContent represents the content of a m.presence ephemeral event.
72 // https://spec.matrix.org/v1.2/client-server-api/#mpresence
73 type PresenceEventContent struct {
74 Presence Presence `json:"presence"`
75 Displayname string `json:"displayname,omitempty"`
76 AvatarURL id.ContentURIString `json:"avatar_url,omitempty"`
77 LastActiveAgo int64 `json:"last_active_ago,omitempty"`
78 CurrentlyActive bool `json:"currently_active,omitempty"`
79 StatusMessage string `json:"status_msg,omitempty"`
80 }