Skip to content

Commit

Permalink
Community type 2 and 3 + update community type op
Browse files Browse the repository at this point in the history
  • Loading branch information
drov0 authored and vogel76 committed Oct 9, 2023
1 parent 2816638 commit d71f429
Show file tree
Hide file tree
Showing 41 changed files with 4,442 additions and 5,094 deletions.
81 changes: 68 additions & 13 deletions hive/db/sql_scripts/hive_post_operations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,66 @@ BEGIN
END
$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, _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
$$
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;
__is_muted bool := TRUE;
__is_parent_muted bool := FALSE;
__community_id hivemind_app.hive_posts.community_id%TYPE;
BEGIN
IF _block_num < _community_support_start_block THEN
__is_muted := FALSE;
__community_id := NULL;
ELSE
IF is_comment = TRUE THEN
SELECT hc.type_id, hc.id, hivemind_app.hive_posts.is_muted INTO __community_type_id, __community_id, __is_parent_muted
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 = _parent_permlink;
END IF;

-- __is_muted can be TRUE here if it's a comment and its parent is muted
IF __is_parent_muted = TRUE THEN
__is_muted := TRUE;
ELSEIF __community_id IS NOT NULL THEN
IF __community_type_id = __community_type_topic THEN
__is_muted := FALSE;
ELSE
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;
ELSE
__is_muted := FALSE;
END IF;
END IF;

RETURN QUERY SELECT __is_muted, __community_id;
END;
$$;


DROP FUNCTION IF EXISTS hivemind_app.process_hive_post_operation;
;
CREATE OR REPLACE FUNCTION hivemind_app.process_hive_post_operation(
Expand Down Expand Up @@ -58,17 +118,14 @@ if _parent_author != '' THEN
root_id, is_muted, is_valid,
author_id, permlink_id, created_at, updated_at, sc_hot, sc_trend, active, payout_at, cashout_time, counter_deleted, block_num, block_num_created)
SELECT php.id AS parent_id, php.depth + 1 AS depth,
(CASE
WHEN _block_num > _community_support_start_block THEN
COALESCE(php.community_id, (select hc.id from hivemind_app.hive_communities hc where hc.name = _parent_permlink))
ELSE NULL
END) AS community_id,
pcp.community_id AS community_id,
COALESCE(php.category_id, (select hcg.id from hivemind_app.hive_category_data hcg where hcg.category = _parent_permlink)) AS category_id,
(CASE(php.root_id)
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,
pcp.is_muted 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,
hivemind_app.calculate_time_part_of_hot(_date) AS sc_hot,
Expand All @@ -77,6 +134,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, _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
Expand Down Expand Up @@ -106,14 +164,10 @@ ELSE
active, payout_at, cashout_time, counter_deleted, block_num, block_num_created,
tags_ids)
SELECT 0 AS parent_id, 0 AS depth,
(CASE
WHEN _block_num > _community_support_start_block THEN
(select hc.id FROM hivemind_app.hive_communities hc WHERE hc.name = _parent_permlink)
ELSE NULL
END) AS community_id,
pcp.community_id AS community_id,
(SELECT hcg.id FROM hivemind_app.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,
pcp.is_muted 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,
hivemind_app.calculate_time_part_of_hot(_date) AS sc_hot,
Expand All @@ -125,7 +179,8 @@ ELSE
FROM hivemind_app.prepare_tags( ARRAY_APPEND(_metadata_tags, _parent_permlink ) )
) as tags_ids
FROM hivemind_app.hive_accounts ha,
hivemind_app.hive_permlink_data hpd
hivemind_app.process_community_post(_block_num, _community_support_start_block, _parent_permlink, ha.id, FALSE) pcp,
hivemind_app.hive_permlink_data hpd
WHERE ha.name = _author and hpd.permlink = _permlink

ON CONFLICT ON CONSTRAINT hive_posts_ux1 DO UPDATE SET
Expand Down
38 changes: 22 additions & 16 deletions hive/indexer/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Role(IntEnum):
TYPE_TOPIC = 1
TYPE_JOURNAL = 2
TYPE_COUNCIL = 3
valid_types = [TYPE_TOPIC, TYPE_JOURNAL, TYPE_COUNCIL]

