Debian Codegraph @

event/events.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 // Event represents a single Matrix event.
16 type Event struct {
17 StateKey *string `json:"state_key,omitempty"` // The state key for the event. Only present on State Events.
18 Sender id.UserID `json:"sender,omitempty"` // The user ID of the sender of the event
19 Type Type `json:"type"` // The event type
20 Timestamp int64 `json:"origin_server_ts,omitempty"` // The unix timestamp when this message was sent by the origin server
21 ID id.EventID `json:"event_id,omitempty"` // The unique ID of this event
22 RoomID id.RoomID `json:"room_id,omitempty"` // The room the event was sent to. May be nil (e.g. for presence)
23 Content Content `json:"content"` // The JSON content of the event.
24 Redacts id.EventID `json:"redacts,omitempty"` // The event ID that was redacted if a m.room.redaction event
25 Unsigned Unsigned `json:"unsigned,omitempty"` // Unsigned content set by own homeserver.
26
27 Mautrix MautrixInfo `json:"-"`
28
29 ToUserID id.UserID `json:"to_user_id,omitempty"` // The user ID that the to-device event was sent to. Only present in MSC2409 appservice transactions.
30 ToDeviceID id.DeviceID `json:"to_device_id,omitempty"` // The device ID that the to-device event was sent to. Only present in MSC2409 appservice transactions.
31 }
32
33 type eventForMarshaling struct {
34 StateKey *string `json:"state_key,omitempty"`
35 Sender id.UserID `json:"sender,omitempty"`
36 Type Type `json:"type"`
37 Timestamp int64 `json:"origin_server_ts,omitempty"`
38 ID id.EventID `json:"event_id,omitempty"`
39 RoomID id.RoomID `json:"room_id,omitempty"`
40 Content Content `json:"content"`
41 Redacts id.EventID `json:"redacts,omitempty"`
42 Unsigned *Unsigned `json:"unsigned,omitempty"`
43
44 PrevContent *Content `json:"prev_content,omitempty"`
45 ReplacesState *id.EventID `json:"replaces_state,omitempty"`
46
47 ToUserID id.UserID `json:"to_user_id,omitempty"`
48 ToDeviceID id.DeviceID `json:"to_device_id,omitempty"`
49 }
50
51 // UnmarshalJSON unmarshals the event, including moving prev_content from the top level to inside unsigned.
52 func (evt *Event) UnmarshalJSON(data []byte) error {
53 var efm eventForMarshaling
54 err := json.Unmarshal(data, &efm)
55 if err != nil {
56 return err
57 }
58 evt.StateKey = efm.StateKey
59 evt.Sender = efm.Sender
60 evt.Type = efm.Type
61 evt.Timestamp = efm.Timestamp
62 evt.ID = efm.ID
63 evt.RoomID = efm.RoomID
64 evt.Content = efm.Content
65 evt.Redacts = efm.Redacts
66 if efm.Unsigned != nil {
67 evt.Unsigned = *efm.Unsigned
68 }
69 if efm.PrevContent != nil && evt.Unsigned.PrevContent == nil {
70 evt.Unsigned.PrevContent = efm.PrevContent
71 }
72 if efm.ReplacesState != nil && *efm.ReplacesState != "" && evt.Unsigned.ReplacesState == "" {
73 evt.Unsigned.ReplacesState = *efm.ReplacesState
74 }
75 evt.ToUserID = efm.ToUserID
76 evt.ToDeviceID = efm.ToDeviceID
77 return nil
78 }
79
80 // MarshalJSON marshals the event, including omitting the unsigned field if it's empty.
81 //
82 // This is necessary because Unsigned is not a pointer (for convenience reasons),
83 // and encoding/json doesn't know how to check if a non-pointer struct is empty.
84 //
85 // TODO(tulir): maybe it makes more sense to make Unsigned a pointer and make an easy and safe way to access it?
86 func (evt *Event) MarshalJSON() ([]byte, error) {
87 unsigned := &evt.Unsigned
88 if unsigned.IsEmpty() {
89 unsigned = nil
90 }
91 return json.Marshal(&eventForMarshaling{
92 StateKey: evt.StateKey,
93 Sender: evt.Sender,
94 Type: evt.Type,
95 Timestamp: evt.Timestamp,
96 ID: evt.ID,
97 RoomID: evt.RoomID,
98 Content: evt.Content,
99 Redacts: evt.Redacts,
100 Unsigned: unsigned,
101 ToUserID: evt.ToUserID,
102 ToDeviceID: evt.ToDeviceID,
103 })
104 }
105
106 type MautrixInfo struct {
107 Verified bool
108 }
109
110 func (evt *Event) GetStateKey() string {
111 if evt.StateKey != nil {
112 return *evt.StateKey
113 }
114 return ""
115 }
116
117 type StrippedState struct {
118 Content Content `json:"content"`
119 Type Type `json:"type"`
120 StateKey string `json:"state_key"`
121 }
122
123 type Unsigned struct {
124 PrevContent *Content `json:"prev_content,omitempty"`
125 PrevSender id.UserID `json:"prev_sender,omitempty"`
126 ReplacesState id.EventID `json:"replaces_state,omitempty"`
127 Age int64 `json:"age,omitempty"`
128 TransactionID string `json:"transaction_id,omitempty"`
129 Relations Relations `json:"m.relations,omitempty"`
130 RedactedBecause *Event `json:"redacted_because,omitempty"`
131 InviteRoomState []StrippedState `json:"invite_room_state,omitempty"`
132 }
133
134 func (us *Unsigned) IsEmpty() bool {
135 return us.PrevContent == nil && us.PrevSender == "" && us.ReplacesState == "" && us.Age == 0 &&
136 us.TransactionID == "" && us.RedactedBecause == nil && us.InviteRoomState == nil && us.Relations.Raw == nil &&
137 us.Relations.Annotations.Map == nil && us.Relations.References.List == nil && us.Relations.Replaces.List == nil
138 }