From 5975627059fa965f5177c2d42afde3fd8e3d5466 Mon Sep 17 00:00:00 2001
From: ABW <andrzejl@syncad.com>
Date: Tue, 3 Nov 2020 18:00:00 +0100
Subject: [PATCH] [ABW]: implementation of get_discussions_by_created now
 reuses bridge api equivalent

---
 hive/db/schema.py                             |   1 -
 ...bridge_get_ranked_post_for_communities.sql |   4 +-
 .../condenser_get_discussions_by_created.sql  | 126 ------------------
 hive/server/bridge_api/methods.py             |   2 +-
 hive/server/condenser_api/methods.py          |   8 +-
 tests/tests_api                               |   2 +-
 6 files changed, 9 insertions(+), 134 deletions(-)
 delete mode 100644 hive/db/sql_scripts/condenser_get_discussions_by_created.sql

diff --git a/hive/db/schema.py b/hive/db/schema.py
index ffd051615..9410a8e30 100644
--- a/hive/db/schema.py
+++ b/hive/db/schema.py
@@ -1604,7 +1604,6 @@ def setup(db):
       "condenser_api_post_ex_type.sql",
       "condenser_get_blog.sql",
       "condenser_get_content.sql",
-      "condenser_get_discussions_by_created.sql",
       "condenser_get_discussions_by_blog.sql",
       "hot_and_trends.sql",
       "update_hive_posts_children_count.sql"
diff --git a/hive/db/sql_scripts/bridge_get_ranked_post_for_communities.sql b/hive/db/sql_scripts/bridge_get_ranked_post_for_communities.sql
index c631deffa..147377f89 100644
--- a/hive/db/sql_scripts/bridge_get_ranked_post_for_communities.sql
+++ b/hive/db/sql_scripts/bridge_get_ranked_post_for_communities.sql
@@ -474,7 +474,7 @@ $function$
 language plpgsql STABLE;
 
 DROP FUNCTION IF EXISTS bridge_get_ranked_post_by_created_for_community;
-CREATE FUNCTION bridge_get_ranked_post_by_created_for_community( in _community VARCHAR, in _author VARCHAR, in _permlink VARCHAR, in _limit SMALLINT )
+CREATE FUNCTION bridge_get_ranked_post_by_created_for_community( in _community VARCHAR, in _author VARCHAR, in _permlink VARCHAR, in _limit SMALLINT, in _bridge_api BOOLEAN )
 RETURNS SETOF bridge_api_post
 AS
 $function$
@@ -528,7 +528,7 @@ BEGIN
           JOIN hive_communities hc ON hp1.community_id = hc.id
           JOIN hive_accounts_view ha ON hp1.author_id = ha.id
       WHERE hc.name = _community AND hp1.counter_deleted = 0 AND hp1.depth = 0
-          AND NOT hp1.is_pinned -- concatenated with bridge_get_ranked_post_pinned_for_community
+          AND ( NOT _bridge_api OR NOT hp1.is_pinned ) -- concatenated with bridge_get_ranked_post_pinned_for_community when called for bridge_api
           AND NOT ha.is_grayed AND ( __post_id = 0 OR hp1.id < __post_id )
       ORDER BY hp1.id DESC
       LIMIT _limit
