Skip to content
Snippets Groups Projects
Commit f6eaaa3f authored by Jason Salyers's avatar Jason Salyers
Browse files

[JES] Final round of updates for blacklisting. Return ID's insteaad of text...

[JES] Final round of updates for blacklisting. Return ID's insteaad of text from the DB query and have the python side do a dictionary lookup to get the name. Response times are good until following a large blacklist, and even then they're still reasonable (under 500ms)
parent 1513c7ed
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ from time import perf_counter as perf ...@@ -5,6 +5,7 @@ from time import perf_counter as perf
from urllib.request import urlopen, Request from urllib.request import urlopen, Request
import ujson as json import ujson as json
from hive.server.common.helpers import valid_account from hive.server.common.helpers import valid_account
from hive.db.adapter import Db
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -13,14 +14,13 @@ WITH blacklisted_users AS ( ...@@ -13,14 +14,13 @@ WITH blacklisted_users AS (
SELECT following, 'my_blacklist' AS source FROM hive_follows WHERE follower = SELECT following, 'my_blacklist' AS source FROM hive_follows WHERE follower =
(SELECT id FROM hive_accounts WHERE name = :observer ) (SELECT id FROM hive_accounts WHERE name = :observer )
AND blacklisted AND blacklisted
UNION ALL UNION
SELECT following, 'my_followed_blacklists' AS source FROM hive_follows WHERE follower IN SELECT following, 'my_followed_blacklists' AS source FROM hive_follows WHERE follower IN
(SELECT following FROM hive_follows WHERE follower = (SELECT following FROM hive_follows WHERE follower =
(SELECT id FROM hive_accounts WHERE name = :observer ) (SELECT id FROM hive_accounts WHERE name = :observer )
AND follow_blacklists) AND blacklisted AND follow_blacklists) AND blacklisted
) )
SELECT hive_accounts.name, blacklisted_users.source FROM SELECT following, source FROM blacklisted_users
blacklisted_users JOIN hive_accounts ON (hive_accounts.id = blacklisted_users.following)
""" """
def _read_url(url): def _read_url(url):
...@@ -36,6 +36,7 @@ class Mutes: ...@@ -36,6 +36,7 @@ class Mutes:
blist = set() # list/any-blacklist blist = set() # list/any-blacklist
blist_map = dict() # cached account-list map blist_map = dict() # cached account-list map
fetched = None fetched = None
all_accounts = dict()
@classmethod @classmethod
def instance(cls): def instance(cls):
...@@ -62,6 +63,13 @@ class Mutes: ...@@ -62,6 +63,13 @@ class Mutes:
jsn = _read_url(self.blacklist_api_url + "/blacklists") jsn = _read_url(self.blacklist_api_url + "/blacklists")
self.blist = set(json.loads(jsn)) self.blist = set(json.loads(jsn))
log.warning("%d muted, %d blacklisted", len(self.accounts), len(self.blist)) log.warning("%d muted, %d blacklisted", len(self.accounts), len(self.blist))
self.all_accounts.clear()
sql = "select id, name from hive_accounts"
db = Db.instance()
sql_result = db.query_all(sql)
for row in sql_result:
self.all_accounts[row['id']] = row['name']
self.fetched = perf() self.fetched = perf()
@classmethod @classmethod
...@@ -75,15 +83,19 @@ class Mutes: ...@@ -75,15 +83,19 @@ class Mutes:
if not observer or not context: if not observer or not context:
return {} return {}
if perf() - cls.instance().fetched > 3600:
cls.instance().load()
blacklisted_users = {} blacklisted_users = {}
db = context['db'] db = context['db']
sql = GET_BLACKLISTED_ACCOUNTS_SQL sql = GET_BLACKLISTED_ACCOUNTS_SQL
sql_result = await db.query_all(sql, observer=observer) sql_result = await db.query_all(sql, observer=observer)
for row in sql_result: for row in sql_result:
if row['name'] not in blacklisted_users: account_name = cls.all_accounts[row['following']]
blacklisted_users[row['name']] = [] if account_name not in blacklisted_users:
blacklisted_users[row['name']].append(row['source']) blacklisted_users[account_name] = []
blacklisted_users[account_name].append(row['source'])
return blacklisted_users return blacklisted_users
@classmethod @classmethod
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment