enzostvs HF staff commited on
Commit
7398577
β€’
1 Parent(s): 404baa5

replace share with a post reauest

Browse files
src/lib/components/community/reactions/Reaction.svelte CHANGED
@@ -23,7 +23,6 @@
23
  </script>
24
 
25
  <UserIsLogged>
26
- <!-- liked -->
27
  <button
28
  class="rounded-full bg-white text-neutral-800 font-bold flex items-center justify-start gap-1.5 px-3 py-1 border border-white hover:bg-neutral-200 text-sm"
29
  class:bg-opacity-60={!liked}
 
23
  </script>
24
 
25
  <UserIsLogged>
 
26
  <button
27
  class="rounded-full bg-white text-neutral-800 font-bold flex items-center justify-start gap-1.5 px-3 py-1 border border-white hover:bg-neutral-200 text-sm"
28
  class:bg-opacity-60={!liked}
src/lib/components/community/reactions/Reactions.svelte CHANGED
@@ -16,7 +16,7 @@
16
  return {
17
  emoji,
18
  count: reactions.filter((reaction) => reaction.emoji === emoji).length,
19
- liked: !!reactions.find((reaction) => reaction.emoji === emoji && reaction.hf_user_id === user?.sub)
20
  };
21
  });
22
  };
@@ -34,7 +34,7 @@
34
  if (deleted) {
35
  reactions = reactions.filter((reaction) => reaction.id !== id);
36
  } else {
37
- reactions = [...reactions, { emoji, hf_user_id: user?.sub, galleryId: gallery_id, id }];
38
  }
39
  }}
40
  />
@@ -44,6 +44,6 @@
44
  reactions={groupedReactions}
45
  {gallery_id}
46
  onAdd={(emoji, id) => {
47
- reactions = [...reactions, { emoji, hf_user_id: user?.sub, galleryId: gallery_id, id }];
48
  }}
49
  />
 
16
  return {
17
  emoji,
18
  count: reactions.filter((reaction) => reaction.emoji === emoji).length,
19
+ liked: !!reactions.find((reaction) => reaction.emoji === emoji && reaction.userId === user?.sub)
20
  };
21
  });
22
  };
 
34
  if (deleted) {
35
  reactions = reactions.filter((reaction) => reaction.id !== id);
36
  } else {
37
+ reactions = [...reactions, { emoji, userId: user?.sub, galleryId: gallery_id, id }];
38
  }
39
  }}
40
  />
 
44
  reactions={groupedReactions}
45
  {gallery_id}
46
  onAdd={(emoji, id) => {
47
+ reactions = [...reactions, { emoji, userId: user?.sub, galleryId: gallery_id, id }];
48
  }}
49
  />
src/lib/components/generate/Response.svelte CHANGED
@@ -1,15 +1,16 @@
1
  <script lang="ts">
2
  import { get } from "svelte/store";
3
  import { generationStore } from "$lib/stores/use-generation";
4
- import UserIsLogged from '$lib/components/UserIsLogged.svelte';
5
 
6
  import Button from "$lib/components/Button.svelte";
 
7
 
8
  let generation = get(generationStore);
9
  export let loading_generation: boolean = false;
10
 
11
  let loading: boolean = false;
12
  let already_saved: boolean = false;
 
13
 
