From d8b4e86a906ca6e08dc1ec9f5f43f6fc4c4d0e1c Mon Sep 17 00:00:00 2001 From: Luke Vella Date: Wed, 28 Jun 2023 15:51:00 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Allow=20admin=20to=20delete=20an?= =?UTF-8?q?y=20comment=20(#715)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web/src/components/discussion/discussion.tsx | 10 +++++++--- apps/web/src/contexts/permissions.ts | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/discussion/discussion.tsx b/apps/web/src/components/discussion/discussion.tsx index b55b48c75..9e06f1a98 100644 --- a/apps/web/src/components/discussion/discussion.tsx +++ b/apps/web/src/components/discussion/discussion.tsx @@ -18,6 +18,8 @@ import * as React from "react"; import { Controller, useForm } from "react-hook-form"; import { Trans } from "@/components/trans"; +import { usePermissions } from "@/contexts/permissions"; +import { useRole } from "@/contexts/role"; import { usePostHog } from "@/utils/posthog"; import { requiredString } from "../../utils/form-validation"; @@ -25,7 +27,7 @@ import NameInput from "../name-input"; import TruncatedLinkify from "../poll/truncated-linkify"; import UserAvatar from "../poll/user-avatar"; import { usePoll } from "../poll-context"; -import { isUnclaimed, useUser } from "../user-provider"; +import { useUser } from "../user-provider"; interface CommentForm { authorName: string; @@ -34,7 +36,7 @@ interface CommentForm { const Discussion: React.FunctionComponent = () => { const { t } = useTranslation(); - const { poll, admin } = usePoll(); + const { poll } = usePoll(); const pollId = poll.id; @@ -80,6 +82,8 @@ const Discussion: React.FunctionComponent = () => { }); const [isWriting, setIsWriting] = React.useState(false); + const role = useRole(); + const { isUser } = usePermissions(); if (!comments) { return null; @@ -95,7 +99,7 @@ const Discussion: React.FunctionComponent = () => {
{comments.map((comment) => { const canDelete = - admin || session.ownsObject(comment) || isUnclaimed(comment); + role === "admin" || (comment.userId && isUser(comment.userId)); return (
diff --git a/apps/web/src/contexts/permissions.ts b/apps/web/src/contexts/permissions.ts index 20baba928..0f7588706 100644 --- a/apps/web/src/contexts/permissions.ts +++ b/apps/web/src/contexts/permissions.ts @@ -19,6 +19,7 @@ export const usePermissions = () => { const { participants } = useParticipants(); const isClosed = poll.closed === true || poll.event !== null; return { + isUser: (userId: string) => userId === user.id || userId === context.userId, canAddNewParticipant: !isClosed, canEditParticipant: (participantId: string) => { if (isClosed) {