Skip to content

Commit d7fb8aa

Browse files
authored
Fixed a bug where moderators cannot delete their own content (#370)
1 parent e3e503b commit d7fb8aa

4 files changed

Lines changed: 19 additions & 41 deletions

File tree

services/agora/src/utils/actions/core/permissions.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,21 @@ export function useActionPermissions(): ContentActionPermissionCheckers {
1515
const canDelete = (context: ContentActionContext): boolean => {
1616
if (context.targetType === "post") {
1717
// For posts, can only delete if owner and not in embedded mode
18-
return context.userRole === "owner" && !context.isEmbeddedMode;
18+
return context.isOwner && !context.isEmbeddedMode;
1919
} else {
2020
// For comments, can delete if owner (regardless of embed mode)
21-
return context.userRole === "owner";
21+
return context.isOwner;
2222
}
2323
};
2424

2525
const canModerate = (context: ContentActionContext): boolean => {
2626
// Can moderate if user is a moderator and not in embedded mode
27-
return context.userRole === "moderator" && !context.isEmbeddedMode;
27+
return context.isModerator && !context.isEmbeddedMode;
2828
};
2929

3030
const canMute = (context: ContentActionContext): boolean => {
3131
// Can mute if not the owner, user is logged in, and not in embedded mode
32-
return (
33-
context.userRole !== "owner" &&
34-
context.isLoggedIn &&
35-
!context.isEmbeddedMode
36-
);
32+
return !context.isOwner && context.isLoggedIn && !context.isEmbeddedMode;
3733
};
3834

3935
const canReport = (): boolean => {
@@ -48,7 +44,7 @@ export function useActionPermissions(): ContentActionPermissionCheckers {
4844

4945
const canViewUserReports = (context: ContentActionContext): boolean => {
5046
// Only moderators can view user reports, and not in embedded mode
51-
return context.userRole === "moderator" && !context.isEmbeddedMode;
47+
return context.isModerator && !context.isEmbeddedMode;
5248
};
5349

5450
const canViewModerationHistory = (): boolean => {
@@ -73,20 +69,6 @@ export function useActionPermissions(): ContentActionPermissionCheckers {
7369
};
7470
}
7571

76-
/**
77-
* Helper function to determine user role based on context
78-
*/
79-
export function determineUserRole(
80-
isOwner: boolean,
81-
isModerator: boolean,
82-
isLoggedIn: boolean
83-
): ContentActionContext["userRole"] {
84-
if (isModerator) return "moderator";
85-
if (isOwner) return "owner";
86-
if (isLoggedIn) return "user";
87-
return "anonymous";
88-
}
89-
9072
/**
9173
* Helper function to create action context
9274
*/
@@ -100,12 +82,12 @@ export function createActionContext(
10082
isEmbeddedMode: boolean
10183
): ContentActionContext {
10284
const isOwner = currentUser === targetAuthor;
103-
const userRole = determineUserRole(isOwner, isModerator, isLoggedIn);
10485

10586
return {
106-
userRole,
107-
isEmbeddedMode,
87+
isOwner,
88+
isModerator,
10889
isLoggedIn,
90+
isEmbeddedMode,
10991
targetType,
11092
targetId,
11193
targetAuthor,

services/agora/src/utils/actions/core/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ export interface BaseContentAction {
1414

1515
// Context for determining available content actions
1616
export interface ContentActionContext {
17-
userRole: "owner" | "moderator" | "user" | "anonymous";
18-
isEmbeddedMode: boolean;
17+
isOwner: boolean;
18+
isModerator: boolean;
1919
isLoggedIn: boolean;
20+
isEmbeddedMode: boolean;
2021
targetType: "post" | "comment";
2122
targetId: string;
2223
targetAuthor: string;

services/agora/src/utils/actions/definitions/comments.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,15 @@ export function getCommentActions(
4545
variant: "warning",
4646
handler: muteUserCallback,
4747
isVisible: (context: ContentActionContext) =>
48-
context.userRole !== "owner" &&
49-
context.isLoggedIn &&
50-
!context.isEmbeddedMode,
48+
!context.isOwner && context.isLoggedIn && !context.isEmbeddedMode,
5149
},
5250
{
5351
id: "delete",
5452
label: translations.delete,
5553
icon: "mdi-delete",
5654
variant: "destructive",
5755
handler: deleteCommentCallback,
58-
isVisible: (context: ContentActionContext) =>
59-
context.userRole === "owner",
56+
isVisible: (context: ContentActionContext) => context.isOwner,
6057
},
6158
{
6259
id: "moderate",
@@ -65,15 +62,15 @@ export function getCommentActions(
6562
variant: "warning",
6663
handler: moderateCommentCallback,
6764
isVisible: (context: ContentActionContext) =>
68-
context.userRole === "moderator" && !context.isEmbeddedMode,
65+
context.isModerator && !context.isEmbeddedMode,
6966
},
7067
{
7168
id: "userReports",
7269
label: translations.userReports,
7370
icon: "mdi-account-alert",
7471
handler: openUserReportsCallback,
7572
isVisible: (context: ContentActionContext) =>
76-
context.userRole === "moderator" && !context.isEmbeddedMode,
73+
context.isModerator && !context.isEmbeddedMode,
7774
},
7875
{
7976
id: "share",

services/agora/src/utils/actions/definitions/posts.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ export function getPostActions(
4747
variant: "warning",
4848
handler: muteUserCallback,
4949
isVisible: (context: ContentActionContext) =>
50-
context.userRole !== "owner" &&
51-
context.isLoggedIn &&
52-
!context.isEmbeddedMode,
50+
!context.isOwner && context.isLoggedIn && !context.isEmbeddedMode,
5351
},
5452
{
5553
id: "delete",
@@ -58,7 +56,7 @@ export function getPostActions(
5856
variant: "destructive",
5957
handler: deletePostCallback,
6058
isVisible: (context: ContentActionContext) =>
61-
context.userRole === "owner" && !context.isEmbeddedMode,
59+
context.isOwner && !context.isEmbeddedMode,
6260
},
6361
{
6462
id: "moderationHistory",
@@ -81,15 +79,15 @@ export function getPostActions(
8179
variant: "warning",
8280
handler: moderatePostCallback,
8381
isVisible: (context: ContentActionContext) =>
84-
context.userRole === "moderator" && !context.isEmbeddedMode,
82+
context.isModerator && !context.isEmbeddedMode,
8583
},
8684
{
8785
id: "userReports",
8886
label: translations.userReports,
8987
icon: "mdi-account-alert",
9088
handler: openUserReportsCallback,
9189
isVisible: (context: ContentActionContext) =>
92-
context.userRole === "moderator" && !context.isEmbeddedMode,
90+
context.isModerator && !context.isEmbeddedMode,
9391
},
9492
];
9593
}

0 commit comments

Comments
 (0)