diff --git a/hive/db/schema.py b/hive/db/schema.py index cc21d8693212b8e7a8d66c8b5bf123b2f9d77908..5bdbb816f69a9f42173e144cf02f131e18bef4ba 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -601,7 +601,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 671c5265187521389a97f09c431d13174eae353f..f011f69903bd937d1db8c9ebdc203f06cb192b0f 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_follows.sql b/hive/db/sql_scripts/condenser_follows.sql index fbfa033f28006311dca9c04c508d6e21a2d61994..035824619169402349509d65a89a4e446ff7263f 100644 --- a/hive/db/sql_scripts/condenser_follows.sql +++ b/hive/db/sql_scripts/condenser_follows.sql @@ -27,7 +27,7 @@ BEGIN __account_id = find_account_id( _account, True ); __start_id = find_account_id( _start, _start <> '' ); IF __start_id <> 0 THEN - SELECT INTO __start_id COALESCE( ( SELECT id FROM hive_follows WHERE following = __account_id AND follower = __start_id ), 0 ); + SELECT INTO __start_id ( SELECT id FROM hive_follows WHERE following = __account_id AND follower = __start_id ); END IF; RETURN QUERY SELECT ha.name @@ -55,7 +55,7 @@ BEGIN __account_id = find_account_id( _account, True ); __start_id = find_account_id( _start, _start <> '' ); IF __start_id <> 0 THEN - SELECT INTO __start_id COALESCE( ( SELECT id FROM hive_follows WHERE follower = __account_id AND following = __start_id ), 0 ); + SELECT INTO __start_id ( SELECT id FROM hive_follows WHERE follower = __account_id AND following = __start_id ); END IF; RETURN QUERY SELECT ha.name 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 6907236879545a6761b2886d4384be4cd59f2dba..0000000000000000000000000000000000000000 --- a/hive/db/sql_scripts/condenser_get_by_replies_to_account.sql +++ /dev/null @@ -1,82 +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; -BEGIN - - IF _permlink <> '' THEN - SELECT - ha_pp.name, hp.id - INTO - _author, __post_id - FROM hive_posts hp - JOIN hive_posts pp ON hp.parent_id = pp.id - JOIN hive_accounts ha_pp ON ha_pp.id = pp.author_id - JOIN hive_permlink_data hpd_pp ON hpd_pp.id = pp.permlink_id - JOIN hive_accounts ha ON hp.author_id = ha.id - WHERE - hpd_pp.permlink = _permlink AND ha.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, - hp.is_muted - FROM hive_posts_view hp - JOIN - ( - SELECT hp.id - FROM hive_posts_view hp - WHERE hp.author = _author - ORDER BY hp.id DESC - LIMIT _limit - ) T ON hp.parent_id = T.id - WHERE ( ( __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 e364b334fd2065acd868d2da3ed41d194c880968..12abddccc436c6ffb9b3c2ff0070b082d9fedd13 100755 --- a/hive/db/sql_scripts/db_upgrade.sh +++ b/hive/db/sql_scripts/db_upgrade.sh @@ -52,7 +52,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 48219219f77abccebd413b32d890828d4a840961..39c2c0ec40c0cc8d448935cfa0cf3601da8e7b7b 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 f93c46d9401f83bd3bbab324651efd37a508de53..70ed8c626ef9a1fe268cb63ac2e36ab166fd5727 100644 --- a/hive/server/condenser_api/cursor.py +++ b/hive/server/condenser_api/cursor.py @@ -21,18 +21,16 @@ 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) return names -async def get_data(db, sql:str, truncate_body: int = 0): - result = await db.query_all(sql); - +async def process_posts(db, sql_result, truncate_body: int = 0): posts = [] - for row in result: + for row in sql_result: row = dict(row) post = _condenser_post_object(row, truncate_body=truncate_body) @@ -43,20 +41,24 @@ async def get_data(db, sql:str, truncate_body: int = 0): async def get_by_blog_without_reblog(db, account: str, start_permlink: str = '', limit: int = 20, truncate_body: int = 0): """Get a list of posts for an author's blog without reblogs.""" - sql = " SELECT * FROM condenser_get_by_blog_without_reblog( '{}', '{}', {} ) ".format( account, start_permlink, limit ) - return await get_data(db, sql, truncate_body ) + sql = "SELECT * FROM condenser_get_by_blog_without_reblog( (:author)::VARCHAR, (:permlink)::VARCHAR, :limit )" + result = await db.query_all(sql, author=account, permlink=start_permlink, limit=limit); + return await process_posts(db, result, truncate_body) async def get_by_account_comments(db, account: str, start_permlink: str = '', limit: int = 20, truncate_body: int = 0): """Get a list of posts representing comments by an author.""" - sql = " SELECT * FROM condenser_get_by_account_comments( '{}', '{}', {} ) ".format( account, start_permlink, limit ) - return await get_data(db, sql, truncate_body ) + sql = "SELECT * FROM condenser_get_by_account_comments( (:author)::VARCHAR, (:permlink)::VARCHAR, :limit )" + result = await db.query_all(sql, author=account, permlink=start_permlink, limit=limit); + return await process_posts(db, result, truncate_body) 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( '{}', '{}', {} ) ".format( start_author, start_permlink, limit ) - return await get_data(db, sql, truncate_body ) + 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): """Get a list of posts for an author's blog.""" - sql = " SELECT * FROM condenser_get_by_blog( '{}', '{}', '{}', {} ) ".format( account, start_author, start_permlink, limit ) - return await get_data(db, sql ) + sql = "SELECT * FROM condenser_get_by_blog( (:account)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, :limit )" + result = await db.query_all(sql, account=account, author=start_author, permlink=start_permlink, limit=limit); + return await process_posts(db, result) diff --git a/hive/server/condenser_api/methods.py b/hive/server/condenser_api/methods.py index a2d308b2fd66f3552919da2424c1556da5e7478d..4b4410b87ab53ebe31e8aad4022a3384ce443f62 100644 --- a/hive/server/condenser_api/methods.py +++ b/hive/server/condenser_api/methods.py @@ -379,8 +379,12 @@ 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.""" - assert start_author, '`start_author` cannot be blank' - + # 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 + # we'll be getting account like above in form of author of parent post to the post pointed by + # given start_author+start_permlink return await cursor.get_by_replies_to_account( context['db'], valid_account(start_author), diff --git a/tests/tests_api b/tests/tests_api index cbb2c1c95d56181310e25d2ddcb9baff1bdaa83f..c50665c8f66121db558caa7c205631c28e0fe2ed 160000 --- a/tests/tests_api +++ b/tests/tests_api @@ -1 +1 @@ -Subproject commit cbb2c1c95d56181310e25d2ddcb9baff1bdaa83f +Subproject commit c50665c8f66121db558caa7c205631c28e0fe2ed