Skip to content

Commit

Permalink
check post body and post username when checking profanity filter
Browse files Browse the repository at this point in the history
seperate post parsing into a new model type
  • Loading branch information
majestrate committed Aug 23, 2022
1 parent 6560229 commit ce185d1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
34 changes: 34 additions & 0 deletions sogs/model/post.py
@@ -0,0 +1,34 @@
from .. import utils
from .. import session_pb2 as protobuf


class Post:
"""Class representing a post made in an open group"""

_proto = None

def __init__(self, raw=None, *, user=None, text=None):
if isinstance(raw, bytes) or isinstance(raw, memoryview):
msg = protobuf.Content()
msg.ParseFromString(utils.remove_session_message_padding(raw))
self._proto = msg.dataMessage
if self._proto is None:
# TODO: implement other kinds of construction methods for Posts
raise ValueError('must provide raw message bytes')

@property
def text(self):
""" accessor for the post body """
return self._proto.body

@property
def username(self):
""" accessor for the username of the post's author """
if self.profile is None:
return
return self.profile.displayName

@property
def profile(self):
""" accessor for the user profile data containing things like username etc """
return self._proto.profile
15 changes: 9 additions & 6 deletions sogs/model/room.py
Expand Up @@ -4,6 +4,7 @@
from ..web import app
from .user import User
from .file import File
from .post import Post
from .exc import (
NoSuchRoom,
NoSuchFile,
Expand Down Expand Up @@ -712,12 +713,14 @@ def should_filter(self, user: User, data: bytes):
if config.PROFANITY_FILTER and not self.check_admin(user):
import better_profanity

if better_profanity.profanity.contains_profanity(utils.message_body(data)):
if config.PROFANITY_SILENT:
return True
else:
# FIXME: can we send back some error code that makes Session not retry?
raise PostRejected("filtration rejected message")
msg = Post(raw=data)
for part in (msg.text, msg.username):
if better_profanity.profanity.contains_profanity(part):
if config.PROFANITY_SILENT:
return True
else:
# FIXME: can we send back some error code that makes Session not retry?
raise PostRejected("filtration rejected message")
return False

def _own_files(self, msg_id: int, files: List[int], user):
Expand Down
8 changes: 0 additions & 8 deletions sogs/utils.py
@@ -1,21 +1,13 @@
from . import crypto
from . import config
from . import http
from . import session_pb2 as protobuf

import base64
from flask import request, abort, Response
import json
from typing import Union, Tuple


def message_body(data: bytes):
"""given a bunch of bytes for a protobuf message return the message's body"""
msg = protobuf.Content()
msg.ParseFromString(remove_session_message_padding(data))
return msg.dataMessage.body


def encode_base64(data: bytes):
return base64.b64encode(data).decode()

Expand Down

0 comments on commit ce185d1

Please sign in to comment.