mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-29 01:33:10 -06:00
Reformat canonical alias stuff
This commit is contained in:
parent
4e68ce3a06
commit
d9ca7565b0
|
|
@ -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.
|
||||||
|
|
|
||||||
|
|
@ -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,18 +248,17 @@ 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,
|
||||||
updatedCanonicalAlias *eventutil.CanonicalAlias,
|
updatedCanonicalAlias *eventutil.CanonicalAlias,
|
||||||
) ( bool, *util.JSONResponse ) {
|
) (bool, *util.JSONResponse) {
|
||||||
updated := false
|
updated := false
|
||||||
stateTuple := gomatrixserverlib.StateKeyTuple{
|
stateTuple := gomatrixserverlib.StateKeyTuple{
|
||||||
EventType: gomatrixserverlib.MRoomCanonicalAlias,
|
EventType: gomatrixserverlib.MRoomCanonicalAlias,
|
||||||
StateKey: "",
|
StateKey: "",
|
||||||
}
|
}
|
||||||
stateReq := roomserverAPI.QueryCurrentStateRequest {
|
stateReq := roomserverAPI.QueryCurrentStateRequest{
|
||||||
RoomID: roomID,
|
RoomID: roomID,
|
||||||
StateTuples: []gomatrixserverlib.StateKeyTuple{stateTuple},
|
StateTuples: []gomatrixserverlib.StateKeyTuple{stateTuple},
|
||||||
}
|
}
|
||||||
|
|
@ -268,17 +270,14 @@ 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 {
|
||||||
canonicalAliasContent := eventutil.CanonicalAlias {
|
canonicalAliasContent := eventutil.CanonicalAlias{
|
||||||
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 {
|
||||||
|
|
@ -312,7 +311,7 @@ func updateCanonicalAlias(
|
||||||
var stateKey = ""
|
var stateKey = ""
|
||||||
// We create a new canonical_alias event with the new alias and alt_aliase
|
// We create a new canonical_alias event with the new alias and alt_aliase
|
||||||
// May cause some auth problems
|
// May cause some auth problems
|
||||||
builder := gomatrixserverlib.EventBuilder {
|
builder := gomatrixserverlib.EventBuilder{
|
||||||
Sender: device.UserID,
|
Sender: device.UserID,
|
||||||
RoomID: roomID,
|
RoomID: roomID,
|
||||||
Type: gomatrixserverlib.MRoomCanonicalAlias,
|
Type: gomatrixserverlib.MRoomCanonicalAlias,
|
||||||
|
|
|
||||||
|
|
@ -245,13 +245,13 @@ func generateSendEvent(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
queryRes := api.GetAliasesForRoomIDResponse {
|
queryRes := api.GetAliasesForRoomIDResponse{
|
||||||
Aliases: make([]string, 1),
|
Aliases: make([]string, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = rsAPI.GetAliasesForRoomID(
|
err = rsAPI.GetAliasesForRoomID(
|
||||||
req.Context(),
|
req.Context(),
|
||||||
&api.GetAliasesForRoomIDRequest {
|
&api.GetAliasesForRoomIDRequest{
|
||||||
RoomID: roomID,
|
RoomID: roomID,
|
||||||
},
|
},
|
||||||
&queryRes,
|
&queryRes,
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue