-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactored groupPermissions #8528
Conversation
packages/commonwealth/server/migrations/20240718135145-group-permission-refactor.js
Outdated
Show resolved
Hide resolved
f44fafe
to
2328c70
Compare
# Conflicts: # packages/commonwealth/client/scripts/utils/Permissions.ts # packages/commonwealth/client/scripts/views/components/NewThreadForm/NewThreadForm.tsx # packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx # packages/commonwealth/client/scripts/views/pages/view_thread/ViewThreadPage.tsx
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FE code looks good to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work. Would like to remove the unknown
type casts and optimize queries where possible. Will run through test plan after comments are handled.
Why is the column removal not included in the migration?
packages/commonwealth/server/controllers/server_groups_methods/create_group.ts
Outdated
Show resolved
Hide resolved
packages/commonwealth/server/controllers/server_groups_methods/refresh_membership.ts
Outdated
Show resolved
Hide resolved
packages/commonwealth/server/controllers/server_groups_methods/update_group.ts
Outdated
Show resolved
Hide resolved
|
||
for (const t of topics) { | ||
for (const g of t.group_ids) { | ||
await queryInterface.sequelize.query( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bulkInsert
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bulkInsert won't work here because we have an array of enums, but we need to specify the field as a string[] in sequelize otherwise the tests will fail when trying to sync. As a result Sequelize tries to cast them into an array of strings which sql complains about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't exactly understand but is this not something we can work-around with type assertions? Also, this is already a raw query so we should be able to just prepare all the values at once (and not use bulkInsert
). A nested for-loop query is a bad idea.
packages/commonwealth/server/migrations/20240718135145-group-permission-refactor.js
Outdated
Show resolved
Hide resolved
packages/commonwealth/server/util/requirementsModule/validateTopicGroupsMembership.ts
Show resolved
Hide resolved
Found a bug with group updates
Screen.Recording.2024-08-06.at.7.18.44.PM.mov |
c20a1a7
to
2caadd7
Compare
packages/commonwealth/test/unit/server_controllers/server_threads_controller.spec.ts
Outdated
Show resolved
Hide resolved
# Conflicts: # packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx
55b52b0
to
ab6393d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some small comments. Lmk when tests/conflicts are fixed and I'll test locally.
const allowedActions = useForumActionGated({ | ||
communityId: app.activeChainId(), | ||
address: user.activeAccount?.address || '', | ||
topicId: thread?.topic?.id, | ||
}); | ||
|
||
const isTopicGated = !!(memberships || []).find((membership) => | ||
membership.topicIds.includes(thread?.topic?.id), | ||
const { canCreateComment, canReactToThread } = canPerformAction( | ||
allowedActions, | ||
isAdmin, | ||
thread?.topic?.id, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The addition of the useForumActionGated
hook is awesome to help simplify FE code. The canPerformAction
call seems a bit redundant though– I think that should be called within the hook and return the result from the hook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, I will merge this into the useForumActionGated
models.sequelize.query( | ||
` | ||
INSERT INTO "GroupPermissions" (group_id, topic_id, allowed_actions, created_at, updated_at) VALUES | ||
(:groupId, :topicId, Array[:allowedActions]::"enum_GroupPermissions_allowed_actions"[], NOW(), NOW()) | ||
ON CONFLICT (group_id, topic_id) DO UPDATE | ||
SET | ||
allowed_actions = EXCLUDED.allowed_actions, | ||
updated_at = EXCLUDED.updated_at | ||
RETURNING *; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should always use a type for raw queries, e.g. models.sequelize.query<GroupPermissionInstance>(...)
, then there's no need to cast the type later.
Also, I think wildcards *
should be avoided. It's easier to write but tricker to debug due to lack of transparency.
return (await Promise.all(upsertPromises)) as unknown as Promise< | ||
GroupPermissionInstance[] | ||
>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to await if you're returning a promise. It'll resolve either way. Also, if setting the type on the query, this would simply become return Promise.all(upsertPromises)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return await
and return Promise...
are not the same and I think return await
is recommended now:
https://github.com/goldbergyoni/nodebestpractices/blob/master/sections/errorhandling/returningpromises.md
# Conflicts: # packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx # packages/commonwealth/server/controllers/server_comments_methods/create_comment_reaction.ts # packages/commonwealth/server/controllers/server_threads_methods/create_thread.ts # packages/commonwealth/server/controllers/server_threads_methods/create_thread_comment.ts # packages/commonwealth/server/controllers/server_threads_methods/create_thread_reaction.ts # packages/commonwealth/test/integration/api/subscriptions.spec.ts # packages/commonwealth/test/unit/server_controllers/server_threads_controller.spec.ts
# Conflicts: # libs/model/src/middleware/authorization.ts # packages/commonwealth/server/controllers/server_groups_methods/delete_group.ts # packages/commonwealth/server/controllers/server_groups_methods/get_groups.ts # packages/commonwealth/server/controllers/server_groups_methods/refresh_membership.ts
@kurtisassad any chance to make this PR mergable? Otherwise, let's close it to clean up the PR list |
Link to Issue
Closes: #7951
Description of Changes
Test Plan
Turn the Users.isAdmin on for your account for a community
Create a group with some member on the allowlist
Update this group, make sure it works. Delete this group, make sure it works.
Re-add the group, ensure it is associated with at least one topic. Make yourself not a User.isAdmin. Now try creating a thread/commenting/reacting on a thread with a specific topic that is part of the group. You should be gated from performing these actions.
Check the gating on the /discussions, /discusssion/[thread-id], and /new/discussion pages. When gated it should look like the following (Depending on the forum actions gated and the specific topics gated for):
Discussions page:
Discussion page:
Create thread page:
Overview page: