Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply profanity filter to username (in addition to post body) #120

Merged
merged 1 commit into from Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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