From 90ea3fb45bd55566b9571e48f27385c01523f9a9 Mon Sep 17 00:00:00 2001
From: Martin Lees <drov0@users.noreply.github.com>
Date: Mon, 11 Sep 2023 15:01:32 -0400
Subject: [PATCH] Fixed query issue where subcomments were not marked as part
 of a community

---
 hive/db/sql_scripts/hive_post_operations.sql  | 36 ++++++++++---------
 .../mock_block_data_community.json            | 12 +++++++
 2 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/hive/db/sql_scripts/hive_post_operations.sql b/hive/db/sql_scripts/hive_post_operations.sql
index 9c29e9ccf..e0115ae8e 100644
--- a/hive/db/sql_scripts/hive_post_operations.sql
+++ b/hive/db/sql_scripts/hive_post_operations.sql
@@ -27,7 +27,7 @@ $function$;
 
 
 DROP FUNCTION IF EXISTS hivemind_app.process_community_post;
-CREATE OR REPLACE FUNCTION hivemind_app.process_community_post(_block_num hivemind_app.hive_posts.block_num%TYPE, _community_support_start_block hivemind_app.hive_posts.block_num%TYPE, _community_id hivemind_app.hive_posts.community_id%TYPE, _community_name hivemind_app.hive_permlink_data.permlink%TYPE, _author_id hivemind_app.hive_posts.author_id%TYPE, is_comment bool)
+CREATE OR REPLACE FUNCTION hivemind_app.process_community_post(_block_num hivemind_app.hive_posts.block_num%TYPE, _community_support_start_block hivemind_app.hive_posts.block_num%TYPE, _parent_permlink hivemind_app.hive_permlink_data.permlink%TYPE, _author_id hivemind_app.hive_posts.author_id%TYPE, is_comment bool)
 RETURNS TABLE(is_muted bool, community_id hivemind_app.hive_posts.community_id%TYPE)
 LANGUAGE plpgsql
 as
@@ -46,25 +46,29 @@ BEGIN
             __is_muted := FALSE;
             __community_id := NULL;
         ELSE
-            IF _community_id IS NOT NULL THEN
-                SELECT type_id INTO __community_type_id FROM hivemind_app.hive_communities WHERE id = _community_id;
-                __community_id = _community_id;
+            IF is_comment = TRUE THEN
+                SELECT hc.type_id, hc.id INTO __community_type_id, __community_id
+                FROM hivemind_app.hive_permlink_data
+                    JOIN hivemind_app.hive_posts ON hivemind_app.hive_permlink_data.id = hivemind_app.hive_posts.permlink_id
+                    JOIN hivemind_app.hive_communities hc ON hivemind_app.hive_posts.community_id = hc.id
+                WHERE hivemind_app.hive_permlink_data.permlink = _parent_permlink;
             ELSE
-                SELECT type_id, id INTO __community_type_id, _community_id from hivemind_app.hive_communities where name = _community_name;
-                __community_id = _community_id;
+                SELECT type_id, id INTO __community_type_id, __community_id from hivemind_app.hive_communities where name = _parent_permlink;
             END IF;
 
-            IF __community_type_id = __community_type_topic THEN
-                __is_muted := FALSE;
-            ELSE
-                IF __community_type_id = __community_type_journal AND is_comment = TRUE THEN
+            IF __community_id IS NOT NULL THEN
+                IF __community_type_id = __community_type_topic THEN
                     __is_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_muted := FALSE;
-                    ELSIF __community_type_id = __community_type_council AND __role_id IS NOT NULL AND __role_id >= __member_role THEN
+                    IF __community_type_id = __community_type_journal AND is_comment = TRUE THEN
                         __is_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_muted := FALSE;
+                        ELSIF __community_type_id = __community_type_council AND __role_id IS NOT NULL AND __role_id >= __member_role THEN
+                            __is_muted := FALSE;
+                        END IF;
                     END IF;
                 END IF;
             END IF;
@@ -124,7 +128,7 @@ if _parent_author != '' THEN
         _block_num as block_num, _block_num as block_num_created
   FROM hivemind_app.hive_accounts ha,
         hivemind_app.hive_permlink_data hpd,
-        hivemind_app.process_community_post(_block_num, _community_support_start_block, NULL, _parent_permlink, ha.id, TRUE) pcp,
+        hivemind_app.process_community_post(_block_num, _community_support_start_block, _parent_permlink, ha.id, TRUE) pcp,
         hivemind_app.hive_posts php
   INNER JOIN hivemind_app.hive_accounts pha ON pha.id = php.author_id
   INNER JOIN hivemind_app.hive_permlink_data phpd ON phpd.id = php.permlink_id
@@ -169,7 +173,7 @@ ELSE
           FROM hivemind_app.prepare_tags( ARRAY_APPEND(_metadata_tags, _parent_permlink ) )
         ) as tags_ids
   FROM hivemind_app.hive_accounts ha,
-       hivemind_app.process_community_post(_block_num, _community_support_start_block, NULL, _parent_permlink, author_id, false) pcp,
+       hivemind_app.process_community_post(_block_num, _community_support_start_block, _parent_permlink, author_id, FALSE) pcp,
         hivemind_app.hive_permlink_data hpd
   WHERE ha.name = _author and hpd.permlink = _permlink
 
diff --git a/mock_data/block_data/community_op/mock_block_data_community.json b/mock_data/block_data/community_op/mock_block_data_community.json
index 94b2a0e00..3b0c6f49e 100644
--- a/mock_data/block_data/community_op/mock_block_data_community.json
+++ b/mock_data/block_data/community_op/mock_block_data_community.json
@@ -5089,6 +5089,18 @@
               "json_metadata": "{}"
             }
           },
+          {
+            "type": "comment_operation",
+            "value": {
+              "parent_author": "ismember",
+              "parent_permlink": "re-ismember-hive-111119",
+              "author": "ismember",
+              "permlink": "re-re-ismember-hive-111119",
+              "title": "ismember, depth 2 comment, This comment will not be muted",
+              "body": "lorem ipsum",
+              "json_metadata": "{}"
+            }
+          },
           {
             "type": "comment_operation",
             "value": {
-- 
GitLab