Skip to content
Snippets Groups Projects
Commit 4744e59c authored by Martin Lees's avatar Martin Lees Committed by Howo
Browse files

create validate_community_set_role instead of doing everything in one query

parent 3cb69698
No related branches found
No related tags found
1 merge request!539Update SetRole/SetUserTitle so that you have to be subscribed to get it, also...
This commit is part of merge request !539. Comments created here will be created in the context of that merge request.
DROP FUNCTION IF EXISTS hivemind_app.set_community_role_or_title; DROP FUNCTION IF EXISTS hivemind_app.validate_community_set_role;
CREATE OR REPLACE FUNCTION hivemind_app.set_community_role_or_title(_community_id hivemind_app.hive_posts.community_id%TYPE, _account_id hivemind_app.hive_posts.author_id%TYPE, _role_id integer, _title varchar(140), _created_at timestamp) CREATE OR REPLACE FUNCTION hivemind_app.validate_community_set_role(_community_id hivemind_app.hive_posts.community_id%TYPE, _account_id hivemind_app.hive_posts.author_id%TYPE, _role_id integer)
RETURNS bool RETURNS bool
LANGUAGE plpgsql LANGUAGE plpgsql
as as
...@@ -24,18 +24,6 @@ BEGIN ...@@ -24,18 +24,6 @@ BEGIN
END IF; END IF;
end if; end if;
IF _role_id IS NOT NULL AND _title IS NULL THEN
INSERT INTO hivemind_app.hive_roles(account_id, community_id, role_id, created_at)
VALUES (_account_id, _community_id, _role_id, _created_at)
ON CONFLICT (account_id, community_id)
DO UPDATE SET role_id = _role_id;
ELSIF _title IS NOT NULL AND _role_id IS NULL THEN
INSERT INTO hivemind_app.hive_roles(account_id, community_id, title, created_at)
VALUES (_account_id, _community_id, _title, _created_at)
ON CONFLICT (account_id, community_id)
DO UPDATE SET title = _title;
END IF;
RETURN TRUE; RETURN TRUE;
END; END;
$$; $$;
...@@ -372,22 +372,24 @@ class CommunityOp: ...@@ -372,22 +372,24 @@ class CommunityOp:
# Account-level actions # Account-level actions
elif action == 'setRole': elif action == 'setRole':
subscribed = DB.query_one( DB.query(
f"""SELECT * FROM {SCHEMA_NAME}.set_community_role_or_title(:community_id, :account_id, :role_id, NULL::varchar, CAST(:date AS timestamp ))""", f"""INSERT INTO {SCHEMA_NAME}.hive_roles
(account_id, community_id, role_id, created_at)
VALUES (:account_id, :community_id, :role_id, :date)
ON CONFLICT (account_id, community_id)
DO UPDATE SET role_id = :role_id """,
**params, **params,
) )
if not subscribed:
log.info("set role failed account '%s' must be subscribed to the community", params['account'])
return
self._notify('set_role', payload=Role(self.role_id).name) self._notify('set_role', payload=Role(self.role_id).name)
elif action == 'setUserTitle': elif action == 'setUserTitle':
subscribed = DB.query_one( DB.query(
f"""SELECT * FROM {SCHEMA_NAME}.set_community_role_or_title(:community_id, :account_id, NULL::integer , :title, CAST(:date AS timestamp ))""", f"""INSERT INTO {SCHEMA_NAME}.hive_roles
(account_id, community_id, title, created_at)
VALUES (:account_id, :community_id, :title, :date)
ON CONFLICT (account_id, community_id)
DO UPDATE SET title = :title""",
**params, **params,
) )
if not subscribed:
log.info("set role failed account '%s' must be subscribed to the community", params['account'])
return
self._notify('set_label', payload=self.title) self._notify('set_label', payload=self.title)
# Post-level actions # Post-level actions
...@@ -575,6 +577,13 @@ class CommunityOp: ...@@ -575,6 +577,13 @@ class CommunityOp:
if self.actor != self.account: if self.actor != self.account:
assert account_role < actor_role, 'cant modify higher-role user' assert account_role < actor_role, 'cant modify higher-role user'
assert account_role != new_role, 'role would not change' assert account_role != new_role, 'role would not change'
subscribed = DB.query_one(
f"""SELECT * FROM {SCHEMA_NAME}.validate_community_set_role(:community_id, :actor_id, :role_id)""",
community_id=self.community_id, actor_id=self.actor_id, role_id=new_role,
)
assert subscribed, f"account {self.account} must be subscribed to the community to execute setRole"
elif action == 'updateProps': elif action == 'updateProps':
assert actor_role >= Role.admin, 'only admins can update props' assert actor_role >= Role.admin, 'only admins can update props'
elif action == 'setUserTitle': elif action == 'setUserTitle':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment