enzostvs HF staff commited on
Commit
0fd75db
β€’
1 Parent(s): 14679c7

delete img

Browse files
src/lib/components/community/Card.svelte CHANGED
@@ -11,8 +11,9 @@
11
  export let card: CommunityCard;
12
  export let form: Record<string, string> | undefined = undefined;
13
  export let displayReactions: boolean = true;
14
- export let onDelete: ((id: string) => Promise<void>) | undefined = undefined;
15
-
 
16
  let loading = false;
17
 
18
  const handleClick = async () => {
@@ -30,45 +31,51 @@
30
  };
31
 
32
  const handleDelete = async (id: string) => {
33
- if (loading || !onDelete) return;
34
  loading = true
35
- await onDelete?.(id);
 
 
 
 
36
  loading = false;
37
- }
38
  </script>
39
 
40
  <!-- svelte-ignore a11y-no-static-element-interactions -->
41
  <!-- svelte-ignore a11y-click-events-have-key-events -->
42
- <div
43
- class="cursor-pointer group bg-neutral-700 rounded-xl h-[400px] relative flex items-start justify-between flex-col p-5 transition-all duration-200 brightness-90 hover:brightness-100 z-[1] overflow-hidden"
44
- on:click={handleClick}
45
- >
46
- <div class="w-full h-full absolute top-0 left-0 -z-[1] rounded-xl overflow-hidden" class:!brightness-50={loading}>
47
- <img class="w-full h-full bg-center bg-cover transition-all duration-200 group-hover:scale-110 object-cover" src="/api/images/{card.image}" alt="{card.prompt}" />
48
- <div class="bg-gradient-to-b from-transparent via-black/50 to-black/70 absolute h-[100px] bottom-0 left-0 w-full"></div>
49
- </div>
50
- <div class="group-hover:opacity-100 opacity-0 translate-y-full group-hover:translate-y-0 transition-all duration-200 flex flex-col gap-4 w-full">
51
- <div class="bg-black/40 backdrop-blur-sm border border-white/30 rounded-lg px-6 py-3 text-white transition-all duration-200 w-full">
52
- <p class="text-white font-semibold text-lg">{card.prompt}</p>
53
- <p class="text-white/75 font-regular text-base">{card.model.id}</p>
 
 
54
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  </div>
56
- {#if displayReactions}
57
- <Reactions reactions={card.reactions} gallery_id={card.id} />
58
- {/if}
59
- {#if onDelete}
60
- <button
61
- class="absolute bottom-3 right-3 p-2.5 rounded-full bg-red-500 backdrop-blur-sm transition-all duration-200 hover:bg-red-700"
62
- on:click={(e) => {
63
- e.stopPropagation();
64
- e.preventDefault();
65
- handleDelete(card.id);
66
- }}
67
- >
68
- <Icon icon="ic:round-delete" class="text-white w-5 h-5" />
69
- </button>
70
- {/if}
71
- {#if loading}
72
- <Loading />
73
- {/if}
74
- </div>
 
11
  export let card: CommunityCard;
12
  export let form: Record<string, string> | undefined = undefined;
13
  export let displayReactions: boolean = true;
14
+ export let displayDelete: boolean = false;
15
+
16
+ let is_visible = true;
17
  let loading = false;
18
 
19
  const handleClick = async () => {
 
31
  };
32
 
33
  const handleDelete = async (id: string) => {
34
+ if (loading) return;
35
  loading = true
36
+ const request = await fetch(`/api/community/${id}`, {
37
+ method: "DELETE"
38
+ });
39
+ const { success } = await request.json();
40
+ if (success) is_visible = false;
41
  loading = false;
42
+ }
43
  </script>
44
 
45
  <!-- svelte-ignore a11y-no-static-element-interactions -->
46
  <!-- svelte-ignore a11y-click-events-have-key-events -->
47
+ {#if is_visible}
48
+ <div
49
+ class="cursor-pointer group bg-neutral-700 rounded-xl h-[400px] relative flex items-start justify-between flex-col p-5 transition-all duration-200 brightness-90 hover:brightness-100 z-[1] overflow-hidden"
50
+ on:click={handleClick}
51
+ >
52
+ <div class="w-full h-full absolute top-0 left-0 -z-[1] rounded-xl overflow-hidden" class:!brightness-50={loading}>
53
+ <img class="w-full h-full bg-center bg-cover transition-all duration-200 group-hover:scale-110 object-cover" src="/api/images/{card.image}" alt="{card.prompt}" />
54
+ <div class="bg-gradient-to-b from-transparent via-black/50 to-black/70 absolute h-[100px] bottom-0 left-0 w-full"></div>
55
+ </div>
56
+ <div class="group-hover:opacity-100 opacity-0 translate-y-full group-hover:translate-y-0 transition-all duration-200 flex flex-col gap-4 w-full">
57
+ <div class="bg-black/40 backdrop-blur-sm border border-white/30 rounded-lg px-6 py-3 text-white transition-all duration-200 w-full">
58
+ <p class="text-white font-semibold text-lg">{card.prompt}</p>
59
+ <p class="text-white/75 font-regular text-base">{card.model.id}</p>
60
+ </div>
61
  </div>
62
+ {#if displayReactions}
63
+ <Reactions reactions={card.reactions} gallery_id={card.id} />
64
+ {/if}
65
+ {#if displayDelete}
66
+ <button
67
+ class="absolute bottom-3 right-3 p-2.5 rounded-full bg-red-500 backdrop-blur-sm transition-all duration-200 hover:bg-red-700"
68
+ on:click={(e) => {
69
+ e.stopPropagation();
70
+ e.preventDefault();
71
+ handleDelete(card.id);
72
+ }}
73
+ >
74
+ <Icon icon="ic:round-delete" class="text-white w-5 h-5" />
75
+ </button>
76
+ {/if}
77
+ {#if loading}
78
+ <Loading />
79
+ {/if}
80
  </div>
81
+ {/if}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/routes/api/community/[id]/+server.ts CHANGED
@@ -145,8 +145,8 @@ export async function DELETE({ params, cookies }: RequestEvent) {
145
  }, { status: 401 })
146
  }
147
 
148
- const is_token_available = await tokenIsAvailable(token)
149
- if (!is_token_available) {
150
  return json({
151
  error: "Invalid token"
152
  }, { status: 401 })
@@ -155,10 +155,16 @@ export async function DELETE({ params, cookies }: RequestEvent) {
155
  const gallery = await prisma.gallery.findFirst({
156
  where: {
157
  id,
158
- userId: is_token_available.sub
159
  }
160
  })
161
 
 
 
 
 
 
 
162
  if (!gallery) {
163
  return json({
164
  error: "Gallery not found"
 
145
  }, { status: 401 })
146
  }
147
 
148
+ const user = await tokenIsAvailable(token)
149
+ if (!user) {
150
  return json({
151
  error: "Invalid token"
152
  }, { status: 401 })
 
155
  const gallery = await prisma.gallery.findFirst({
156
  where: {
157
  id,
158
+ userId: user.sub
159
  }
160
  })
161
 
162
+ if (gallery?.userId !== user.sub || !process?.env?.SECRET_HF_ADMIN?.includes(user?.sub) ) {
163
+ return json({
164
+ error: "You are not authorized to delete this gallery"
165
+ }, { status: 401 })
166
+ }
167
+
168
  if (!gallery) {
169
  return json({
170
  error: "Gallery not found"
src/routes/gallery/+page.svelte CHANGED
@@ -8,11 +8,12 @@
8
  import Radio from "$lib/components/fields/Radio.svelte";
9
  import { COMMUNITY_FILTER_OPTIONS } from "$lib/utils/index.js";
10
  import GoTop from "$lib/components/GoTop.svelte";
11
- // import GalleryViewer from "$lib/components/community/viewer/Viewer.svelte";
12
  import GalleryDrawer from "$lib/components/community/drawer/Drawer.svelte";
13
- // import UserIsLogged from "$lib/components/UserIsLogged.svelte";
 
14
 
15
  export let data
 
16
 
17
  let form = {
18
  filter: "new",
@@ -75,7 +76,7 @@
75
  <!-- mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-4 3xl:grid-cols-5 gap-5 mt-8 lg:mt-10 -->
76
  <div class="mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 2xl:grid-cols-4 gap-5 mt-8 lg:mt-10">
77
  {#each data.cards as card}
78
- <Card card={card} form={form} />
79
  {/each}
80
  <InfiniteScroll
81
  elementScroll="{elementScroll ?? undefined}"
 
8
  import Radio from "$lib/components/fields/Radio.svelte";
9
  import { COMMUNITY_FILTER_OPTIONS } from "$lib/utils/index.js";
10
  import GoTop from "$lib/components/GoTop.svelte";
 
11
  import GalleryDrawer from "$lib/components/community/drawer/Drawer.svelte";
12
+ import { get } from "svelte/store";
13
+ import { userStore } from "$lib/stores/use-user";
14
 
15
  export let data
16
+ let user = get(userStore);
17
 
18
  let form = {
19
  filter: "new",
 
76
  <!-- mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-4 3xl:grid-cols-5 gap-5 mt-8 lg:mt-10 -->
77
  <div class="mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 2xl:grid-cols-4 gap-5 mt-8 lg:mt-10">
78
  {#each data.cards as card}
79
+ <Card card={card} form={form} displayDelete={user?.is_admin} />
80
  {/each}
81
  <InfiniteScroll
82
  elementScroll="{elementScroll ?? undefined}"
src/routes/saved-generations/+page.svelte CHANGED
@@ -4,19 +4,6 @@
4
  import GalleryViewer from "$lib/components/community/viewer/Viewer.svelte";
5
 
6
  export let data
7
-
8
- const handleDelete = async (id: string) => {
9
- const request = await fetch(`/api/community/${id}`, {
10
- method: "DELETE"
11
- });
12
- const { success } = await request.json();
13
- if (success) {
14
- data = {
15
- ...data,
16
- cards: data.cards.filter((card: any) => card.id !== id)
17
- }
18
- }
19
- }
20
  </script>
21
 
22
  <svelte:head>
@@ -30,7 +17,7 @@
30
  </h1>
31
  <div class="mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 2xl:grid-cols-4 gap-5 mt-8 lg:mt-10">
32
  {#each data.cards as card}
33
- <Card card={card} displayReactions={false} onDelete={handleDelete} />
34
  {/each}
35
  <GoTop />
36
  </div>
 
4
  import GalleryViewer from "$lib/components/community/viewer/Viewer.svelte";
5
 
6
  export let data
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  </script>
8
 
9
  <svelte:head>
 
17
  </h1>
18
  <div class="mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 2xl:grid-cols-4 gap-5 mt-8 lg:mt-10">
19
  {#each data.cards as card}
20
+ <Card card={card} displayReactions={false} displayDelete={true} />
21
  {/each}
22
  <GoTop />
23
  </div>