# https://en.wikipedia.org/wiki/ISO_639-1
LANGS = (
Expand Down Expand Up @@ -103,6 +104,13 @@ def read_key_dict(obj, key):
assert isinstance(obj[key], dict), f'key `{key}` not a dict'
return obj[key]

def read_key_integer(op, key):
"""Reads a key from dict, ensuring valid integer if present."""
if key in op:
assert isinstance(op[key], int), 'must be int: %s' % key
return op[key]
return None


class Community:
"""Handles hive community registration and operations."""
Expand All @@ -124,7 +132,7 @@ def register(cls, name, block_date, block_num):
"""

# if not re.match(r'^hive-[123]\d{4,6}$', name):
if not re.match(r'^hive-[1]\d{4,6}$', name):
if not re.match(r'^hive-[123]\d{4,6}$', name):
return
type_id = int(name[5])
_id = Accounts.get_id(name)
Expand Down Expand Up @@ -201,10 +209,10 @@ def get_user_role(cls, community_id, account_id):
WHERE community_id = :community_id
AND account_id = :account_id
LIMIT 1""",
community_id=community_id,
account_id=account_id,
)
or Role.guest.value
community_id=community_id,
account_id=account_id,
)
or Role.guest.value
)

@classmethod
Expand All @@ -213,26 +221,20 @@ def is_post_valid(cls, community_id, comment_op: dict):
For a comment to be valid, these conditions apply:
- Author is not muted in this community
- For council post/comment, author must be a member
- For journal post, author must be a member
- Community must exist
Note that the checks related to community types are performed on insert
via the sql function process_community_post
"""

assert community_id, 'no community_id'
community = cls._get_name(community_id)
account_id = Accounts.get_id(comment_op['author'])
role = cls.get_user_role(community_id, account_id)
type_id = int(community[5])

# TODO: check `nsfw` tag requirement #267
# TODO: (1.5) check that beneficiaries are valid

if type_id == TYPE_JOURNAL:
if not comment_op['parent_author']:
return role >= Role.member
elif type_id == TYPE_COUNCIL:
return role >= Role.member
return role >= Role.guest # or at least not muted
return role >= Role.guest # At least not muted


class CommunityOp:
Expand Down Expand Up @@ -535,7 +537,7 @@ def _read_title(self):
def _read_props(self):
# TODO: assert props changed?
props = read_key_dict(self.op, 'props')
valid = ['title', 'about', 'lang', 'is_nsfw', 'description', 'flag_text', 'settings']
valid = ['title', 'about', 'lang', 'is_nsfw', 'description', 'flag_text', 'settings', 'type_id']
assert_keys_match(props.keys(), valid, allow_missing=True)

out = {}
Expand All @@ -560,6 +562,10 @@ def _read_props(self):
avatar_url = settings['avatar_url']
assert not avatar_url or _valid_url_proto(avatar_url)
out['avatar_url'] = avatar_url
if 'type_id' in props:
community_type = read_key_integer(props, 'type_id')
assert community_type in valid_types, 'invalid community type'
out['type_id'] = community_type
assert out, 'props were blank'
self.props = out

Expand Down
6 changes: 2 additions & 4 deletions hive/indexer/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ def comment_op(cls, op, block_date):
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:
Expand Down Expand Up @@ -392,12 +391,11 @@ def delete(cls, op, block_date):
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
Expand Down
38 changes: 37 additions & 1 deletion mock_data/block_data/community_op/flow.txt
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ account_create_operation( `ismember` )
account_create_operation( `notmembermuted` )
account_create_operation( `isoldmember` )
account_create_operation( `hive-199999` )
account_create_operation( `hive-111119` )
account_create_operation( `hive-211119` )
account_create_operation( `hive-311119` )
***block 5000012***
custom_json_operation("ismember" -> "[\"subscribe\", {\"community\": \"hive-199999\"}]")
custom_json_operation("isoldmember" -> "[\"subscribe\", {\"community\": \"hive-199999\"}]")
Expand All @@ -379,4 +382,37 @@ custom_json_operation("[\"setRole\", {\"community\": \"hive-199999\", \"account\
custom_json_operation("[\"setRole\", {\"community\": \"hive-199999\", \"account\": \"notmembermuted\", \"role\": \"muted\"}]")
custom_json_operation("[\"setRole\", {\"community\": \"hive-199999\", \"account\": \"isoldmember\", \"role\": \"mod\"}]")
custom_json_operation("isoldmember" -> "[\"unsubscribe\", {\"community\": \"hive-199999\"}]")
custom_json_operation("[\"setRole\", {\"community\": \"hive-199999\", \"account\": \"isoldmember\", \"role\": \"member\"}]")
custom_json_operation("[\"setRole\", {\"community\": \"hive-199999\", \"account\": \"isoldmember\", \"role\": \"member\"}]")
custom_json_operation("ismember" -> "[\"subscribe\", {\"community\": \"hive-111119\"}]")
custom_json_operation("ismember" -> "[\"subscribe\", {\"community\": \"hive-211119\"}]")
custom_json_operation("ismember" -> "[\"subscribe\", {\"community\": \"hive-311119\"}]")
custom_json_operation("[\"setRole\", {\"community\": \"hive-111119\", \"account\": \"ismember\", \"role\": \"member\"}]")
custom_json_operation("[\"setRole\", {\"community\": \"hive-211119\", \"account\": \"ismember\", \"role\": \"member\"}]")
custom_json_operation("[\"setRole\", {\"community\": \"hive-311119\", \"account\": \"ismember\", \"role\": \"member\"}]")
comment_operation( `hive-111119`, `ismember`,`ismember-hive-111119`)
comment_operation( `hive-111119`, `notmember`,`notmember-hive-111119`)
comment_operation( `ismember-hive-111119`, `ismember`,`re-ismember-hive-111119`)
comment_operation( `re-ismember-hive-111119`, `ismember`,`re-re-ismember-hive-111119`)
comment_operation( `ismember-hive-111119`, `notmember`,`re-2-ismember-hive-111119`)
comment_operation( `hive-211119`, `ismember`,`ismember-hive-211119`)
comment_operation( `hive-211119`, `notmember`,`notmember-hive-211119`)
comment_operation( `ismember-hive-211119`, `ismember`,`re-ismember-hive-211119`)
comment_operation( `re-ismember-hive-211119`, `ismember`,`re-re-ismember-hive-211119`)
comment_operation( `ismember-hive-211119`, `notmember`,`re-2-ismember-hive-211119`)
comment_operation( `hive-211119`, `ismember`,`ismember-hive-211119`)
comment_operation( `hive-211119`, `notmember`,`notmember-hive-211119`)
comment_operation( `ismember-hive-211119`, `ismember`,`re-ismember-hive-211119`)
comment_operation( `re-ismember-hive-211119`, `ismember`,`re-re-ismember-hive-211119`)
comment_operation( `ismember-hive-211119`, `notmember`,`re-2-ismember-hive-211119`)
comment_operation( `hive-311119`, `ismember`,`ismember-hive-311119`)
comment_operation( `hive-311119`, `notmember`,`notmember-hive-311119`)
comment_operation( `ismember-hive-311119`, `ismember`,`re-ismember-hive-311119`)
comment_operation( `re-ismember-hive-311119`, `ismember`,`re-re-ismember-hive-311119`)
comment_operation( `ismember-hive-311119`, `notmember`,`re-2-ismember-hive-311119`)
custom_json_operation("[\"setRole\", {\"community\": \"hive-111119\", \"account\": \"ismember\", \"role\": \"mod\"}]")
custom_json_operation("[\"updateProps\",{\"community\":\"hive-111119\",\"props\":{\"type_id\": 3}}]")
comment_operation( `hive-111119`, `ismember`,`ismember-hive-111119-changed-type-3`)
comment_operation( `hive-111119`, `notmember`,`notmember-hive-111119-changed-type-3`)
comment_operation( `ismember-hive-111119-changed-type-3`, `ismember`,`re-ismember-hive-111119-changed-type-3`)
comment_operation( `re-ismember-hive-111119-changed-type-3`, `ismember`,`re-re-ismember-hive-111119-changed-type-3`)
comment_operation( `ismember-hive-111119-changed-type-3`, `notmember`,`re-2-ismember-hive-111119-changed-type-3`)
Loading

0 comments on commit d71f429

Please sign in to comment.