Skip to content

Commit 59222ce

Browse files
author
majestrate
authored
Merge pull request #120 from majestrate/pysogs-better-filter-2022-08-23
apply profanity filter to username (in addition to post body)
2 parents 6f1cb2c + ce185d1 commit 59222ce

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

sogs/model/post.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from .. import utils
2+
from .. import session_pb2 as protobuf
3+
4+
5+
class Post:
6+
"""Class representing a post made in an open group"""
7+
8+
_proto = None
9+
10+
def __init__(self, raw=None, *, user=None, text=None):
11+
if isinstance(raw, bytes) or isinstance(raw, memoryview):
12+
msg = protobuf.Content()
13+
msg.ParseFromString(utils.remove_session_message_padding(raw))
14+
self._proto = msg.dataMessage
15+
if self._proto is None:
16+
# TODO: implement other kinds of construction methods for Posts
17+
raise ValueError('must provide raw message bytes')
18+
19+
@property
20+
def text(self):
21+
""" accessor for the post body """
22+
return self._proto.body
23+
24+
@property
25+
def username(self):
26+
""" accessor for the username of the post's author """
27+
if self.profile is None:
28+
return
29+
return self.profile.displayName
30+
31+
@property
32+
def profile(self):
33+
""" accessor for the user profile data containing things like username etc """
34+
return self._proto.profile

sogs/model/room.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ..web import app
55
from .user import User
66
from .file import File
7+
from .post import Post
78
from .exc import (
89
NoSuchRoom,
910
NoSuchFile,
@@ -712,12 +713,14 @@ def should_filter(self, user: User, data: bytes):
712713
if config.PROFANITY_FILTER and not self.check_admin(user):
713714
import better_profanity
714715

715-
if better_profanity.profanity.contains_profanity(utils.message_body(data)):
716-
if config.PROFANITY_SILENT:
717-
return True
718-
else:
719-
# FIXME: can we send back some error code that makes Session not retry?
720-
raise PostRejected("filtration rejected message")
716+
msg = Post(raw=data)
717+
for part in (msg.text, msg.username):
718+
if better_profanity.profanity.contains_profanity(part):
719+
if config.PROFANITY_SILENT:
720+
return True
721+
else:
722+
# FIXME: can we send back some error code that makes Session not retry?
723+
raise PostRejected("filtration rejected message")
721724
return False
722725

723726
def _own_files(self, msg_id: int, files: List[int], user):

sogs/utils.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
from . import crypto
22
from . import config
33
from . import http
4-
from . import session_pb2 as protobuf
54

65
import base64
76
from flask import request, abort, Response
87
import json
98
from typing import Union, Tuple
109

1110

12-
def message_body(data: bytes):
13-
"""given a bunch of bytes for a protobuf message return the message's body"""
14-
msg = protobuf.Content()
15-
msg.ParseFromString(remove_session_message_padding(data))
16-
return msg.dataMessage.body
17-
18-
1911
def encode_base64(data: bytes):
2012
return base64.b64encode(data).decode()
2113

0 commit comments

Comments
 (0)