Skip to content

Commit 6d2f047

Browse files
committed
fix: share feature
1 parent 7fb29ea commit 6d2f047

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

src/lib/shareConversation.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { base } from "$app/paths";
22
import { ERROR_MESSAGES, error } from "$lib/stores/errors";
33
import { share } from "./utils/share";
4-
import { page } from "$app/stores";
5-
import { get } from "svelte/store";
6-
import { getShareUrl } from "./utils/getShareUrl";
4+
import { page } from "$app/state";
5+
76
export async function shareConversation(id: string, title: string) {
87
try {
98
if (id.length === 7) {
10-
const url = get(page).url;
11-
await share(getShareUrl(url, id), title, true);
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);
1214
} else {
1315
const res = await fetch(`${base}/conversation/${id}/share`, {
1416
method: "POST",
@@ -23,8 +25,14 @@ export async function shareConversation(id: string, title: string) {
2325
return;
2426
}
2527

26-
const { url } = await res.json();
27-
await share(url, title, true);
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);
2836
}
2937
} catch (err) {
3038
error.set(ERROR_MESSAGES.default);

src/lib/utils/getShareUrl.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/routes/conversation/[id]/share/+server.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { authCondition } from "$lib/server/auth";
22
import { collections } from "$lib/server/database";
33
import type { SharedConversation } from "$lib/types/SharedConversation";
4-
import { getShareUrl } from "$lib/utils/getShareUrl";
54
import { hashConv } from "$lib/utils/hashConv";
65
import { error } from "@sveltejs/kit";
76
import { ObjectId } from "mongodb";
87
import { nanoid } from "nanoid";
98

10-
export async function POST({ params, url, locals }) {
9+
export async function POST({ params, locals }) {
1110
const conversation = await collections.conversations.findOne({
1211
_id: new ObjectId(params.id),
1312
...authCondition(locals),
@@ -24,7 +23,7 @@ export async function POST({ params, url, locals }) {
2423
if (existingShare) {
2524
return new Response(
2625
JSON.stringify({
27-
url: getShareUrl(url, existingShare._id),
26+
shareId: existingShare._id,
2827
}),
2928
{ headers: { "Content-Type": "application/json" } }
3029
);
@@ -65,7 +64,7 @@ export async function POST({ params, url, locals }) {
6564

6665
return new Response(
6766
JSON.stringify({
68-
url: getShareUrl(url, shared._id),
67+
shareId: shared._id,
6968
}),
7069
{ headers: { "Content-Type": "application/json" } }
7170
);

0 commit comments

Comments
 (0)