mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-08-02 06:12:45 +00:00
Use utility methods from gomatrixserverlib. (#152)
* Use utility methods from gomatrixserverlib, rather than reimplementing them * Return string rather than pointer to string * Update gomatrixserverlib
This commit is contained in:
parent
b13cbb18fb
commit
69c29172c3
21 changed files with 118 additions and 129 deletions
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"),
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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})
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"),
|
||||
|
|
|
@ -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 ""
|
||||
}
|
||||
|
|
|
@ -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":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue