Skip to content

Commit

Permalink
Added group permission logic for all adding/updating/removing groups
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisassad committed Jul 25, 2024
1 parent a076462 commit f44fafe
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 125 deletions.
4 changes: 3 additions & 1 deletion libs/model/src/models/associations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ export const buildAssociations = (db: DB) => {
asMany: 'threads',
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
}).withMany(db.ContestTopic);
})
.withMany(db.ContestTopic)
.withMany(db.GroupPermission);

db.Thread.withMany(db.Poll)
.withMany(db.ContestAction, {
Expand Down
2 changes: 2 additions & 0 deletions libs/model/src/models/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { Group } from '@hicommonwealth/schemas';
import Sequelize from 'sequelize';
import z from 'zod';
import type { CommunityAttributes } from './community';
import { GroupPermissionAttributes } from './groupPermission';
import type { MembershipAttributes } from './membership';
import type { ModelInstance } from './types';

export type GroupAttributes = z.infer<typeof Group> & {
// associations
community?: CommunityAttributes;
memberships?: MembershipAttributes[];
groupPermissions?: GroupPermissionAttributes[];
};

export type GroupInstance = ModelInstance<GroupAttributes>;
Expand Down
5 changes: 5 additions & 0 deletions libs/model/src/models/groupPermission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export default (
type: Sequelize.ARRAY(Sequelize.STRING),
allowNull: false,
},
topic_id: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
},
},
{
tableName: 'GroupPermissions',
Expand Down
7 changes: 4 additions & 3 deletions libs/schemas/src/entities/group-permission.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { z } from 'zod';
import { PG_INT } from '../utils';

export enum PermissionEnum {
export enum ForumActionsEnum {
CREATE_THREAD = 'CREATE_THREAD',
CREATE_COMMENT = 'CREATE_COMMENT',
CREATE_THREAD_REACTION = 'CREATE_THREAD_REACTION',
CREATE_COMMENT_REACTION = 'CREATE_COMMENT_REACTION',
UPDATE_POLL = 'UPDATE_POLL',
}

export type GroupPermissionAction = keyof typeof PermissionEnum;
export type ForumActions = keyof typeof ForumActionsEnum;

export const GroupPermission = z.object({
group_id: PG_INT.optional(),
allowed_actions: z.array(z.nativeEnum(PermissionEnum)),
topic_id: PG_INT.optional(),
allowed_actions: z.array(z.nativeEnum(ForumActionsEnum)),
created_at: z.any().optional(),
updated_at: z.any().optional(),
});
1 change: 0 additions & 1 deletion libs/schemas/src/entities/topic.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ export const Topic = z.object({
default_offchain_template: z.string().optional().nullable(),
order: PG_INT.nullish(),
channel_id: z.string().max(255).optional().nullable(),
group_ids: z.array(PG_INT).default([]),
default_offchain_template_backup: z.string().optional().nullable(),
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
UserInstance,
commonProtocol as commonProtocolService,
} from '@hicommonwealth/model';
import { PermissionEnum } from '@hicommonwealth/schemas';
import { ForumActionsEnum } from '@hicommonwealth/schemas';
import { NotificationCategories, commonProtocol } from '@hicommonwealth/shared';
import { MixpanelCommunityInteractionEvent } from '../../../shared/analytics/types';
import { config } from '../../config';
Expand Down Expand Up @@ -95,7 +95,7 @@ export async function __createCommentReaction(
thread.topic_id,
thread.community_id,
address,
PermissionEnum.CREATE_COMMENT_REACTION,
ForumActionsEnum.CREATE_COMMENT_REACTION,
);
canReact = isValid;
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
GroupAttributes,
UserInstance,
} from '@hicommonwealth/model';
import { GroupMetadata, PermissionEnum } from '@hicommonwealth/schemas';
import { ForumActionsEnum, GroupMetadata } from '@hicommonwealth/schemas';
import { Requirement } from '@hicommonwealth/shared';
import { Op, Transaction } from 'sequelize';
import z from 'zod';
Expand Down Expand Up @@ -116,21 +116,19 @@ export async function __createGroup(
{ transaction: t },
);
if (topicsToAssociate.length > 0) {
// Create an array of promises
const createPermissionsPromises = topicsToAssociate.map((topic) => {
return this.models.GroupPermission.create(
{
group_id: group.id,
topic_id: topic.id,
allowed_actions: Object.values(PermissionEnum),
allowed_actions: Object.values(ForumActionsEnum),
},
{
transaction,
transaction: t,
},
);
});

// Await all promises to complete
await Promise.all(createPermissionsPromises);
}
return group.toJSON();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { AppError } from '@hicommonwealth/core';
import {
AddressInstance,
UserInstance,
sequelize,
} from '@hicommonwealth/model';
import { Op } from 'sequelize';
import { AddressInstance, UserInstance } from '@hicommonwealth/model';
import { validateOwner } from '../../util/validateOwner';
import { ServerCommunitiesController } from '../server_communities_controller';

Expand Down Expand Up @@ -48,24 +43,11 @@ export async function __deleteGroup(
}

await this.models.sequelize.transaction(async (transaction) => {
// remove group from all associated topics
await this.models.Topic.update(
{
group_ids: sequelize.fn(
'array_remove',
sequelize.col('group_ids'),
group.id,
),
},
{
where: {
group_ids: {
[Op.contains]: [group.id],
},
},
transaction,
},
);
// remove all group permissions of the group
await this.models.GroupPermission.destroy({
where: { group_id: group.id },
transaction,
});
// delete all memberships of group
await this.models.Membership.destroy({
where: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ export async function __getGroups(
this: ServerGroupsController,
{ communityId, includeMembers, includeTopics }: GetGroupsOptions,
): Promise<GetGroupsResult> {
const include = includeTopics
? {
model: this.models.GroupPermission,
include: {
model: this.models.Topic,
},
}
: undefined;

const groups = await this.models.Group.findAll({
where: {
community_id: communityId,
},
include,
});

let groupsResult = groups.map((group) => group.toJSON() as GroupWithExtras);
Expand Down Expand Up @@ -59,23 +69,5 @@ export async function __getGroups(
}));
}

if (includeTopics) {
const topics = await this.models.Topic.findAll({
where: {
community_id: communityId,
group_ids: {
[Op.overlap]: groupsResult.map(({ id }) => id),
},
},
});
groupsResult = groupsResult.map((group) => ({
...group,
topics: topics
.map((t) => t.toJSON())
// @ts-expect-error StrictNullChecks
.filter((t) => t.group_ids.includes(group.id)),
}));
}

return groupsResult;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppError } from '@hicommonwealth/core/src/index';
import { AppError } from '@hicommonwealth/core';
import {
AddressInstance,
MembershipRejectReason,
Expand Down
Loading

0 comments on commit f44fafe

Please sign in to comment.