From 66812f66d3c4554af7ae60f1376319c5177f4dc7 Mon Sep 17 00:00:00 2001 From: mtrela <mtrela@syncad.com> Date: Mon, 23 Nov 2020 15:55:09 +0100 Subject: [PATCH] Mock blocks with reblog operations - part 2 --- hive/db/schema.py | 3 +- hive/db/sql_scripts/reblog_delete.sql | 34 +++++++ hive/indexer/reblog.py | 92 ++++++++++--------- .../reblog_op/mock_block_data_reblog.json | 80 ++++++++-------- 4 files changed, 124 insertions(+), 85 deletions(-) create mode 100644 hive/db/sql_scripts/reblog_delete.sql diff --git a/hive/db/schema.py b/hive/db/schema.py index 3d3140ee1..9709fd39d 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -611,7 +611,8 @@ def setup(db): "condenser_get_names_by_reblogged.sql", "condenser_get_discussions_by_comments.sql", "condenser_get_account_reputations.sql", - "update_follow_count.sql" + "update_follow_count.sql", + "reblog_delete.sql" ] from os.path import dirname, realpath dir_path = dirname(realpath(__file__)) diff --git a/hive/db/sql_scripts/reblog_delete.sql b/hive/db/sql_scripts/reblog_delete.sql new file mode 100644 index 000000000..332027849 --- /dev/null +++ b/hive/db/sql_scripts/reblog_delete.sql @@ -0,0 +1,34 @@ +DROP FUNCTION IF EXISTS delete_reblog(character varying,character varying,character varying, BOOLEAN) +; + +CREATE OR REPLACE FUNCTION delete_reblog( + in _author hive_accounts.name%TYPE, + in _permlink hive_permlink_data.permlink%TYPE, + in _account hive_accounts.name%TYPE, + in _delete_feed_cache BOOLEAN) +RETURNS INTEGER +LANGUAGE plpgsql +AS +$function$ +DECLARE + __post_id INT; +BEGIN + + __post_id = find_comment_id( _author, _permlink, False ); + + IF __post_id = 0 THEN + RETURN 0; + END IF; + + DELETE FROM hive_reblogs + WHERE blogger_id = __account_id AND post_id = __post_id; + + IF _delete_feed_cache THEN + DELETE FROM hive_feed_cache + WHERE account_id = __account_id AND post_id = __post_id; + END IF; + + RETURN 1; +END +$function$ +; diff --git a/hive/indexer/reblog.py b/hive/indexer/reblog.py index 983655926..a0023fe63 100644 --- a/hive/indexer/reblog.py +++ b/hive/indexer/reblog.py @@ -11,50 +11,54 @@ from hive.utils.normalize import escape_characters log = logging.getLogger(__name__) -DELETE_SQL = """ - WITH processing_set AS ( - SELECT hp.id as post_id, ha.id as account_id - FROM hive_posts hp - INNER JOIN hive_accounts ha ON hp.author_id = ha.id - INNER JOIN hive_permlink_data hpd ON hp.permlink_id = hpd.id - WHERE ha.name = :a AND hpd.permlink = :permlink AND hp.depth = 0 AND hp.counter_deleted = 0 - ) - DELETE FROM hive_reblogs AS hr - WHERE hr.account = :a AND hr.post_id IN (SELECT ps.post_id FROM processing_set ps) - RETURNING hr.post_id, (SELECT ps.account_id FROM processing_set ps) AS account_id -""" - class Reblog(DbAdapterHolder): """ Class for reblog operations """ reblog_items_to_flush = [] @classmethod - def reblog_op(cls, account, op_json, block_date, block_num): - """ Process reblog operation """ - if 'account' not in op_json or \ - 'author' not in op_json or \ - 'permlink' not in op_json: - return + def _validated_op(cls, actor, op, block_date, block_num): + if 'account' not in op or \ + 'author' not in op or \ + 'permlink' not in op: + return None - blogger = op_json['account'] - author = op_json['author'] - permlink = escape_characters(op_json['permlink']) + if op['account'] != actor: + return None # impersonation - if blogger != account: - return # impersonation - if not all(map(Accounts.exists, [author, blogger])): + if not Accounts.exists(op['account']): + return None + if not Accounts.exists(op['author']): + return None + + return dict(author = op['author'], + permlink = op['permlink'], + account = op['account'], + block_date = block_date, + block_num = block_num) + + @classmethod + def reblog_op(cls, actor, op, block_date, block_num): + """ Process reblog operation """ + op = cls._validated_op(actor, op, block_date, block_num) + if not op: return - if 'delete' in op_json and op_json['delete'] == 'delete': - row = cls.db.query_row(DELETE_SQL, a=blogger, permlink=permlink) - if row is None: - log.debug("reblog: post not found: %s/%s", author, op_json['permlink']) - return - if not DbState.is_initial_sync(): - result = dict(row) - FeedCache.delete(result['post_id'], result['account_id']) + if 'delete' in op and op['delete'] == 'delete': + cls.delete( op['author'], op['permlink'], op['account'] ) else: - cls.reblog_items_to_flush.append((blogger, author, permlink, block_date, block_num)) + cls.reblog_items_to_flush.append(op) + + @classmethod + async def delete(cls, author, permlink, account ): + """Remove a reblog from hive_reblogs + feed from hive_feed_cache. + """ + delete_feed_cache = not DbState.is_initial_sync() + + sql = "SELECT delete_reblog( (:author)::VARCHAR, (:permlink)::VARCHAR, (:permlink)::VARCHAR, (:delete_feed_cache)::BOOLEAN );" + status = await db.query_col(sql, author=author, permlink=permlink, account=account, delete_feed_cache=delete_feed_cache); + assert status is not None + if status == 0: + log.debug("reblog: post not found: %s/%s", author, permlink) @classmethod def flush(cls): @@ -87,22 +91,22 @@ class Reblog(DbAdapterHolder): cls.beginTx() for reblog_item in cls.reblog_items_to_flush: if count < limit: - values.append("('{}', '{}', {}, '{}'::timestamp, {})".format(reblog_item[0], - reblog_item[1], - reblog_item[2], - reblog_item[3], - reblog_item[4])) + values.append("('{}', '{}', '{}', '{}'::timestamp, {})".format(reblog_item['account'], + reblog_item['author'], + reblog_item['permlink'], + reblog_item['block_date'], + reblog_item['block_num'])) count = count + 1 else: values_str = ",".join(values) query = sql_prefix.format(values_str, values_str) cls.db.query(query) values.clear() - values.append("('{}', '{}', {}, '{}'::timestamp, {})".format(reblog_item[0], - reblog_item[1], - reblog_item[2], - reblog_item[3], - reblog_item[4])) + values.append("('{}', '{}', '{}', '{}'::timestamp, {})".format(reblog_item['account'], + reblog_item['author'], + reblog_item['permlink'], + reblog_item['block_date'], + reblog_item['block_num'])) count = 1 if len(values) > 0: diff --git a/mock_data/block_data/reblog_op/mock_block_data_reblog.json b/mock_data/block_data/reblog_op/mock_block_data_reblog.json index ef3d12063..86bff1322 100644 --- a/mock_data/block_data/reblog_op/mock_block_data_reblog.json +++ b/mock_data/block_data/reblog_op/mock_block_data_reblog.json @@ -353,7 +353,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-01" ], "id": "reblog", "json": "{\"account\":\"test-reblog-01\",\"author\":\"test-reblog-02\",\"permlink\":\"parrot-02\"}" @@ -364,7 +364,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-01" ], "id": "reblog", "json": "{\"account\":\"test-reblog-01\",\"author\":\"test-reblog-03\",\"permlink\":\"parrot-03\"}" @@ -375,7 +375,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-01" ], "id": "reblog", "json": "{\"account\":\"test-reblog-01\",\"author\":\"test-reblog-04\",\"permlink\":\"parrot-04\"}" @@ -386,7 +386,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-01" ], "id": "reblog", "json": "{\"delete\":\"delete\",\"account\":\"test-reblog-01\",\"author\":\"test-reblog-04\",\"permlink\":\"parrot-04\"}" @@ -397,7 +397,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-01" ], "id": "reblog", "json": "{\"delete\":\"delete\",\"account\":\"test-reblog-01\",\"author\":\"test-reblog-03\",\"permlink\":\"parrot-03\"}" @@ -408,7 +408,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-01" ], "id": "reblog", "json": "{\"delete\":\"delete\",\"account\":\"test-reblog-01\",\"author\":\"test-reblog-02\",\"permlink\":\"parrot-02\"}" @@ -439,7 +439,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-02" ], "id": "reblog", "json": "{\"account\":\"test-reblog-02\",\"author\":\"test-reblog-01\",\"permlink\":\"parrot-01\"}" @@ -450,7 +450,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-02" ], "id": "reblog", "json": "{\"account\":\"test-reblog-02\",\"author\":\"test-reblog-02\",\"permlink\":\"parrot-02\"}" @@ -461,7 +461,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-02" ], "id": "reblog", "json": "{\"account\":\"test-reblog-02\",\"author\":\"test-reblog-03\",\"permlink\":\"parrot-03\"}" @@ -472,7 +472,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-03" ], "id": "reblog", "json": "{\"account\":\"test-reblog-03\",\"author\":\"test-reblog-02\",\"permlink\":\"parrot-02\"}" @@ -483,7 +483,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-03" ], "id": "reblog", "json": "{\"account\":\"test-reblog-03\",\"author\":\"test-reblog-03\",\"permlink\":\"parrot-03\"}" @@ -494,7 +494,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-03" ], "id": "reblog", "json": "{\"account\":\"test-reblog-03\",\"author\":\"test-reblog-04\",\"permlink\":\"parrot-04\"}" @@ -525,7 +525,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-04" ], "id": "reblog", "json": "{\"account\":\"test-reblog-04\",\"author\":\"test-reblog-04\",\"permlink\":\"parrot-04\"}" @@ -536,7 +536,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-05" ], "id": "reblog", "json": "{\"account\":\"test-reblog-05\",\"author\":\"test-reblog-05\",\"permlink\":\"parrot-05\"}" @@ -547,7 +547,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-06" ], "id": "reblog", "json": "{\"account\":\"test-reblog-06\",\"author\":\"test-reblog-06\",\"permlink\":\"parrot-06\"}" @@ -558,7 +558,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-06" ], "id": "reblog", "json": "{\"account\":\"test-reblog-06\",\"author\":\"test-reblog-06\",\"permlink\":\"parrot-06\"}" @@ -569,7 +569,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-04" ], "id": "reblog", "json": "{\"delete\":\"delete\",\"account\":\"test-reblog-04\",\"author\":\"test-reblog-04\",\"permlink\":\"parrot-04\"}" @@ -580,7 +580,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-06" ], "id": "reblog", "json": "{\"delete\":\"delete\",\"account\":\"test-reblog-06\",\"author\":\"test-reblog-06\",\"permlink\":\"parrot-06\"}" @@ -611,7 +611,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-05" ], "id": "reblog", "json": "{\"delete\":\"delete\",\"account\":\"test-reblog-05\",\"author\":\"test-reblog-05\",\"permlink\":\"parrot-05\"}" @@ -622,7 +622,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-06" ], "id": "reblog", "json": "{\"delete\":\"delete\",\"account\":\"test-reblog-06\",\"author\":\"test-reblog-05\",\"permlink\":\"parrot-05\"}" @@ -653,7 +653,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-04" ], "id": "reblog", "json": "{\"account\":\"test-reblog-04\",\"author\":\"test-reblog-04\",\"permlink\":\"parrot-04\"}" @@ -664,7 +664,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-05" ], "id": "reblog", "json": "{\"account\":\"test-reblog-05\",\"author\":\"test-reblog-05\",\"permlink\":\"parrot-05\"}" @@ -675,7 +675,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-06" ], "id": "reblog", "json": "{\"account\":\"test-reblog-06\",\"author\":\"test-reblog-06\",\"permlink\":\"parrot-06\"}" @@ -686,7 +686,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-06" ], "id": "reblog", "json": "{\"account\":\"test-reblog-06\",\"author\":\"test-reblog-05\",\"permlink\":\"parrot-05\"}" @@ -798,7 +798,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-01" ], "id": "reblog", "json": "{\"account\":\"test-reblog-01\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-01\"}" @@ -836,7 +836,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-01" ], "id": "reblog", "json": "{\"account\":\"test-reblog-01\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-02\"}" @@ -847,7 +847,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-03" ], "id": "reblog", "json": "{\"account\":\"test-reblog-03\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-02\"}" @@ -858,7 +858,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-04" ], "id": "reblog", "json": "{\"account\":\"test-reblog-04\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-02\"}" @@ -896,7 +896,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-03" ], "id": "reblog", "json": "{\"delete\":\"delete\",\"account\":\"test-reblog-03\",\"author\":\"test-reblog-04\",\"permlink\":\"parrot-04\"}" @@ -907,7 +907,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-01" ], "id": "reblog", "json": "{\"account\":\"test-reblog-01\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-03\"}" @@ -918,7 +918,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-02" ], "id": "reblog", "json": "{\"account\":\"test-reblog-02\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-03\"}" @@ -929,7 +929,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-03" ], "id": "reblog", "json": "{\"account\":\"test-reblog-03\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-03\"}" @@ -940,7 +940,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-04" ], "id": "reblog", "json": "{\"account\":\"test-reblog-04\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-03\"}" @@ -951,7 +951,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-05" ], "id": "reblog", "json": "{\"account\":\"test-reblog-05\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-03\"}" @@ -962,7 +962,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-05" ], "id": "reblog", "json": "{\"account\":\"test-reblog-05\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-04\"}" @@ -973,7 +973,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-05" ], "id": "reblog", "json": "{\"account\":\"test-reblog-05\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-05\"}" @@ -1004,7 +1004,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-05" ], "id": "reblog", "json": "{\"delete\":\"delete\",\"account\":\"test-reblog-05\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-04\"}" @@ -1015,7 +1015,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-05" ], "id": "reblog", "json": "{\"account\":\"test-reblog-05\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-04\"}" @@ -1026,7 +1026,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-05" ], "id": "reblog", "json": "{\"delete\":\"delete\",\"account\":\"test-reblog-05\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-04\"}" @@ -1037,7 +1037,7 @@ "value": { "required_auths": [], "required_posting_auths": [ - "test-safari" + "test-reblog-05" ], "id": "reblog", "json": "{\"delete\":\"delete\",\"account\":\"test-reblog-05\",\"author\":\"test-reblog-01\",\"permlink\":\"monkey-04\"}" -- GitLab