mirror of
https://github.com/1f349/dendrite.git
synced 2024-11-22 19:51:39 +00:00
Consolidate AS interest checking (#539)
* Methods for checking if an AS is interested in events * Look through rooms namespace for matching room IDs
This commit is contained in:
parent
d4b24462d1
commit
49b63089f5
@ -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 {
|
||||||
|
@ -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
|
||||||
|
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user