From d1d08f626ced15c2db0fe0c5b87f8b8694b82c7d Mon Sep 17 00:00:00 2001
From: Martin Lees <drov0@users.noreply.github.com>
Date: Sun, 13 Mar 2022 17:01:55 -0400
Subject: [PATCH] added the flow + mocked ops

---
 hive/db/sql_scripts/hive_post_operations.sql  |  45 ++-
 hive/indexer/posts.py                         |  10 +-
 mock_data/block_data/community_op/flow.txt    |  16 +
 .../mock_block_data_community.json            | 286 +++++++++++++++++-
 .../mock_block_data_community_test.json       | 269 ----------------
 5 files changed, 348 insertions(+), 278 deletions(-)
 delete mode 100644 mock_data/block_data/community_op/mock_block_data_community_test.json

diff --git a/hive/db/sql_scripts/hive_post_operations.sql b/hive/db/sql_scripts/hive_post_operations.sql
index 1e6ee77fe..026b0a31a 100644
--- a/hive/db/sql_scripts/hive_post_operations.sql
+++ b/hive/db/sql_scripts/hive_post_operations.sql
@@ -25,6 +25,38 @@ BEGIN
 END
 $function$;
 
+DROP FUNCTION IF EXISTS is_user_allowed_to_post_to_community;
+CREATE OR REPLACE FUNCTION is_user_allowed_to_post_to_community(_community_id hive_posts.community_id%TYPE, _author_id hive_posts.author_id%TYPE, is_comment bool)
+RETURNS bool
+LANGUAGE plpgsql
+as
+    $$
+declare
+        _community_type_id SMALLINT;
+        _role_id SMALLINT;
+        _member_role CONSTANT SMALLINT := 2;
+        _community_type_topic CONSTANT SMALLINT := 1;
+        _community_type_journal CONSTANT SMALLINT := 2;
+        _community_type_council CONSTANT SMALLINT := 3;
+BEGIN
+        SELECT type_id INTO _community_type_id FROM hive_communities WHERE id = _community_id;
+        IF _community_type_id = _community_type_topic THEN
+            return true; -- Community type 1 allows everyone to post & comment
+        ELSE
+            IF _community_type_id = _community_type_journal AND is_comment = TRUE THEN
+                RETURN TRUE; -- Community type journal allows everyone to comment
+            END IF;
+            select role_id into _role_id from hive_roles where 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
+                RETURN TRUE; -- You have to be at least a member to post
+            ELSIF _community_type_id = _community_type_council AND _role_id IS NOT NULL AND _role_id >= _member_role THEN
+                RETURN TRUE; -- You have to be at least a member to post or comment
+            END IF;
+        END IF;
+        RETURN FALSE;
+    END;
+$$;
+
 DROP FUNCTION IF EXISTS process_hive_post_operation;
 ;
 CREATE OR REPLACE FUNCTION process_hive_post_operation(
@@ -68,7 +100,12 @@ if _parent_author != '' THEN
           WHEN 0 THEN php.id
           ELSE php.root_id
         END) AS root_id,
-      php.is_muted AS is_muted, php.is_valid AS is_valid,
+      (CASE
+          WHEN community_id is not null and php.is_muted = false THEN
+            is_user_allowed_to_post_to_community(community_id, author_id, true)
+          ELSE php.is_muted
+      END) AS is_muted,
+      php.is_valid AS is_valid,
       ha.id AS author_id, hpd.id AS permlink_id, _date AS created_at,
       _date AS updated_at,
       calculate_time_part_of_hot(_date) AS sc_hot,
@@ -113,7 +150,11 @@ ELSE
       END) AS community_id,
       (SELECT hcg.id FROM hive_category_data hcg WHERE hcg.category = _parent_permlink) AS category_id,
       0 as root_id, -- will use id as root one if no parent
