From 039f75f7aa6632afd3d84ca33c3d43cb5cf0acdc Mon Sep 17 00:00:00 2001
From: ABW <andrzejl@syncad.com>
Date: Fri, 13 Nov 2020 17:22:56 +0100
Subject: [PATCH] [ABW]: fixed query call for get_reblogged_by
 bridge_get_account_posts_by_replies reused for get_by_replies_to_account -
 old condenser_get_by_replies_to_account removed (note: this changes paging to
 nonoverlapping)

---
 hive/db/schema.py                             |  1 -
 .../bridge_get_account_posts_by_replies.sql   | 16 +++-
 .../condenser_get_by_replies_to_account.sql   | 81 -------------------
 hive/db/sql_scripts/db_upgrade.sh             |  1 -
 hive/server/bridge_api/methods.py             |  2 +-
 hive/server/condenser_api/cursor.py           |  8 +-
 hive/server/condenser_api/methods.py          |  1 +
 tests/tests_api                               |  2 +-
 8 files changed, 20 insertions(+), 92 deletions(-)
 delete mode 100644 hive/db/sql_scripts/condenser_get_by_replies_to_account.sql

diff --git a/hive/db/schema.py b/hive/db/schema.py
index 56501b157..8c2d13da3 100644
--- a/hive/db/schema.py
+++ b/hive/db/schema.py
@@ -598,7 +598,6 @@ def setup(db):
       "database_api_list_votes.sql",
       "update_posts_rshares.sql",
       "update_hive_post_root_id.sql",
-      "condenser_get_by_replies_to_account.sql",
       "condenser_get_by_account_comments.sql",
       "condenser_get_by_blog_without_reblog.sql",
       "bridge_get_by_feed_with_reblog.sql",
diff --git a/hive/db/sql_scripts/bridge_get_account_posts_by_replies.sql b/hive/db/sql_scripts/bridge_get_account_posts_by_replies.sql
index 74a17016a..70a15a234 100644
--- a/hive/db/sql_scripts/bridge_get_account_posts_by_replies.sql
+++ b/hive/db/sql_scripts/bridge_get_account_posts_by_replies.sql
@@ -1,6 +1,6 @@
 DROP FUNCTION IF EXISTS bridge_get_account_posts_by_replies;
 
-CREATE FUNCTION bridge_get_account_posts_by_replies( in _account VARCHAR, in _author VARCHAR, in _permlink VARCHAR, in _limit SMALLINT )
+CREATE FUNCTION bridge_get_account_posts_by_replies( in _account VARCHAR, in _author VARCHAR, in _permlink VARCHAR, in _limit SMALLINT, in _bridge_api BOOLEAN )
 RETURNS SETOF bridge_api_post
 AS
 $function$
@@ -8,8 +8,18 @@ DECLARE
   __account_id INT;
   __post_id INT;
 BEGIN
-  __account_id = find_account_id( _account, True );
-  __post_id = find_comment_id( _author, _permlink, True );
+  IF NOT _bridge_api AND _permlink <> '' THEN
+      -- find blogger account using parent author of page defining post
+      __post_id = find_comment_id( _author, _permlink, True );
+      SELECT pp.author_id INTO __account_id
+      FROM hive_posts hp
+      JOIN hive_posts pp ON hp.parent_id = pp.id
+      WHERE hp.id = __post_id;
+      IF __account_id = 0 THEN __account_id = NULL; END IF;
+  ELSE
+      __account_id = find_account_id( _account, True );
+      __post_id = find_comment_id( _author, _permlink, True );
+  END IF;
   RETURN QUERY SELECT
       hp.id,
       hp.author,
diff --git a/hive/db/sql_scripts/condenser_get_by_replies_to_account.sql b/hive/db/sql_scripts/condenser_get_by_replies_to_account.sql
deleted file mode 100644
index c3ea90c1a..000000000
--- a/hive/db/sql_scripts/condenser_get_by_replies_to_account.sql
+++ /dev/null
@@ -1,81 +0,0 @@
-DROP FUNCTION IF EXISTS condenser_get_by_replies_to_account;
-
-CREATE OR REPLACE FUNCTION condenser_get_by_replies_to_account(
-  in _author VARCHAR,
-  in _permlink VARCHAR,
-  in _limit INTEGER
-)
-RETURNS SETOF bridge_api_post
-AS
-$function$
-DECLARE
-  __post_id INTEGER := 0;
-  __parent_author_id INTEGER := 0;
-BEGIN
-
-  IF _permlink <> '' THEN
-
-    __post_id = find_comment_id( _author, _permlink, True );
-
-    SELECT
-      pp.author_id
-    INTO
-      __parent_author_id
-    FROM hive_posts hp
-    JOIN hive_posts pp ON hp.parent_id = pp.id
-    WHERE hp.id = __post_id;
-  ELSE
-    SELECT
-      id
-    INTO
-      __parent_author_id
-    FROM hive_accounts
-    WHERE name = _author;
-  END IF;
-
-  RETURN QUERY SELECT
-      hp.id,
-      hp.author,
-      hp.parent_author,
-      hp.author_rep,
-      hp.root_title,
-      hp.beneficiaries,
-      hp.max_accepted_payout,
-      hp.percent_hbd,
-      hp.url,
-      hp.permlink,
-      hp.parent_permlink_or_category,
-      hp.title,
-      hp.body,
-      hp.category,
-      hp.depth,
-      hp.promoted,
-      hp.payout,
-      hp.pending_payout,
-      hp.payout_at,
-      hp.is_paidout,
-      hp.children,
-      hp.votes,
-      hp.created_at,
-      hp.updated_at,
-      hp.rshares,
-      hp.abs_rshares,
-      hp.json,
-      hp.is_hidden,
-      hp.is_grayed,
-      hp.total_votes,
-      hp.sc_trend,
-      hp.role_title,
-      hp.community_title,
-      hp.role_id,
-      hp.is_pinned,
-      hp.curator_payout_value
-    FROM hive_posts_view hp
-    JOIN hive_posts pp ON hp.parent_id = pp.id
-    WHERE pp.author_id = __parent_author_id AND ( ( __post_id = 0 ) OR ( hp.id <= __post_id ) )
-    ORDER BY hp.id DESC
-    LIMIT _limit;
-
-END
-$function$
-language plpgsql STABLE;
diff --git a/hive/db/sql_scripts/db_upgrade.sh b/hive/db/sql_scripts/db_upgrade.sh
index 887959718..f547f1008 100755
--- a/hive/db/sql_scripts/db_upgrade.sh
+++ b/hive/db/sql_scripts/db_upgrade.sh
@@ -51,7 +51,6 @@ for sql in postgres_handle_view_changes.sql \
           database_api_list_votes.sql \
           update_posts_rshares.sql \
           update_hive_post_root_id.sql \
