13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
from synapse .api .constants import EventTypes
16
+ from synapse .util .caches .descriptors import cachedInlineCallbacks
16
17
17
18
from twisted .internet import defer
18
19
@@ -124,29 +125,23 @@ def _check_namespaces(self, namespaces):
124
125
raise ValueError (
125
126
"Expected bool for 'exclusive' in ns '%s'" % ns
126
127
)
127
- if not isinstance (regex_obj .get ("regex" ), basestring ):
128
+ regex = regex_obj .get ("regex" )
129
+ if isinstance (regex , basestring ):
130
+ regex_obj ["regex" ] = re .compile (regex ) # Pre-compile regex
131
+ else :
128
132
raise ValueError (
129
133
"Expected string for 'regex' in ns '%s'" % ns
130
134
)
131
135
return namespaces
132
136
133
- def _matches_regex (self , test_string , namespace_key , return_obj = False ):
134
- if not isinstance (test_string , basestring ):
135
- logger .error (
136
- "Expected a string to test regex against, but got %s" ,
137
- test_string
138
- )
139
- return False
140
-
137
+ def _matches_regex (self , test_string , namespace_key ):
141
138
for regex_obj in self .namespaces [namespace_key ]:
142
- if re .match (regex_obj ["regex" ], test_string ):
143
- if return_obj :
144
- return regex_obj
145
- return True
146
- return False
139
+ if regex_obj ["regex" ].match (test_string ):
140
+ return regex_obj
141
+ return None
147
142
148
143
def _is_exclusive (self , ns_key , test_string ):
149
- regex_obj = self ._matches_regex (test_string , ns_key , return_obj = True )
144
+ regex_obj = self ._matches_regex (test_string , ns_key )
150
145
if regex_obj :
151
146
return regex_obj ["exclusive" ]
152
147
return False
@@ -166,7 +161,14 @@ def _matches_user(self, event, store):
166
161
if not store :
167
162
defer .returnValue (False )
168
163
169
- member_list = yield store .get_users_in_room (event .room_id )
164
+ does_match = yield self ._matches_user_in_member_list (event .room_id , store )
165
+ defer .returnValue (does_match )
166
+
167
+ @cachedInlineCallbacks (num_args = 1 , cache_context = True )
168
+ def _matches_user_in_member_list (self , room_id , store , cache_context ):
169
+ member_list = yield store .get_users_in_room (
170
+ room_id , on_invalidate = cache_context .invalidate
171
+ )
170
172
171
173
# check joined member events
172
174
for user_id in member_list :
@@ -219,10 +221,10 @@ def is_interested_in_user(self, user_id):
219
221
)
220
222
221
223
def is_interested_in_alias (self , alias ):
222
- return self ._matches_regex (alias , ApplicationService .NS_ALIASES )
224
+ return bool ( self ._matches_regex (alias , ApplicationService .NS_ALIASES ) )
223
225
224
226
def is_interested_in_room (self , room_id ):
225
- return self ._matches_regex (room_id , ApplicationService .NS_ROOMS )
227
+ return bool ( self ._matches_regex (room_id , ApplicationService .NS_ROOMS ) )
226
228
227
229
def is_exclusive_user (self , user_id ):
228
230
return (
0 commit comments