Skip to content

Commit

Permalink
validate nsfw, close #267
Browse files Browse the repository at this point in the history
  • Loading branch information
roadscape committed Nov 20, 2019
1 parent e418a74 commit 48eee6a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
33 changes: 27 additions & 6 deletions hive/indexer/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from hive.indexer.accounts import Accounts
from hive.indexer.notify import Notify
from hive.db.db_state import DbState
from hive.utils.post import parse_md, parse_tags

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -170,6 +171,11 @@ def _get_name(cls, cid):
cls._names[cid] = name
return name

@classmethod
def _is_nsfw(cls, cid):
sql = "SELECT is_nsfw FROM hive_communities WHERE id = :id"
return DB.query_one(sql, id=cid)

@classmethod
def get_all_muted(cls, community_id):
"""Return a list of all muted accounts."""
Expand Down Expand Up @@ -207,15 +213,30 @@ def is_post_valid(cls, community_id, comment_op: dict):
role = cls.get_user_role(community_id, account_id)
type_id = int(community[5])

# TODO: check `nsfw` tag requirement #267
# TODO: (1.5) check that beneficiaries are valid

# role checking
if role < Role.guest:
# mutes cannot post/comment
return False
if type_id == TYPE_JOURNAL:
# posting restricted to members
if not comment_op['parent_author']:
return role >= Role.member
if role < Role.member:
return False
elif type_id == TYPE_COUNCIL:
return role >= Role.member
return role >= Role.guest # or at least not muted
# posting/commenting restricted to members
if role < Role.member:
return False

# nsfw tag compliance
if cls._is_nsfw(community_id):
md = parse_md(comment_op)
tags = parse_tags(md)
if 'nsfw' not in tags:
return False

# TODO: (1.5) check that beneficiaries are valid

return True

@classmethod
def recalc_pending_payouts(cls):
Expand Down
21 changes: 15 additions & 6 deletions hive/utils/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def post_to_internal(post, post_id, level='insert', promoted=None):
return values


def post_basic(post):
"""Basic post normalization: json-md, tags, and flags."""
def parse_md(post):
"""Safely parse json_metadata from post object."""
md = {}
# At least one case where jsonMetadata was double-encoded: condenser#895
# jsonMetadata = JSON.parse(jsonMetadata);
Expand All @@ -107,6 +107,18 @@ def post_basic(post):
md = {}
except Exception:
pass
return md

def parse_tags(md):
"""Safely read `tags` list from metadata dict."""
# if (typeof tags == 'string') tags = tags.split(' '); # legacy condenser compat
if md and 'tags' in md and isinstance(md['tags'], list):
return md['tags']
return []

def post_basic(post):
"""Basic post normalization: json-md, tags, and flags."""
md = parse_md(post)

thumb_url = ''
if md and 'image' in md:
Expand All @@ -120,10 +132,7 @@ def post_basic(post):
del md['image']

# clean up tags, check if nsfw
tags = [post['category']]
# if (typeof tags == 'string') tags = tags.split(' '); # legacy condenser compat
if md and 'tags' in md and isinstance(md['tags'], list):
tags = tags + md['tags']
tags = [post['category']] + parse_tags(md)
tags = map(lambda tag: (str(tag) or '').strip('# ').lower()[:32], tags)
tags = filter(None, tags)
tags = list(distinct(tags))[:5]
Expand Down

0 comments on commit 48eee6a

Please sign in to comment.