Consolidate AS interest checking (#539)

* Methods for checking if an AS is interested in events

* Look through rooms namespace for matching room IDs
main
Andrew Morgan 2018-07-16 05:30:04 -07:00 committed by GitHub
parent d4b24462d1
commit 49b63089f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 23 deletions

View File

@ -185,18 +185,9 @@ func (s *OutputRoomEventConsumer) appserviceIsInterestedInEvent(ctx context.Cont
return false return false
} }
// Check sender of the event if appservice.IsInterestedInUserID(event.Sender()) ||
for _, userNamespace := range appservice.NamespaceMap["users"] { appservice.IsInterestedInRoomID(event.RoomID()) {
if userNamespace.RegexpObject.MatchString(event.Sender()) { return true
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 // Check all known room aliases of the room the event came from
@ -204,10 +195,8 @@ func (s *OutputRoomEventConsumer) appserviceIsInterestedInEvent(ctx context.Cont
var queryRes api.GetAliasesForRoomIDResponse var queryRes api.GetAliasesForRoomIDResponse
if err := s.alias.GetAliasesForRoomID(ctx, &queryReq, &queryRes); err == nil { if err := s.alias.GetAliasesForRoomID(ctx, &queryReq, &queryRes); err == nil {
for _, alias := range queryRes.Aliases { for _, alias := range queryRes.Aliases {
for _, aliasNamespace := range appservice.NamespaceMap["aliases"] { if appservice.IsInterestedInRoomAlias(alias) {
if aliasNamespace.RegexpObject.MatchString(alias) { return true
return true
}
} }
} }
} else { } else {

View File

@ -337,17 +337,15 @@ func UsernameMatchesMultipleExclusiveNamespaces(
) bool { ) bool {
// Check namespaces and see if more than one match // Check namespaces and see if more than one match
matchCount := 0 matchCount := 0
userID := userutil.MakeUserID(username, cfg.Matrix.ServerName)
for _, appservice := range cfg.Derived.ApplicationServices { for _, appservice := range cfg.Derived.ApplicationServices {
for _, namespaceSlice := range appservice.NamespaceMap { if appservice.IsInterestedInUserID(userID) {
for _, namespace := range namespaceSlice { if matchCount++; matchCount > 1 {
// Check if we have a match on this username return true
if namespace.RegexpObject.MatchString(username) {
matchCount++
}
} }
} }
} }
return matchCount > 1 return false
} }
// validateApplicationService checks if a provided application service token // validateApplicationService checks if a provided application service token

View File

@ -66,6 +66,54 @@ type ApplicationService struct {
Protocols []string `yaml:"protocols"` Protocols []string `yaml:"protocols"`
} }
// IsInterestedInRoomID returns a bool on whether an application service's
// namespace includes the given room ID
func (a *ApplicationService) IsInterestedInRoomID(
roomID string,
) bool {
if namespaceSlice, ok := a.NamespaceMap["rooms"]; ok {
for _, namespace := range namespaceSlice {
if namespace.RegexpObject.MatchString(roomID) {
return true
}
}
}
return false
}
// IsInterestedInUserID returns a bool on whether an application service's
// namespace includes the given user ID
func (a *ApplicationService) IsInterestedInUserID(
userID string,
) bool {
if namespaceSlice, ok := a.NamespaceMap["users"]; ok {
for _, namespace := range namespaceSlice {
if namespace.RegexpObject.MatchString(userID) {
return true
}
}
}
return false
}
// IsInterestedInRoomAlias returns a bool on whether an application service's
// namespace includes the given room alias
func (a *ApplicationService) IsInterestedInRoomAlias(
roomAlias string,
) bool {
if namespaceSlice, ok := a.NamespaceMap["aliases"]; ok {
for _, namespace := range namespaceSlice {
if namespace.RegexpObject.MatchString(roomAlias) {
return true
}
}
}
return false
}
// loadAppservices iterates through all application service config files // loadAppservices iterates through all application service config files
// and loads their data into the config object for later access. // and loads their data into the config object for later access.
func loadAppservices(config *Dendrite) error { func loadAppservices(config *Dendrite) error {