From 91de121933f559c8714688d61abc6b24304da947 Mon Sep 17 00:00:00 2001
From: mtrela <mtrela@syncad.com>
Date: Tue, 1 Dec 2020 14:38:11 +0100
Subject: [PATCH] Final fixes in SQL functions: `delete_reblog_feed_cache`,
 `delete_hive_post`

---
 .../sql_scripts/delete_reblog_feed_cache.sql  | 11 ++++-------
 hive/db/sql_scripts/hive_post_operations.sql  | 11 ++++-------
 hive/indexer/posts.py                         |  7 ++-----
 hive/indexer/reblog.py                        | 19 ++++++++-----------
 .../reblog_op/mock_block_data_reblog.json     | 11 +++++++++++
 5 files changed, 29 insertions(+), 30 deletions(-)

diff --git a/hive/db/sql_scripts/delete_reblog_feed_cache.sql b/hive/db/sql_scripts/delete_reblog_feed_cache.sql
index 6ad3604d5..39ae1e1ed 100644
--- a/hive/db/sql_scripts/delete_reblog_feed_cache.sql
+++ b/hive/db/sql_scripts/delete_reblog_feed_cache.sql
@@ -1,12 +1,11 @@
 
-DROP FUNCTION IF EXISTS delete_reblog_feed_cache(character varying,character varying,character varying, BOOLEAN)
+DROP FUNCTION IF EXISTS delete_reblog_feed_cache(character varying,character varying,character varying)
 ;
 
 CREATE OR REPLACE FUNCTION delete_reblog_feed_cache(
   in _author hive_accounts.name%TYPE,
   in _permlink hive_permlink_data.permlink%TYPE,
-  in _account hive_accounts.name%TYPE,
-  in _delete_feed_cache BOOLEAN)
+  in _account hive_accounts.name%TYPE)
 RETURNS INTEGER
 LANGUAGE plpgsql
 AS
@@ -26,10 +25,8 @@ BEGIN
   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;
+  DELETE FROM hive_feed_cache
+  WHERE account_id = __account_id AND post_id = __post_id;
 
   RETURN 1;
 END
diff --git a/hive/db/sql_scripts/hive_post_operations.sql b/hive/db/sql_scripts/hive_post_operations.sql
index 0dc5a5e18..099380bbc 100644
--- a/hive/db/sql_scripts/hive_post_operations.sql
+++ b/hive/db/sql_scripts/hive_post_operations.sql
@@ -144,13 +144,12 @@ END
 $function$
 ;
 
-DROP FUNCTION if exists delete_hive_post(character varying,character varying,character varying, integer, BOOLEAN)
+DROP FUNCTION if exists delete_hive_post(character varying,character varying,character varying, integer)
 ;
 CREATE OR REPLACE FUNCTION delete_hive_post(
   in _author hive_accounts.name%TYPE,
   in _permlink hive_permlink_data.permlink%TYPE,
-  in _block_num hive_blocks.num%TYPE,
-  in _delete_feed_cache BOOLEAN)
+  in _block_num hive_blocks.num%TYPE)
 RETURNS VOID
 LANGUAGE plpgsql
 AS
@@ -180,10 +179,8 @@ BEGIN
   DELETE FROM hive_reblogs
   WHERE post_id = __post_id;
 
-  IF _delete_feed_cache THEN
-    DELETE FROM hive_feed_cache
-    WHERE post_id = __post_id;
-  END IF;
+  DELETE FROM hive_feed_cache
+  WHERE post_id = __post_id;
 
 END
 $function$
diff --git a/hive/indexer/posts.py b/hive/indexer/posts.py
index 0a3ce6b3b..96f535be9 100644
--- a/hive/indexer/posts.py
+++ b/hive/indexer/posts.py
@@ -390,11 +390,8 @@ class Posts(DbAdapterHolder):
     @classmethod
     def delete(cls, op):
         """Marks a post record as being deleted."""
-
-        delete_feed_cache = not DbState.is_initial_sync()
-
-        sql = "SELECT delete_hive_post((:author)::varchar, (:permlink)::varchar, (:block_num)::int, (:delete_feed_cache)::BOOLEAN );"
-        DB.query_no_return(sql, author=op['author'], permlink = op['permlink'], block_num=op['block_num'], delete_feed_cache=delete_feed_cache)
+        sql = "SELECT delete_hive_post((:author)::varchar, (:permlink)::varchar, (:block_num)::int );"
+        DB.query_no_return(sql, author=op['author'], permlink = op['permlink'], block_num=op['block_num'])
 
     @classmethod
     def _verify_post_against_community(cls, op, community_id, is_valid, is_muted):
diff --git a/hive/indexer/reblog.py b/hive/indexer/reblog.py
index 523b8dbb7..f126b8024 100644
--- a/hive/indexer/reblog.py
+++ b/hive/indexer/reblog.py
@@ -60,11 +60,8 @@ class Reblog(DbAdapterHolder):
     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_feed_cache( (:author)::VARCHAR, (:permlink)::VARCHAR, (:account)::VARCHAR, (:delete_feed_cache)::BOOLEAN );"
-        status = DB.query_col(sql, author=author, permlink=permlink, account=account, delete_feed_cache=delete_feed_cache);
-        log.info( "mario: author: %s, permlink: %s, account: %s status: %s", author, permlink, account, status )
+        sql = "SELECT delete_reblog_feed_cache( (:author)::VARCHAR, (:permlink)::VARCHAR, (:account)::VARCHAR );"
+        status = DB.query_col(sql, author=author, permlink=permlink, account=account);
         assert status is not None
         if status == 0:
           log.debug("reblog: post not found: %s/%s", author, permlink)
@@ -103,9 +100,9 @@ class Reblog(DbAdapterHolder):
                   continue
                 reblog_item = v['op']
                 if count < limit:
-                    values.append("('{}', '{}', '{}', '{}'::timestamp, {})".format(reblog_item['account'],
-                                                                                reblog_item['author'],
-                                                                                reblog_item['permlink'],
+                    values.append("({}, {}, {}, '{}'::timestamp, {})".format(escape_characters(reblog_item['account']),
+                                                                                escape_characters(reblog_item['author']),
+                                                                                escape_characters(reblog_item['permlink']),
                                                                                 reblog_item['block_date'],
                                                                                 reblog_item['block_num']))
                     count = count + 1
@@ -114,9 +111,9 @@ class Reblog(DbAdapterHolder):
                     query = sql_prefix.format(values_str, values_str)
                     cls.db.query(query)
                     values.clear()
-                    values.append("('{}', '{}', '{}', '{}'::timestamp, {})".format(reblog_item['account'],
-                                                                                reblog_item['author'],
-                                                                                reblog_item['permlink'],
+                    values.append("({}, {}, {}, '{}'::timestamp, {})".format(escape_characters(reblog_item['account']),
+                                                                                escape_characters(reblog_item['author']),
+                                                                                escape_characters(reblog_item['permlink']),
                                                                                 reblog_item['block_date'],
                                                                                 reblog_item['block_num']))
                     count = 1
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 ad0a31b3a..24f07cc5b 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
@@ -12,6 +12,17 @@
         "ref_block_prefix": 0,
         "expiration": "2020-03-23T12:08:00",
         "operations": [
+          {
+            "type": "custom_json_operation",
+            "value": {
+              "required_auths": [],
+              "required_posting_auths": [
+                "funny"
+              ],
+              "id": "follow",
+              "json": "[\"reblog\",{\"account\":\"funny\",\"author\":\"steemit\",\"permlink\":\"fi\nrst'%@post\"}]"
+            }
+          },
           {
             "type": "custom_json_operation",
             "value": {
-- 
GitLab