diff --git a/hive/db/schema.py b/hive/db/schema.py index e4a9b7ef759f2d0a50929a990cdb82f54fd15b76..ffd0516155ace61ecdaa33dbf06961a4a58f9c40 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -1607,8 +1607,6 @@ def setup(db): "condenser_get_discussions_by_created.sql", "condenser_get_discussions_by_blog.sql", "hot_and_trends.sql", - "condenser_get_discussions_by_trending.sql", - "condenser_get_discussions_by_hot.sql", "update_hive_posts_children_count.sql" ] from os.path import dirname, realpath 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 58a167d8cfb6a44f6d5eee79f43aa966a52db4b3..c631deffabf66090b1a8c5870d70e51fbec88874 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 @@ -50,7 +50,7 @@ $function$ language sql STABLE; DROP FUNCTION IF EXISTS bridge_get_ranked_post_by_trends_for_community; -CREATE FUNCTION bridge_get_ranked_post_by_trends_for_community( in _community VARCHAR, in _author VARCHAR, in _permlink VARCHAR, in _limit SMALLINT ) +CREATE FUNCTION bridge_get_ranked_post_by_trends_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$ @@ -107,7 +107,8 @@ BEGIN FROM hive_posts hp1 JOIN hive_communities hc ON hp1.community_id = hc.id - WHERE hc.name = _community AND hp1.counter_deleted = 0 AND NOT hp1.is_paidout AND hp1.depth = 0 AND NOT hp1.is_pinned -- concatenated with bridge_get_ranked_post_pinned_for_community + WHERE hc.name = _community AND hp1.counter_deleted = 0 AND NOT hp1.is_paidout AND hp1.depth = 0 + AND ( NOT _bridge_api OR NOT hp1.is_pinned ) -- concatenated with bridge_get_ranked_post_pinned_for_community when called for bridge_api AND ( __post_id = 0 OR hp1.sc_trend < __trending_limit OR ( hp1.sc_trend = __trending_limit AND hp1.id < __post_id ) ) ORDER BY hp1.sc_trend DESC, hp1.id DESC LIMIT _limit @@ -526,7 +527,8 @@ BEGIN hive_posts hp1 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 + 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 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_hot.sql b/hive/db/sql_scripts/condenser_get_discussions_by_hot.sql deleted file mode 100644 index 981ff24fdb70f8bed492f210231f2340ccfc90dd..0000000000000000000000000000000000000000 --- a/hive/db/sql_scripts/condenser_get_discussions_by_hot.sql +++ /dev/null @@ -1,126 +0,0 @@ -DROP FUNCTION IF EXISTS condenser_get_discussions_by_hot; - -CREATE FUNCTION condenser_get_discussions_by_hot( 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 hp.is_paidout = '0' AND ( __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 ) AND hp.depth = 0 ) ) ) - ORDER BY hp.sc_hot DESC, hp.id DESC LIMIT _limit; -END -$function$ -language plpgsql STABLE; - -DROP FUNCTION IF EXISTS condenser_get_discussions_by_hot_with_empty_tag; - -CREATE FUNCTION condenser_get_discussions_by_hot_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 hp.is_paidout = '0' 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 ) AND hp.depth = 0 ) ) ) - ORDER BY hp.sc_hot DESC, hp.id DESC LIMIT _limit; -END -$function$ -language plpgsql STABLE; diff --git a/hive/db/sql_scripts/condenser_get_discussions_by_trending.sql b/hive/db/sql_scripts/condenser_get_discussions_by_trending.sql deleted file mode 100644 index 277e845083349a2c11365d2937fba8880e10aa33..0000000000000000000000000000000000000000 --- a/hive/db/sql_scripts/condenser_get_discussions_by_trending.sql +++ /dev/null @@ -1,126 +0,0 @@ -DROP FUNCTION IF EXISTS condenser_get_discussions_by_trending; - -CREATE FUNCTION condenser_get_discussions_by_trending( 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 hp.is_paidout = '0' AND ( __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 ) AND hp.depth = 0 ) ) ) - ORDER BY hp.sc_trend DESC, hp.id DESC LIMIT _limit; -END -$function$ -language plpgsql STABLE; - -DROP FUNCTION IF EXISTS condenser_get_discussions_by_trending_with_empty_tag; - -CREATE FUNCTION condenser_get_discussions_by_trending_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 hp.is_paidout = '0' 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 ) AND hp.depth = 0 ) ) ) - ORDER BY hp.sc_trend DESC, 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 39651c09dae6f882c80c09796f839b962b23dab8..3d27022a7294e88b375d7c740daa6a034e626855 100644 --- a/hive/server/bridge_api/methods.py +++ b/hive/server/bridge_api/methods.py @@ -117,7 +117,7 @@ async def _get_ranked_posts_for_communities( db, sort:str, community, start_auth if sort == 'trending': result_with_pinned_posts = [] - sql = "SELECT * FROM bridge_get_ranked_post_by_trends_for_community( (:community)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" + sql = "SELECT * FROM bridge_get_ranked_post_by_trends_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 6025245e4da94a4397ff70f171c5dca278497c1a..0d511267215f1e1a1af3d6fd35954113052efacd 100644 --- a/hive/server/condenser_api/methods.py +++ b/hive/server/condenser_api/methods.py @@ -247,6 +247,7 @@ async def get_posts_by_given_sort(context, sort: str, start_author: str = '', st tag = valid_tag(tag, allow_empty=True) posts = [] + is_community = tag[:5] == 'hive-' if sort == 'created': if tag == '': @@ -254,17 +255,21 @@ async def get_posts_by_given_sort(context, sort: str, start_author: str = '', st else: sql = "SELECT * FROM condenser_get_discussions_by_created( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" elif sort == 'trending': - if tag == '': - sql = "SELECT * FROM condenser_get_discussions_by_trending_with_empty_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" + if is_community: + sql = "SELECT * FROM bridge_get_ranked_post_by_trends_for_community( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, False )" + elif tag == '': + sql = "SELECT * FROM bridge_get_ranked_post_by_trends( (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" else: - sql = "SELECT * FROM condenser_get_discussions_by_trending( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" + sql = "SELECT * FROM bridge_get_ranked_post_by_trends_for_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" elif sort == 'hot': - if tag == '': - sql = "SELECT * FROM condenser_get_discussions_by_hot_with_empty_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" + if is_community: + sql = "SELECT * FROM bridge_get_ranked_post_by_hot_for_community( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" + elif tag == '': + sql = "SELECT * FROM bridge_get_ranked_post_by_hot( (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" else: - sql = "SELECT * FROM condenser_get_discussions_by_hot( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" + sql = "SELECT * FROM bridge_get_ranked_post_by_hot_for_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" elif sort == 'promoted': - if tag[:5] == 'hive-': + if is_community: sql = "SELECT * FROM bridge_get_ranked_post_by_promoted_for_community( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" elif tag == '': sql = "SELECT * FROM bridge_get_ranked_post_by_promoted( (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )" diff --git a/tests/tests_api b/tests/tests_api index a605f56f0c449f233961db0405bc9f9605a2caa6..ad99cf64b053ec80869ce03ab0d4d02a96964d18 160000 --- a/tests/tests_api +++ b/tests/tests_api @@ -1 +1 @@ -Subproject commit a605f56f0c449f233961db0405bc9f9605a2caa6 +Subproject commit ad99cf64b053ec80869ce03ab0d4d02a96964d18