-      false AS is_muted, true AS is_valid,
+      (CASE
+          WHEN community_id is not null THEN
+            is_user_allowed_to_post_to_community(community_id, author_id, false)
+          ELSE false
+      END) AS is_muted, true AS is_valid,
       ha.id AS author_id, hpd.id AS permlink_id, _date AS created_at,
       _date AS updated_at,
       calculate_time_part_of_hot(_date) AS sc_hot,
diff --git a/hive/indexer/posts.py b/hive/indexer/posts.py
index eb512a40d..b489a39df 100644
--- a/hive/indexer/posts.py
+++ b/hive/indexer/posts.py
@@ -83,8 +83,7 @@ class Posts(DbAdapterHolder):
             return
         result = dict(row)
 
-        # TODO we need to enhance checking related community post validation and honor is_muted.
-        error = cls._verify_post_against_community(op, result['community_id'], result['is_valid'], result['is_muted'])
+        error = cls._verify_post_against_community(op, result['community_id'], result['is_valid'])
 
         img_url = None
         if 'image' in md:
@@ -209,8 +208,8 @@ class Posts(DbAdapterHolder):
             # total payout for comment
             #comment_author_reward     = None
             #curators_vesting_payout   = None
-            total_payout_value        = None;
-            curator_payout_value      = None;
+            total_payout_value        = None
+            curator_payout_value      = None
             #beneficiary_payout_value  = None;
 
             payout                    = None
@@ -369,12 +368,11 @@ class Posts(DbAdapterHolder):
         Votes.drop_votes_of_deleted_comment(op)
 
     @classmethod
-    def _verify_post_against_community(cls, op, community_id, is_valid, is_muted):
+    def _verify_post_against_community(cls, op, community_id, is_valid):
         error = None
         if community_id and is_valid and not Community.is_post_valid(community_id, op):
             error = 'not authorized'
             #is_valid = False # TODO: reserved for future blacklist status?
-            is_muted = True
         return error
 
     @classmethod
diff --git a/mock_data/block_data/community_op/flow.txt b/mock_data/block_data/community_op/flow.txt
index 2e087cd58..bb9cd4b83 100644
--- a/mock_data/block_data/community_op/flow.txt
+++ b/mock_data/block_data/community_op/flow.txt
@@ -12,6 +12,9 @@ account_create_operation( `hive-103459` )
 account_create_operation( `hive-188204` )
 account_create_operation( `hive-149232` )
 account_create_operation( `hive-104647` )
+account_create_operation( `hive-111111` )
+account_create_operation( `hive-222222` )
+account_create_operation( `hive-333333` )
 comment_operation( `hive-135485`, `test-safari`, `secrets1`)
 comment_operation( `hive-135485`, `test-safari`, `secrets2`)
 transfer_opearation( `test-safari`, `null`, `0.010666 HBD`, `@test-safari/secrets2` ) - post promotion (with bad amount precision to see what happens - rounding occurs)
@@ -22,6 +25,19 @@ comment_operation( `hive-117600`, `test-safari`, `secrets6`)
 custom_json_operation("[\"setRole\",{\"community\":\"hive-135485\",\"account\":\"test-safari\",\"role\":\"admin\"}]")
 comment_operation( `test-safari`, `muted-and-recreated`)
 comment_operation( `test-safari`, `muted-and-edited`)
+comment_operation( `test-safari`, `displayed`)
+comment_operation( `test-safari`, `hidden1`)
+comment_operation( `test-safari`, `hidden2`)
+custom_json_operation("[\"setRole\",{\"community\":\"hive-222222\",\"account\":\"test-safari\",\"role\":\"member\"}]")
+custom_json_operation("[\"setRole\",{\"community\":\"hive-333333\",\"account\":\"test-safari\",\"role\":\"member\"}]")
+comment_operation( `test-safari`, `display2`)
+comment_operation( `blocktrades`, `display_comment`)
+comment_operation( `test-safari`, `display3`)
+comment_operation( `test-safari`, `display_comment1`)
+comment_operation( `blocktrades`, `hidden_comment`)
+comment_operation( `blocktrades`, `hidden`)
+custom_json_operation("[\"updateProps\",{\"community\":\"hive-222222\",\"props\":{\"type_id\":1,\"title\":\"World News\",\"about\":\"A place for major news from around the world.\",\"is_nsfw\":true,\"description\":\"\",\"flag_text\":\"\"}}]")
+comment_operation( `blocktrades`, `display4`)
 ***block 4998002***
 custom_json_operation("[\"updateProps\",{\"community\":\"hive-135485\",\"props\":{\"title\":\"World News\",\"about\":\"A place for major news from around the world.\",\"is_nsfw\":true,\"description\":\"\",\"flag_text\":\"\"}}]")
 custom_json_operation("[\"setRole\",{\"community\":\"hive-135485\",\"account\":\"blocktrades\",\"role\":\"mod\"}]")
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 2ce026a1d..7771a4595 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
@@ -565,7 +565,127 @@
               "json_metadata": "",
               "extensions": []
             }
