mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-29 01:33:10 -06:00
Refactor send events
Signed-off-by: Colin Weill--Duflos <lieunoir@rezel.net>
This commit is contained in:
parent
9f7fb3e90e
commit
4e68ce3a06
|
|
@ -15,6 +15,7 @@
|
||||||
package routing
|
package routing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
@ -232,6 +233,8 @@ func generateSendEvent(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the event is a canonical_alias one, we have to check if each alias is correctly formed
|
||||||
|
// and if each alias exists pointing to the correct room
|
||||||
if eventType == gomatrixserverlib.MRoomCanonicalAlias {
|
if eventType == gomatrixserverlib.MRoomCanonicalAlias {
|
||||||
var content *eventutil.CanonicalAlias
|
var content *eventutil.CanonicalAlias
|
||||||
err = json.Unmarshal(builder.Content, &content)
|
err = json.Unmarshal(builder.Content, &content)
|
||||||
|
|
@ -260,15 +263,39 @@ 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)
|
||||||
|
if resErr != nil {
|
||||||
|
return nil, resErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if content.Alias != "" {
|
||||||
|
resErr = checkAlias(req.Context(), device, content.Alias, queryRes.Aliases, federation, cfg)
|
||||||
|
if resErr != nil {
|
||||||
|
return nil, resErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return e.Event, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkAlias(
|
||||||
|
ctx context.Context,
|
||||||
|
device *userapi.Device,
|
||||||
|
alias string,
|
||||||
|
roomAliases []string,
|
||||||
|
federation *gomatrixserverlib.FederationClient,
|
||||||
|
cfg *config.ClientAPI,
|
||||||
|
) *util.JSONResponse {
|
||||||
_, domain, err := gomatrixserverlib.SplitID('#', alias)
|
_, domain, err := gomatrixserverlib.SplitID('#', alias)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, &util.JSONResponse{
|
return &util.JSONResponse{
|
||||||
Code: http.StatusBadRequest,
|
Code: http.StatusBadRequest,
|
||||||
JSON: jsonerror.InvalidParam("Room alias must be in the form '#localpart:domain'"),
|
JSON: jsonerror.InvalidParam("Room alias must be in the form '#localpart:domain'"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
found := false
|
found := false
|
||||||
for _, s := range queryRes.Aliases {
|
for _, s := range roomAliases {
|
||||||
if alias == s {
|
if alias == s {
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
|
|
@ -276,66 +303,25 @@ func generateSendEvent(
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
if domain == cfg.Matrix.ServerName {
|
if domain == cfg.Matrix.ServerName {
|
||||||
return nil, &util.JSONResponse{
|
return &util.JSONResponse{
|
||||||
Code: http.StatusBadRequest,
|
|
||||||
JSON: jsonerror.BadAlias("Alt alias not present in the room aliases"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fedRes, fedErr := federation.LookupRoomAlias(req.Context(), domain, alias)
|
|
||||||
if fedErr != nil {
|
|
||||||
// TODO: Return 502 if the remote server errored.
|
|
||||||
// TODO: Return 504 if the remote server timed out.
|
|
||||||
util.GetLogger(req.Context()).WithError(fedErr).Error("federation.LookupRoomAlias failed")
|
|
||||||
resErr := jsonerror.InternalServerError()
|
|
||||||
return nil, &resErr
|
|
||||||
}
|
|
||||||
if fedRes.RoomID == "" {
|
|
||||||
return nil, &util.JSONResponse{
|
|
||||||
Code: http.StatusBadRequest,
|
|
||||||
JSON: jsonerror.BadAlias("Alt alias not present in the room aliases"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if content.Alias != "" {
|
|
||||||
_, domain, err := gomatrixserverlib.SplitID('#', content.Alias)
|
|
||||||
if err != nil {
|
|
||||||
return nil, &util.JSONResponse{
|
|
||||||
Code: http.StatusBadRequest,
|
|
||||||
JSON: jsonerror.InvalidParam("Room alias must be in the form '#localpart:domain'"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
found := false
|
|
||||||
for _, s := range queryRes.Aliases {
|
|
||||||
if content.Alias == s {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
if domain == cfg.Matrix.ServerName {
|
|
||||||
return nil, &util.JSONResponse{
|
|
||||||
Code: http.StatusBadRequest,
|
Code: http.StatusBadRequest,
|
||||||
JSON: jsonerror.BadAlias("Canonical alias not present in the room aliases"),
|
JSON: jsonerror.BadAlias("Canonical alias not present in the room aliases"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fedRes, fedErr := federation.LookupRoomAlias(req.Context(), domain, content.Alias)
|
fedRes, fedErr := federation.LookupRoomAlias(ctx, domain, alias)
|
||||||
if fedErr != nil {
|
if fedErr != nil {
|
||||||
// TODO: Return 502 if the remote server errored.
|
// TODO: Return 502 if the remote server errored.
|
||||||
// TODO: Return 504 if the remote server timed out.
|
// TODO: Return 504 if the remote server timed out.
|
||||||
util.GetLogger(req.Context()).WithError(fedErr).Error("federation.LookupRoomAlias failed")
|
util.GetLogger(ctx).WithError(fedErr).Error("federation.LookupRoomAlias failed")
|
||||||
resErr := jsonerror.InternalServerError()
|
resErr := jsonerror.InternalServerError()
|
||||||
return nil, &resErr
|
return &resErr
|
||||||
}
|
}
|
||||||
if fedRes.RoomID == "" {
|
if fedRes.RoomID == "" {
|
||||||
return nil, &util.JSONResponse{
|
return &util.JSONResponse{
|
||||||
Code: http.StatusBadRequest,
|
Code: http.StatusBadRequest,
|
||||||
JSON: jsonerror.BadAlias("Canonical alias not present in the room aliases"),
|
JSON: jsonerror.BadAlias("Canonical alias not present in the room aliases"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return nil
|
||||||
}
|
|
||||||
return e.Event, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue