From 32f83158611435cfc4dd97fba5e3b7b43e6b15c7 Mon Sep 17 00:00:00 2001
From: ABW <andrzejl@syncad.com>
Date: Thu, 26 Nov 2020 19:36:27 +0100
Subject: [PATCH] [ABW]: find_account_id changed to be similar to
 find_comment_id (other routines like that will likely follow) - True means
 the check will be performed, but not when account is empty (that needs to be
 blocked/allowed in python code) get_discussion SQL function renamed to
 bridge_get_discussion added validation of observer in get_discussion

---
 hive/db/sql_scripts/bridge_get_discussion.sql        |  8 ++++----
 hive/db/sql_scripts/condenser_follows.sql            |  4 ++--
 hive/db/sql_scripts/database_api_list_votes.sql      |  2 +-
 hive/db/sql_scripts/upgrade/upgrade_table_schema.sql |  4 ++--
 hive/db/sql_scripts/utility_functions.sql            | 12 +++++++-----
 hive/server/bridge_api/thread.py                     |  7 +++----
 hive/server/condenser_api/get_state.py               |  2 +-
 .../block_data/follow_op/mock_block_data_follow.json |  2 +-
 tests/tests_api                                      |  2 +-
 9 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/hive/db/sql_scripts/bridge_get_discussion.sql b/hive/db/sql_scripts/bridge_get_discussion.sql
index 61d334333..e4450cfd0 100644
--- a/hive/db/sql_scripts/bridge_get_discussion.sql
+++ b/hive/db/sql_scripts/bridge_get_discussion.sql
@@ -1,6 +1,6 @@
-DROP FUNCTION IF EXISTS get_discussion
+DROP FUNCTION IF EXISTS bridge_get_discussion
 ;
-CREATE OR REPLACE FUNCTION get_discussion(
+CREATE OR REPLACE FUNCTION bridge_get_discussion(
     in _author hive_accounts.name%TYPE,
     in _permlink hive_permlink_data.permlink%TYPE,
     in _observer VARCHAR
@@ -14,9 +14,9 @@ DECLARE
     __observer_id INT;
 BEGIN
     __post_id = find_comment_id( _author, _permlink, True );
-    __observer_id = find_account_id(_observer, False);
+    __observer_id = find_account_id( _observer, True );
     RETURN QUERY
-    SELECT
+    SELECT -- bridge_get_discussion
         hpv.id,
         hpv.author,
         hpv.parent_author,
diff --git a/hive/db/sql_scripts/condenser_follows.sql b/hive/db/sql_scripts/condenser_follows.sql
index 035824619..9c90c4fec 100644
--- a/hive/db/sql_scripts/condenser_follows.sql
+++ b/hive/db/sql_scripts/condenser_follows.sql
@@ -25,7 +25,7 @@ DECLARE
   __start_id INT;
 BEGIN
   __account_id = find_account_id( _account, True );
-  __start_id = find_account_id( _start, _start <> '' );
+  __start_id = find_account_id( _start, True );
   IF __start_id <> 0 THEN
       SELECT INTO __start_id ( SELECT id FROM hive_follows WHERE following = __account_id AND follower = __start_id );
   END IF;
@@ -53,7 +53,7 @@ DECLARE
   __start_id INT;
 BEGIN
   __account_id = find_account_id( _account, True );
-  __start_id = find_account_id( _start, _start <> '' );
+  __start_id = find_account_id( _start, True );
   IF __start_id <> 0 THEN
       SELECT INTO __start_id ( SELECT id FROM hive_follows WHERE follower = __account_id AND following = __start_id );
   END IF;
diff --git a/hive/db/sql_scripts/database_api_list_votes.sql b/hive/db/sql_scripts/database_api_list_votes.sql
index 89cd9256d..df591ebf1 100644
--- a/hive/db/sql_scripts/database_api_list_votes.sql
+++ b/hive/db/sql_scripts/database_api_list_votes.sql
@@ -117,7 +117,7 @@ DECLARE __voter_id INT;
 DECLARE __post_id INT;
 BEGIN
 
-__voter_id = find_account_id( _VOTER, _VOTER != '' ); -- voter is optional
+__voter_id = find_account_id( _VOTER, True );
 __post_id = find_comment_id( _AUTHOR, _PERMLINK, True );
 
 RETURN QUERY
diff --git a/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql b/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql
index b4bb9f5b8..1bfc959c0 100644
--- a/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql
+++ b/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql
@@ -228,7 +228,7 @@ IF NOT EXISTS(SELECT data_type FROM information_schema.columns
 
     UPDATE hive_posts hp
     SET
-    	tags_ids = tags.tags
+        tags_ids = tags.tags
     FROM
     (
       SELECT
@@ -237,7 +237,7 @@ IF NOT EXISTS(SELECT data_type FROM information_schema.columns
       FROM
         hive_post_tags hpt
       GROUP BY post_id
-    	) as tags
+    ) as tags
     WHERE hp.id = tags.post_id;
 ELSE
     RAISE NOTICE 'SKIPPING hive_posts upgrade - adding a tags_ids column';
diff --git a/hive/db/sql_scripts/utility_functions.sql b/hive/db/sql_scripts/utility_functions.sql
index 7c632ac7f..098af5bf6 100644
--- a/hive/db/sql_scripts/utility_functions.sql
+++ b/hive/db/sql_scripts/utility_functions.sql
@@ -64,13 +64,15 @@ LANGUAGE 'plpgsql'
 AS
 $function$
 DECLARE
-  account_id INT;
+  __account_id INT = 0;
 BEGIN
-  SELECT INTO account_id COALESCE( ( SELECT id FROM hive_accounts WHERE name=_account ), 0 );
-  IF _check AND account_id = 0 THEN
-    RAISE EXCEPTION 'Account % does not exist', _account;
+  IF (_account <> '') THEN
+    SELECT INTO __account_id COALESCE( ( SELECT id FROM hive_accounts WHERE name=_account ), 0 );
+    IF _check AND __account_id = 0 THEN
+      RAISE EXCEPTION 'Account % does not exist', _account;
+    END IF;
   END IF;
-  RETURN account_id;
+  RETURN __account_id;
 END
 $function$
 ;
diff --git a/hive/server/bridge_api/thread.py b/hive/server/bridge_api/thread.py
index d15e48a5b..bafe0c768 100644
--- a/hive/server/bridge_api/thread.py
+++ b/hive/server/bridge_api/thread.py
@@ -13,20 +13,19 @@ from hive.server.common.mutes import Mutes
 log = logging.getLogger(__name__)
 
 @return_error_info
-async def get_discussion(context, author, permlink, observer=None):
+async def get_discussion(context, author:str, permlink:str, observer:str=''):
     """Modified `get_state` thread implementation."""
-    # New index was created: hive_posts_parent_id_btree (CREATE INDEX "hive_posts_parent_id_btree" ON hive_posts btree(parent_id)
-    # We thougth this would be covered by "hive_posts_ix4" btree (parent_id, id) WHERE counter_deleted = 0 but it was not
     db = context['db']
 
     author = valid_account(author)
     permlink = valid_permlink(permlink)
+    observer = valid_account(observer, allow_empty=True)
 
     blacklisted_for_user = None
     if observer:
         blacklisted_for_user = await Mutes.get_blacklisted_for_observer(observer, context)
 
-    sql = "SELECT * FROM get_discussion(:author,:permlink,:observer)"
+    sql = "SELECT * FROM bridge_get_discussion(:author,:permlink,:observer)"
     rows = await db.query_all(sql, author=author, permlink=permlink, observer=observer)
     if not rows or len(rows) == 0:
         return {}
diff --git a/hive/server/condenser_api/get_state.py b/hive/server/condenser_api/get_state.py
index 05221d462..53081f127 100644
--- a/hive/server/condenser_api/get_state.py
+++ b/hive/server/condenser_api/get_state.py
@@ -237,7 +237,7 @@ async def _child_ids(db, parent_ids):
 async def _load_discussion(db, author, permlink, observer=None):
     """Load a full discussion thread."""
 
-    sql = "SELECT * FROM get_discussion(:author,:permlink,:observer)"
+    sql = "SELECT * FROM bridge_get_discussion(:author,:permlink,:observer)"
     sql_result = await db.query_all(sql, author=author, permlink=permlink, observer=observer)
 
     muted_accounts = Mutes.all()
diff --git a/mock_data/block_data/follow_op/mock_block_data_follow.json b/mock_data/block_data/follow_op/mock_block_data_follow.json
index c018e6c81..2d6c189d2 100644
--- a/mock_data/block_data/follow_op/mock_block_data_follow.json
+++ b/mock_data/block_data/follow_op/mock_block_data_follow.json
@@ -693,7 +693,7 @@
                 "spaminator"
               ],
               "id": "follow",
-              "json": "[\"follow\",{\"follower\":\"spaminator\",\"following\":[\"lyubovbar\",\"zaitsevalesyaa\",\"kingscrown\",\"trevonjb\",\"craig-grant\",\"ned\"],\"what\":[\"blacklist\"]}]"
+              "json": "[\"follow\",{\"follower\":\"spaminator\",\"following\":[\"lyubovbar\",\"zaitsevalesyaa\",\"kingscrown\",\"trevonjb\",\"craig-grant\",\"ned\",\"mindhunter\"],\"what\":[\"blacklist\"]}]"
             }
           },
           {
diff --git a/tests/tests_api b/tests/tests_api
index 88c50bc23..87c445782 160000
--- a/tests/tests_api
+++ b/tests/tests_api
@@ -1 +1 @@
-Subproject commit 88c50bc23cece0e6ba9a80018d689a3eeea27e18
+Subproject commit 87c4457827200ef54f8fe64f942df641a9f1b1af
-- 
GitLab