-          },          
+          },
+          {
+            "type": "account_create_operation",
+            "value": {
+              "creator": "test-safari",
+              "new_account_name": "hive-111111",
+              "owner": {
+                "weight_threshold": 1,
+                "account_auths": [],
+                "key_auths": [
+                  [
+                    "",
+                    1
+                  ]
+                ]
+              },
+              "active": {
+                "weight_threshold": 1,
+                "account_auths": [],
+                "key_auths": [
+                  [
+                    "",
+                    1
+                  ]
+                ]
+              },
+              "posting": {
+                "weight_threshold": 1,
+                "account_auths": [],
+                "key_auths": [
+                  [
+                    "",
+                    1
+                  ]
+                ]
+              },
+              "memo_key": "",
+              "json_metadata": "",
+              "extensions": []
+            }
+          },
+          {
+            "type": "account_create_operation",
+            "value": {
+              "creator": "test-safari",
+              "new_account_name": "hive-222222",
+              "owner": {
+                "weight_threshold": 1,
+                "account_auths": [],
+                "key_auths": [
+                  [
+                    "",
+                    1
+                  ]
+                ]
+              },
+              "active": {
+                "weight_threshold": 1,
+                "account_auths": [],
+                "key_auths": [
+                  [
+                    "",
+                    1
+                  ]
+                ]
+              },
+              "posting": {
+                "weight_threshold": 1,
+                "account_auths": [],
+                "key_auths": [
+                  [
+                    "",
+                    1
+                  ]
+                ]
+              },
+              "memo_key": "",
+              "json_metadata": "",
+              "extensions": []
+            }
+          },
+          {
+            "type": "account_create_operation",
+            "value": {
+              "creator": "test-safari",
+              "new_account_name": "hive-333333",
+              "owner": {
+                "weight_threshold": 1,
+                "account_auths": [],
+                "key_auths": [
+                  [
+                    "",
+                    1
+                  ]
+                ]
+              },
+              "active": {
+                "weight_threshold": 1,
+                "account_auths": [],
+                "key_auths": [
+                  [
+                    "",
+                    1
+                  ]
+                ]
+              },
+              "posting": {
+                "weight_threshold": 1,
+                "account_auths": [],
+                "key_auths": [
+                  [
+                    "",
+                    1
+                  ]
+                ]
+              },
+              "memo_key": "",
+              "json_metadata": "",
+              "extensions": []
+            }
+          },
           {
             "type": "comment_operation",
             "value": {
@@ -681,6 +801,164 @@
               "body": "This test is muted in community after creation, then edited - all in the same block for good measure. Test checks if muting survives edit (it should).",
               "json_metadata": "{}"
             }
+          },
+      {
+        "ref_block_num": 100000,
+        "ref_block_prefix": 0,
+        "expiration": "2020-03-23T12:08:00",
+        "operations": [
+          {
+            "type": "comment_operation",
+            "value": {
+              "parent_author": "",
+              "parent_permlink": "hive-111111",
+              "author": "test-safari",
+              "permlink": "displayed",
+              "title": "normal post",
+              "body": "This post is displayed",
+              "json_metadata": "{}"
+            }
+          },
+          {
+            "type": "comment_operation",
+            "value": {
+              "parent_author": "",
+              "parent_permlink": "hive-222222",
+              "author": "test-safari",
+              "permlink": "hidden1",
+              "title": "This post is hidden",
+              "body": "This post is hidden",
+              "json_metadata": "{}"
+            }
+          },
+          {
+            "type": "comment_operation",
+            "value": {
+              "parent_author": "",
+              "parent_permlink": "hive-333333",
+              "author": "test-safari",
+              "permlink": "hidden2",
+              "title": "This post is hidden",
+              "body": "This post is hidden",
+              "json_metadata": "{}"
+            }
+          },
+          {
+            "type": "custom_json_operation",
+            "value": {
+              "required_auths": [],
+              "required_posting_auths": [
+                "hive-222222"
+              ],
+              "id": "community",
+              "json": "[\"setRole\",{\"community\":\"hive-222222\",\"account\":\"test-safari\",\"role\":\"member\"}]"
+            }
+          },
+          {
+            "type": "custom_json_operation",
+            "value": {
+              "required_auths": [],
+              "required_posting_auths": [
+                "hive-333333"
+              ],
+              "id": "community",
+              "json": "[\"setRole\",{\"community\":\"hive-333333\",\"account\":\"test-safari\",\"role\":\"member\"}]"
+            }
+          },
+          {
+            "type": "comment_operation",
+            "value": {
+              "parent_author": "",
+              "parent_permlink": "hive-222222",
+              "author": "test-safari",
+              "permlink": "display2",
+              "title": "This post is shown because I am a member",
+              "body": "This post is shown because I am a member",
+              "json_metadata": "{}"
+            }
+          },
+          {
+            "type": "comment_operation",
+            "value": {
+              "parent_author": "test-safari",
+              "parent_permlink": "display2",
+              "author": "blocktrades",
+              "permlink": "display_comment",
+              "title": "This comment is shown even though I'm a guest because community type 2",
+              "body": "hello",
+              "json_metadata": "{}"
+            }
+          },
+          {
+            "type": "comment_operation",
+            "value": {
+              "parent_author": "",
+              "parent_permlink": "hive-333333",
+              "author": "test-safari",
+              "permlink": "display3",
+              "title": "This post is shown because I am a member",
+              "body": "Hello",
+              "json_metadata": "{}"
+            }
+          },
+          {
+            "type": "comment_operation",
+            "value": {
+              "parent_author": "test-safari",
+              "parent_permlink": "display3",
+              "author": "test-safari",
+              "permlink": "display_comment1",
+              "title": "This comment is shown because member and community type 3",
+              "body": "hello",
+              "json_metadata": "{}"
+            }
+          },
+          {
+            "type": "comment_operation",
+            "value": {
+              "parent_author": "test-safari",
+              "parent_permlink": "display3",
+              "author": "blocktrades",
+              "permlink": "hidden_comment",
+              "title": "This comment is hidden because guest and community type 3",
+              "body": "hello",
+              "json_metadata": "{}"
+            }
+          },
+          {
+            "type": "comment_operation",
+            "value": {
+              "parent_author": "",
+              "parent_permlink": "hive-222222",
+              "author": "blocktrades",
+              "permlink": "hidden",
+              "title": "This post is hidden  because the community type is now 2",
+              "body": "Hello",
+              "json_metadata": "{}"
+            }
+          },
+          {
+            "type": "custom_json_operation",
+            "value": {
+              "required_auths": [],
+              "required_posting_auths": [
+                "hive-222222"
+              ],
+              "id": "community",
+              "json": "[\"updateProps\",{\"community\":\"hive-222222\",\"props\":{\"type_id\":1,\"title\":\"World News\",\"about\":\"A place for major news from around the world.\",\"is_nsfw\":true,\"description\":\"\",\"flag_text\":\"\"}}]"
+            }
+          },
+          {
+            "type": "comment_operation",
+            "value": {
+              "parent_author": "",
+              "parent_permlink": "hive-222222",
+              "author": "blocktrades",
+              "permlink": "display4",
+              "title": "This post is shown because the community type is now 1",
+              "body": "Hello",
+              "json_metadata": "{}"
+            }
           }
         ],
         "extensions": [],
@@ -688,6 +966,12 @@
           ""
         ]
       }
