diff --git a/hive/db/schema.py b/hive/db/schema.py index 4fffe4c35a1d0965e31a0852f58a871f8ee770b6..8be645fe1bce0f3377778b4c85c8f22efc1f7cd6 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -1606,6 +1606,7 @@ def setup(db): "condenser_get_content.sql", "condenser_get_discussions_by_blog.sql", "condenser_tags.sql", + "condenser_follows.sql", "hot_and_trends.sql", "update_hive_posts_children_count.sql" ] diff --git a/hive/db/sql_scripts/condenser_follows.sql b/hive/db/sql_scripts/condenser_follows.sql new file mode 100644 index 0000000000000000000000000000000000000000..52237835e7c00c0d11406be0592360e6460a9c65 --- /dev/null +++ b/hive/db/sql_scripts/condenser_follows.sql @@ -0,0 +1,15 @@ +DROP FUNCTION IF EXISTS condenser_get_follow_count; +CREATE FUNCTION condenser_get_follow_count( in _account VARCHAR, + out following hive_accounts.following%TYPE, out followers hive_accounts.followers%TYPE ) +AS +$function$ +DECLARE + __account_id INT; +BEGIN + __account_id = find_account_id( _account, True ); + SELECT ha.following, ha.followers INTO following, followers FROM hive_accounts ha WHERE ha.id = __account_id; + -- following equals (SELECT COUNT(*) FROM hive_follows WHERE state = 1 AND following = __account_id) + -- followers equals (SELECT COUNT(*) FROM hive_follows WHERE state = 1 AND follower = __account_id) +END +$function$ +language plpgsql STABLE; diff --git a/hive/server/condenser_api/cursor.py b/hive/server/condenser_api/cursor.py index 66f8a279b792d8f0ac1751e037ad0f2b173dc5db..5a69fdb12ccd0cddd9f861a5d90d5dbc672c0eb2 100644 --- a/hive/server/condenser_api/cursor.py +++ b/hive/server/condenser_api/cursor.py @@ -93,15 +93,6 @@ async def get_following(db, account: str, start: str, follow_type: str, limit: i state=state, limit=limit) -async def get_follow_counts(db, account: str): - """Return following/followers count for `account`.""" - account_id = await _get_account_id(db, account) - sql = """SELECT following, followers - FROM hive_accounts - WHERE id = :account_id""" - return dict(await db.query_row(sql, account_id=account_id)) - - async def get_reblogged_by(db, author: str, permlink: str): """Return all rebloggers of a post.""" post_id = await _get_post_id(db, author, permlink) diff --git a/hive/server/condenser_api/methods.py b/hive/server/condenser_api/methods.py index 466c23ccca5b7d690628dbec4794af54c9e2b611..43659cf9f9383885b210335f9d12e2b472d2882d 100644 --- a/hive/server/condenser_api/methods.py +++ b/hive/server/condenser_api/methods.py @@ -115,12 +115,13 @@ async def get_following(context, account: str, start: str, follow_type: str = No @return_error_info async def get_follow_count(context, account: str): """Get follow count stats. (EOL)""" - count = await cursor.get_follow_counts( - context['db'], - valid_account(account)) + db = context['db'] + account = valid_account(account) + sql = "SELECT * FROM condenser_get_follow_count( (:account)::VARCHAR )" + counters = await db.query_row(sql, account=account) return dict(account=account, - following_count=count['following'], - follower_count=count['followers']) + following_count=counters[0], + follower_count=counters[1]) @return_error_info async def get_reblogged_by(context, author: str, permlink: str): diff --git a/tests/tests_api b/tests/tests_api index fbc0c97245465eb46377a642b57ae1f9fce770ae..143134910db644cb159e1f3e60f1d76e8eebc485 160000 --- a/tests/tests_api +++ b/tests/tests_api @@ -1 +1 @@ -Subproject commit fbc0c97245465eb46377a642b57ae1f9fce770ae +Subproject commit 143134910db644cb159e1f3e60f1d76e8eebc485