From e5f022c3c4efec553569ae9f75257162c5c3c5ae Mon Sep 17 00:00:00 2001 From: Malik Zulqurnain Date: Thu, 10 Oct 2024 22:11:05 +0500 Subject: [PATCH] Updated `/refresh-memberships` response to include topic level permissioning --- .../state/api/groups/refreshMembership.ts | 5 +++-- .../NewThreadFormLegacy/NewThreadForm.tsx | 6 +++--- .../NewThreadFormModern/NewThreadForm.tsx | 4 ++-- .../CWContentPage/CWContentPage.tsx | 7 +++---- .../client/scripts/views/components/feed.tsx | 5 +++-- .../Members/GroupsSection/GroupsSection.tsx | 2 -- .../pages/discussions/DiscussionsPage.tsx | 4 ++-- .../views/pages/overview/TopicSummaryRow.tsx | 4 ++-- .../pages/view_thread/ViewThreadPage.tsx | 7 +++---- .../server_groups_methods/get_groups.ts | 15 ++++++++++--- .../refresh_membership.ts | 21 +++++++++++++++++-- 11 files changed, 52 insertions(+), 28 deletions(-) diff --git a/packages/commonwealth/client/scripts/state/api/groups/refreshMembership.ts b/packages/commonwealth/client/scripts/state/api/groups/refreshMembership.ts index d35d70ee31f..2f38f036e3b 100644 --- a/packages/commonwealth/client/scripts/state/api/groups/refreshMembership.ts +++ b/packages/commonwealth/client/scripts/state/api/groups/refreshMembership.ts @@ -1,3 +1,4 @@ +import { GroupTopicPermissionEnum } from '@hicommonwealth/schemas'; import { useQuery } from '@tanstack/react-query'; import axios from 'axios'; import { ApiEndpoints, SERVER_URL } from 'state/api/config'; @@ -14,7 +15,7 @@ interface RefreshMembershipProps { export interface Memberships { groupId: number; - topicIds: number[]; + topics: { id: number; permission: GroupTopicPermissionEnum }[]; isAllowed: boolean; rejectReason?: string; } @@ -34,7 +35,7 @@ const refreshMembership = async ({ return response?.data?.result?.map((r) => ({ groupId: r.groupId, - topicIds: r.topicIds, + topics: r.topics, isAllowed: r.allowed, rejectReason: r.rejectReason, })); diff --git a/packages/commonwealth/client/scripts/views/components/NewThreadFormLegacy/NewThreadForm.tsx b/packages/commonwealth/client/scripts/views/components/NewThreadFormLegacy/NewThreadForm.tsx index 907a8e3dca9..9cf98d969d8 100644 --- a/packages/commonwealth/client/scripts/views/components/NewThreadFormLegacy/NewThreadForm.tsx +++ b/packages/commonwealth/client/scripts/views/components/NewThreadFormLegacy/NewThreadForm.tsx @@ -141,13 +141,13 @@ export const NewThreadForm = () => { const isTopicGated = !!(memberships || []).find( (membership) => - threadTopic?.id && membership.topicIds.includes(threadTopic.id), + threadTopic?.id && membership.topics.find((t) => t.id === threadTopic.id), ); const isActionAllowedInGatedTopic = !!(memberships || []).find( (membership) => - threadTopic.id && + threadTopic && threadTopic?.id && - membership.topicIds.includes(threadTopic?.id) && + membership.topics.find((t) => t.id === threadTopic?.id) && membership.isAllowed, ); const gatedGroupNames = groups diff --git a/packages/commonwealth/client/scripts/views/components/NewThreadFormModern/NewThreadForm.tsx b/packages/commonwealth/client/scripts/views/components/NewThreadFormModern/NewThreadForm.tsx index a49282fa545..c566845a28e 100644 --- a/packages/commonwealth/client/scripts/views/components/NewThreadFormModern/NewThreadForm.tsx +++ b/packages/commonwealth/client/scripts/views/components/NewThreadFormModern/NewThreadForm.tsx @@ -134,13 +134,13 @@ export const NewThreadForm = () => { const isTopicGated = !!(memberships || []).find( (membership) => - threadTopic?.id && membership.topicIds.includes(threadTopic.id), + threadTopic?.id && membership.topics.find((t) => t.id === threadTopic.id), ); const isActionAllowedInGatedTopic = !!(memberships || []).find( (membership) => threadTopic.id && threadTopic?.id && - membership.topicIds.includes(threadTopic?.id) && + membership.topics.find((t) => t.id === threadTopic?.id) && membership.isAllowed, ); const gatedGroupNames = groups diff --git a/packages/commonwealth/client/scripts/views/components/component_kit/CWContentPage/CWContentPage.tsx b/packages/commonwealth/client/scripts/views/components/component_kit/CWContentPage/CWContentPage.tsx index ea04b676a9e..fcd1b7355de 100644 --- a/packages/commonwealth/client/scripts/views/components/component_kit/CWContentPage/CWContentPage.tsx +++ b/packages/commonwealth/client/scripts/views/components/component_kit/CWContentPage/CWContentPage.tsx @@ -132,14 +132,13 @@ export const CWContentPage = ({ }); const isTopicGated = !!(memberships || []).find((membership) => - // @ts-expect-error - membership.topicIds.includes(thread?.topic?.id), + membership.topics.find((t) => t.id === thread?.topic?.id), ); const isActionAllowedInGatedTopic = !!(memberships || []).find( (membership) => - // @ts-expect-error - membership.topicIds.includes(thread?.topic?.id) && membership.isAllowed, + membership.topics.find((t) => t.id === thread?.topic?.id) && + membership.isAllowed, ); const isAdmin = Permissions.isSiteAdmin() || Permissions.isCommunityAdmin(); diff --git a/packages/commonwealth/client/scripts/views/components/feed.tsx b/packages/commonwealth/client/scripts/views/components/feed.tsx index 43416e4208e..4cb11b14f71 100644 --- a/packages/commonwealth/client/scripts/views/components/feed.tsx +++ b/packages/commonwealth/client/scripts/views/components/feed.tsx @@ -66,13 +66,14 @@ const FeedThread = ({ thread }: { thread: Thread }) => { const isTopicGated = !!(memberships || []).find( (membership) => - thread?.topic?.id && membership.topicIds.includes(thread.topic.id), + thread?.topic?.id && + membership.topics.find((t) => t.id === thread.topic.id), ); const isActionAllowedInGatedTopic = !!(memberships || []).find( (membership) => thread?.topic?.id && - membership.topicIds.includes(thread.topic.id) && + membership.topics.find((t) => t.id === thread.topic.id) && membership.isAllowed, ); diff --git a/packages/commonwealth/client/scripts/views/pages/CommunityGroupsAndMembers/Members/GroupsSection/GroupsSection.tsx b/packages/commonwealth/client/scripts/views/pages/CommunityGroupsAndMembers/Members/GroupsSection/GroupsSection.tsx index a6717a709b7..585b836a54b 100644 --- a/packages/commonwealth/client/scripts/views/pages/CommunityGroupsAndMembers/Members/GroupsSection/GroupsSection.tsx +++ b/packages/commonwealth/client/scripts/views/pages/CommunityGroupsAndMembers/Members/GroupsSection/GroupsSection.tsx @@ -40,8 +40,6 @@ const GroupsSection = ({ profiles?.map((p) => [p.address, p]), ); - console.log('filteredGroups => ', filteredGroups); - return (
{hasNoGroups && } diff --git a/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx b/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx index 126aa48bf7b..008bb76acdf 100644 --- a/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx +++ b/packages/commonwealth/client/scripts/views/pages/discussions/DiscussionsPage.tsx @@ -209,13 +209,13 @@ const DiscussionsPage = ({ topicName }: DiscussionsPageProps) => { const isTopicGated = !!(memberships || []).find( (membership) => thread?.topic?.id && - membership.topicIds.includes(thread.topic.id), + membership.topics.find((t) => t.id === thread.topic.id), ); const isActionAllowedInGatedTopic = !!(memberships || []).find( (membership) => thread?.topic?.id && - membership.topicIds.includes(thread.topic.id) && + membership.topics.find((t) => t.id === thread.topic.id) && membership.isAllowed, ); diff --git a/packages/commonwealth/client/scripts/views/pages/overview/TopicSummaryRow.tsx b/packages/commonwealth/client/scripts/views/pages/overview/TopicSummaryRow.tsx index bc402155416..6494c8684c0 100644 --- a/packages/commonwealth/client/scripts/views/pages/overview/TopicSummaryRow.tsx +++ b/packages/commonwealth/client/scripts/views/pages/overview/TopicSummaryRow.tsx @@ -92,13 +92,13 @@ export const TopicSummaryRow = ({ const isTopicGated = !!(memberships || []).find( (membership) => thread?.topic?.id && - membership.topicIds.includes(thread.topic.id), + membership.topics.find((t) => t.id === thread.topic.id), ); const isActionAllowedInGatedTopic = !!(memberships || []).find( (membership) => thread?.topic?.id && - membership.topicIds.includes(thread.topic.id) && + membership.topics.find((t) => t.id === thread.topic.id) && membership.isAllowed, ); diff --git a/packages/commonwealth/client/scripts/views/pages/view_thread/ViewThreadPage.tsx b/packages/commonwealth/client/scripts/views/pages/view_thread/ViewThreadPage.tsx index 807e5f859ac..5e019d3af55 100644 --- a/packages/commonwealth/client/scripts/views/pages/view_thread/ViewThreadPage.tsx +++ b/packages/commonwealth/client/scripts/views/pages/view_thread/ViewThreadPage.tsx @@ -155,14 +155,13 @@ const ViewThreadPage = ({ identifier }: ViewThreadPageProps) => { }); const isTopicGated = !!(memberships || []).find((membership) => - // @ts-expect-error - membership.topicIds.includes(thread?.topic?.id), + membership.topics.find((t) => t.id === thread?.topic?.id), ); const isActionAllowedInGatedTopic = !!(memberships || []).find( (membership) => - // @ts-expect-error - membership.topicIds.includes(thread?.topic?.id) && membership.isAllowed, + membership.topics.find((t) => t.id === thread?.topic?.id) && + membership.isAllowed, ); const isRestrictedMembership = diff --git a/packages/commonwealth/server/controllers/server_groups_methods/get_groups.ts b/packages/commonwealth/server/controllers/server_groups_methods/get_groups.ts index 2ec088d4bb1..0a56395bbd1 100644 --- a/packages/commonwealth/server/controllers/server_groups_methods/get_groups.ts +++ b/packages/commonwealth/server/controllers/server_groups_methods/get_groups.ts @@ -1,5 +1,6 @@ import { GroupAttributes, + GroupInstance, MembershipAttributes, TopicAttributes, } from '@hicommonwealth/model'; @@ -23,6 +24,13 @@ type GroupWithExtras = GroupAttributes & { }; export type GetGroupsResult = GroupWithExtras[]; +export type GroupInstanceWithTopicPermissions = GroupInstance & { + GroupTopicPermissions: { + topic_id: number; + allowed_actions: GroupTopicPermissionEnum; + }[]; +}; + export async function __getGroups( this: ServerGroupsController, { communityId, includeMembers, includeTopics }: GetGroupsOptions, @@ -87,9 +95,10 @@ export async function __getGroups( .map((t) => { const temp: TopicAttributesWithPermission = { ...t.toJSON() }; temp.permission = - ((group as any).GroupTopicPermissions || []).find( - (gtp) => gtp.topic_id === t.id, - )?.allowed_actions || + ( + (group as GroupInstanceWithTopicPermissions) + .GroupTopicPermissions || [] + ).find((gtp) => gtp.topic_id === t.id)?.allowed_actions || // TODO: this fallback should be via a migration for existing communities GroupTopicPermissionEnum.UPVOTE_AND_COMMENT_AND_POST; return temp; diff --git a/packages/commonwealth/server/controllers/server_groups_methods/refresh_membership.ts b/packages/commonwealth/server/controllers/server_groups_methods/refresh_membership.ts index c59552b43c6..2c33f527a93 100644 --- a/packages/commonwealth/server/controllers/server_groups_methods/refresh_membership.ts +++ b/packages/commonwealth/server/controllers/server_groups_methods/refresh_membership.ts @@ -4,9 +4,11 @@ import { MembershipRejectReason, UserInstance, } from '@hicommonwealth/model'; +import { GroupTopicPermissionEnum } from '@hicommonwealth/schemas'; import { Op } from 'sequelize'; import { refreshMembershipsForAddress } from '../../util/requirementsModule/refreshMembershipsForAddress'; import { ServerGroupsController } from '../server_groups_controller'; +import { GroupInstanceWithTopicPermissions } from './get_groups'; const Errors = { TopicNotFound: 'Topic not found', @@ -32,6 +34,12 @@ export async function __refreshMembership( where: { community_id: address.community_id, }, + include: [ + { + model: this.models.GroupTopicPermission, + attributes: ['topic_id', 'allowed_actions'], + }, + ], }); // optionally filter to only groups associated with topic @@ -63,9 +71,18 @@ export async function __refreshMembership( // transform memberships to result shape const results = memberships.map((membership) => ({ groupId: membership.group_id, - topicIds: topics + topics: topics .filter((t) => t.group_ids!.includes(membership.group_id)) - .map((t) => t.id), + .map((t) => ({ + id: t.id, + permission: + (groups as GroupInstanceWithTopicPermissions[]) + .find((g) => g.id === membership.group_id) + ?.GroupTopicPermissions?.find((gtp) => gtp.topic_id === t.id) + ?.allowed_actions || + // TODO: this fallback should be via a migration for existing communities + GroupTopicPermissionEnum.UPVOTE_AND_COMMENT_AND_POST, + })), allowed: !membership.reject_reason, rejectReason: membership.reject_reason, }));