forked from matrix-org/gomatrixserverlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvitev2.go
132 lines (113 loc) · 3.71 KB
/
invitev2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package gomatrixserverlib
import (
"encoding/json"
"errors"
"github.com/tidwall/gjson"
)
// InviteV2Request and InviteV2StrippedState are defined in
// https://matrix.org/docs/spec/server_server/r0.1.3#put-matrix-federation-v2-invite-roomid-eventid
func NewInviteV2Request(event *HeaderedEvent, state []InviteV2StrippedState) (
request InviteV2Request, err error,
) {
if ver, ok := SupportedRoomVersions()[event.RoomVersion]; !ok || !ver.Supported {
err = UnsupportedRoomVersionError{
Version: event.RoomVersion,
}
return
}
request.fields.inviteV2RequestHeaders = inviteV2RequestHeaders{
RoomVersion: event.RoomVersion,
InviteRoomState: state,
}
request.fields.Event = event.Unwrap()
return
}
type inviteV2RequestHeaders struct {
RoomVersion RoomVersion `json:"room_version"`
InviteRoomState []InviteV2StrippedState `json:"invite_room_state"`
}
// InviteV2Request is used in the body of a /_matrix/federation/v2/invite request.
type InviteV2Request struct {
fields struct {
inviteV2RequestHeaders
Event *Event `json:"event"`
}
}
// MarshalJSON implements json.Marshaller
func (i InviteV2Request) MarshalJSON() ([]byte, error) {
return json.Marshal(i.fields)
}
// UnmarshalJSON implements json.Unmarshaller
func (i *InviteV2Request) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, &i.fields.inviteV2RequestHeaders)
if err != nil {
return err
}
eventJSON := gjson.GetBytes(data, "event")
if !eventJSON.Exists() {
return errors.New("gomatrixserverlib: request doesn't contain event")
}
if ver, ok := SupportedRoomVersions()[i.fields.RoomVersion]; !ok || !ver.Supported {
return UnsupportedRoomVersionError{
Version: i.fields.RoomVersion,
}
}
i.fields.Event, err = NewEventFromUntrustedJSON([]byte(eventJSON.String()), i.fields.RoomVersion)
return err
}
// Event returns the invite event.
func (i *InviteV2Request) Event() *Event {
return i.fields.Event
}
// RoomVersion returns the room version of the invited room.
func (i *InviteV2Request) RoomVersion() RoomVersion {
return i.fields.RoomVersion
}
// InviteRoomState returns stripped state events for the room, containing
// enough information for the client to identify the room.
func (i *InviteV2Request) InviteRoomState() []InviteV2StrippedState {
return i.fields.InviteRoomState
}
// InviteV2StrippedState is a cut-down set of fields from room state
// events that allow the invited server to identify the room.
type InviteV2StrippedState struct {
fields struct {
Content RawJSON `json:"content"`
StateKey *string `json:"state_key"`
Type string `json:"type"`
Sender string `json:"sender"`
}
}
// NewInviteV2StrippedState creates a stripped state event from a
// regular state event.
func NewInviteV2StrippedState(event *Event) (ss InviteV2StrippedState) {
ss.fields.Content = event.Content()
ss.fields.StateKey = event.StateKey()
ss.fields.Type = event.Type()
ss.fields.Sender = event.Sender()
return
}
// MarshalJSON implements json.Marshaller
func (i InviteV2StrippedState) MarshalJSON() ([]byte, error) {
return json.Marshal(i.fields)
}
// UnmarshalJSON implements json.Unmarshaller
func (i *InviteV2StrippedState) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &i.fields)
}
// Content returns the content of the stripped state.
func (i *InviteV2StrippedState) Content() RawJSON {
return i.fields.Content
}
// StateKey returns the state key of the stripped state.
func (i *InviteV2StrippedState) StateKey() *string {
return i.fields.StateKey
}
// Type returns the type of the stripped state.
func (i *InviteV2StrippedState) Type() string {
return i.fields.Type
}
// Sender returns the sender of the stripped state.
func (i *InviteV2StrippedState) Sender() string {
return i.fields.Sender
}