diff --git a/hive/db/schema.py b/hive/db/schema.py index af94e56d5c57b5170a07770d555d1b9e9efb1af6..0b733eae5873d68f50e6403ab1faf279489b701b 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -335,7 +335,7 @@ def build_metadata_community(metadata=None): sa.Column('role_id', SMALLINT, nullable=False, server_default='0'), sa.Column('title', sa.String(140), nullable=False, server_default=''), - sa.UniqueConstraint('account_id', 'community_id', name='hive_roles_ux1'), + sa.PrimaryKeyConstraint('account_id', 'community_id', name='hive_roles_pk'), sa.Index('hive_roles_ix1', 'community_id', 'account_id', 'role_id'), ) @@ -587,9 +587,9 @@ def setup(db): db.query_no_return(sql) sql = """ - DROP VIEW IF EXISTS public.vw_hive_posts; + DROP VIEW IF EXISTS public.hive_posts_view; - CREATE OR REPLACE VIEW public.vw_hive_posts + CREATE OR REPLACE VIEW public.hive_posts_view AS SELECT hp.id, hp.community_id, @@ -635,7 +635,12 @@ def setup(db): rpd.title AS root_title, hp.sc_trend, hp.sc_hot, - hp.is_deleted + hp.is_deleted, + hp.is_pinned, + hr.title AS role_title, + hr.role_id AS role_is, + hc.title AS community_title, + hc.name AS community_name FROM hive_posts hp JOIN hive_posts rp ON rp.author_id = hp.root_author_id AND rp.permlink_id = hp.root_permlink_id JOIN hive_post_data rpd ON rp.id = rpd.id @@ -647,7 +652,9 @@ def setup(db): JOIN hive_accounts ha_pa ON ha_pa.id = hp.parent_author_id JOIN hive_permlink_data hpd_pp ON hpd_pp.id = hp.parent_permlink_id JOIN hive_accounts ha_ra ON ha_ra.id = hp.root_author_id - JOIN hive_permlink_data hpd_rp ON hpd_rp.id = hp.root_permlink_id; + JOIN hive_permlink_data hpd_rp ON hpd_rp.id = hp.root_permlink_id + LEFT OUTER JOIN hive_communities hc ON (hp.community_id = hc.id) + LEFT OUTER JOIN hive_roles hr ON (hp.author_id = hr.account_id AND hp.community_id = hr.community_id) ; """ db.query_no_return(sql) @@ -669,11 +676,11 @@ def setup(db): WITH recursive tblChild AS ( SELECT s.queried_parent, s.id - from - (SELECT h1.Parent_Id as queried_parent, h1.id + from + (SELECT h1.Parent_Id as queried_parent, h1.id FROM hive_posts h1 WHERE h1.depth > 0 - order by h1.depth desc - ) s + order by h1.depth desc + ) s UNION ALL SELECT tblChild.queried_parent, p.id FROM hive_posts p JOIN tblChild ON p.Parent_Id = tblChild.Id ) @@ -689,9 +696,9 @@ def setup(db): db.query_no_return(sql) sql = """ - DROP VIEW IF EXISTS wv_hive_votes_accounts_permlinks + DROP VIEW IF EXISTS hive_votes_accounts_permlinks_view ; - CREATE VIEW wv_hive_votes_accounts_permlinks + CREATE VIEW hive_votes_accounts_permlinks_view AS SELECT ha_v.id as id, diff --git a/hive/server/bridge_api/methods.py b/hive/server/bridge_api/methods.py index 21b38d49fc839152bc4f028eeabce072ebd91632..e4285c9f65858442041be15279e95705544c7e34 100644 --- a/hive/server/bridge_api/methods.py +++ b/hive/server/bridge_api/methods.py @@ -17,55 +17,46 @@ from hive.server.common.mutes import Mutes ROLES = {-2: 'muted', 0: 'guest', 2: 'member', 4: 'mod', 6: 'admin', 8: 'owner'} SQL_TEMPLATE = """ - SELECT hp.id, - ha_a.name as author, - ha_r.name as root_author, - hp.author_rep as author_rep, - hp.allow_replies AS allow_replies, - hp.allow_votes AS allow_votes, - hp.allow_curation_rewards AS allow_curation_rewards, - hp.root_title AS root_title, - hp.beneficiaries AS beneficiaries, - hp.max_accepted_payout AS max_accepted_payout, - hp.percent_hbd AS percent_hbd, - hp.url AS url, - hpd_p.permlink as permlink, - hpd_r.permlink as root_permlink, - hpd.title as title, - hpd.body as body, - (SELECT category FROM hive_category_data WHERE hive_category_data.id = hp.category_id) as category, - depth, - promoted, - payout, - payout_at, - is_paidout, - children, - (0) as votes, + SELECT + hp.id, + hp.author, + hp.root_author, + hp.author_rep, + hp.allow_replies, + hp.allow_votes, + hp.allow_curation_rewards, + hp.root_title, + hp.beneficiaries, + hp.max_accepted_payout, + hp.percent_hbd, + hp.url, + hp.permlink, + hp.root_permlink, + hp.title, + hp.body, + hp.category, + hp.depth, + hp.promoted, + hp.payout, + hp.payout_at, + hp.is_paidout, + hp.children, + hp.votes, hp.created_at, - updated_at, - rshares, - hpd.json as json, - is_hidden, - is_grayed, - total_votes, - flag_weight, - sc_trend, - ha_a.id AS acct_author_id, - hive_roles.title as role_title, - hive_communities.title AS community_title, - hive_roles.role_id AS role_id, - hp.is_pinned AS is_pinned, - curator_payout_value - FROM hive_posts hp - INNER JOIN hive_accounts ha_a ON ha_a.id = hp.author_id - INNER JOIN hive_accounts ha_r ON ha_r.id = hp.root_author_id - INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id - INNER JOIN hive_permlink_data hpd_r ON hpd_r.id = hp.root_permlink_id - INNER JOIN hive_post_data hpd ON hpd.id = hp.id - INNER JOIN hive_post_tags hpt ON hpt.post_id = hp.id - INNER JOIN hive_tag_data htd ON hpt.tag_id=htd.id - LEFT OUTER JOIN hive_communities ON (hp.community_id = hive_communities.id) - LEFT OUTER JOIN hive_roles ON (ha_a.id = hive_roles.account_id AND hp.community_id = hive_roles.community_id) + hp.updated_at, + hp.rshares, + hp.json, + hp.is_hidden, + hp.is_grayed, + hp.total_votes, + hp.flag_weight, + hp.sc_trend, + hp.role_title, + hp.community_title, + hr.role_id, + hp.is_pinned, + hp.curator_payout_value + FROM hive_posts_view hp WHERE """ @@ -125,7 +116,7 @@ async def get_post(context, author, permlink, observer=None): if observer and context: blacklists_for_user = await Mutes.get_blacklists_for_observer(observer, context) - sql = "---bridge_api.get_post\n" + SQL_TEMPLATE + """ ha_a.name = :author AND hpd_p.permlink = :permlink AND NOT hp.is_deleted """ + sql = "---bridge_api.get_post\n" + SQL_TEMPLATE + """ hp.author = :author AND hp.permlink = :permlink AND NOT hp.is_deleted """ result = await db.query_all(sql, author=author, permlink=permlink) assert len(result) == 1, 'invalid author/permlink or post not found in cache' @@ -153,10 +144,10 @@ async def get_ranked_posts(context, sort, start_author='', start_permlink='', if sort == 'trending': sql = SQL_TEMPLATE + """ NOT hp.is_paidout AND hp.depth = 0 AND NOT hp.is_deleted - %s ORDER BY sc_trend desc, hp.id LIMIT :limit """ + %s ORDER BY hp.sc_trend DESC, hp.id LIMIT :limit """ elif sort == 'hot': sql = SQL_TEMPLATE + """ NOT hp.is_paidout AND hp.depth = 0 AND NOT hp.is_deleted - %s ORDER BY sc_hot desc, hp.id LIMIT :limit """ + %s ORDER BY hp.sc_hot DESC, hp.id LIMIT :limit """ elif sort == 'created': sql = SQL_TEMPLATE + """ hp.depth = 0 AND NOT hp.is_deleted AND NOT hp.is_grayed %s ORDER BY hp.created_at DESC, hp.id LIMIT :limit """ @@ -165,7 +156,7 @@ async def get_ranked_posts(context, sort, start_author='', start_permlink='', AND NOT hp.is_paidout %s ORDER BY hp.promoted DESC, hp.id LIMIT :limit """ elif sort == 'payout': sql = SQL_TEMPLATE + """ NOT hp.is_paidout AND NOT hp.is_deleted %s - AND payout_at BETWEEN now() + interval '12 hours' AND now() + interval '36 hours' + AND hp.payout_at BETWEEN now() + interval '12 hours' AND now() + interval '36 hours' ORDER BY hp.payout DESC, hp.id LIMIT :limit """ elif sort == 'payout_comments': sql = SQL_TEMPLATE + """ NOT hp.is_paidout AND NOT hp.is_deleted AND hp.depth > 0 @@ -201,18 +192,23 @@ async def get_ranked_posts(context, sort, start_author='', start_permlink='', (SELECT id FROM hive_accounts WHERE name = :observer) ) """ elif tag[:5] == 'hive-': if start_author and start_permlink: - sql = sql % """ AND hp.community_id = (SELECT hive_communities.id FROM hive_communities WHERE name = :community_name ) """ + sql = sql % """ AND hp.community_id = (SELECT hive_communities.id FROM hive_communities WHERE name = :community_name ) """ else: - sql = sql % """ AND hive_communities.name = :community_name """ + sql = sql % """ AND hp.community_name = :community_name """ if sort == 'trending' or sort == 'created': - pinned_sql = SQL_TEMPLATE + """ is_pinned AND hive_communities.name = :community_name ORDER BY hp.created_at DESC """ + pinned_sql = SQL_TEMPLATE + """ hp.is_pinned AND hp.community_name = :community_name ORDER BY hp.created_at DESC """ else: if sort in ['payout', 'payout_comments']: sql = sql % """ AND hp.category = :tag """ else: - sql = sql % """ AND htd.tag = :tag """ + sql = sql % """ AND EXISTS + (SELECT NULL + FROM hive_post_tags hpt + INNER JOIN hive_tag_data htd ON hpt.tag_id=htd.id + WHERE hp.id = hpt.post_id AND htd.tag = :tag + ) """ if not observer: observer = '' @@ -292,7 +288,7 @@ async def get_account_posts(context, sort, account, start_author='', start_perml # pylint: disable=unused-variable observer_id = await get_account_id(db, observer) if observer else None # TODO - sql = "---bridge_api.get_account_posts\n " + SQL_TEMPLATE + """ %s """ + sql = "---bridge_api.get_account_posts\n " + SQL_TEMPLATE + """ %s """ if sort == 'blog': ids = await cursor.pids_by_blog(db, account, *start, limit) @@ -302,11 +298,11 @@ async def get_account_posts(context, sort, account, start_author='', start_perml post['reblogged_by'] = [account] return posts elif sort == 'posts': - sql = sql % """ ha_a.name = :account AND NOT hp.is_deleted AND hp.depth = 0 %s ORDER BY hp.id DESC LIMIT :limit""" + sql = sql % """ hp.author = :account AND NOT hp.is_deleted AND hp.depth = 0 %s ORDER BY hp.id DESC LIMIT :limit""" elif sort == 'comments': - sql = sql % """ ha_a.name = :account AND NOT hp.is_deleted AND hp.depth > 0 %s ORDER BY hp.id DESC, depth LIMIT :limit""" + sql = sql % """ hp.author = :account AND NOT hp.is_deleted AND hp.depth > 0 %s ORDER BY hp.id DESC, hp.depth LIMIT :limit""" elif sort == 'payout': - sql = sql % """ ha_a.name = :account AND NOT hp.is_deleted AND NOT hp.is_paidout %s ORDER BY payout DESC, hp.id LIMIT :limit""" + sql = sql % """ hp.author = :account AND NOT hp.is_deleted AND NOT hp.is_paidout %s ORDER BY hp.payout DESC, hp.id LIMIT :limit""" elif sort == 'feed': res = await cursor.pids_by_feed_with_reblog(db, account, *start, limit) return await load_posts_reblogs(context['db'], res) diff --git a/hive/server/condenser_api/cursor.py b/hive/server/condenser_api/cursor.py index bfb1e5f58f50fb7e2f70f14a1c74d766f9f182a2..fa200c11310a3911677a30088d6d416e0fe166c1 100644 --- a/hive/server/condenser_api/cursor.py +++ b/hive/server/condenser_api/cursor.py @@ -284,7 +284,7 @@ async def pids_by_blog_by_index(db, account: str, start_index: int, limit: int = """ if start_index in (-1, 0): - sql = """SELECT COUNT(*) - 1 FROM vw_hive_posts hp + sql = """SELECT COUNT(*) - 1 FROM hive_posts_view hp WHERE hp.author = :account""" start_index = await db.query_one(sql, account=account) if start_index < 0: @@ -296,7 +296,7 @@ async def pids_by_blog_by_index(db, account: str, start_index: int, limit: int = sql = """ SELECT hp.id - FROM vw_hive_posts hp + FROM hive_posts_view hp WHERE hp.author = :account ORDER BY hp.created_at LIMIT :limit diff --git a/hive/server/condenser_api/methods.py b/hive/server/condenser_api/methods.py index fbe9060ee5e605ec832ae75a2475b269deb999a5..bf1a0c0ec63b0022ce0d4f29c98b5ef0a02893e4 100644 --- a/hive/server/condenser_api/methods.py +++ b/hive/server/condenser_api/methods.py @@ -41,7 +41,7 @@ SQL_TEMPLATE = """ hp.created_at, hp.updated_at, hp.rshares, - hp.json as json, + hp.json, hp.is_hidden, hp.is_grayed, hp.total_votes, @@ -59,7 +59,7 @@ SQL_TEMPLATE = """ hp.beneficiaries, hp.url, hp.root_title - FROM vw_hive_posts hp + FROM hive_posts_view hp WHERE """ @@ -226,9 +226,9 @@ async def get_discussions_by(discussion_type, context, start_author: str = '', sql = sql + """ NOT hp.is_deleted """ if discussion_type == 'trending': - sql = sql + """ AND NOT hp.is_paidout %s ORDER BY sc_trend DESC LIMIT :limit """ + sql = sql + """ AND NOT hp.is_paidout %s ORDER BY hp.sc_trend DESC LIMIT :limit """ elif discussion_type == 'hot': - sql = sql + """ AND NOT hp.is_paidout %s ORDER BY sc_hot DESC LIMIT :limit """ + sql = sql + """ AND NOT hp.is_paidout %s ORDER BY hp.sc_hot DESC LIMIT :limit """ elif discussion_type == 'created': sql = sql + """ AND hp.depth = 0 %s ORDER BY hp.created_at DESC LIMIT :limit """ elif discussion_type == 'promoted': @@ -245,14 +245,12 @@ async def get_discussions_by(discussion_type, context, start_author: str = '', if tag[:5] == 'hive-': sql = sql % """ %s AND hp.category = :tag """ else: - sql = sql % """ %s AND hp.post_id IN - (SELECT - post_id - FROM - hive_post_tags hpt - INNER JOIN hive_tag_data htd ON hpt.tag_id=htd.id - WHERE htd.tag = :tag - ) """ + sql = sql % """ %s AND EXISTS + (SELECT NULL + FROM hive_post_tags hpt + INNER JOIN hive_tag_data htd ON hpt.tag_id=htd.id + WHERE hp.id = hpt.post_id AND htd.tag = :tag + ) """ if start_author and start_permlink: if discussion_type == 'trending': @@ -433,7 +431,7 @@ async def get_discussions_by_comments(context, start_author: str = None, start_p """ sql += """ - ORDER BY hp.id DESC, depth LIMIT :limit + ORDER BY hp.id DESC, hp.depth LIMIT :limit """ posts = [] diff --git a/hive/server/condenser_api/objects.py b/hive/server/condenser_api/objects.py index 9a3f3b7b79554e73a5ca6f2cb34d905b6ca86698..efe6486c150408fb3fec302eebb3bd9927cd6dc6 100644 --- a/hive/server/condenser_api/objects.py +++ b/hive/server/condenser_api/objects.py @@ -78,7 +78,7 @@ async def load_posts_keyed(db, ids, truncate_body=0): hp.beneficiaries, hp.url, hp.root_title - FROM vw_hive_posts hp + FROM hive_posts_view hp WHERE hp.id IN :ids""" result = await db.query_all(sql, ids=tuple(ids)) diff --git a/hive/server/database_api/methods.py b/hive/server/database_api/methods.py index fb9654c5322ac9ecfa330fea72b9b58a903cbc65..636a92767b0de8a38ecae50ec9d41af038f2e600 100644 --- a/hive/server/database_api/methods.py +++ b/hive/server/database_api/methods.py @@ -6,48 +6,43 @@ from hive.server.common.objects import condenser_post_object from hive.utils.normalize import rep_to_raw, number_to_json_value, time_string_with_t SQL_TEMPLATE = """ - SELECT hp.id, + SELECT + hp.id, hp.community_id, - ha_a.name as author, - hpd_p.permlink as permlink, - (SELECT title FROM hive_post_data WHERE hive_post_data.id = hp.id) as title, - (SELECT body FROM hive_post_data WHERE hive_post_data.id = hp.id) as body, - (SELECT category FROM hive_category_data WHERE hive_category_data.id = hp.category_id) as category, - depth, - promoted, - payout, - payout_at, - is_paidout, - children, - (0) as votes, + hp.author, + hp.permlink, + hp.title, + hp.body, + hp.category, + hp.depth, + hp.promoted, + hp.payout, + hp.payout_at, + hp.is_paidout, + hp.children, + hp.votes, hp.created_at, - updated_at, - rshares, - (SELECT json FROM hive_post_data WHERE hive_post_data.id = hp.id) as json, - is_hidden, - is_grayed, - total_votes, - flag_weight, - ha_pa.name as parent_author, - hpd_pp.permlink as parent_permlink, - curator_payout_value, - ha_ra.name as root_author, - hpd_rp.permlink as root_permlink, - max_accepted_payout, - percent_hbd, - allow_replies, - allow_votes, - allow_curation_rewards, - beneficiaries, - url, - root_title - FROM hive_posts hp - INNER JOIN hive_accounts ha_a ON ha_a.id = hp.author_id - INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id - INNER JOIN hive_accounts ha_pa ON ha_pa.id = hp.parent_author_id - INNER JOIN hive_permlink_data hpd_pp ON hpd_pp.id = hp.parent_permlink_id - INNER JOIN hive_accounts ha_ra ON ha_ra.id = hp.root_author_id - INNER JOIN hive_permlink_data hpd_rp ON hpd_rp.id = hp.root_permlink_id + hp.updated_at, + hp.rshares, + hp.json, + hp.is_hidden, + hp.is_grayed, + hp.total_votes, + hp.flag_weight, + hp.parent_author, + hp.parent_permlink, + hp.curator_payout_value, + hp.root_author, + hp.root_permlink, + hp.max_accepted_payout, + hp.percent_hbd, + hp.allow_replies, + hp.allow_votes, + hp.allow_curation_rewards, + hp.beneficiaries, + hp.url, + hp.root_title + FROM hive_posts_view hp WHERE """ @@ -140,7 +135,7 @@ async def list_comments(context, start: list, limit: int, order: str): post_id = await get_post_id_by_author_and_permlink(db, child_author, child_permlink, 1) sql = str(SQL_TEMPLATE) - sql += "ha_pa.name >= :parent_author AND hp.updated_at >= :updated_at AND hp.id >= :post_id ORDER BY ha_pa.name ASC, updated_at ASC, hp.id ASC LIMIT :limit" + sql += "hp.parent_author >= :parent_author AND hp.updated_at >= :updated_at AND hp.id >= :post_id ORDER BY hp.parent_author ASC, hp.updated_at ASC, hp.id ASC LIMIT :limit" result = await db.query_all(sql, parent_author=start[0], updated_at=start[1], post_id=post_id, limit=limit) for row in result: @@ -159,7 +154,7 @@ async def list_comments(context, start: list, limit: int, order: str): post_id = await get_post_id_by_author_and_permlink(db, author, permlink, 1) sql = str(SQL_TEMPLATE) - sql += "ha_a.name >= :author AND hp.updated_at >= :updated_at AND hp.id >= :post_id ORDER BY ha_a.name ASC, hp.updated_at ASC, hp.id ASC LIMIT :limit" + sql += "hp.author >= :author AND hp.updated_at >= :updated_at AND hp.id >= :post_id ORDER BY hp.author ASC, hp.updated_at ASC, hp.id ASC LIMIT :limit" result = await db.query_all(sql, author=start[0], updated_at=start[1], post_id=post_id, limit=limit) for row in result: @@ -184,7 +179,7 @@ async def find_comments(context, start: list, limit: int, order: str): for arg in start: if idx > 0: sql += " OR " - sql += "(ha_a.name = '{}' AND hpd_p.permlink = '{}')".format(arg[0], arg[1]) + sql += "(hp.author = '{}' AND hp.permlink = '{}')".format(arg[0], arg[1]) idx += 1 result = await db.query_all(sql) @@ -218,7 +213,7 @@ async def find_votes(context, params: dict, votes_presentation = VotesPresentati num_changes, reputation FROM - wv_hive_votes_accounts_permlinks + hive_votes_accounts_permlinks_view WHERE author = :author AND permlink = :permlink ORDER BY id