Skip to content

Commit

Permalink
Merge pull request #199 from steemit/mutes
Browse files Browse the repository at this point in the history
account mutes
  • Loading branch information
jredbeard authored Mar 13, 2019
2 parents 147cf83 + 0cf8313 commit cd78b95
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions hive/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def init_argparse(cls, strict=True, **kwargs):
# common
add('--database-url', env_var='DATABASE_URL', required=False, help='database connection url', default='')
add('--steemd-url', env_var='STEEMD_URL', required=False, help='steemd/jussi endpoint', default='https://api.steemit.com')
add('--muted-accounts-url', env_var='MUTED_ACCOUNTS_URL', required=False, help='url to flat list of muted accounts', default='')

# server
add('--http-server-port', type=int, env_var='HTTP_SERVER_PORT', default=8080)
Expand Down
33 changes: 33 additions & 0 deletions hive/server/common/mutes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""List of muted accounts for server process."""

import logging
from urllib.request import urlopen

log = logging.getLogger(__name__)

class Mutes:
"""Singleton tracking muted accounts."""

_instance = None
accounts = set()

@classmethod
def instance(cls):
"""Get the shared instance."""
assert cls._instance, 'set_shared_instance was never called'
return cls._instance

@classmethod
def set_shared_instance(cls, instance):
"""Set the global/shared instance."""
cls._instance = instance

def __init__(self, url):
"""Initialize a muted account list by loading from URL"""
if url:
self.accounts = set(urlopen(url).read().decode('utf8').split())

@classmethod
def all(cls):
"""Return the set of all muted accounts from singleton instance."""
return cls.instance().accounts
14 changes: 14 additions & 0 deletions hive/server/condenser_api/get_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ujson as json

from hive.utils.normalize import legacy_amount
from hive.server.common.mutes import Mutes

from hive.server.condenser_api.objects import (
load_accounts,
Expand Down Expand Up @@ -253,6 +254,19 @@ async def _load_discussion(db, author, permlink):

# load all post objects, build ref-map
posts = await load_posts_keyed(db, ids)

# remove posts/comments from muted accounts
muted_accounts = Mutes.all()
rem_pids = []
for pid, post in posts.items():
if post['author'] in muted_accounts:
rem_pids.append(pid)
for pid in rem_pids:
if pid in posts:
del posts[pid]
if pid in tree:
rem_pids.extend(tree[pid])

refs = {pid: _ref(post) for pid, post in posts.items()}

# add child refs to parent posts
Expand Down
8 changes: 8 additions & 0 deletions hive/server/condenser_api/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ujson as json

from hive.utils.normalize import sbd_amount, rep_to_raw
from hive.server.common.mutes import Mutes

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -45,15 +46,22 @@ async def load_posts_keyed(db, ids, truncate_body=0):
result = await db.query_all(sql, ids=tuple(ids))
author_reps = await _query_author_rep_map(db, result)

muted_accounts = Mutes.all()
posts_by_id = {}
for row in result:
row = dict(row)
row['author_rep'] = author_reps[row['author']]
post = _condenser_post_object(row, truncate_body=truncate_body)
post['active_votes'] = _mute_votes(post['active_votes'], muted_accounts)
posts_by_id[row['post_id']] = post

return posts_by_id

def _mute_votes(votes, muted_accounts):
if not muted_accounts:
return votes
return [v for v in votes if v['voter'] not in muted_accounts]

async def load_posts(db, ids, truncate_body=0):
"""Given an array of post ids, returns full objects in the same order."""
if not ids:
Expand Down
4 changes: 4 additions & 0 deletions hive/server/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from hive.server.condenser_api.get_state import get_state as condenser_api_get_state
from hive.server.condenser_api.call import call as condenser_api_call
from hive.server import hive_api
from hive.server.common.mutes import Mutes

from hive.server.db import Db

Expand Down Expand Up @@ -124,6 +125,9 @@ def run_server(conf):
log = logging.getLogger(__name__)
methods = build_methods()

mutes = Mutes(conf.get('muted_accounts_url'))
Mutes.set_shared_instance(mutes)

app = web.Application()
app['config'] = dict()
app['config']['args'] = conf.args()
Expand Down

0 comments on commit cd78b95

Please sign in to comment.