mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-14 18:33:09 -06:00
App service filters roomserver events
Doing so based on namespace regexes that each app service has defined. To get the aliases for a roomID a new aliasAPI endpoint was defined, GetAliasesFromRoomID, which does exactly what it says on the tin. Next step is to queue events to be sent off to each homeserver.
This commit is contained in:
parent
3d8de687e3
commit
ce210d167d
|
|
@ -36,10 +36,10 @@ func SetupAppServiceAPIComponent(
|
||||||
transactionsCache *transactions.Cache,
|
transactionsCache *transactions.Cache,
|
||||||
) {
|
) {
|
||||||
consumer := consumers.NewOutputRoomEventConsumer(
|
consumer := consumers.NewOutputRoomEventConsumer(
|
||||||
base.Cfg, base.KafkaConsumer, accountsDB, queryAPI,
|
base.Cfg, base.KafkaConsumer, accountsDB, queryAPI, aliasAPI,
|
||||||
)
|
)
|
||||||
if err := consumer.Start(); err != nil {
|
if err := consumer.Start(); err != nil {
|
||||||
logrus.WithError(err).Panicf("failed to start app service's room server consumer")
|
logrus.WithError(err).Panicf("failed to start app service roomserver consumer")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up HTTP Endpoints
|
// Set up HTTP Endpoints
|
||||||
|
|
|
||||||
|
|
@ -29,11 +29,16 @@ import (
|
||||||
sarama "gopkg.in/Shopify/sarama.v1"
|
sarama "gopkg.in/Shopify/sarama.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
appServices []config.ApplicationService
|
||||||
|
)
|
||||||
|
|
||||||
// OutputRoomEventConsumer consumes events that originated in the room server.
|
// OutputRoomEventConsumer consumes events that originated in the room server.
|
||||||
type OutputRoomEventConsumer struct {
|
type OutputRoomEventConsumer struct {
|
||||||
roomServerConsumer *common.ContinualConsumer
|
roomServerConsumer *common.ContinualConsumer
|
||||||
db *accounts.Database
|
db *accounts.Database
|
||||||
query api.RoomserverQueryAPI
|
query api.RoomserverQueryAPI
|
||||||
|
alias api.RoomserverAliasAPI
|
||||||
serverName string
|
serverName string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,7 +48,9 @@ func NewOutputRoomEventConsumer(
|
||||||
kafkaConsumer sarama.Consumer,
|
kafkaConsumer sarama.Consumer,
|
||||||
store *accounts.Database,
|
store *accounts.Database,
|
||||||
queryAPI api.RoomserverQueryAPI,
|
queryAPI api.RoomserverQueryAPI,
|
||||||
|
aliasAPI api.RoomserverAliasAPI,
|
||||||
) *OutputRoomEventConsumer {
|
) *OutputRoomEventConsumer {
|
||||||
|
appServices = cfg.Derived.ApplicationServices
|
||||||
|
|
||||||
consumer := common.ContinualConsumer{
|
consumer := common.ContinualConsumer{
|
||||||
Topic: string(cfg.Kafka.Topics.OutputRoomEvent),
|
Topic: string(cfg.Kafka.Topics.OutputRoomEvent),
|
||||||
|
|
@ -54,6 +61,7 @@ func NewOutputRoomEventConsumer(
|
||||||
roomServerConsumer: &consumer,
|
roomServerConsumer: &consumer,
|
||||||
db: store,
|
db: store,
|
||||||
query: queryAPI,
|
query: queryAPI,
|
||||||
|
alias: aliasAPI,
|
||||||
serverName: string(cfg.Matrix.ServerName),
|
serverName: string(cfg.Matrix.ServerName),
|
||||||
}
|
}
|
||||||
consumer.ProcessMessage = s.onMessage
|
consumer.ProcessMessage = s.onMessage
|
||||||
|
|
@ -86,7 +94,6 @@ func (s *OutputRoomEventConsumer) onMessage(msg *sarama.ConsumerMessage) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
ev := output.NewRoomEvent.Event
|
ev := output.NewRoomEvent.Event
|
||||||
fmt.Println("got event", ev)
|
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"event_id": ev.EventID(),
|
"event_id": ev.EventID(),
|
||||||
"room_id": ev.RoomID(),
|
"room_id": ev.RoomID(),
|
||||||
|
|
@ -98,7 +105,12 @@ func (s *OutputRoomEventConsumer) onMessage(msg *sarama.ConsumerMessage) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.db.UpdateMemberships(context.TODO(), events, output.NewRoomEvent.RemovesStateEventIDs)
|
if err = s.db.UpdateMemberships(context.TODO(), events, output.NewRoomEvent.RemovesStateEventIDs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if any events need to passed on to external application services
|
||||||
|
return s.filterRoomserverEvents(events)
|
||||||
}
|
}
|
||||||
|
|
||||||
// lookupStateEvents looks up the state events that are added by a new event.
|
// lookupStateEvents looks up the state events that are added by a new event.
|
||||||
|
|
@ -144,3 +156,55 @@ func (s *OutputRoomEventConsumer) lookupStateEvents(
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// filterRoomserverEvents takes in events and decides whether any of them need
|
||||||
|
// to be passed on to an external application service. It does this by checking
|
||||||
|
// each namespace of each registered application service, and if there is a
|
||||||
|
// match, adds the event to the queue for events to be sent to a particular
|
||||||
|
// application service.
|
||||||
|
func (s *OutputRoomEventConsumer) filterRoomserverEvents(events []gomatrixserverlib.Event) error {
|
||||||
|
for _, event := range events {
|
||||||
|
for _, appservice := range appServices {
|
||||||
|
// Check if this event is interesting to this application service
|
||||||
|
if s.appserviceIsInterestedInEvent(event, appservice) {
|
||||||
|
// TODO: Queue this event to be sent off to the application service
|
||||||
|
fmt.Println(appservice.ID, "was interested in", event.Sender(), event.Type(), event.RoomID())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// appserviceIsInterestedInEvent returns a boolean depending on whether a given
|
||||||
|
// event falls within one of a given application service's namespaces.
|
||||||
|
func (s *OutputRoomEventConsumer) appserviceIsInterestedInEvent(event gomatrixserverlib.Event, appservice config.ApplicationService) bool {
|
||||||
|
// Check sender of the event
|
||||||
|
for _, userNamespace := range appservice.NamespaceMap["users"] {
|
||||||
|
if userNamespace.RegexpObject.MatchString(event.Sender()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check room id of the event
|
||||||
|
for _, roomNamespace := range appservice.NamespaceMap["rooms"] {
|
||||||
|
if roomNamespace.RegexpObject.MatchString(event.RoomID()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check all known room aliases of the room the event came from
|
||||||
|
queryReq := api.GetAliasesForRoomIDRequest{RoomID: event.RoomID()}
|
||||||
|
var queryRes api.GetAliasesForRoomIDResponse
|
||||||
|
if err := s.alias.GetAliasesForRoomID(context.TODO(), &queryReq, &queryRes); err == nil {
|
||||||
|
for _, alias := range queryRes.Aliases {
|
||||||
|
for _, aliasNamespace := range appservice.NamespaceMap["aliases"] {
|
||||||
|
if aliasNamespace.RegexpObject.MatchString(alias) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@ package consumers
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
|
@ -86,7 +85,6 @@ func (s *OutputRoomEventConsumer) onMessage(msg *sarama.ConsumerMessage) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
ev := output.NewRoomEvent.Event
|
ev := output.NewRoomEvent.Event
|
||||||
fmt.Println("ClientAPI got an event:", ev)
|
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"event_id": ev.EventID(),
|
"event_id": ev.EventID(),
|
||||||
"room_id": ev.RoomID(),
|
"room_id": ev.RoomID(),
|
||||||
|
|
|
||||||
|
|
@ -46,9 +46,9 @@ func DirectoryRoom(
|
||||||
var resp gomatrixserverlib.RespDirectory
|
var resp gomatrixserverlib.RespDirectory
|
||||||
|
|
||||||
if domain == cfg.Matrix.ServerName {
|
if domain == cfg.Matrix.ServerName {
|
||||||
queryReq := api.GetAliasRoomIDRequest{Alias: roomAlias}
|
queryReq := api.GetRoomIDForAliasRequest{Alias: roomAlias}
|
||||||
var queryRes api.GetAliasRoomIDResponse
|
var queryRes api.GetRoomIDForAliasResponse
|
||||||
if err = aliasAPI.GetAliasRoomID(req.Context(), &queryReq, &queryRes); err != nil {
|
if err = aliasAPI.GetRoomIDForAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
||||||
return httputil.LogThenError(req, err)
|
return httputil.LogThenError(req, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -145,9 +145,9 @@ func (r joinRoomReq) joinRoomByAlias(roomAlias string) util.JSONResponse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if domain == r.cfg.Matrix.ServerName {
|
if domain == r.cfg.Matrix.ServerName {
|
||||||
queryReq := api.GetAliasRoomIDRequest{Alias: roomAlias}
|
queryReq := api.GetRoomIDForAliasRequest{Alias: roomAlias}
|
||||||
var queryRes api.GetAliasRoomIDResponse
|
var queryRes api.GetRoomIDForAliasResponse
|
||||||
if err = r.aliasAPI.GetAliasRoomID(r.req.Context(), &queryReq, &queryRes); err != nil {
|
if err = r.aliasAPI.GetRoomIDForAlias(r.req.Context(), &queryReq, &queryRes); err != nil {
|
||||||
return httputil.LogThenError(r.req, err)
|
return httputil.LogThenError(r.req, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ApplicationServiceNamespace is the namespace that a specific application
|
// ApplicationServiceNamespace is the namespace that a specific application
|
||||||
|
|
@ -79,7 +79,8 @@ func loadAppservices(config *Dendrite) error {
|
||||||
|
|
||||||
// Append the parsed application service to the global config
|
// Append the parsed application service to the global config
|
||||||
config.Derived.ApplicationServices = append(
|
config.Derived.ApplicationServices = append(
|
||||||
config.Derived.ApplicationServices, appservice)
|
config.Derived.ApplicationServices, appservice,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for any errors in the loaded application services
|
// Check for any errors in the loaded application services
|
||||||
|
|
@ -89,7 +90,7 @@ func loadAppservices(config *Dendrite) error {
|
||||||
// setupRegexps will create regex objects for exclusive and non-exclusive
|
// setupRegexps will create regex objects for exclusive and non-exclusive
|
||||||
// usernames, aliases and rooms of all application services, so that other
|
// usernames, aliases and rooms of all application services, so that other
|
||||||
// methods can quickly check if a particular string matches any of them.
|
// methods can quickly check if a particular string matches any of them.
|
||||||
func setupRegexps(cfg *Dendrite) {
|
func setupRegexps(cfg *Dendrite) (err error) {
|
||||||
// Combine all exclusive namespaces for later string checking
|
// Combine all exclusive namespaces for later string checking
|
||||||
var exclusiveUsernameStrings, exclusiveAliasStrings, exclusiveRoomStrings []string
|
var exclusiveUsernameStrings, exclusiveAliasStrings, exclusiveRoomStrings []string
|
||||||
|
|
||||||
|
|
@ -129,9 +130,17 @@ func setupRegexps(cfg *Dendrite) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store compiled Regex
|
// Store compiled Regex
|
||||||
cfg.Derived.ExclusiveApplicationServicesUsernameRegexp, _ = regexp.Compile(exclusiveUsernames)
|
if cfg.Derived.ExclusiveApplicationServicesUsernameRegexp, err = regexp.Compile(exclusiveUsernames); err != nil {
|
||||||
cfg.Derived.ExclusiveApplicationServicesUsernameRegexp, _ = regexp.Compile(exclusiveAliases)
|
return err
|
||||||
cfg.Derived.ExclusiveApplicationServicesUsernameRegexp, _ = regexp.Compile(exclusiveRooms)
|
}
|
||||||
|
if cfg.Derived.ExclusiveApplicationServicesAliasRegexp, err = regexp.Compile(exclusiveAliases); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if cfg.Derived.ExclusiveApplicationServicesRoomRegexp, err = regexp.Compile(exclusiveRooms); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// appendExclusiveNamespaceRegexs takes a slice of strings and a slice of
|
// appendExclusiveNamespaceRegexs takes a slice of strings and a slice of
|
||||||
|
|
@ -140,7 +149,7 @@ func setupRegexps(cfg *Dendrite) {
|
||||||
func appendExclusiveNamespaceRegexs(
|
func appendExclusiveNamespaceRegexs(
|
||||||
exclusiveStrings *[]string, namespaces []ApplicationServiceNamespace,
|
exclusiveStrings *[]string, namespaces []ApplicationServiceNamespace,
|
||||||
) {
|
) {
|
||||||
for _, namespace := range namespaces {
|
for index, namespace := range namespaces {
|
||||||
if namespace.Exclusive {
|
if namespace.Exclusive {
|
||||||
// We append parenthesis to later separate each regex when we compile
|
// We append parenthesis to later separate each regex when we compile
|
||||||
// i.e. "app1.*", "app2.*" -> "(app1.*)|(app2.*)"
|
// i.e. "app1.*", "app2.*" -> "(app1.*)|(app2.*)"
|
||||||
|
|
@ -148,13 +157,13 @@ func appendExclusiveNamespaceRegexs(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compile this regex into a Regexp object for later use
|
// Compile this regex into a Regexp object for later use
|
||||||
namespace.RegexpObject, _ = regexp.Compile(namespace.Regex)
|
namespaces[index].RegexpObject, _ = regexp.Compile(namespace.Regex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkErrors checks for any configuration errors amongst the loaded
|
// checkErrors checks for any configuration errors amongst the loaded
|
||||||
// application services according to the application service spec.
|
// application services according to the application service spec.
|
||||||
func checkErrors(config *Dendrite) error {
|
func checkErrors(config *Dendrite) (err error) {
|
||||||
var idMap = make(map[string]bool)
|
var idMap = make(map[string]bool)
|
||||||
var tokenMap = make(map[string]bool)
|
var tokenMap = make(map[string]bool)
|
||||||
|
|
||||||
|
|
@ -190,9 +199,9 @@ func checkErrors(config *Dendrite) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setupRegexps(config)
|
|
||||||
|
|
||||||
return nil
|
err = setupRegexps(config)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsValidRegex returns true or false based on whether the
|
// IsValidRegex returns true or false based on whether the
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ import (
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
"gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
|
||||||
jaegerconfig "github.com/uber/jaeger-client-go/config"
|
jaegerconfig "github.com/uber/jaeger-client-go/config"
|
||||||
jaegermetrics "github.com/uber/jaeger-lib/metrics"
|
jaegermetrics "github.com/uber/jaeger-lib/metrics"
|
||||||
|
|
@ -235,12 +235,19 @@ type Dendrite struct {
|
||||||
// The paths of which were given above in the main config file
|
// The paths of which were given above in the main config file
|
||||||
ApplicationServices []ApplicationService
|
ApplicationServices []ApplicationService
|
||||||
|
|
||||||
// A meta-regex compiled from all exclusive Application Service
|
// Meta-regexes compiled from all exclusive Application Service
|
||||||
// Regexes. When a user registers, we check that their username
|
// Regexes.
|
||||||
// does not match any exclusive Application Service namespaces
|
//
|
||||||
|
// When a user registers, we check that their username does not match any
|
||||||
|
// exclusive Application Service namespaces
|
||||||
ExclusiveApplicationServicesUsernameRegexp *regexp.Regexp
|
ExclusiveApplicationServicesUsernameRegexp *regexp.Regexp
|
||||||
|
// When a user creates a room alias, we check that it isn't already
|
||||||
// TODO: Exclusive alias, room regexp's
|
// reserved by an application service
|
||||||
|
ExclusiveApplicationServicesAliasRegexp *regexp.Regexp
|
||||||
|
// TODO: Huh? When a room ID is created, we block the client from
|
||||||
|
// creating it if it falls under an application service's exclusive
|
||||||
|
// roomID regex? But why tho
|
||||||
|
ExclusiveApplicationServicesRoomRegexp *regexp.Regexp
|
||||||
} `yaml:"-"`
|
} `yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,9 +52,9 @@ func RoomAliasToID(
|
||||||
var resp gomatrixserverlib.RespDirectory
|
var resp gomatrixserverlib.RespDirectory
|
||||||
|
|
||||||
if domain == cfg.Matrix.ServerName {
|
if domain == cfg.Matrix.ServerName {
|
||||||
queryReq := api.GetAliasRoomIDRequest{Alias: roomAlias}
|
queryReq := api.GetRoomIDForAliasRequest{Alias: roomAlias}
|
||||||
var queryRes api.GetAliasRoomIDResponse
|
var queryRes api.GetRoomIDForAliasResponse
|
||||||
if err = aliasAPI.GetAliasRoomID(httpReq.Context(), &queryReq, &queryRes); err != nil {
|
if err = aliasAPI.GetRoomIDForAlias(httpReq.Context(), &queryReq, &queryRes); err != nil {
|
||||||
return httputil.LogThenError(httpReq, err)
|
return httputil.LogThenError(httpReq, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,10 +35,10 @@ type RoomserverAliasAPIDatabase interface {
|
||||||
SetRoomAlias(ctx context.Context, alias string, roomID string) error
|
SetRoomAlias(ctx context.Context, alias string, roomID string) error
|
||||||
// Look up the room ID a given alias refers to.
|
// Look up the room ID a given alias refers to.
|
||||||
// Returns an error if there was a problem talking to the database.
|
// Returns an error if there was a problem talking to the database.
|
||||||
GetRoomIDFromAlias(ctx context.Context, alias string) (string, error)
|
GetRoomIDForAlias(ctx context.Context, alias string) (string, error)
|
||||||
// Look up all aliases referring to a given room ID.
|
// Look up all aliases referring to a given room ID.
|
||||||
// Returns an error if there was a problem talking to the database.
|
// Returns an error if there was a problem talking to the database.
|
||||||
GetAliasesFromRoomID(ctx context.Context, roomID string) ([]string, error)
|
GetAliasesForRoomID(ctx context.Context, roomID string) ([]string, error)
|
||||||
// Remove a given room alias.
|
// Remove a given room alias.
|
||||||
// Returns an error if there was a problem talking to the database.
|
// Returns an error if there was a problem talking to the database.
|
||||||
RemoveRoomAlias(ctx context.Context, alias string) error
|
RemoveRoomAlias(ctx context.Context, alias string) error
|
||||||
|
|
@ -59,7 +59,7 @@ func (r *RoomserverAliasAPI) SetRoomAlias(
|
||||||
response *api.SetRoomAliasResponse,
|
response *api.SetRoomAliasResponse,
|
||||||
) error {
|
) error {
|
||||||
// Check if the alias isn't already referring to a room
|
// Check if the alias isn't already referring to a room
|
||||||
roomID, err := r.DB.GetRoomIDFromAlias(ctx, request.Alias)
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -82,14 +82,14 @@ func (r *RoomserverAliasAPI) SetRoomAlias(
|
||||||
return r.sendUpdatedAliasesEvent(context.TODO(), request.UserID, request.RoomID)
|
return r.sendUpdatedAliasesEvent(context.TODO(), request.UserID, request.RoomID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAliasRoomID implements api.RoomserverAliasAPI
|
// GetRoomIDForAlias implements api.RoomserverAliasAPI
|
||||||
func (r *RoomserverAliasAPI) GetAliasRoomID(
|
func (r *RoomserverAliasAPI) GetRoomIDForAlias(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
request *api.GetAliasRoomIDRequest,
|
request *api.GetRoomIDForAliasRequest,
|
||||||
response *api.GetAliasRoomIDResponse,
|
response *api.GetRoomIDForAliasResponse,
|
||||||
) error {
|
) error {
|
||||||
// Look up the room ID in the database
|
// Look up the room ID in the database
|
||||||
roomID, err := r.DB.GetRoomIDFromAlias(ctx, request.Alias)
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -98,6 +98,22 @@ func (r *RoomserverAliasAPI) GetAliasRoomID(
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAliasesForRoomID implements api.RoomserverAliasAPI
|
||||||
|
func (r *RoomserverAliasAPI) GetAliasesForRoomID(
|
||||||
|
ctx context.Context,
|
||||||
|
request *api.GetAliasesForRoomIDRequest,
|
||||||
|
response *api.GetAliasesForRoomIDResponse,
|
||||||
|
) error {
|
||||||
|
// Look up the aliases in the database for the given RoomID
|
||||||
|
aliases, err := r.DB.GetAliasesForRoomID(ctx, request.RoomID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
response.Aliases = aliases
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveRoomAlias implements api.RoomserverAliasAPI
|
// RemoveRoomAlias implements api.RoomserverAliasAPI
|
||||||
func (r *RoomserverAliasAPI) RemoveRoomAlias(
|
func (r *RoomserverAliasAPI) RemoveRoomAlias(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
|
|
@ -105,7 +121,7 @@ func (r *RoomserverAliasAPI) RemoveRoomAlias(
|
||||||
response *api.RemoveRoomAliasResponse,
|
response *api.RemoveRoomAliasResponse,
|
||||||
) error {
|
) error {
|
||||||
// Look up the room ID in the database
|
// Look up the room ID in the database
|
||||||
roomID, err := r.DB.GetRoomIDFromAlias(ctx, request.Alias)
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -142,7 +158,7 @@ func (r *RoomserverAliasAPI) sendUpdatedAliasesEvent(
|
||||||
|
|
||||||
// Retrieve the updated list of aliases, marhal it and set it as the
|
// Retrieve the updated list of aliases, marhal it and set it as the
|
||||||
// event's content
|
// event's content
|
||||||
aliases, err := r.DB.GetAliasesFromRoomID(ctx, roomID)
|
aliases, err := r.DB.GetAliasesForRoomID(ctx, roomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -229,14 +245,14 @@ func (r *RoomserverAliasAPI) SetupHTTP(servMux *http.ServeMux) {
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
servMux.Handle(
|
servMux.Handle(
|
||||||
api.RoomserverGetAliasRoomIDPath,
|
api.RoomserverGetRoomIDForAliasPath,
|
||||||
common.MakeInternalAPI("getAliasRoomID", func(req *http.Request) util.JSONResponse {
|
common.MakeInternalAPI("GetRoomIDForAlias", func(req *http.Request) util.JSONResponse {
|
||||||
var request api.GetAliasRoomIDRequest
|
var request api.GetRoomIDForAliasRequest
|
||||||
var response api.GetAliasRoomIDResponse
|
var response api.GetRoomIDForAliasResponse
|
||||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||||
return util.ErrorResponse(err)
|
return util.ErrorResponse(err)
|
||||||
}
|
}
|
||||||
if err := r.GetAliasRoomID(req.Context(), &request, &response); err != nil {
|
if err := r.GetRoomIDForAlias(req.Context(), &request, &response); err != nil {
|
||||||
return util.ErrorResponse(err)
|
return util.ErrorResponse(err)
|
||||||
}
|
}
|
||||||
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
||||||
|
|
|
||||||
|
|
@ -37,18 +37,30 @@ type SetRoomAliasResponse struct {
|
||||||
AliasExists bool `json:"alias_exists"`
|
AliasExists bool `json:"alias_exists"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAliasRoomIDRequest is a request to GetAliasRoomID
|
// GetRoomIDForAliasRequest is a request to GetRoomIDForAlias
|
||||||
type GetAliasRoomIDRequest struct {
|
type GetRoomIDForAliasRequest struct {
|
||||||
// Alias we want to lookup
|
// Alias we want to lookup
|
||||||
Alias string `json:"alias"`
|
Alias string `json:"alias"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAliasRoomIDResponse is a response to GetAliasRoomID
|
// GetRoomIDForAliasResponse is a response to GetRoomIDForAlias
|
||||||
type GetAliasRoomIDResponse struct {
|
type GetRoomIDForAliasResponse struct {
|
||||||
// The room ID the alias refers to
|
// The room ID the alias refers to
|
||||||
RoomID string `json:"room_id"`
|
RoomID string `json:"room_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAliasesForRoomIDRequest is a request to GetAliasesForRoomID
|
||||||
|
type GetAliasesForRoomIDRequest struct {
|
||||||
|
// The room ID we want to find aliases for
|
||||||
|
RoomID string `json:"room_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAliasesForRoomIDResponse is a response to GetAliasesForRoomID
|
||||||
|
type GetAliasesForRoomIDResponse struct {
|
||||||
|
// The aliases the alias refers to
|
||||||
|
Aliases []string `json:"aliases"`
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveRoomAliasRequest is a request to RemoveRoomAlias
|
// RemoveRoomAliasRequest is a request to RemoveRoomAlias
|
||||||
type RemoveRoomAliasRequest struct {
|
type RemoveRoomAliasRequest struct {
|
||||||
// ID of the user removing the alias
|
// ID of the user removing the alias
|
||||||
|
|
@ -70,10 +82,17 @@ type RoomserverAliasAPI interface {
|
||||||
) error
|
) error
|
||||||
|
|
||||||
// Get the room ID for an alias
|
// Get the room ID for an alias
|
||||||
GetAliasRoomID(
|
GetRoomIDForAlias(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
req *GetAliasRoomIDRequest,
|
req *GetRoomIDForAliasRequest,
|
||||||
response *GetAliasRoomIDResponse,
|
response *GetRoomIDForAliasResponse,
|
||||||
|
) error
|
||||||
|
|
||||||
|
// Get all known aliases for a room ID
|
||||||
|
GetAliasesForRoomID(
|
||||||
|
ctx context.Context,
|
||||||
|
req *GetAliasesForRoomIDRequest,
|
||||||
|
response *GetAliasesForRoomIDResponse,
|
||||||
) error
|
) error
|
||||||
|
|
||||||
// Remove a room alias
|
// Remove a room alias
|
||||||
|
|
@ -87,8 +106,11 @@ type RoomserverAliasAPI interface {
|
||||||
// RoomserverSetRoomAliasPath is the HTTP path for the SetRoomAlias API.
|
// RoomserverSetRoomAliasPath is the HTTP path for the SetRoomAlias API.
|
||||||
const RoomserverSetRoomAliasPath = "/api/roomserver/setRoomAlias"
|
const RoomserverSetRoomAliasPath = "/api/roomserver/setRoomAlias"
|
||||||
|
|
||||||
// RoomserverGetAliasRoomIDPath is the HTTP path for the GetAliasRoomID API.
|
// RoomserverGetRoomIDForAliasPath is the HTTP path for the GetRoomIDForAlias API.
|
||||||
const RoomserverGetAliasRoomIDPath = "/api/roomserver/getAliasRoomID"
|
const RoomserverGetRoomIDForAliasPath = "/api/roomserver/GetRoomIDForAlias"
|
||||||
|
|
||||||
|
// RoomserverGetAliasesForRoomIDPath is the HTTP path for the GetAliasesForRoomID API.
|
||||||
|
const RoomserverGetAliasesForRoomIDPath = "/api/roomserver/GetAliasesForRoomID"
|
||||||
|
|
||||||
// RoomserverRemoveRoomAliasPath is the HTTP path for the RemoveRoomAlias API.
|
// RoomserverRemoveRoomAliasPath is the HTTP path for the RemoveRoomAlias API.
|
||||||
const RoomserverRemoveRoomAliasPath = "/api/roomserver/removeRoomAlias"
|
const RoomserverRemoveRoomAliasPath = "/api/roomserver/removeRoomAlias"
|
||||||
|
|
@ -120,16 +142,29 @@ func (h *httpRoomserverAliasAPI) SetRoomAlias(
|
||||||
return postJSON(ctx, span, h.httpClient, apiURL, request, response)
|
return postJSON(ctx, span, h.httpClient, apiURL, request, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAliasRoomID implements RoomserverAliasAPI
|
// GetRoomIDForAlias implements RoomserverAliasAPI
|
||||||
func (h *httpRoomserverAliasAPI) GetAliasRoomID(
|
func (h *httpRoomserverAliasAPI) GetRoomIDForAlias(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
request *GetAliasRoomIDRequest,
|
request *GetRoomIDForAliasRequest,
|
||||||
response *GetAliasRoomIDResponse,
|
response *GetRoomIDForAliasResponse,
|
||||||
) error {
|
) error {
|
||||||
span, ctx := opentracing.StartSpanFromContext(ctx, "GetAliasRoomID")
|
span, ctx := opentracing.StartSpanFromContext(ctx, "GetRoomIDForAlias")
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
apiURL := h.roomserverURL + RoomserverGetAliasRoomIDPath
|
apiURL := h.roomserverURL + RoomserverGetRoomIDForAliasPath
|
||||||
|
return postJSON(ctx, span, h.httpClient, apiURL, request, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAliasesForRoomID implements RoomserverAliasAPI
|
||||||
|
func (h *httpRoomserverAliasAPI) GetAliasesForRoomID(
|
||||||
|
ctx context.Context,
|
||||||
|
request *GetAliasesForRoomIDRequest,
|
||||||
|
response *GetAliasesForRoomIDResponse,
|
||||||
|
) error {
|
||||||
|
span, ctx := opentracing.StartSpanFromContext(ctx, "GetAliasesForRoomID")
|
||||||
|
defer span.Finish()
|
||||||
|
|
||||||
|
apiURL := h.roomserverURL + RoomserverGetAliasesForRoomIDPath
|
||||||
return postJSON(ctx, span, h.httpClient, apiURL, request, response)
|
return postJSON(ctx, span, h.httpClient, apiURL, request, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -422,13 +422,13 @@ func (d *Database) SetRoomAlias(ctx context.Context, alias string, roomID string
|
||||||
return d.statements.insertRoomAlias(ctx, alias, roomID)
|
return d.statements.insertRoomAlias(ctx, alias, roomID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRoomIDFromAlias implements alias.RoomserverAliasAPIDB
|
// GetRoomIDForAlias implements alias.RoomserverAliasAPIDB
|
||||||
func (d *Database) GetRoomIDFromAlias(ctx context.Context, alias string) (string, error) {
|
func (d *Database) GetRoomIDForAlias(ctx context.Context, alias string) (string, error) {
|
||||||
return d.statements.selectRoomIDFromAlias(ctx, alias)
|
return d.statements.selectRoomIDFromAlias(ctx, alias)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAliasesFromRoomID implements alias.RoomserverAliasAPIDB
|
// GetAliasesForRoomID implements alias.RoomserverAliasAPIDB
|
||||||
func (d *Database) GetAliasesFromRoomID(ctx context.Context, roomID string) ([]string, error) {
|
func (d *Database) GetAliasesForRoomID(ctx context.Context, roomID string) ([]string, error) {
|
||||||
return d.statements.selectAliasesFromRoomID(ctx, roomID)
|
return d.statements.selectAliasesFromRoomID(ctx, roomID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue