Reformat canonical alias stuff

This commit is contained in:
Colin Weill--Duflos 2021-07-29 18:34:57 +02:00
parent 4e68ce3a06
commit d9ca7565b0
6 changed files with 196 additions and 197 deletions

View file

@ -169,6 +169,7 @@ func NotTrusted(serverName string) *MatrixError {
Err: fmt.Sprintf("Untrusted server '%s'", serverName), Err: fmt.Sprintf("Untrusted server '%s'", serverName),
} }
} }
// BadAlias is an error which is returned when one or more aliases within a // BadAlias is an error which is returned when one or more aliases within a
// m.room.canonical_alias event do not point to the room ID for which the state // m.room.canonical_alias event do not point to the room ID for which the state
// event is to be sent to. // event is to be sent to.

View file

@ -15,14 +15,14 @@
package routing package routing
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"encoding/json"
"github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/internal/eventutil"
federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api" federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api"
"github.com/matrix-org/dendrite/internal/eventutil"
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/api"
@ -223,17 +223,20 @@ func RemoveLocalAlias(
} }
} }
var updatedCanonicalAlias *eventutil.CanonicalAlias updatedCanonicalAlias := eventutil.CanonicalAlias{
updated, resErr := getUpdatedCanonicalAliasState(req, device, queryRes.RoomID, alias, rsAPI, updatedCanonicalAlias) Alias: "",
AltAliases: []string{""},
}
updated, resErr := getUpdatedCanonicalAliasState(req, queryRes.RoomID, alias, rsAPI, &updatedCanonicalAlias)
if resErr != nil { if resErr != nil {
return *resErr; return *resErr
} }
// If the alias removed is one of the alt_aliases or the canonical one, // If the alias removed is one of the alt_aliases or the canonical one,
// we need to also remove it from the canonical_alias event // we need to also remove it from the canonical_alias event
if updated { if updated {
resErr := updateCanonicalAlias(req, device, queryRes.RoomID, cfg, rsAPI, updatedCanonicalAlias) resErr := updateCanonicalAlias(req, device, queryRes.RoomID, cfg, rsAPI, &updatedCanonicalAlias)
if resErr != nil { if resErr != nil {
return *resErr; return *resErr
} }
} }
@ -245,7 +248,6 @@ func RemoveLocalAlias(
func getUpdatedCanonicalAliasState( func getUpdatedCanonicalAliasState(
req *http.Request, req *http.Request,
device *api.Device,
roomID string, roomID string,
alias string, alias string,
rsAPI roomserverAPI.RoomserverInternalAPI, rsAPI roomserverAPI.RoomserverInternalAPI,
@ -268,10 +270,6 @@ func getUpdatedCanonicalAliasState(
return false, &resErr return false, &resErr
} }
updatedCanonicalAlias = &eventutil.CanonicalAlias {
Alias: "",
AltAliases: []string{""},
}
// We try to get the current canonical_alias state, and if found compare its content // We try to get the current canonical_alias state, and if found compare its content
// to the removed alias // to the removed alias
if canonicalAliasEvent, ok := stateRes.StateEvents[stateTuple]; ok { if canonicalAliasEvent, ok := stateRes.StateEvents[stateTuple]; ok {
@ -279,6 +277,7 @@ func getUpdatedCanonicalAliasState(
Alias: "", Alias: "",
AltAliases: []string{""}, AltAliases: []string{""},
} }
// TODO handle differently malformed event?
err := json.Unmarshal(canonicalAliasEvent.Content(), &canonicalAliasContent) err := json.Unmarshal(canonicalAliasEvent.Content(), &canonicalAliasContent)
if err != nil { if err != nil {
util.GetLogger(req.Context()).WithError(err).Error("Get canonical_alias event content failed") util.GetLogger(req.Context()).WithError(err).Error("Get canonical_alias event content failed")
@ -290,7 +289,7 @@ func getUpdatedCanonicalAliasState(
} else { } else {
updatedCanonicalAlias.Alias = canonicalAliasContent.Alias updatedCanonicalAlias.Alias = canonicalAliasContent.Alias
} }
for _, s := range(canonicalAliasContent.AltAliases) { for _, s := range canonicalAliasContent.AltAliases {
if alias == s { if alias == s {
updated = true updated = true
} else { } else {

View file

@ -263,14 +263,14 @@ func generateSendEvent(
//TODO: maybe do something like synapse where state is retrieved in order to only check new aliases //TODO: maybe do something like synapse where state is retrieved in order to only check new aliases
for _, alias := range content.AltAliases { for _, alias := range content.AltAliases {
resErr = checkAlias(req.Context(), device, alias, queryRes.Aliases, federation, cfg) resErr = checkAlias(req.Context(), alias, queryRes.Aliases, federation, cfg)
if resErr != nil { if resErr != nil {
return nil, resErr return nil, resErr
} }
} }
if content.Alias != "" { if content.Alias != "" {
resErr = checkAlias(req.Context(), device, content.Alias, queryRes.Aliases, federation, cfg) resErr = checkAlias(req.Context(), content.Alias, queryRes.Aliases, federation, cfg)
if resErr != nil { if resErr != nil {
return nil, resErr return nil, resErr
} }
@ -281,7 +281,6 @@ func generateSendEvent(
func checkAlias( func checkAlias(
ctx context.Context, ctx context.Context,
device *userapi.Device,
alias string, alias string,
roomAliases []string, roomAliases []string,
federation *gomatrixserverlib.FederationClient, federation *gomatrixserverlib.FederationClient,