Skip to content
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

Closed
wants to merge 47 commits into from
Closed

Conversation

kurtisassad
Copy link
Contributor

@kurtisassad kurtisassad commented Jul 18, 2024

Link to Issue

Closes: #7951

Description of Changes

  • Removes Topics.group_ids, and migrates them to a unique GroupPermission record
  • Refactors all application code to use the new model. On the backend this includes create_group, update_group, delete_group, as well as the unused CreateGroup.command.ts.
  • Refactors front end to use the new fine grained permissions model to gate UI features.

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:
image

Discussion page:
image

Create thread page:
image

Overview page:
image

@kurtisassad kurtisassad force-pushed the ka.groupPermissionsCleanup branch from f44fafe to 2328c70 Compare July 25, 2024 13:10
# 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
@kurtisassad kurtisassad requested a review from rbennettcw August 1, 2024 12:50
@kurtisassad kurtisassad marked this pull request as ready for review August 1, 2024 13:35
Copy link
Contributor

@masvelio masvelio left a 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

Copy link
Collaborator

@timolegros timolegros left a 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?

libs/model/src/community/CreateGroup.command.ts Outdated Show resolved Hide resolved

for (const t of topics) {
for (const g of t.group_ids) {
await queryInterface.sequelize.query(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bulkInsert?

Copy link
Contributor Author

@kurtisassad kurtisassad Aug 7, 2024

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.

Copy link
Collaborator

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.

@mzparacha
Copy link
Contributor

Found a bug with group updates

  1. Create a group with no members in allow list (adds requirement that allow some existing users)
  2. Now verify you have some community members who are part of that group
  3. Update that group and add some members to allow list
  4. Now notice in the /members page when you filter by that specific group, only the member added in the allow list is displayed, and other members aren't (you'd have to rebase master to test the filter on /members as it got changes in Fix UI issues with community members page #8471)
Screen.Recording.2024-08-06.at.7.18.44.PM.mov

@kurtisassad kurtisassad marked this pull request as draft August 7, 2024 12:01
@kurtisassad kurtisassad requested a review from ForestMars August 19, 2024 14:29
@kurtisassad kurtisassad force-pushed the ka.groupPermissionsCleanup branch from c20a1a7 to 2caadd7 Compare August 19, 2024 14:46
@kurtisassad kurtisassad self-assigned this Aug 19, 2024
@kurtisassad kurtisassad marked this pull request as draft August 19, 2024 14:48
@kurtisassad kurtisassad marked this pull request as ready for review August 19, 2024 15:22
# Conflicts:
#	packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx
@kurtisassad kurtisassad force-pushed the ka.groupPermissionsCleanup branch from 55b52b0 to ab6393d Compare August 22, 2024 13:44
Copy link
Contributor

@rbennettcw rbennettcw left a 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.

Comment on lines 54 to 64
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,
);
Copy link
Contributor

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.

Copy link
Contributor Author

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

Comment on lines +201 to +209
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 *;
Copy link
Contributor

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.

Comment on lines +223 to +225
return (await Promise.all(upsertPromises)) as unknown as Promise<
GroupPermissionInstance[]
>;
Copy link
Contributor

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)

Copy link
Collaborator

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
@timolegros timolegros marked this pull request as draft December 18, 2024 15:10
@masvelio
Copy link
Contributor

masvelio commented Jan 6, 2025

@kurtisassad any chance to make this PR mergable? Otherwise, let's close it to clean up the PR list

@kurtisassad kurtisassad closed this Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Group Permissions Cleanup
6 participants