nsarrazin commited on
Commit
0790b8b
·
1 Parent(s): 29e4d49

fix: share feature

Browse files
src/lib/shareConversation.ts CHANGED
@@ -1,14 +1,16 @@
1
  import { base } from "$app/paths";
2
  import { ERROR_MESSAGES, error } from "$lib/stores/errors";
3
  import { share } from "./utils/share";
4
- import { page } from "$app/stores";
5
- import { get } from "svelte/store";
6
- import { getShareUrl } from "./utils/getShareUrl";
7
  export async function shareConversation(id: string, title: string) {
8
  try {
9
  if (id.length === 7) {
10
- const url = get(page).url;
11
- await share(getShareUrl(url, id), title, true);
 
 
 
12
  } else {
13
  const res = await fetch(`${base}/conversation/${id}/share`, {
14
  method: "POST",
@@ -23,8 +25,14 @@ export async function shareConversation(id: string, title: string) {
23
  return;
24
  }
25
 
26
- const { url } = await res.json();
27
- await share(url, title, true);
 
 
 
 
 
 
28
  }
29
  } catch (err) {
30
  error.set(ERROR_MESSAGES.default);
 
1
  import { base } from "$app/paths";
2
  import { ERROR_MESSAGES, error } from "$lib/stores/errors";
3
  import { share } from "./utils/share";
4
+ import { page } from "$app/state";
5
+
 
6
  export async function shareConversation(id: string, title: string) {
7
  try {
8
  if (id.length === 7) {
9
+ const shareUrl = `${
10
+ page.data.publicConfig.PUBLIC_SHARE_PREFIX ||
11
+ `${page.data.publicConfig.PUBLIC_ORIGIN || page.url.origin}${base}`
12
+ }/r/${id}`;
13
+ await share(shareUrl, title, true);
14
  } else {
15
  const res = await fetch(`${base}/conversation/${id}/share`, {
16
  method: "POST",
 
25
  return;
26
  }
27
 
28
+ const { shareId } = await res.json();
29
+
30
+ const shareUrl = `${
31
+ page.data.publicConfig.PUBLIC_SHARE_PREFIX ||
32
+ `${page.data.publicConfig.PUBLIC_ORIGIN || page.url.origin}${base}`
33
+ }/r/${shareId}`;
34
+
35
+ await share(shareUrl, title, true);
36
  }
37
  } catch (err) {
38
  error.set(ERROR_MESSAGES.default);
src/lib/utils/getShareUrl.ts DELETED
@@ -1,9 +0,0 @@
1
- import { base } from "$app/paths";
2
- import { page } from "$app/state";
3
-
4
- export function getShareUrl(url: URL, shareId: string): string {
5
- return `${
6
- page.data.publicConfig.PUBLIC_SHARE_PREFIX ||
7
- `${page.data.publicConfig.PUBLIC_ORIGIN || url.origin}${base}`
8
- }/r/${shareId}`;
9
- }
 
 
 
 
 
 
 
 
 
 
src/routes/conversation/[id]/share/+server.ts CHANGED
@@ -1,13 +1,12 @@
1
  import { authCondition } from "$lib/server/auth";
2
  import { collections } from "$lib/server/database";
3
  import type { SharedConversation } from "$lib/types/SharedConversation";
4
- import { getShareUrl } from "$lib/utils/getShareUrl";
5
  import { hashConv } from "$lib/utils/hashConv";
6
  import { error } from "@sveltejs/kit";
7
  import { ObjectId } from "mongodb";
8
  import { nanoid } from "nanoid";
9
 
10
- export async function POST({ params, url, locals }) {
11
  const conversation = await collections.conversations.findOne({
12
  _id: new ObjectId(params.id),
13
  ...authCondition(locals),
@@ -24,7 +23,7 @@ export async function POST({ params, url, locals }) {
24
  if (existingShare) {
25
  return new Response(
26
  JSON.stringify({
27
- url: getShareUrl(url, existingShare._id),
28
  }),
29
  { headers: { "Content-Type": "application/json" } }
30
  );
@@ -65,7 +64,7 @@ export async function POST({ params, url, locals }) {
65
 
66
  return new Response(
67
  JSON.stringify({
68
- url: getShareUrl(url, shared._id),
69
  }),
70
  { headers: { "Content-Type": "application/json" } }
71
  );
 
1
  import { authCondition } from "$lib/server/auth";
2
  import { collections } from "$lib/server/database";
3
  import type { SharedConversation } from "$lib/types/SharedConversation";
 
4
  import { hashConv } from "$lib/utils/hashConv";
5
  import { error } from "@sveltejs/kit";
6
  import { ObjectId } from "mongodb";
7
  import { nanoid } from "nanoid";
8
 
9
+ export async function POST({ params, locals }) {
10
  const conversation = await collections.conversations.findOne({
11
  _id: new ObjectId(params.id),
12
  ...authCondition(locals),
 
23
  if (existingShare) {
24
  return new Response(
25
  JSON.stringify({
26
+ shareId: existingShare._id,
27
  }),
28
  { headers: { "Content-Type": "application/json" } }
29
  );
 
64
 
65
  return new Response(
66
  JSON.stringify({
67
+ shareId: shared._id,
68
  }),
69
  { headers: { "Content-Type": "application/json" } }
70
  );