From 70ebb9241a58b1786e3de07142460bf0c154d400 Mon Sep 17 00:00:00 2001 From: Howo Date: Fri, 28 Nov 2025 17:38:37 -0500 Subject: [PATCH] Revert "Return the role directly in process_hive_post_operation to optimize community muted role check" This reverts commit d218ba2e5e523b8e83e34668e7b3b1751e4c1474. --- hive/db/sql_scripts/hive_post_operations.sql | 31 ++++++-------------- hive/indexer/community.py | 22 +++++++++++++- hive/indexer/posts.py | 8 ++--- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/hive/db/sql_scripts/hive_post_operations.sql b/hive/db/sql_scripts/hive_post_operations.sql index 3635b6f4a..ea1523954 100644 --- a/hive/db/sql_scripts/hive_post_operations.sql +++ b/hive/db/sql_scripts/hive_post_operations.sql @@ -44,8 +44,7 @@ DROP TYPE IF EXISTS hivemind_app.process_community_post_result CASCADE; CREATE TYPE hivemind_app.process_community_post_result AS ( is_post_muted bool, community_id integer, -- hivemind_app.hive_posts.community_id%TYPE - post_muted_reasons INTEGER, - role_id SMALLINT + post_muted_reasons INTEGER ); DROP FUNCTION IF EXISTS hivemind_app.process_community_post; @@ -68,7 +67,6 @@ BEGIN IF _block_num < _community_support_start_block THEN __is_post_muted := FALSE; __community_id := NULL; - __role_id := NULL; ELSE IF _is_comment = TRUE THEN SELECT type_id, id INTO __community_type_id, __community_id from hivemind_app.hive_communities where id = _community_id; @@ -77,14 +75,13 @@ BEGIN END IF; IF __community_id IS NOT NULL THEN - select role_id into __role_id from hivemind_app.hive_roles where hivemind_app.hive_roles.community_id = __community_id AND account_id = _author_id; - IF __community_type_id = __community_type_topic THEN __is_post_muted := FALSE; ELSE IF __community_type_id = __community_type_journal AND _is_comment = TRUE THEN __is_post_muted := FALSE; ELSE + select role_id into __role_id from hivemind_app.hive_roles where hivemind_app.hive_roles.community_id = __community_id AND account_id = _author_id; IF __community_type_id = __community_type_journal AND _is_comment = FALSE AND __role_id IS NOT NULL AND __role_id >= __member_role THEN __is_post_muted := FALSE; ELSIF __community_type_id = __community_type_council AND __role_id IS NOT NULL AND __role_id >= __member_role THEN @@ -97,7 +94,6 @@ BEGIN END IF; ELSE __is_post_muted := FALSE; - __role_id := NULL; END IF; -- __is_post_muted can be TRUE here if it's a comment and its parent is muted @@ -109,13 +105,7 @@ BEGIN END IF; - - -- if role is NULL it means the user is type GUEST - IF __role_id IS NULL THEN - __role_id = 0; - END IF; - - RETURN (__is_post_muted, __community_id, hivemind_app.encode_bitwise_mask(__post_muted_reasons), __role_id)::hivemind_app.process_community_post_result; + RETURN (__is_post_muted, __community_id, hivemind_app.encode_bitwise_mask(__post_muted_reasons))::hivemind_app.process_community_post_result; END; $$ STABLE; @@ -132,7 +122,7 @@ CREATE OR REPLACE FUNCTION hivemind_app.process_hive_post_operation( in _metadata_tags VARCHAR[]) RETURNS TABLE (is_new_post boolean, id hivemind_app.hive_posts.id%TYPE, author_id hivemind_app.hive_posts.author_id%TYPE, permlink_id hivemind_app.hive_posts.permlink_id%TYPE, post_category hivemind_app.hive_category_data.category%TYPE, parent_id hivemind_app.hive_posts.parent_id%TYPE, parent_author_id hivemind_app.hive_posts.author_id%TYPE, community_id hivemind_app.hive_posts.community_id%TYPE, - is_valid hivemind_app.hive_posts.is_valid%TYPE, is_post_muted hivemind_app.hive_posts.is_muted%TYPE, depth hivemind_app.hive_posts.depth%TYPE, muted_reasons hivemind_app.hive_posts.muted_reasons%TYPE, role_id SMALLINT) + is_valid hivemind_app.hive_posts.is_valid%TYPE, is_post_muted hivemind_app.hive_posts.is_muted%TYPE, depth hivemind_app.hive_posts.depth%TYPE, muted_reasons hivemind_app.hive_posts.muted_reasons%TYPE) LANGUAGE plpgsql AS $function$ @@ -170,8 +160,7 @@ BEGIN s.counter_deleted, s.block_num, s.block_num_created, - (s.composite).post_muted_reasons, - (s.composite).role_id + (s.composite).post_muted_reasons FROM ( SELECT hivemind_app.process_community_post(_block_num, _community_support_start_block, _parent_permlink, ha.id, TRUE, php.is_muted, php.community_id) as composite, @@ -233,7 +222,7 @@ BEGIN updated_at = _date, active = _date, block_num = _block_num - RETURNING (xmax = 0) as is_new_post, hp.id, hp.author_id, hp.permlink_id, (SELECT hcd.category FROM hivemind_app.hive_category_data hcd WHERE hcd.id = hp.category_id) as post_category, hp.parent_id, (SELECT s.parent_author_id FROM selected_posts AS s) AS parent_author_id, hp.community_id, hp.is_valid, hp.is_muted, hp.depth, hp.muted_reasons, (SELECT s.role_id FROM selected_posts AS s) AS role_id + RETURNING (xmax = 0) as is_new_post, hp.id, hp.author_id, hp.permlink_id, (SELECT hcd.category FROM hivemind_app.hive_category_data hcd WHERE hcd.id = hp.category_id) as post_category, hp.parent_id, (SELECT s.parent_author_id FROM selected_posts AS s) AS parent_author_id, hp.community_id, hp.is_valid, hp.is_muted, hp.depth, hp.muted_reasons ; ELSE INSERT INTO hivemind_app.hive_category_data @@ -264,8 +253,7 @@ BEGIN s.counter_deleted, s.block_num, s.block_num_created, - (s.composite).post_muted_reasons, - (s.composite).role_id + (s.composite).post_muted_reasons FROM ( SELECT hivemind_app.process_community_post(_block_num, _community_support_start_block, _parent_permlink, ha.id, FALSE,FALSE, NULL) as composite, @@ -321,7 +309,7 @@ BEGIN updated_at = _date, active = _date, block_num = _block_num - RETURNING (xmax = 0) as is_new_post, hp.id, hp.author_id, hp.permlink_id, _parent_permlink as post_category, hp.parent_id, hp.community_id, hp.is_valid, hp.is_muted, hp.depth, hp.muted_reasons, (SELECT pdi.role_id FROM posts_data_to_insert AS pdi) AS role_id + RETURNING (xmax = 0) as is_new_post, hp.id, hp.author_id, hp.permlink_id, _parent_permlink as post_category, hp.parent_id, hp.community_id, hp.is_valid, hp.is_muted, hp.depth, hp.muted_reasons ) -- WITH inserted_post , tagsid_and_posts AS MATERIALIZED ( SELECT prepare_tags FROM hivemind_app.prepare_tags( ARRAY_APPEND(_metadata_tags, _parent_permlink ) ) @@ -354,8 +342,7 @@ BEGIN ip.is_valid, ip.is_muted, ip.depth, - ip.muted_reasons, - ip.role_id + ip.muted_reasons FROM inserted_post as ip; END IF; END diff --git a/hive/indexer/community.py b/hive/indexer/community.py index 8d72f08e6..55e262904 100644 --- a/hive/indexer/community.py +++ b/hive/indexer/community.py @@ -215,7 +215,23 @@ class Community: return cid @classmethod - def is_post_valid(cls, role): + def get_user_role(cls, community_id, account_id): + """Get user role within a specific community.""" + + return ( + DbAdapterHolder.common_block_processing_db().query_one( + f"""SELECT role_id FROM {SCHEMA_NAME}.hive_roles + WHERE community_id = :community_id + AND account_id = :account_id + LIMIT 1""", + community_id=community_id, + account_id=account_id, + ) + or Role.guest.value + ) + + @classmethod + def is_post_valid(cls, community_id, comment_op: dict): """Given a new post/comment, check if valid as per community rules For a comment to be valid, these conditions apply: @@ -226,6 +242,10 @@ class Community: via the sql function process_community_post """ + assert community_id, 'no community_id' + account_id = Accounts.get_id(comment_op['author']) + role = cls.get_user_role(community_id, account_id) + # TODO: check `nsfw` tag requirement #267 # TODO: (1.5) check that beneficiaries are valid diff --git a/hive/indexer/posts.py b/hive/indexer/posts.py index 10a2fd2f4..8dd95a792 100644 --- a/hive/indexer/posts.py +++ b/hive/indexer/posts.py @@ -60,7 +60,7 @@ class Posts(DbAdapterHolder): tags.append(tag) # No escaping needed due to used sqlalchemy formatting features sql = f""" - SELECT is_new_post, id, author_id, permlink_id, post_category, parent_id, parent_author_id, community_id, is_valid, is_post_muted, depth, muted_reasons, role_id + SELECT is_new_post, id, author_id, permlink_id, post_category, parent_id, parent_author_id, community_id, is_valid, is_post_muted, depth, muted_reasons FROM {SCHEMA_NAME}.process_hive_post_operation((:author)::varchar, (:permlink)::varchar, (:parent_author)::varchar, (:parent_permlink)::varchar, (:date)::timestamp, (:community_support_start_block)::integer, (:block_num)::integer, (:tags)::VARCHAR[]); """ @@ -81,7 +81,7 @@ class Posts(DbAdapterHolder): return result = dict(row) - error = cls._verify_post_against_community(op, result['community_id'], result['is_valid'], result['role_id']) + error = cls._verify_post_against_community(op, result['community_id'], result['is_valid']) is_new_post = result['is_new_post'] parent_author = op.get('parent_author') @@ -415,10 +415,10 @@ class Posts(DbAdapterHolder): Votes.drop_votes_of_deleted_comment(op) @classmethod - def _verify_post_against_community(cls, op, community_id, is_valid, role_id): + def _verify_post_against_community(cls, op, community_id, is_valid): error = None # is_valid is always set to true for now - if community_id and is_valid and not Community.is_post_valid(role_id): + if community_id and is_valid and not Community.is_post_valid(community_id, op): error = 'not allowed to post in this community (role is muted)' # is_valid = False # TODO: reserved for future blacklist status? return error -- GitLab