Skip to content

Commit 3109a5e

Browse files
nsarrazingary149
andauthored
Ask for a reason when reporting assistants (#825)
* Show modal asking for assistant report reason * fix disabled button styling * parse with zod * Update src/routes/settings/assistants/[assistantId]/ReportModal.svelte Co-authored-by: Victor Muštar <victor.mustar@gmail.com> * use a text area with max-w --------- Co-authored-by: Victor Muštar <victor.mustar@gmail.com>
1 parent 7b7114b commit 3109a5e

File tree

5 files changed

+85
-9
lines changed

5 files changed

+85
-9
lines changed

src/lib/components/Modal.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
role="presentation"
4747
tabindex="-1"
4848
bind:this={backdropEl}
49-
on:click={handleBackdropClick}
49+
on:click|stopPropagation={handleBackdropClick}
5050
transition:fade|global={{ easing: cubicOut, duration: 300 }}
5151
class="fixed inset-0 z-40 flex items-center justify-center bg-black/80 p-8 backdrop-blur-sm dark:bg-black/50"
5252
>

src/lib/types/Report.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export interface Report extends Timestamps {
77
_id: ObjectId;
88
createdBy: User["_id"] | string;
99
assistantId: Assistant["_id"];
10+
reason?: string;
1011
}

src/routes/settings/assistants/[assistantId]/+page.server.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { authCondition } from "$lib/server/auth";
55
import { base } from "$app/paths";
66
import { PUBLIC_ORIGIN, PUBLIC_SHARE_PREFIX } from "$env/static/public";
77
import { WEBHOOK_URL_REPORT_ASSISTANT } from "$env/static/private";
8-
8+
import { z } from "zod";
99
async function assistantOnlyIfAuthor(locals: App.Locals, assistantId?: string) {
1010
const assistant = await collections.assistants.findOne({ _id: new ObjectId(assistantId) });
1111

@@ -53,7 +53,7 @@ export const actions: Actions = {
5353

5454
throw redirect(302, `${base}/settings`);
5555
},
56-
report: async ({ params, locals, url }) => {
56+
report: async ({ request, params, locals, url }) => {
5757
// is there already a report from this user for this model ?
5858
const report = await collections.reports.findOne({
5959
assistantId: new ObjectId(params.assistantId),
@@ -64,12 +64,20 @@ export const actions: Actions = {
6464
return fail(400, { error: true, message: "Already reported" });
6565
}
6666

67+
const formData = await request.formData();
68+
const result = z.string().min(1).max(128).safeParse(formData?.get("reportReason"));
69+
70+
if (!result.success) {
71+
return fail(400, { error: true, message: "Invalid report reason" });
72+
}
73+
6774
const { acknowledged } = await collections.reports.insertOne({
6875
_id: new ObjectId(),
6976
assistantId: new ObjectId(params.assistantId),
7077
createdBy: locals.user?._id ?? locals.sessionId,
7178
createdAt: new Date(),
7279
updatedAt: new Date(),
80+
reason: result.data,
7381
});
7482

7583
if (!acknowledged) {
@@ -91,7 +99,7 @@ export const actions: Actions = {
9199
"Content-type": "application/json",
92100
},
93101
body: JSON.stringify({
94-
text: `Assistant <${assistantUrl}|${assistant?.name}> reported by <http://hf.co/${locals.user?.username}|${locals.user?.username}>`,
102+
text: `Assistant <${assistantUrl}|${assistant?.name}> reported by <http://hf.co/${locals.user?.username}|${locals.user?.username}>. The following reason was given \n\n> ${result.data}`,
95103
}),
96104
});
97105

src/routes/settings/assistants/[assistantId]/+page.svelte

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import CarbonFlag from "~icons/carbon/flag";
1313
import CarbonLink from "~icons/carbon/link";
1414
import CopyToClipBoardBtn from "$lib/components/CopyToClipBoardBtn.svelte";
15+
import ReportModal from "./ReportModal.svelte";
1516
1617
export let data: PageData;
1718
@@ -24,8 +25,13 @@
2425
const prefix = PUBLIC_SHARE_PREFIX || `${PUBLIC_ORIGIN || $page.url.origin}${base}`;
2526
2627
$: shareUrl = `${prefix}/assistant/${assistant?._id}`;
28+
29+
let displayReportModal = false;
2730
</script>
2831

32+
{#if displayReportModal}
33+
<ReportModal on:close={() => (displayReportModal = false)} />
34+
{/if}
2935
<div class="flex h-full flex-col gap-2">
3036
<div class="flex gap-6">
3137
{#if assistant?.avatar}
@@ -102,11 +108,15 @@
102108
>
103109
</form>
104110
{#if !assistant?.reported}
105-
<form method="POST" action="?/report" use:enhance>
106-
<button type="submit" class="underline">
107-
<CarbonFlag class="mr-1.5 inline text-xs" />Report</button
108-
>
109-
</form>
111+
<button
112+
type="button"
113+
on:click={() => {
114+
displayReportModal = true;
115+
}}
116+
class="underline"
117+
>
118+
<CarbonFlag class="mr-1.5 inline text-xs" />Report
119+
</button>
110120
{:else}
111121
<button type="button" disabled class="text-gray-700">
112122
<CarbonFlag class="mr-1.5 inline text-xs" />Reported</button
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<script lang="ts">
2+
import { applyAction, enhance } from "$app/forms";
3+
import { invalidateAll } from "$app/navigation";
4+
import Modal from "$lib/components/Modal.svelte";
5+
import { createEventDispatcher } from "svelte";
6+
7+
const dispatch = createEventDispatcher<{ close: void }>();
8+
9+
let reason = "";
10+
</script>
11+
12+
<Modal on:close>
13+
<form
14+
method="POST"
15+
action="?/report"
16+
use:enhance={() => {
17+
return async ({ result }) => {
18+
await applyAction(result);
19+
dispatch("close");
20+
invalidateAll();
21+
};
22+
}}
23+
class="w-full min-w-64 p-4"
24+
>
25+
<span class="mb-1 text-sm font-semibold">Report an assistant</span>
26+
27+
<p class="text-sm text-gray-500">
28+
Please provide a brief description of why you are reporting this assistant.
29+
</p>
30+
31+
<textarea
32+
name="reportReason"
33+
class="mt-6 max-h-48 w-full resize-y rounded-lg border-2 border-gray-200 bg-gray-100 p-2 text-smd"
34+
placeholder="Reason(s) for the report"
35+
maxlength="128"
36+
bind:value={reason}
37+
/>
38+
39+
<div class="flex w-full flex-row justify-between px-2 pt-4">
40+
<button
41+
type="button"
42+
class="text-sm text-gray-700 hover:underline"
43+
on:click={() => dispatch("close")}>Cancel</button
44+
>
45+
46+
<button
47+
type="submit"
48+
class="rounded-full bg-black px-4 py-2 text-sm font-semibold text-white md:px-8"
49+
disabled={!reason}
50+
class:bg-gray-200={!reason}
51+
class:!text-gray-400={!reason}
52+
>
53+
Submit report
54+
</button>
55+
</div>
56+
</form>
57+
</Modal>

0 commit comments

Comments
 (0)