Debian Codegraph @

appservice/protocol.go

raw
1 // Copyright (c) 2021 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 appservice
8
9 import (
10 "encoding/json"
11 "fmt"
12 "net/http"
13 "strings"
14
15 "maunium.net/go/mautrix"
16 "maunium.net/go/mautrix/event"
17 "maunium.net/go/mautrix/id"
18 )
19
20 // Transaction contains a list of events.
21 type Transaction struct {
22 Events []*event.Event `json:"events"`
23 EphemeralEvents []*event.Event `json:"ephemeral,omitempty"`
24 DeviceLists *mautrix.DeviceLists `json:"device_lists,omitempty"`
25 DeviceOTKCount map[id.UserID]mautrix.OTKCount `json:"device_one_time_keys_count,omitempty"`
26
27 MSC2409EphemeralEvents []*event.Event `json:"de.sorunome.msc2409.ephemeral,omitempty"`
28 MSC3202DeviceLists *mautrix.DeviceLists `json:"org.matrix.msc3202.device_lists,omitempty"`
29 MSC3202DeviceOTKCount map[id.UserID]mautrix.OTKCount `json:"org.matrix.msc3202.device_one_time_keys_count,omitempty"`
30 }
31
32 func (txn *Transaction) ContentString() string {
33 var parts []string
34 if len(txn.Events) > 0 {
35 parts = append(parts, fmt.Sprintf("%d PDUs", len(txn.Events)))
36 }
37 if len(txn.EphemeralEvents) > 0 {
38 parts = append(parts, fmt.Sprintf("%d EDUs", len(txn.EphemeralEvents)))
39 } else if len(txn.MSC2409EphemeralEvents) > 0 {
40 parts = append(parts, fmt.Sprintf("%d EDUs (unstable)", len(txn.MSC2409EphemeralEvents)))
41 }
42 if len(txn.DeviceOTKCount) > 0 {
43 parts = append(parts, fmt.Sprintf("OTK counts for %d users", len(txn.DeviceOTKCount)))
44 } else if len(txn.MSC3202DeviceOTKCount) > 0 {
45 parts = append(parts, fmt.Sprintf("OTK counts for %d users (unstable)", len(txn.MSC3202DeviceOTKCount)))
46 }
47 if txn.DeviceLists != nil {
48 parts = append(parts, fmt.Sprintf("%d device list changes", len(txn.DeviceLists.Changed)))
49 } else if txn.MSC3202DeviceLists != nil {
50 parts = append(parts, fmt.Sprintf("%d device list changes (unstable)", len(txn.MSC3202DeviceLists.Changed)))
51 }
52 return strings.Join(parts, ", ")
53 }
54
55 // EventListener is a function that receives events.
56 type EventListener func(evt *event.Event)
57
58 // WriteBlankOK writes a blank OK message as a reply to a HTTP request.
59 func WriteBlankOK(w http.ResponseWriter) {
60 w.Header().Add("Content-Type", "application/json")
61 w.WriteHeader(http.StatusOK)
62 _, _ = w.Write([]byte("{}"))
63 }
64
65 // Respond responds to a HTTP request with a JSON object.
66 func Respond(w http.ResponseWriter, data interface{}) error {
67 w.Header().Add("Content-Type", "application/json")
68 dataStr, err := json.Marshal(data)
69 if err != nil {
70 return err
71 }
72 _, err = w.Write(dataStr)
73 return err
74 }
75
76 // Error represents a Matrix protocol error.
77 type Error struct {
78 HTTPStatus int `json:"-"`
79 ErrorCode ErrorCode `json:"errcode"`
80 Message string `json:"error"`
81 }
82
83 func (err Error) Write(w http.ResponseWriter) {
84 w.Header().Add("Content-Type", "application/json")
85 w.WriteHeader(err.HTTPStatus)
86 _ = Respond(w, &err)
87 }
88
89 // ErrorCode is the machine-readable code in an Error.
90 type ErrorCode string
91
92 // Native ErrorCodes
93 const (
94 ErrUnknownToken ErrorCode = "M_UNKNOWN_TOKEN"
95 ErrBadJSON ErrorCode = "M_BAD_JSON"
96 ErrNotJSON ErrorCode = "M_NOT_JSON"
97 ErrUnknown ErrorCode = "M_UNKNOWN"
98 )
99
100 // Custom ErrorCodes
101 const (
102 ErrNoTransactionID ErrorCode = "NET.MAUNIUM.NO_TRANSACTION_ID"
103 )