diff --git a/hive/db/sql_scripts/condenser_get_discussions_by_created.sql b/hive/db/sql_scripts/condenser_get_discussions_by_created.sql
deleted file mode 100644
index c3a1d6c54..000000000
--- a/hive/db/sql_scripts/condenser_get_discussions_by_created.sql
+++ /dev/null
@@ -1,126 +0,0 @@
-DROP FUNCTION IF EXISTS condenser_get_discussions_by_created;
-
-CREATE FUNCTION condenser_get_discussions_by_created( in _tag VARCHAR, in _author VARCHAR, in _permlink VARCHAR, in _limit SMALLINT )
-RETURNS SETOF bridge_api_post
-AS
-$function$
-DECLARE
-  __post_id INT;
-  __community VARCHAR;
-  __tag_id INT;
-  __category_id INT;
-BEGIN
-
-  __post_id     = find_comment_id( _author, _permlink, True );
-  __community   = ( SELECT substring(_tag from '^hive-') );
-
-  __tag_id      = ( SELECT id FROM hive_tag_data WHERE tag = _tag );
-  __category_id = ( SELECT id FROM hive_category_data WHERE category = _tag );
-
-  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
-    INNER JOIN hive_post_tags hpt ON hpt.post_id = hp.id
-    WHERE ( __tag_id = 0 OR hpt.tag_id = __tag_id ) AND ( __post_id = 0 OR hp.id < __post_id )
-          AND ( ( __community IS NULL ) OR ( ( __community IS NOT NULL ) AND ( __category_id = 0 OR hp.category_id = __category_id ) ) )
-    ORDER BY hp.id DESC LIMIT _limit;
-END
-$function$
-language plpgsql STABLE;
-
-DROP FUNCTION IF EXISTS condenser_get_discussions_by_created_with_empty_tag;
-
-CREATE FUNCTION condenser_get_discussions_by_created_with_empty_tag( in _tag VARCHAR, in _author VARCHAR, in _permlink VARCHAR, in _limit SMALLINT )
-RETURNS SETOF bridge_api_post
-AS
-$function$
-DECLARE
-  __post_id INT;
-  __community VARCHAR;
-  __category_id INT;
-BEGIN
-
-  __post_id     = find_comment_id( _author, _permlink, True );
-  __community   = ( SELECT substring(_tag from '^hive-') );
-
-  __category_id = ( SELECT id FROM hive_category_data WHERE category = _tag );
-
-  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
-    WHERE ( __post_id = 0 OR hp.id < __post_id )
-          AND ( ( __community IS NULL ) OR ( ( __community IS NOT NULL ) AND ( __category_id = 0 OR hp.category_id = __category_id ) ) )
-    ORDER BY hp.id DESC LIMIT _limit;
-END
-$function$
-language plpgsql STABLE;
diff --git a/hive/server/bridge_api/methods.py b/hive/server/bridge_api/methods.py
index 3d27022a7..2ed848d6e 100644
--- a/hive/server/bridge_api/methods.py
+++ b/hive/server/bridge_api/methods.py
@@ -129,7 +129,7 @@ async def _get_ranked_posts_for_communities( db, sort:str, community, start_auth
         return await execute_community_query(db, sql, limit)
 
     if sort == 'created':
-        sql = "SELECT * FROM bridge_get_ranked_post_by_created_for_community( (:community)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
+        sql = "SELECT * FROM bridge_get_ranked_post_by_created_for_community( (:community)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, True )"
         result_with_pinned_posts = await execute_community_query(db, pinned_sql, limit)
         limit -= len(result_with_pinned_posts)
         if limit > 0:
diff --git a/hive/server/condenser_api/methods.py b/hive/server/condenser_api/methods.py
index 0d5112672..466c23ccc 100644
--- a/hive/server/condenser_api/methods.py
+++ b/hive/server/condenser_api/methods.py
@@ -250,10 +250,12 @@ async def get_posts_by_given_sort(context, sort: str, start_author: str = '', st
     is_community = tag[:5] == 'hive-'
    
     if sort == 'created':
-      if tag == '':
-        sql = "SELECT * FROM condenser_get_discussions_by_created_with_empty_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
+      if is_community:
+        sql = "SELECT * FROM bridge_get_ranked_post_by_created_for_community( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, False )"
+      elif tag == '':
+        sql = "SELECT * FROM bridge_get_ranked_post_by_created( (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
       else:
-        sql = "SELECT * FROM condenser_get_discussions_by_created( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
+        sql = "SELECT * FROM bridge_get_ranked_post_by_created_for_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
     elif sort == 'trending':
       if is_community:
         sql = "SELECT * FROM bridge_get_ranked_post_by_trends_for_community( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, False )"
diff --git a/tests/tests_api b/tests/tests_api
index ad99cf64b..ef358b5ae 160000
--- a/tests/tests_api
+++ b/tests/tests_api
@@ -1 +1 @@
-Subproject commit ad99cf64b053ec80869ce03ab0d4d02a96964d18
+Subproject commit ef358b5ae508326e56e69de1c0aafda2bd4cddab
-- 
GitLab