14
  const saveImage = () => {
15
  const link = document.createElement('a');
@@ -23,12 +24,11 @@
23
  const share = () => {
24
  if (loading) return;
25
  loading = true;
26
- fetch(`/api/generate/share`, {
27
  method: "POST",
28
  headers: {
29
  "Content-Type": "application/json"
30
  },
31
- body: JSON.stringify({ image: generation?.image, generation: generation?.form })
32
  }).then(() => {
33
  loading = false;
34
  already_saved = true;
@@ -85,7 +85,7 @@
85
  <div class="p-8 w-full">
86
  <div class="w-full flex items-center justify-end gap-4">
87
  <Button size="lg" theme="light" icon="material-symbols:save" iconPosition="right" onClick={saveImage}>Download</Button>
88
- <!-- <UserIsLogged> -->
89
  <Button
90
  size="lg"
91
  theme="blue"
@@ -93,21 +93,16 @@
93
  iconPosition="right"
94
  loading={loading}
95
  onClick={share}
96
- >
97
- <!-- disabled={loading || already_saved} -->
98
  {#if already_saved}
99
  Shared!
100
  {:else}
101
  Share with community
102
  {/if}
103
  </Button>
104
- <!-- </UserIsLogged> -->
105
  </div>
106
- <p class="text-neutral-500 text-sm text-right mt-2.5">
107
- All images not shared with the community are deleted right after generation.
108
- <br>
109
- Your informations are not shared with anyone.
110
- </p>
111
  {#if generation?.form}
112
  <div class="mt-6 grid grid-cols-1 gap-4">
113
  <div>
 
1
  <script lang="ts">
2
  import { get } from "svelte/store";
3
  import { generationStore } from "$lib/stores/use-generation";
 
4
 
5
  import Button from "$lib/components/Button.svelte";
6
+ import { userStore } from "$lib/stores/use-user";
7
 
8
  let generation = get(generationStore);
9
  export let loading_generation: boolean = false;
10
 
11
  let loading: boolean = false;
12
  let already_saved: boolean = false;
13
+ let user = get(userStore)
14
 
15
  const saveImage = () => {
16
  const link = document.createElement('a');
 
24
  const share = () => {
25
  if (loading) return;
26
  loading = true;
27
+ fetch(`/api/community/${generation?.gallery?.id}/publish`, {
28
  method: "POST",
29
  headers: {
30
  "Content-Type": "application/json"
31
  },
 
32
  }).then(() => {
33
  loading = false;
34
  already_saved = true;
 
85
  <div class="p-8 w-full">
86
  <div class="w-full flex items-center justify-end gap-4">
87
  <Button size="lg" theme="light" icon="material-symbols:save" iconPosition="right" onClick={saveImage}>Download</Button>
88
+ {#if user?.sub}
89
  <Button
90
  size="lg"
91
  theme="blue"
 
93
  iconPosition="right"
94
  loading={loading}
95
  onClick={share}
96
+ disabled={loading || already_saved}
97
+ >
98
  {#if already_saved}
99
  Shared!
100
  {:else}
101
  Share with community
102
  {/if}
103
  </Button>
104
+ {/if}
105
  </div>
 
 
 
 
 
106
  {#if generation?.form}
107
  <div class="mt-6 grid grid-cols-1 gap-4">
108
  <div>
src/routes/api/community/+server.ts CHANGED
@@ -39,6 +39,7 @@ export async function GET(request : RequestEvent) {
39
 
40
  const total_reposId = await prisma.gallery.count({
41
  where: {
 
42
  OR: [
43
  { prompt: { contains: search } },
44
  ]
 
39
 
40
  const total_reposId = await prisma.gallery.count({
41
  where: {
42
+ isPublic: true,
43
  OR: [
44
  { prompt: { contains: search } },
45
  ]
src/routes/api/community/[id]/publish/+server.ts ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { json, type RequestEvent } from '@sveltejs/kit';
2
+ import { tokenIsAvailable } from '$lib/utils';
3
+ import prisma from '$lib/prisma';
4
+
5
+ /** @type {import('./$types').RequestHandler} */
6
+
7
+ export async function POST({ cookies, params } : RequestEvent) {
8
+ const id = params.id
9
+
10
+ const gallery = await prisma.gallery.findFirst({
11
+ where: {
12
+ id,
13
+ },
14
+ })
15
+ if (!gallery) {
16
+ return json({
17
+ error: "Image not found",
18
+ }, { status: 404 })
19
+ }
20
+
21
+ const token = cookies.get('hf_access_token')
22
+ if (!token) {
23
+ return json({
24
+ error: "You must be logged",
25
+ }, { status: 401 })
26
+ }
27
+
28
+ const is_token_available = await tokenIsAvailable(token)
29
+ if (!is_token_available) {
30
+ return json({
31
+ error: "Invalid token",
32
+ }, { status: 401 })
33
+ }
34
+
35
+ await prisma.gallery.update({
36
+ where: {
37
+ id,
38
+ },
39
+ data: {
40
+ isPublic: true,
41
+ }
42
+ })
43
+
44
+ return json({
45
+ success: true,
46
+ })
47
+ }
src/routes/api/generate/+server.ts CHANGED
@@ -63,9 +63,9 @@ export async function POST({ request, cookies } : RequestEvent) {
63
  if (token) {
64
  const user = await tokenIsAvailable(token)
65
  if (user?.sub) {
66
- const dir = await promises.opendir(env.SECRET_FILE_UPLOAD_DIR).catch(() => null)
67
  if (!dir) {
68
- await promises.mkdir(env.SECRET_FILE_UPLOAD_DIR)
69
  }
70
  const file_name_formatted = randomUUID() + "_" + generation?.inputs?.replaceAll(/[^a-zA-Z0-9]/g, "-") + ".png"
71
  await promises.writeFile(`${publicEnv.PUBLIC_FILE_UPLOAD_DIR}/${file_name_formatted}`, response)
 
63
  if (token) {
64
  const user = await tokenIsAvailable(token)
65
  if (user?.sub) {
66
+ const dir = await promises.opendir(publicEnv.PUBLIC_FILE_UPLOAD_DIR).catch(() => null)
67
  if (!dir) {
68
+ await promises.mkdir(publicEnv.PUBLIC_FILE_UPLOAD_DIR)
69
  }
70
  const file_name_formatted = randomUUID() + "_" + generation?.inputs?.replaceAll(/[^a-zA-Z0-9]/g, "-") + ".png"
71
  await promises.writeFile(`${publicEnv.PUBLIC_FILE_UPLOAD_DIR}/${file_name_formatted}`, response)
src/routes/api/models/[id]/+server.ts CHANGED
@@ -25,6 +25,9 @@ export async function GET({ url, params } : RequestEvent) {
25
  prompt: true,
26
  image: true,
27
  createdAt: true,
 
 
 
28
  }
29
  },
30
  comments: {
 
25
  prompt: true,
26
  image: true,
27
  createdAt: true,
28
+ },
29
+ where: {
30
+ isPublic: true
31
  }
32
  },
33
  comments: {