Skip to content

Commit

Permalink
Updated /refresh-memberships response to include topic level permis…
Browse files Browse the repository at this point in the history
…sioning
  • Loading branch information
mzparacha committed Oct 10, 2024
1 parent abd1584 commit e5f022c
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -14,7 +15,7 @@ interface RefreshMembershipProps {

export interface Memberships {
groupId: number;
topicIds: number[];
topics: { id: number; permission: GroupTopicPermissionEnum }[];
isAllowed: boolean;
rejectReason?: string;
}
Expand All @@ -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,
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,13 @@ export const CWContentPage = ({
});

const isTopicGated = !!(memberships || []).find((membership) =>
// @ts-expect-error <StrictNullChecks/>
membership.topicIds.includes(thread?.topic?.id),
membership.topics.find((t) => t.id === thread?.topic?.id),
);

const isActionAllowedInGatedTopic = !!(memberships || []).find(
(membership) =>
// @ts-expect-error <StrictNullChecks/>
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ const GroupsSection = ({
profiles?.map((p) => [p.address, p]),
);

console.log('filteredGroups => ', filteredGroups);

return (
<section className="GroupsSection">
{hasNoGroups && <TopicGatingHelpMessage />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,13 @@ const ViewThreadPage = ({ identifier }: ViewThreadPageProps) => {
});

const isTopicGated = !!(memberships || []).find((membership) =>
// @ts-expect-error <StrictNullChecks/>
membership.topicIds.includes(thread?.topic?.id),
membership.topics.find((t) => t.id === thread?.topic?.id),
);

const isActionAllowedInGatedTopic = !!(memberships || []).find(
(membership) =>
// @ts-expect-error <StrictNullChecks/>
membership.topicIds.includes(thread?.topic?.id) && membership.isAllowed,
membership.topics.find((t) => t.id === thread?.topic?.id) &&
membership.isAllowed,
);

const isRestrictedMembership =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
GroupAttributes,
GroupInstance,
MembershipAttributes,
TopicAttributes,
} from '@hicommonwealth/model';
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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,
}));
Expand Down

0 comments on commit e5f022c

Please sign in to comment.