-          condenser_get_by_replies_to_account.sql \
           condenser_get_by_account_comments.sql \
           condenser_get_by_blog_without_reblog.sql \
           bridge_get_by_feed_with_reblog.sql \
diff --git a/hive/server/bridge_api/methods.py b/hive/server/bridge_api/methods.py
index 077c0cf8b..4d41816ba 100644
--- a/hive/server/bridge_api/methods.py
+++ b/hive/server/bridge_api/methods.py
@@ -289,7 +289,7 @@ async def get_account_posts(context, sort:str, account:str, start_author:str='',
         sql = "SELECT * FROM bridge_get_account_posts_by_comments( (:account)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
     elif sort == 'replies':
         account_posts = False
-        sql = "SELECT * FROM bridge_get_account_posts_by_replies( (:account)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
+        sql = "SELECT * FROM bridge_get_account_posts_by_replies( (:account)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, True )"
     elif sort == 'payout':
         sql = "SELECT * FROM bridge_get_account_posts_by_payout( (:account)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
 
diff --git a/hive/server/condenser_api/cursor.py b/hive/server/condenser_api/cursor.py
index eb15fb644..70ed8c626 100644
--- a/hive/server/condenser_api/cursor.py
+++ b/hive/server/condenser_api/cursor.py
@@ -21,8 +21,8 @@ async def get_following(db, account: str, start: str, state: int, limit: int):
 async def get_reblogged_by(db, author: str, permlink: str):
     """Return all rebloggers of a post."""
 
-    sql = "SELECT * FROM condenser_get_names_by_reblogged( '{}', '{}' )".format( author, permlink )
-    names = await db.query_col(sql)
+    sql = "SELECT * FROM condenser_get_names_by_reblogged( (:author)::VARCHAR, (:permlink)::VARCHAR )"
+    names = await db.query_col(sql, author=author, permlink=permlink)
 
     if author in names:
         names.remove(author)
@@ -53,8 +53,8 @@ async def get_by_account_comments(db, account: str, start_permlink: str = '', li
 
 async def get_by_replies_to_account(db, start_author: str, start_permlink: str = '', limit: int = 20, truncate_body: int = 0):
   """Get a list of posts representing replies to an author."""
-  sql = "SELECT * FROM condenser_get_by_replies_to_account( (:author)::VARCHAR, (:permlink)::VARCHAR, :limit )"
-  result = await db.query_all(sql, author=start_author, permlink=start_permlink, limit=limit);
+  sql = "SELECT * FROM bridge_get_account_posts_by_replies( (:account)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, False )"
+  result = await db.query_all(sql, account=start_author, author=start_author if start_permlink else '', permlink=start_permlink, limit=limit);
   return await process_posts(db, result, truncate_body)
 
 async def get_by_blog(db, account: str = '', start_author: str = '', start_permlink: str = '', limit: int = 20):
diff --git a/hive/server/condenser_api/methods.py b/hive/server/condenser_api/methods.py
index f94b8d066..00721b311 100644
--- a/hive/server/condenser_api/methods.py
+++ b/hive/server/condenser_api/methods.py
@@ -378,6 +378,7 @@ async def get_discussions_by_comments(context, start_author: str = None, start_p
 async def get_replies_by_last_update(context, start_author: str = None, start_permlink: str = '',
                                      limit: int = 20, truncate_body: int = 0):
     """Get all replies made to any of author's posts."""
+    # despite the name time of last edit is not used, posts ranked by creation time (that is, their id)
     # note that in this call start_author has dual meaning:
     # - when there is only start_author it means account that authored posts that we seek replies to
     # - when there is also start_permlink it points to one of replies (last post of previous page) and
diff --git a/tests/tests_api b/tests/tests_api
index 45a18a1ea..4bf9b35bc 160000
--- a/tests/tests_api
+++ b/tests/tests_api
@@ -1 +1 @@
-Subproject commit 45a18a1ea9617a753a4b1cfac61793247d7adbc8
+Subproject commit 4bf9b35bc06048ca23b7486191133aa50cab0536
-- 
GitLab