-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy path+page.server.ts
More file actions
82 lines (73 loc) · 3.3 KB
/
Copy path+page.server.ts
File metadata and controls
82 lines (73 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { error, redirect } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
import { getConfig } from "$lib/server/config";
import { getFormatter } from "$lib/server/i18n";
import { getById, getItems, type GetItemsOptions } from "$lib/server/list";
import { getActiveMembership } from "$lib/server/group-membership";
import type { UserGroupMembership } from "$lib/generated/prisma/client";
export const load = (async ({ params, url, locals, depends, cookies }) => {
const $t = await getFormatter();
const list = await getById(params.id);
let activeMembership: UserGroupMembership | undefined;
if (!locals.user) {
// Unauthenticated users can only view public lists
if (!list || !list.public) {
// Redirect to login so we don't expose details if the list does exist
const redirectTo = url.pathname + url.search;
const params = new URLSearchParams({ redirectTo });
redirect(307, `/login?${params}`);
}
} else {
// Logged in users must be in the correct group, or viewing a public list
activeMembership = await getActiveMembership(locals.user);
if (!list || (!list.public && list.groupId !== activeMembership.groupId)) {
error(404, $t("errors.list-not-found"));
}
// Owners of the public list need to be in the correct group still
if (list.owner.id === locals.user.id && list?.public && list.groupId !== activeMembership.groupId) {
error(401, $t("errors.user-must-be-in-the-correct-group"));
}
}
const config = await getConfig(list.groupId);
const options: GetItemsOptions = {
filter: url.searchParams.get("filter"),
sort: url.searchParams.get("sort"),
sortDir: url.searchParams.get("dir"),
suggestionMethod: config.suggestions.method,
listOwnerId: list.owner.id,
listManagers: new Set(list.managers.map(({ userId }) => userId)),
loggedInUserId: locals.user?.id || null
};
const items = await getItems(list.id, options);
depends("data:items");
// Read view preference from cookie (for SSR to prevent flicker)
const viewPreference = cookies.get("listViewPreference") as "list" | "tile" | undefined;
return {
list: {
...list,
owner: {
...list.owner,
isMe: list.owner.id === locals.user?.id,
activeGroupId: list.groupId
},
isManager: list.managers.find(({ userId }) => userId === locals.user?.id) !== undefined,
items
},
loggedInUser: locals.user
? {
id: locals.user.id,
username: locals.user.username,
name: locals.user.name,
activeGroupId: activeMembership!.groupId
}
: undefined,
listMode: config.listMode,
showClaimedName: config.claims.showName,
showNameAcrossGroups: config.claims.showNameAcrossGroups,
showClaimForOwner: config.claims.showForOwner,
showPublicClaimName: config.claims.showNamePublic,
requireClaimEmail: config.claims.requireEmail,
suggestionsEnabled: config.suggestions.enable,
initialViewPreference: viewPreference || "list"
};
}) satisfies PageServerLoad;