From 5f42cb194af9f07e0d1eb64e5e9e8417fb0bb0f8 Mon Sep 17 00:00:00 2001
From: Bartek Wrona <wrona@syncad.com>
Date: Fri, 6 Nov 2020 16:44:43 +0100
Subject: [PATCH] Reimplemented condenser_api.get_discussions_by_feed to reuse
 bridge_api SQL function and match previous behavior

---
 hive/server/bridge_api/methods.py      |  4 ----
 hive/server/condenser_api/get_state.py |  6 ++----
 hive/server/condenser_api/methods.py   | 27 +++++++++++++++++++++++---
 hive/server/hive_api/public.py         | 21 --------------------
 4 files changed, 26 insertions(+), 32 deletions(-)

diff --git a/hive/server/bridge_api/methods.py b/hive/server/bridge_api/methods.py
index 15177ea32..93e03a948 100644
--- a/hive/server/bridge_api/methods.py
+++ b/hive/server/bridge_api/methods.py
@@ -12,7 +12,6 @@ from hive.server.hive_api.common import get_account_id
 from hive.server.hive_api.objects import _follow_contexts
 from hive.server.hive_api.community import list_top_communities
 from hive.server.common.mutes import Mutes
-from hive.server.hive_api.public import get_by_feed_with_reblog_impl
 
 #pylint: disable=too-many-arguments, no-else-return
 
@@ -263,9 +262,6 @@ async def get_ranked_posts(context, sort:str, start_author:str='', start_permlin
     result = await _get_ranked_posts_for_all(db, sort, start_author, start_permlink, limit)
     return await process_query_results(result)
 
-async def _get_account_posts_by_feed(db, account : str, start_author : str, start_permlink : str, limit : int):
-  return await get_by_feed_with_reblog_impl(db, account, start_author, start_permlink, limit)
-
 @return_error_info
 async def get_account_posts(context, sort:str, account:str, start_author:str='', start_permlink:str='',
                             limit:int=20, observer:str=None):
diff --git a/hive/server/condenser_api/get_state.py b/hive/server/condenser_api/get_state.py
index 4b5dbb4cd..663abb50d 100644
--- a/hive/server/condenser_api/get_state.py
+++ b/hive/server/condenser_api/get_state.py
@@ -26,9 +26,7 @@ from hive.server.condenser_api.tags import (
 
 import hive.server.condenser_api.cursor as cursor
 
-from hive.server.condenser_api.methods import get_posts_by_given_sort
-
-from hive.server.hive_api.public import get_by_feed_with_reblog_impl
+from hive.server.condenser_api.methods import get_posts_by_given_sort, get_discussions_by_feed_impl
 from hive.server.database_api.methods import find_votes_impl, VotesPresentation
 
 log = logging.getLogger(__name__)
@@ -170,7 +168,7 @@ async def _get_account_discussion_by_key(db, account, key):
     elif key == 'blog':
         posts = await cursor.get_by_blog(db, account, '', '', 20)
     elif key == 'feed':
-        posts = await get_by_feed_with_reblog_impl(db, account, '', '', 20)
+        posts = await get_discussions_by_feed_impl(db, account, '', '', 20)
     else:
         raise ApiError("unknown account discussion key %s" % key)
 
diff --git a/hive/server/condenser_api/methods.py b/hive/server/condenser_api/methods.py
index ebde88edb..575393753 100644
--- a/hive/server/condenser_api/methods.py
+++ b/hive/server/condenser_api/methods.py
@@ -16,8 +16,6 @@ from hive.server.common.helpers import (
 from hive.server.common.mutes import Mutes
 from hive.server.database_api.methods import find_votes_impl, VotesPresentation
 
-from hive.server.hive_api.public import get_by_feed_with_reblog_impl
-
 # pylint: disable=too-many-arguments,line-too-long,too-many-lines
 
 @return_error_info
@@ -302,6 +300,29 @@ async def get_discussions_by_blog(context, tag: str = None, start_author: str =
 
     return posts_by_id
 
+async def get_discussions_by_feed_impl(db, account: str, start_author: str = '',
+                                   start_permlink: str = '', limit: int = 20, truncate_body: int = 0):
+    """Get a list of posts for an account's feed."""
+    sql = "SELECT * FROM bridge_get_by_feed_with_reblog((:account)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::INTEGER)"
+    result = await db.query_all(sql, account=account, author=start_author, permlink=start_permlink, limit=limit)
+
+    posts = []
+    for row in result:
+        row = dict(row)
+        post = _condenser_post_object(row, truncate_body=truncate_body)
+        reblogged_by = set(row['reblogged_by'])
+        reblogged_by.discard(row['author']) # Eliminate original author of reblogged post
+        if reblogged_by:
+            reblogged_by_list = list(reblogged_by)
+            reblogged_by_list.sort()
+            post['reblogged_by'] = reblogged_by_list
+
+        post['active_votes'] = await find_votes_impl(db, row['author'], row['permlink'], VotesPresentation.CondenserApi)
+        posts.append(post)
+
+    return posts
+
+
 @return_error_info
 @nested_query_compat
 async def get_discussions_by_feed(context, tag: str = None, start_author: str = '',
@@ -310,7 +331,7 @@ async def get_discussions_by_feed(context, tag: str = None, start_author: str =
     """Retrieve account's personalized feed."""
     assert tag, '`tag` cannot be blank'
     assert not filter_tags, 'filter_tags not supported'
-    return await get_by_feed_with_reblog_impl(
+    return await get_discussions_by_feed_impl(
         context['db'],
         valid_account(tag),
         valid_account(start_author, allow_empty=True),
diff --git a/hive/server/hive_api/public.py b/hive/server/hive_api/public.py
index 4d3177b39..7e02d8b32 100644
--- a/hive/server/hive_api/public.py
+++ b/hive/server/hive_api/public.py
@@ -80,24 +80,3 @@ async def get_info(context):
     }
 
     return ret
-
-async def get_by_feed_with_reblog_impl(db, account: str, start_author: str = '',
-                                   start_permlink: str = '', limit: int = 20, truncate_body: int = 0):
-    """Get a list of posts for an account's feed."""
-    sql = " SELECT * FROM bridge_get_by_feed_with_reblog( '{}', '{}', '{}', {} ) ".format( account, start_author, start_permlink, limit )
-    result = await db.query_all(sql)
-
-    posts = []
-    for row in result:
-        row = dict(row)
-        post = _bridge_post_object(row, truncate_body=truncate_body)
-        reblogged_by = set(row['reblogged_by'])
-        reblogged_by.discard(row['author']) # Eliminate original author of reblogged post
-        if reblogged_by:
-            post['reblogged_by'] = list(reblogged_by)
-        log.info("Reblogged_by: {}".format(reblogged_by))
-
-        post['active_votes'] = await find_votes_impl(db, row['author'], row['permlink'], VotesPresentation.BridgeApi)
-        posts.append(post)
-
-    return posts
-- 
GitLab