-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,622 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package fclient | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/matrix-org/gomatrixserverlib" | ||
) | ||
|
||
func NewInviteV3Request(event gomatrixserverlib.ProtoEvent, version gomatrixserverlib.RoomVersion, state []gomatrixserverlib.InviteStrippedState) ( | ||
request InviteV3Request, err error, | ||
) { | ||
if !gomatrixserverlib.KnownRoomVersion(version) { | ||
err = gomatrixserverlib.UnsupportedRoomVersionError{ | ||
Version: version, | ||
} | ||
return | ||
} | ||
request.fields.inviteV2RequestHeaders = inviteV2RequestHeaders{ | ||
RoomVersion: version, | ||
InviteRoomState: state, | ||
} | ||
request.fields.Event = event | ||
return | ||
} | ||
|
||
// InviteV3Request is used in the body of a /_matrix/federation/v3/invite request. | ||
type InviteV3Request struct { | ||
fields struct { | ||
inviteV2RequestHeaders | ||
Event gomatrixserverlib.ProtoEvent `json:"event"` | ||
} | ||
} | ||
|
||
// MarshalJSON implements json.Marshaller | ||
func (i InviteV3Request) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(i.fields) | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaller | ||
func (i *InviteV3Request) UnmarshalJSON(data []byte) error { | ||
err := json.Unmarshal(data, &i.fields) | ||
if err != nil { | ||
return err | ||
} | ||
return err | ||
} | ||
|
||
// Event returns the invite event. | ||
func (i *InviteV3Request) Event() gomatrixserverlib.ProtoEvent { | ||
return i.fields.Event | ||
} | ||
|
||
// RoomVersion returns the room version of the invited room. | ||
func (i *InviteV3Request) RoomVersion() gomatrixserverlib.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 *InviteV3Request) InviteRoomState() []gomatrixserverlib.InviteStrippedState { | ||
return i.fields.InviteRoomState | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package fclient | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/matrix-org/gomatrixserverlib" | ||
"github.com/matrix-org/gomatrixserverlib/spec" | ||
) | ||
|
||
func TestMarshalInviteV3Request(t *testing.T) { | ||
expected := `{"room_version":"org.matrix.msc4014","invite_room_state":[],"event":{"sender":"@test:localhost","room_id":"!19Mp0U9hjajeIiw1:localhost","type":"m.room.name","state_key":"","prev_events":["upCsBqUhNUgT2/+zkzg8TbqdQpWWKQnZpGJc6KcbUC4"],"auth_events":["abjkiDSg1RkuZrbj2jZoGMlQaaj1Ue3Jhi7I7NlKfXY","X7RUj46hM/8sUHNBIFkStbOauPvbDzjSdH4NibYWnko","k9eM6utkCH8vhLW9/oRsH74jOBS/6RVK42iGDFbylno"],"depth":7,"signatures":{"localhost":{"ed25519:u9kP":"5IzSuRXkxvbTp0vZhhXYZeOe+619iG3AybJXr7zfNn/4vHz4TH7qSJVQXSaHHvcTcDodAKHnTG1WDulgO5okAQ"}},"content":{"name":"test3"}}}` | ||
|
||
senderID := "@test:localhost" | ||
roomID := "!19Mp0U9hjajeIiw1:localhost" | ||
eventType := "m.room.name" | ||
stateKey := "" | ||
prevEvents := []string{"upCsBqUhNUgT2/+zkzg8TbqdQpWWKQnZpGJc6KcbUC4"} | ||
authEvents := []string{"abjkiDSg1RkuZrbj2jZoGMlQaaj1Ue3Jhi7I7NlKfXY", "X7RUj46hM/8sUHNBIFkStbOauPvbDzjSdH4NibYWnko", "k9eM6utkCH8vhLW9/oRsH74jOBS/6RVK42iGDFbylno"} | ||
depth := int64(7) | ||
signatures := spec.RawJSON(`{"localhost": {"ed25519:u9kP": "5IzSuRXkxvbTp0vZhhXYZeOe+619iG3AybJXr7zfNn/4vHz4TH7qSJVQXSaHHvcTcDodAKHnTG1WDulgO5okAQ"}}`) | ||
content := spec.RawJSON(`{"name":"test3"}`) | ||
|
||
output := gomatrixserverlib.ProtoEvent{ | ||
SenderID: senderID, | ||
RoomID: roomID, | ||
Type: eventType, | ||
StateKey: &stateKey, | ||
PrevEvents: prevEvents, | ||
AuthEvents: authEvents, | ||
Depth: depth, | ||
Signature: signatures, | ||
Content: content, | ||
} | ||
|
||
inviteReq, err := NewInviteV3Request(output, gomatrixserverlib.RoomVersionPseudoIDs, []gomatrixserverlib.InviteStrippedState{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
j, err := json.Marshal(inviteReq) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if string(j) != expected { | ||
t.Fatalf("\nresult: %q\nwanted: %q", string(j), expected) | ||
} | ||
|
||
var newRequest InviteV3Request | ||
err = json.Unmarshal(j, &newRequest) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if newRequest.RoomVersion() != gomatrixserverlib.RoomVersionPseudoIDs { | ||
t.Fatalf("unmatched room version. expected: %v, got: %v", gomatrixserverlib.RoomVersionPseudoIDs, newRequest.RoomVersion()) | ||
} | ||
if len(newRequest.InviteRoomState()) != 0 { | ||
t.Fatalf("invite room state should not have any events") | ||
} | ||
if newRequest.Event().SenderID != senderID { | ||
t.Fatalf("unmatched senderID. expected: %v, got: %v", newRequest.Event().SenderID, senderID) | ||
|
||
} | ||
if newRequest.Event().RoomID != roomID { | ||
t.Fatalf("unmatched roomID. expected: %v, got: %v", newRequest.Event().RoomID, roomID) | ||
} | ||
if newRequest.Event().Type != eventType { | ||
t.Fatalf("unmatched type. expected: %v, got: %v", newRequest.Event().Type, eventType) | ||
|
||
} | ||
if *newRequest.Event().StateKey != stateKey { | ||
t.Fatalf("unmatched state key. expected: %v, got: %v", *newRequest.Event().StateKey, stateKey) | ||
} | ||
if newRequest.Event().Depth != depth { | ||
t.Fatalf("unmatched depth. expected: %v, got: %v", newRequest.Event().Depth, depth) | ||
} | ||
} |
Oops, something went wrong.