| 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 |
"strconv" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"golang.org/x/net/html" |
| 15 |
|
| 16 |
"maunium.net/go/mautrix/crypto/attachment" |
| 17 |
"maunium.net/go/mautrix/id" |
| 18 |
) |
| 19 |
|
| 20 |
// MessageType is the sub-type of a m.room.message event. |
| 21 |
// https://spec.matrix.org/v1.2/client-server-api/#mroommessage-msgtypes |
| 22 |
type MessageType string |
| 23 |
|
| 24 |
// Msgtypes |
| 25 |
const ( |
| 26 |
MsgText MessageType = "m.text" |
| 27 |
MsgEmote MessageType = "m.emote" |
| 28 |
MsgNotice MessageType = "m.notice" |
| 29 |
MsgImage MessageType = "m.image" |
| 30 |
MsgLocation MessageType = "m.location" |
| 31 |
MsgVideo MessageType = "m.video" |
| 32 |
MsgAudio MessageType = "m.audio" |
| 33 |
MsgFile MessageType = "m.file" |
| 34 |
|
| 35 |
MsgVerificationRequest MessageType = "m.key.verification.request" |
| 36 |
) |
| 37 |
|
| 38 |
// Format specifies the format of the formatted_body in m.room.message events. |
| 39 |
// https://spec.matrix.org/v1.2/client-server-api/#mroommessage-msgtypes |
| 40 |
type Format string |
| 41 |
|
| 42 |
// Message formats |
| 43 |
const ( |
| 44 |
FormatHTML Format = "org.matrix.custom.html" |
| 45 |
) |
| 46 |
|
| 47 |
// RedactionEventContent represents the content of a m.room.redaction message event. |
| 48 |
// |
| 49 |
// The redacted event ID is still at the top level, but will move in a future room version. |
| 50 |
// See https://github.com/matrix-org/matrix-doc/pull/2244 and https://github.com/matrix-org/matrix-doc/pull/2174 |
| 51 |
// |
| 52 |
// https://spec.matrix.org/v1.2/client-server-api/#mroomredaction |
| 53 |
type RedactionEventContent struct { |
| 54 |
Reason string `json:"reason,omitempty"` |
| 55 |
} |
| 56 |
|
| 57 |
// ReactionEventContent represents the content of a m.reaction message event. |
| 58 |
// This is not yet in a spec release, see https://github.com/matrix-org/matrix-doc/pull/1849 |
| 59 |
type ReactionEventContent struct { |
| 60 |
RelatesTo RelatesTo `json:"m.relates_to"` |
| 61 |
} |
| 62 |
|
| 63 |
func (content *ReactionEventContent) GetRelatesTo() *RelatesTo { |
| 64 |
return &content.RelatesTo |
| 65 |
} |
| 66 |
|
| 67 |
func (content *ReactionEventContent) OptionalGetRelatesTo() *RelatesTo { |
| 68 |
return &content.RelatesTo |
| 69 |
} |
| 70 |
|
| 71 |
func (content *ReactionEventContent) SetRelatesTo(rel *RelatesTo) { |
| 72 |
content.RelatesTo = *rel |
| 73 |
} |
| 74 |
|
| 75 |
// MessageEventContent represents the content of a m.room.message event. |
| 76 |
// |
| 77 |
// It is also used to represent m.sticker events, as they are equivalent to m.room.message |
| 78 |
// with the exception of the msgtype field. |
| 79 |
// |
| 80 |
// https://spec.matrix.org/v1.2/client-server-api/#mroommessage |
| 81 |
type MessageEventContent struct { |
| 82 |
// Base m.room.message fields |
| 83 |
MsgType MessageType `json:"msgtype,omitempty"` |
| 84 |
Body string `json:"body"` |
| 85 |
|
| 86 |
// Extra fields for text types |
| 87 |
Format Format `json:"format,omitempty"` |
| 88 |
FormattedBody string `json:"formatted_body,omitempty"` |
| 89 |
|
| 90 |
// Extra field for m.location |
| 91 |
GeoURI string `json:"geo_uri,omitempty"` |
| 92 |
|
| 93 |
// Extra fields for media types |
| 94 |
URL id.ContentURIString `json:"url,omitempty"` |
| 95 |
Info *FileInfo `json:"info,omitempty"` |
| 96 |
File *EncryptedFileInfo `json:"file,omitempty"` |
| 97 |
|
| 98 |
// Edits and relations |
| 99 |
NewContent *MessageEventContent `json:"m.new_content,omitempty"` |
| 100 |
RelatesTo *RelatesTo `json:"m.relates_to,omitempty"` |
| 101 |
|
| 102 |
// In-room verification |
| 103 |
To id.UserID `json:"to,omitempty"` |
| 104 |
FromDevice id.DeviceID `json:"from_device,omitempty"` |
| 105 |
Methods []VerificationMethod `json:"methods,omitempty"` |
| 106 |
|
| 107 |
replyFallbackRemoved bool |
| 108 |
} |
| 109 |
|
| 110 |
func (content *MessageEventContent) GetRelatesTo() *RelatesTo { |
| 111 |
if content.RelatesTo == nil { |
| 112 |
content.RelatesTo = &RelatesTo{} |
| 113 |
} |
| 114 |
return content.RelatesTo |
| 115 |
} |
| 116 |
|
| 117 |
func (content *MessageEventContent) OptionalGetRelatesTo() *RelatesTo { |
| 118 |
return content.RelatesTo |
| 119 |
} |
| 120 |
|
| 121 |
func (content *MessageEventContent) SetRelatesTo(rel *RelatesTo) { |
| 122 |
content.RelatesTo = rel |
| 123 |
} |
| 124 |
|
| 125 |
func (content *MessageEventContent) SetEdit(original id.EventID) { |
| 126 |
newContent := *content |
| 127 |
content.NewContent = &newContent |
| 128 |
content.RelatesTo = &RelatesTo{ |
| 129 |
Type: RelReplace, |
| 130 |
EventID: original, |
| 131 |
} |
| 132 |
if content.MsgType == MsgText || content.MsgType == MsgNotice { |
| 133 |
content.Body = "* " + content.Body |
| 134 |
if content.Format == FormatHTML && len(content.FormattedBody) > 0 { |
| 135 |
content.FormattedBody = "* " + content.FormattedBody |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
func (content *MessageEventContent) EnsureHasHTML() { |
| 141 |
if len(content.FormattedBody) == 0 || content.Format != FormatHTML { |
| 142 |
content.FormattedBody = strings.ReplaceAll(html.EscapeString(content.Body), "\n", "<br/>") |
| 143 |
content.Format = FormatHTML |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func (content *MessageEventContent) GetFile() *EncryptedFileInfo { |
| 148 |
if content.File == nil { |
| 149 |
content.File = &EncryptedFileInfo{} |
| 150 |
} |
| 151 |
return content.File |
| 152 |
} |
| 153 |
|
| 154 |
func (content *MessageEventContent) GetInfo() *FileInfo { |
| 155 |
if content.Info == nil { |
| 156 |
content.Info = &FileInfo{} |
| 157 |
} |
| 158 |
return content.Info |
| 159 |
} |
| 160 |
|
| 161 |
type EncryptedFileInfo struct { |
| 162 |
attachment.EncryptedFile |
| 163 |
URL id.ContentURIString `json:"url"` |
| 164 |
} |
| 165 |
|
| 166 |
type FileInfo struct { |
| 167 |
MimeType string `json:"mimetype,omitempty"` |
| 168 |
ThumbnailInfo *FileInfo `json:"thumbnail_info,omitempty"` |
| 169 |
ThumbnailURL id.ContentURIString `json:"thumbnail_url,omitempty"` |
| 170 |
ThumbnailFile *EncryptedFileInfo `json:"thumbnail_file,omitempty"` |
| 171 |
Width int `json:"-"` |
| 172 |
Height int `json:"-"` |
| 173 |
Duration int `json:"-"` |
| 174 |
Size int `json:"-"` |
| 175 |
} |
| 176 |
|
| 177 |
type serializableFileInfo struct { |
| 178 |
MimeType string `json:"mimetype,omitempty"` |
| 179 |
ThumbnailInfo *serializableFileInfo `json:"thumbnail_info,omitempty"` |
| 180 |
ThumbnailURL id.ContentURIString `json:"thumbnail_url,omitempty"` |
| 181 |
ThumbnailFile *EncryptedFileInfo `json:"thumbnail_file,omitempty"` |
| 182 |
|
| 183 |
Width json.Number `json:"w,omitempty"` |
| 184 |
Height json.Number `json:"h,omitempty"` |
| 185 |
Duration json.Number `json:"duration,omitempty"` |
| 186 |
Size json.Number `json:"size,omitempty"` |
| 187 |
} |
| 188 |
|
| 189 |
func (sfi *serializableFileInfo) CopyFrom(fileInfo *FileInfo) *serializableFileInfo { |
| 190 |
if fileInfo == nil { |
| 191 |
return nil |
| 192 |
} |
| 193 |
*sfi = serializableFileInfo{ |
| 194 |
MimeType: fileInfo.MimeType, |
| 195 |
ThumbnailURL: fileInfo.ThumbnailURL, |
| 196 |
ThumbnailInfo: (&serializableFileInfo{}).CopyFrom(fileInfo.ThumbnailInfo), |
| 197 |
ThumbnailFile: fileInfo.ThumbnailFile, |
| 198 |
} |
| 199 |
if fileInfo.Width > 0 { |
| 200 |
sfi.Width = json.Number(strconv.Itoa(fileInfo.Width)) |
| 201 |
} |
| 202 |
if fileInfo.Height > 0 { |
| 203 |
sfi.Height = json.Number(strconv.Itoa(fileInfo.Height)) |
| 204 |
} |
| 205 |
if fileInfo.Size > 0 { |
| 206 |
sfi.Size = json.Number(strconv.Itoa(fileInfo.Size)) |
| 207 |
|
| 208 |
} |
| 209 |
if fileInfo.Duration > 0 { |
| 210 |
sfi.Duration = json.Number(strconv.Itoa(int(fileInfo.Duration))) |
| 211 |
} |
| 212 |
return sfi |
| 213 |
} |
| 214 |
|
| 215 |
func (sfi *serializableFileInfo) CopyTo(fileInfo *FileInfo) { |
| 216 |
*fileInfo = FileInfo{ |
| 217 |
Width: numberToInt(sfi.Width), |
| 218 |
Height: numberToInt(sfi.Height), |
| 219 |
Size: numberToInt(sfi.Size), |
| 220 |
Duration: numberToInt(sfi.Duration), |
| 221 |
MimeType: sfi.MimeType, |
| 222 |
ThumbnailURL: sfi.ThumbnailURL, |
| 223 |
ThumbnailFile: sfi.ThumbnailFile, |
| 224 |
} |
| 225 |
if sfi.ThumbnailInfo != nil { |
| 226 |
fileInfo.ThumbnailInfo = &FileInfo{} |
| 227 |
sfi.ThumbnailInfo.CopyTo(fileInfo.ThumbnailInfo) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
func (fileInfo *FileInfo) UnmarshalJSON(data []byte) error { |
| 232 |
sfi := &serializableFileInfo{} |
| 233 |
if err := json.Unmarshal(data, sfi); err != nil { |
| 234 |
return err |
| 235 |
} |
| 236 |
sfi.CopyTo(fileInfo) |
| 237 |
return nil |
| 238 |
} |
| 239 |
|
| 240 |
func (fileInfo *FileInfo) MarshalJSON() ([]byte, error) { |
| 241 |
return json.Marshal((&serializableFileInfo{}).CopyFrom(fileInfo)) |
| 242 |
} |
| 243 |
|
| 244 |
func numberToInt(val json.Number) int { |
| 245 |
f64, _ := val.Float64() |
| 246 |
if f64 > 0 { |
| 247 |
return int(f64) |
| 248 |
} |
| 249 |
return 0 |
| 250 |
} |
| 251 |
|
| 252 |
func (fileInfo *FileInfo) GetThumbnailInfo() *FileInfo { |
| 253 |
if fileInfo.ThumbnailInfo == nil { |
| 254 |
fileInfo.ThumbnailInfo = &FileInfo{} |
| 255 |
} |
| 256 |
return fileInfo.ThumbnailInfo |
| 257 |
} |