Use utility methods from gomatrixserverlib. (#152)

* Use utility methods from gomatrixserverlib, rather than reimplementing them

* Return string rather than pointer to string

* Update gomatrixserverlib
main
Mark Haines 2017-07-07 14:11:32 +01:00 committed by GitHub
parent b13cbb18fb
commit 69c29172c3
21 changed files with 118 additions and 129 deletions

View File

@ -49,7 +49,7 @@ func (c *RoomserverProducer) SendEvents(events []gomatrixserverlib.Event, sendAs
ires[i] = api.InputRoomEvent{
Kind: api.KindNew,
Event: event.JSON(),
AuthEventIDs: authEventIDs(event),
AuthEventIDs: event.AuthEventIDs(),
SendAsServer: string(sendAsServer),
}
eventIDs[i] = event.EventID()
@ -71,7 +71,7 @@ func (c *RoomserverProducer) SendEventWithState(state gomatrixserverlib.RespStat
ires[i] = api.InputRoomEvent{
Kind: api.KindOutlier,
Event: outlier.JSON(),
AuthEventIDs: authEventIDs(outlier),
AuthEventIDs: outlier.AuthEventIDs(),
}
eventIDs[i] = outlier.EventID()
}
@ -84,7 +84,7 @@ func (c *RoomserverProducer) SendEventWithState(state gomatrixserverlib.RespStat
ires[len(outliers)] = api.InputRoomEvent{
Kind: api.KindNew,
Event: event.JSON(),
AuthEventIDs: authEventIDs(event),
AuthEventIDs: event.AuthEventIDs(),
HasState: true,
StateEventIDs: stateEventIDs,
}
@ -93,14 +93,6 @@ func (c *RoomserverProducer) SendEventWithState(state gomatrixserverlib.RespStat
return c.SendInputRoomEvents(ires, eventIDs)
}
// TODO Make this a method on gomatrixserverlib.Event
func authEventIDs(event gomatrixserverlib.Event) (ids []string) {
for _, ref := range event.AuthEvents() {
ids = append(ids, ref.EventID)
}
return
}
// SendInputRoomEvents writes the given input room events to the roomserver input log. The length of both
// arrays must match, and each element must correspond to the same event.
func (c *RoomserverProducer) SendInputRoomEvents(ires []api.InputRoomEvent, eventIDs []string) error {

View File

@ -16,6 +16,8 @@ package readers
import (
"fmt"
"net/http"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
@ -23,8 +25,6 @@ import (
"github.com/matrix-org/gomatrix"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
"net/http"
"strings"
)
// DirectoryRoom looks up a room alias
@ -35,7 +35,7 @@ func DirectoryRoom(
federation *gomatrixserverlib.FederationClient,
cfg *config.Dendrite,
) util.JSONResponse {
domain, err := domainFromID(roomAlias)
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
if err != nil {
return util.JSONResponse{
Code: 400,
@ -69,19 +69,3 @@ func DirectoryRoom(
}
}
}
// domainFromID returns everything after the first ":" character to extract
// the domain part of a matrix ID.
// TODO: duplicated from gomatrixserverlib.
func domainFromID(id string) (gomatrixserverlib.ServerName, error) {
// IDs have the format: SIGIL LOCALPART ":" DOMAIN
// Split on the first ":" character since the domain can contain ":"
// characters.
parts := strings.SplitN(id, ":", 2)
if len(parts) != 2 {
// The ID must have a ":" character.
return "", fmt.Errorf("invalid ID: %q", id)
}
// Return everything after the first ":" character.
return gomatrixserverlib.ServerName(parts[1]), nil
}

View File

@ -60,14 +60,7 @@ func (r createRoomRequest) Validate() *util.JSONResponse {
// It should be a struct (with pointers into a single string to avoid copying) and
// we should update all refs to use UserID types rather than strings.
// https://github.com/matrix-org/synapse/blob/v0.19.2/synapse/types.py#L92
if len(userID) == 0 || userID[0] != '@' {
return &util.JSONResponse{
Code: 400,
JSON: jsonerror.BadJSON("user id must start with '@'"),
}
}
parts := strings.SplitN(userID[1:], ":", 2)
if len(parts) != 2 {
if _, _, err := gomatrixserverlib.SplitID('@', userID); err != nil {
return &util.JSONResponse{
Code: 400,
JSON: jsonerror.BadJSON("user id must be in the form @localpart:domain"),

View File

@ -16,6 +16,10 @@ package writers
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
@ -25,9 +29,6 @@ import (
"github.com/matrix-org/gomatrix"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
"net/http"
"strings"
"time"
)
// JoinRoomByIDOrAlias implements the "/join/{roomIDOrAlias}" API.
@ -88,7 +89,7 @@ func (r joinRoomReq) joinRoomByID() util.JSONResponse {
// joinRoomByAlias joins a room using a room alias.
func (r joinRoomReq) joinRoomByAlias(roomAlias string) util.JSONResponse {
domain, err := domainFromID(roomAlias)
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
if err != nil {
return util.JSONResponse{
Code: 400,
@ -245,19 +246,3 @@ func (r joinRoomReq) joinRoomUsingServer(roomID string, server gomatrixserverlib
}{roomID},
}, nil
}
// domainFromID returns everything after the first ":" character to extract
// the domain part of a matrix ID.
// TODO: duplicated from gomatrixserverlib.
func domainFromID(id string) (gomatrixserverlib.ServerName, error) {
// IDs have the format: SIGIL LOCALPART ":" DOMAIN
// Split on the first ":" character since the domain can contain ":"
// characters.
parts := strings.SplitN(id, ":", 2)
if len(parts) != 2 {
// The ID must have a ":" character.
return "", fmt.Errorf("invalid ID: %q", id)
}
// Return everything after the first ":" character.
return gomatrixserverlib.ServerName(parts[1]), nil
}

View File

@ -118,11 +118,7 @@ type unknownRoomError struct {
func (e unknownRoomError) Error() string { return fmt.Sprintf("unknown room %q", e.roomID) }
func (t *txnReq) processEvent(e gomatrixserverlib.Event) error {
refs := e.PrevEvents()
prevEventIDs := make([]string, len(refs))
for i := range refs {
prevEventIDs[i] = refs[i].EventID
}
prevEventIDs := e.PrevEventIDs()
// Fetch the state needed to authenticate the event.
needed := gomatrixserverlib.StateNeededForAuth([]gomatrixserverlib.Event{e})

View File

@ -17,7 +17,6 @@ package consumers
import (
"encoding/json"
"fmt"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/matrix-org/dendrite/common"
@ -220,16 +219,14 @@ func joinedHostsFromEvents(evs []gomatrixserverlib.Event) ([]types.JoinedHost, e
if ev.Type() != "m.room.member" || ev.StateKey() == nil {
continue
}
var content struct {
Membership string `json:"membership"`
}
if err := json.Unmarshal(ev.Content(), &content); err != nil {
membership, err := ev.Membership()
if err != nil {
return nil, err
}
if content.Membership != "join" {
if membership != "join" {
continue
}
serverName, err := domainFromID(*ev.StateKey())
_, serverName, err := gomatrixserverlib.SplitID('@', *ev.StateKey())
if err != nil {
return nil, err
}
@ -343,19 +340,3 @@ func missingEventsFrom(events []gomatrixserverlib.Event, required []string) []st
}
return missing
}
// domainFromID returns everything after the first ":" character to extract
// the domain part of a matrix ID.
// TODO: duplicated from gomatrixserverlib.
func domainFromID(id string) (gomatrixserverlib.ServerName, error) {
// IDs have the format: SIGIL LOCALPART ":" DOMAIN
// Split on the first ":" character since the domain can contain ":"
// characters.
parts := strings.SplitN(id, ":", 2)
if len(parts) != 2 {
// The ID must have a ":" character.
return "", fmt.Errorf("invalid ID: %q", id)
}
// Return everything after the first ":" character.
return gomatrixserverlib.ServerName(parts[1]), nil
}

View File

@ -20,7 +20,6 @@ import (
"net/http"
"net/url"
"path"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
@ -29,6 +28,7 @@ import (
"github.com/matrix-org/dendrite/mediaapi/storage"
"github.com/matrix-org/dendrite/mediaapi/thumbnailer"
"github.com/matrix-org/dendrite/mediaapi/types"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
)
@ -193,14 +193,7 @@ func (r *uploadRequest) Validate(maxFileSizeBytes config.FileSizeBytes) *util.JS
// It should be a struct (with pointers into a single string to avoid copying) and
// we should update all refs to use UserID types rather than strings.
// https://github.com/matrix-org/synapse/blob/v0.19.2/synapse/types.py#L92
if len(r.MediaMetadata.UserID) == 0 || r.MediaMetadata.UserID[0] != '@' {
return &util.JSONResponse{
Code: 400,
JSON: jsonerror.Unknown("user id must start with '@'"),
}
}
parts := strings.SplitN(string(r.MediaMetadata.UserID[1:]), ":", 2)
if len(parts) != 2 {
if _, _, err := gomatrixserverlib.SplitID('@', string(r.MediaMetadata.UserID)); err != nil {
return &util.JSONResponse{
Code: 400,
JSON: jsonerror.BadJSON("user id must be in the form @localpart:domain"),

View File

@ -16,11 +16,9 @@ package storage
import (
"database/sql"
"encoding/json"
"fmt"
// Import the postgres database driver.
_ "github.com/lib/pq"
"github.com/matrix-org/dendrite/clientapi/events"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/syncapi/types"
"github.com/matrix-org/gomatrixserverlib"
@ -129,11 +127,11 @@ func (d *SyncServerDatabase) updateRoomState(
}
var membership *string
if event.Type() == "m.room.member" {
var memberContent events.MemberContent
if err := json.Unmarshal(event.Content(), &memberContent); err != nil {
value, err := event.Membership()
if err != nil {
return err
}
membership = &memberContent.Membership
membership = &value
}
if err := d.roomstate.upsertRoomState(txn, event, membership, int64(streamPos)); err != nil {
return err
@ -473,11 +471,11 @@ func removeDuplicates(stateEvents, recentEvents []gomatrixserverlib.Event) []gom
// with type 'm.room.member' and state_key of userID. Otherwise, an empty string is returned.
func getMembershipFromEvent(ev *gomatrixserverlib.Event, userID string) string {
if ev.Type() == "m.room.member" && ev.StateKeyEquals(userID) {
var memberContent events.MemberContent
if err := json.Unmarshal(ev.Content(), &memberContent); err != nil {
membership, err := ev.Membership()
if err != nil {
return ""
}
return memberContent.Membership
return membership
}
return ""
}

View File

@ -15,11 +15,9 @@
package sync
import (
"encoding/json"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/matrix-org/dendrite/clientapi/events"
"github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/types"
"github.com/matrix-org/gomatrixserverlib"
@ -68,14 +66,14 @@ func (n *Notifier) OnNewEvent(ev *gomatrixserverlib.Event, pos types.StreamPosit
// If this is an invite, also add in the invitee to this list.
if ev.Type() == "m.room.member" && ev.StateKey() != nil {
userID := *ev.StateKey()
var memberContent events.MemberContent
if err := json.Unmarshal(ev.Content(), &memberContent); err != nil {
membership, err := ev.Membership()
if err != nil {
log.WithError(err).WithField("event_id", ev.EventID()).Errorf(
"Notifier.OnNewEvent: Failed to unmarshal member event",
)
} else {
// Keep the joined user map up-to-date
switch memberContent.Membership {
switch membership {
case "invite":
userIDs = append(userIDs, userID)
case "join":

2
vendor/manifest vendored
View File

@ -98,7 +98,7 @@
{
"importpath": "github.com/matrix-org/gomatrixserverlib",
"repository": "https://github.com/matrix-org/gomatrixserverlib",
"revision": "0e1596ae7b0a034ec572cd1448aeaf7e96bff95a",
"revision": "30652b26ec2e83b97c941eb1c293bf7d67340f74",
"branch": "master"
},
{

View File

@ -18,8 +18,10 @@ package gomatrixserverlib
import (
"encoding/json"
"fmt"
"golang.org/x/crypto/ed25519"
"strings"
"time"
"golang.org/x/crypto/ed25519"
)
// A StateKeyTuple is the combination of an event type and an event state key.
@ -54,7 +56,7 @@ type EventBuilder struct {
Type string `json:"type"`
// The state_key of the event if the event is a state event or nil if the event is not a state event.
StateKey *string `json:"state_key,omitempty"`
// The events that immediately preceeded this event in the room history.
// The events that immediately preceded this event in the room history.
PrevEvents []EventReference `json:"prev_events"`
// The events needed to authenticate this event.
AuthEvents []EventReference `json:"auth_events"`
@ -112,7 +114,7 @@ var emptyEventReferenceList = []EventReference{}
// Build a new Event.
// This is used when a local event is created on this server.
// Call this after filling out the necessary fields.
// This can be called mutliple times on the same builder.
// This can be called multiple times on the same builder.
// A different event ID must be supplied each time this is called.
func (eb *EventBuilder) Build(eventID string, now time.Time, origin ServerName, keyID KeyID, privateKey ed25519.PrivateKey) (result Event, err error) {
var event struct {
@ -467,11 +469,44 @@ func (e Event) PrevEvents() []EventReference {
return e.fields.PrevEvents
}
// PrevEventIDs returns the event IDs of the direct ancestors of the event.
func (e Event) PrevEventIDs() []string {
result := make([]string, len(e.fields.PrevEvents))
for i := range e.fields.PrevEvents {
result[i] = e.fields.PrevEvents[i].EventID
}
return result
}
// Membership returns the value of the content.membership field if this event
// is an "m.room.member" event.
// Returns an error if the event is not a m.room.member event or if the content
// is not valid m.room.member content.
func (e Event) Membership() (string, error) {
if e.fields.Type != MRoomMember {
return "", fmt.Errorf("gomatrixserverlib: not an m.room.member event")
}
var content memberContent
if err := json.Unmarshal(e.fields.Content, &content); err != nil {
return "", err
}
return content.Membership, nil
}
// AuthEvents returns references to the events needed to auth the event.
func (e Event) AuthEvents() []EventReference {
return e.fields.AuthEvents
}
// AuthEventIDs returns the event IDs of the events needed to auth the event.
func (e Event) AuthEventIDs() []string {
result := make([]string, len(e.fields.AuthEvents))
for i := range e.fields.AuthEvents {
result[i] = e.fields.AuthEvents[i].EventID
}
return result
}
// Redacts returns the event ID of the event this event redacts.
func (e Event) Redacts() string {
return e.fields.Redacts
@ -534,3 +569,19 @@ func (er EventReference) MarshalJSON() ([]byte, error) {
return json.Marshal(&tuple)
}
// SplitID splits a matrix ID into a local part and a server name.
func SplitID(sigil byte, id string) (local string, domain ServerName, err error) {
// IDs have the format: SIGIL LOCALPART ":" DOMAIN
// Split on the first ":" character since the domain can contain ":"
// characters.
if len(id) == 0 || id[0] != sigil {
return "", "", fmt.Errorf("gomatriserverlib: invalid ID %q doesn't start with %q", id, sigil)
}
parts := strings.SplitN(id, ":", 2)
if len(parts) != 2 {
// The ID must have a ":" character.
return "", "", fmt.Errorf("gomatrixserverlib: invalid ID %q missing ':'", id)
}
return parts[0][1:], ServerName(parts[1]), nil
}

View File

@ -379,6 +379,9 @@ func aliasEventAllowed(event Event, authEvents AuthEventProvider) error {
// https://github.com/matrix-org/synapse/blob/v0.18.5/synapse/api/auth.py#L143-L160
create, err := newCreateContentFromAuthEvents(authEvents)
if err != nil {
return err
}
senderDomain, err := domainFromID(event.Sender())
if err != nil {

View File

@ -213,7 +213,7 @@ type respSendJoinFields struct {
AuthEvents []Event `json:"auth_chain"`
}
// Check that a reponse to /send_join is valid.
// Check that a response to /send_join is valid.
// This checks that it would be valid as a response to /state
// This also checks that the join event is allowed by the state.
func (r RespSendJoin) Check(keyRing KeyRing, joinEvent Event) error {

View File

@ -3,7 +3,22 @@
set -eu
golint ./...
go fmt
misspell -error .
# gofmt doesn't exit with an error code if the files don't match the expected
# format. So we have to run it and see if it outputs anything.
if gofmt -l -s . 2>&1 | read
then
echo "Error: not all code had been formatted with gofmt."
echo "Fixing the following files"
gofmt -s -w -l .
echo
echo "Please add them to the commit"
git status --short
exit 1
fi
ineffassign .
go tool vet --all --shadow .
gocyclo -over 16 .
go test -timeout 5s . ./...

View File

@ -23,7 +23,7 @@ import (
"unicode/utf8"
)
// CanonicalJSON re-encodes the JSON in a cannonical encoding. The encoding is
// CanonicalJSON re-encodes the JSON in a canonical encoding. The encoding is
// the shortest possible encoding using integer values with sorted object keys.
// https://matrix.org/docs/spec/server_server/unstable.html#canonical-json
func CanonicalJSON(input []byte) ([]byte, error) {
@ -223,7 +223,7 @@ func compactUnicodeEscape(input, output []byte, index int) ([]byte, int) {
// Taken from https://github.com/NegativeMjark/indolentjson-rust/blob/8b959791fe2656a88f189c5d60d153be05fe3deb/src/readhex.rs#L21
func readHexDigits(input []byte) uint32 {
hex := binary.BigEndian.Uint32(input)
// substract '0'
// subtract '0'
hex -= 0x30303030
// strip the higher bits, maps 'a' => 'A'
hex &= 0x1F1F1F1F

View File

@ -167,7 +167,7 @@ type KeyChecks struct {
AllEd25519ChecksOK *bool // All the Ed25519 checks are ok. or null if there weren't any to check.
Ed25519Checks map[KeyID]Ed25519Checks // Checks for Ed25519 keys.
HasTLSFingerprint bool // The server has at least one fingerprint.
AllTLSFingerprintChecksOK *bool // All the fingerpint checks are ok.
AllTLSFingerprintChecksOK *bool // All the fingerprint checks are ok.
TLSFingerprintChecks []TLSFingerprintChecks // Checks for TLS fingerprints.
MatchingTLSFingerprint *bool // The TLS fingerprint for the connection matches one of the listed fingerprints.
}

View File

@ -268,7 +268,7 @@ func readHTTPRequest(req *http.Request) (*FederationRequest, error) {
}
result.fields.Origin = origin
if result.fields.Signatures == nil {
result.fields.Signatures = map[ServerName]map[KeyID]string{origin: map[KeyID]string{key: sig}}
result.fields.Signatures = map[ServerName]map[KeyID]string{origin: {key: sig}}
} else {
result.fields.Signatures[origin][key] = sig
}

View File

@ -57,7 +57,7 @@ func LookupServer(serverName ServerName) (*DNSResult, error) {
return nil, err
}
// If there isn't a SRV record in DNS then fallback to "serverName:8448".
hosts[string(serverName)] = []net.SRV{net.SRV{
hosts[string(serverName)] = []net.SRV{{
Target: string(serverName),
Port: 8448,
}}
@ -80,7 +80,7 @@ func LookupServer(serverName ServerName) (*DNSResult, error) {
if err != nil {
return nil, err
}
hosts[host] = []net.SRV{net.SRV{
hosts[host] = []net.SRV{{
Target: host,
Port: uint16(port),
}}

View File

@ -179,7 +179,7 @@ func TestSignJSONTestVectors(t *testing.T) {
type MyMessage struct {
Unsigned *json.RawMessage `json:"unsigned"`
Content *json.RawMessage `json:"content"`
Signatures *json.RawMessage `json:"signature,omitempty"`
Signatures *json.RawMessage `json:"signatures,omitempty"`
}
func TestSignJSONWithUnsigned(t *testing.T) {
@ -215,7 +215,7 @@ func TestSignJSONWithUnsigned(t *testing.T) {
t.Fatal(err)
}
err = VerifyJSON(entityName, keyID, publicKey, signed)
err = VerifyJSON(entityName, keyID, publicKey, input)
if err != nil {
t.Errorf("VerifyJSON(%q)", signed)
t.Fatal(err)

View File

@ -12,15 +12,15 @@ const (
func TestConflictEventSorter(t *testing.T) {
input := []Event{
Event{fields: eventFields{Depth: 1, EventID: "@1:a"}},
Event{fields: eventFields{Depth: 2, EventID: "@2:a"}},
Event{fields: eventFields{Depth: 2, EventID: "@3:b"}},
{fields: eventFields{Depth: 1, EventID: "@1:a"}},
{fields: eventFields{Depth: 2, EventID: "@2:a"}},
{fields: eventFields{Depth: 2, EventID: "@3:b"}},
}
got := sortConflictedEventsByDepthAndSHA1(input)
want := []conflictedEvent{
conflictedEvent{depth: 1, event: &input[0]},
conflictedEvent{depth: 2, event: &input[2]},
conflictedEvent{depth: 2, event: &input[1]},
{depth: 1, event: &input[0]},
{depth: 2, event: &input[2]},
{depth: 2, event: &input[1]},
}
copy(want[0].eventIDSHA1[:], sha1OfEventID1A)
copy(want[1].eventIDSHA1[:], sha1OfEventID3B)

View File

@ -24,7 +24,7 @@ type Transaction struct {
}
// A TransactionID identifies a transaction sent by a matrix server to another
// matrix server. The ID must be unique amoungst the transactions sent from the
// matrix server. The ID must be unique amongst the transactions sent from the
// origin server to the destination, but doesn't have to be globally unique.
// The ID must be safe to insert into a URL path segment. The ID should have a
// format matching '^[0-9A-Za-z\-_]*$'