+        ],
+        "extensions": [],
+        "signatures": [
+          ""
+        ]
+      }
     ]
   },
   "4998002": {
diff --git a/mock_data/block_data/community_op/mock_block_data_community_test.json b/mock_data/block_data/community_op/mock_block_data_community_test.json
deleted file mode 100644
index 5bf1d1525..000000000
--- a/mock_data/block_data/community_op/mock_block_data_community_test.json
+++ /dev/null
@@ -1,269 +0,0 @@
-{
-  "4999997": {
-    "previous": "004c4b3c51ee947feceeb1812702816114aea6e4",
-    "timestamp": "2016-09-15 19:47:12.000000",
-    "transactions": [
-      {
-        "ref_block_num": 100000,
-        "ref_block_prefix": 0,
-        "expiration": "2020-03-23T12:08:00",
-        "operations": [
-          {
-            "type": "account_create_operation",
-            "value": {
-              "creator": "test-safari",
-              "new_account_name": "hive-111111",
-              "owner": {
-                "weight_threshold": 1,
-                "account_auths": [],
-                "key_auths": [
-                  [
-                    "",
-                    1
-                  ]
-                ]
-              },
-              "active": {
-                "weight_threshold": 1,
-                "account_auths": [],
-                "key_auths": [
-                  [
-                    "",
-                    1
-                  ]
-                ]
-              },
-              "posting": {
-                "weight_threshold": 1,
-                "account_auths": [],
-                "key_auths": [
-                  [
-                    "",
-                    1
-                  ]
-                ]
-              },
-              "memo_key": "",
-              "json_metadata": "",
-              "extensions": []
-            }
-          },
-          {
-            "type": "account_create_operation",
-            "value": {
-              "creator": "test-safari",
-              "new_account_name": "hive-222222",
-              "owner": {
-                "weight_threshold": 1,
-                "account_auths": [],
-                "key_auths": [
-                  [
-                    "",
-                    1
-                  ]
-                ]
-              },
-              "active": {
-                "weight_threshold": 1,
-                "account_auths": [],
-                "key_auths": [
-                  [
-                    "",
-                    1
-                  ]
-                ]
-              },
-              "posting": {
-                "weight_threshold": 1,
-                "account_auths": [],
-                "key_auths": [
-                  [
-                    "",
-                    1
-                  ]
-                ]
-              },
-              "memo_key": "",
-              "json_metadata": "",
-              "extensions": []
-            }
-          },
-          {
-            "type": "account_create_operation",
-            "value": {
-              "creator": "test-safari",
-              "new_account_name": "hive-333333",
-              "owner": {
-                "weight_threshold": 1,
-                "account_auths": [],
-                "key_auths": [
-                  [
-                    "",
-                    1
-                  ]
-                ]
-              },
-              "active": {
-                "weight_threshold": 1,
-                "account_auths": [],
-                "key_auths": [
-                  [
-                    "",
-                    1
-                  ]
-                ]
-              },
-              "posting": {
-                "weight_threshold": 1,
-                "account_auths": [],
-                "key_auths": [
-                  [
-                    "",
-                    1
-                  ]
-                ]
-              },
-              "memo_key": "",
-              "json_metadata": "",
-              "extensions": []
-            }
-          },
-          {
-            "type": "comment_operation",
-            "value": {
-              "parent_author": "",
-              "parent_permlink": "hive-111111",
-              "author": "test-safari",
-              "permlink": "displayed",
-              "title": "normal post",
-              "body": "This post is displayed",
-              "json_metadata": "{}"
-            }
-          },
-          {
-            "type": "comment_operation",
-            "value": {
-              "parent_author": "",
-              "parent_permlink": "hive-222222",
-              "author": "test-safari",
-              "permlink": "hidden1",
-              "title": "This post is hidden",
-              "body": "This post is hidden",
-              "json_metadata": "{}"
-            }
-          },
-          {
-            "type": "comment_operation",
-            "value": {
-              "parent_author": "",
-              "parent_permlink": "hive-333333",
-              "author": "test-safari",
-              "permlink": "hidden1",
-              "title": "This post is hidden",
-              "body": "This post is hidden",
-              "json_metadata": "{}"
-            }
-          },
-          {
-            "type": "custom_json_operation",
-            "value": {
-              "required_auths": [],
-              "required_posting_auths": [
-                "hive-222222"
-              ],
-              "id": "community",
-              "json": "[\"setRole\",{\"community\":\"hive-222222\",\"account\":\"test-safari\",\"role\":\"member\"}]"
-            }
-          },
-          {
-            "type": "custom_json_operation",
-            "value": {
-              "required_auths": [],
-              "required_posting_auths": [
-                "hive-333333"
-              ],
-              "id": "community",
-              "json": "[\"setRole\",{\"community\":\"hive-333333\",\"account\":\"test-safari\",\"role\":\"member\"}]"
-            }
-          },
-          {
-            "type": "comment_operation",
-            "value": {
-              "parent_author": "",
-              "parent_permlink": "hive-222222",
-              "author": "test-safari",
-              "permlink": "display2",
-              "title": "This post is shown because I am a member",
-              "body": "This post is shown because I am a member",
-              "json_metadata": "{}"
-            }
-          },
-          {
-            "type": "comment_operation",
-            "value": {
-              "parent_author": "test-safari",
-              "parent_permlink": "display2",
-              "author": "blocktrades",
-              "permlink": "display_comment",
-              "title": "This comment is shown even though I'm a guest because community type 2",
-              "body": "hello",
-              "json_metadata": "{}"
-            }
-          },
-          {
-            "type": "comment_operation",
-            "value": {
-              "parent_author": "",
-              "parent_permlink": "hive-333333",
-              "author": "test-safari",
-              "permlink": "display3",
-              "title": "This post is shown because I am a member",
-              "body": "Hello",
-              "json_metadata": "{}"
-            }
-          },
-          {
-            "type": "comment_operation",
-            "value": {
-              "parent_author": "test-safari",
-              "parent_permlink": "display3",
-              "author": "test-safari",
-              "permlink": "display_comment1",
-              "title": "This comment is shown because member and community type 3",
-              "body": "hello",
-              "json_metadata": "{}"
-            }
-          },
-          {
-            "type": "comment_operation",
-            "value": {
-              "parent_author": "test-safari",
-              "parent_permlink": "display3",
-              "author": "blocktrades",
-              "permlink": "hidden_comment",
-              "title": "This comment is hidden because guest and community type 3",
-              "body": "hello",
-              "json_metadata": "{}"
-            }
-          },
-          {
-            "type": "custom_json_operation",
-            "value": {
-              "required_auths": [],
-              "required_posting_auths": [
-                "hive-222222"
-              ],
-              "id": "community",
-              "json": "[\"updateProps\",{\"community\":\"hive-222222\",\"props\":{\"type_id\":1,\"title\":\"World News\",\"about\":\"A place for major news from around the world.\",\"is_nsfw\":true,\"description\":\"\",\"flag_text\":\"\"}}]"
-            }
-          }
-        ],
-        "extensions": [],
-        "signatures": [
-          ""
-        ]
-      }
-    ]
-  }
-
-}
-- 
GitLab