Skip to content

Commit d81414e

Browse files
committed
Defer post parsing until needed
If we aren't filtering there's no need to parse (this also unbreaks the test suite, which uses various unparseable fake message data).
1 parent a03504d commit d81414e

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

sogs/model/room.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,13 @@ def should_filter(self, user: User, data: bytes):
793793
- Throws PostRejected if the message should be rejected (and rejection passed back to the
794794
user).
795795
"""
796-
msg = Post(raw=data)
796+
msg_ = None
797+
798+
def msg():
799+
nonlocal msg_
800+
if msg_ is None:
801+
msg_ = Post(raw=data)
802+
return msg_
797803

798804
if not config.FILTER_MODS and self.check_moderator(user):
799805
return None
@@ -807,7 +813,7 @@ def should_filter(self, user: User, data: bytes):
807813
if lang not in alphabets:
808814
continue
809815

810-
if not pattern.search(msg.text):
816+
if not pattern.search(msg().text):
811817
continue
812818

813819
# Filter it!
@@ -817,7 +823,7 @@ def should_filter(self, user: User, data: bytes):
817823
if not filter_type and filt['profanity_filter']:
818824
import better_profanity
819825

820-
for part in (msg.text, msg.username):
826+
for part in (msg().text, msg().username):
821827
if better_profanity.profanity.contains_profanity(part):
822828
filter_type = 'profanity'
823829
break
@@ -831,7 +837,7 @@ def should_filter(self, user: User, data: bytes):
831837
if msg_fmt:
832838
pbmsg = protobuf.Content()
833839
body = msg_fmt.format(
834-
profile_name=(user.session_id if msg.username is None else msg.username),
840+
profile_name=(user.session_id if msg().username is None else msg().username),
835841
profile_at="@" + user.session_id,
836842
room_name=self.name,
837843
room_token=self.token,

0 commit comments

Comments
 (0)