-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdal.js
More file actions
291 lines (239 loc) · 6.33 KB
/
dal.js
File metadata and controls
291 lines (239 loc) · 6.33 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import "server-only"
import { cache } from "react"
import { redirect } from "next/navigation"
import { cookies } from "next/headers"
import { decrypt } from "@/server/session/session"
import * as users from "@/server/db/users"
import * as jokes from "@/server/db/jokes"
import * as votes from "@/server/db/votes"
import * as recommendation from "@/server/db/rec"
// Session
export const verifySession = cache(async () => {
const cookie = (await cookies()).get("session")?.value
const session = await decrypt(cookie)
if (!session?.userId) {
redirect("/login")
}
return { isAuth: true, userId: session.userId }
})
// Reads
export const getUser = cache(async () => {
const session = await verifySession()
if (!session) return null
try {
return users.getUserById(session.userId) ?? null
} catch {
console.log("Failed to fetch user")
return null
}
})
export const getUserById = cache(async userId => {
await verifySession()
try {
return users.getUserById(userId) ?? null
} catch {
console.log("Failed to fetch user by id")
return null
}
})
export const searchUsersQuery = cache(async (query, limit = 50) => {
await verifySession()
try {
return users.searchUsers(query, limit)
} catch {
console.log("Failed to search users")
return []
}
})
export const getJoke = cache(async jokeId => {
await verifySession()
try {
return jokes.getJokeById(jokeId) ?? null
} catch {
console.log("Failed to fetch joke")
return null
}
})
export const getJokesQuery = cache(async (sort, order, limit = 50, text) => {
await verifySession()
const offset = 0
try {
const allowedSortBy = ["date", "popularity"]
if (!allowedSortBy.includes(sort)) throw new Error("Invalid sortBy")
let sortBy
if (sort === "recent") {
sortBy = "date"
} else if (sort === "top") {
sortBy = "popularity"
}
return jokes.getJokes({ sortBy, order, limit, offset, text })
} catch {
console.log("Failed to fetch jokes")
return []
}
})
export const getJokesByAuthor = cache(async authorId => {
await verifySession()
try {
return jokes.getJokesByAuthor(authorId)
} catch {
console.log("Failed to fetch jokes by author")
return []
}
})
export const getTopJokesByAuthor = cache(async (userId, limit = 3) => {
await verifySession()
try {
return jokes.getTopJokesByAuthor(userId, limit)
} catch {
console.log("Failed to fetch top jokes by author")
return []
}
})
export const getUserScoreRatio = cache(async userId => {
userId ??= (await verifySession()).userId
try {
return users.getUserScoreRatio(userId)
} catch {
console.log("Failed to fetch user score ratio")
return 0
}
})
export const getRecommendation = cache(async opts => {
const session = await verifySession()
if (!session) return null
try {
return recommendation.recommendHottestUnvoted(session.userId, opts)
} catch {
console.log("Failed to fetch recommendation")
return null
}
})
export const getVote = cache(async jokeId => {
const session = await verifySession()
if (!session) return null
try {
return votes.getVote(session.userId, jokeId) ?? null
} catch {
console.log("Failed to fetch vote")
return null
}
})
export const getVotesForJoke = cache(async jokeId => {
await verifySession()
try {
return votes.getVotesForJoke(jokeId)
} catch {
console.log("Failed to fetch votes for joke")
return []
}
})
export const getVotesByUser = cache(async () => {
const session = await verifySession()
if (!session) return null
try {
return votes.getVotesByUser(session.userId)
} catch {
console.log("Failed to fetch votes by user")
return []
}
})
export const getAgreeability = cache(async userId => {
const session = await verifySession()
if (!session) return null
try {
return votes.getAgreeability(userId)
} catch {
console.log("Failed to fetch agreeability")
return null
}
})
export const getHotnessForJoke = cache(async jokeId => {
const session = await verifySession()
if (!session) return null
try {
return votes.getHotnessForJoke(jokeId)
} catch {
console.log("Failed to fetch hotness")
return null
}
})
export const getJokesPage = cache(
async ({ page, sort, order, limit = 50, text }) => {
await verifySession()
try {
const allowedSortBy = ["recent", "top", "hot"]
if (!allowedSortBy.includes(sort)) throw new Error("Invalid sortBy")
const realSortBy = ["date", "popularity", "hotness"]
const sortBy = realSortBy[allowedSortBy.indexOf(sort)]
const { jokes: results, total } = jokes.getJokesPage({
page,
sortBy,
order,
limit,
text
})
return { jokes: results, count: total }
} catch {
console.log("Failed to fetch joke page")
return { jokes: [], count: 0 }
}
}
)
// Writes
export async function insertJoke(body) {
const session = await verifySession()
if (!session) return null
try {
return jokes.createJoke({ authorId: session.userId, body })
} catch {
console.log("Failed to insert joke")
return null
}
}
export async function updateVote(jokeId, action) {
const session = await verifySession()
if (!session) return null
try {
return votes.setVote(session.userId, jokeId, action)
} catch {
console.log("Failed to update vote")
return null
}
}
export async function removeJoke(jokeId) {
const session = await verifySession()
if (!session) return null
try {
const joke = jokes.getJokeById(jokeId)
if (!joke || joke.author_id !== session.userId) return null
const result = jokes.deleteJoke(jokeId)
if (result.changes === 0) throw new Error("Joke not found")
return result
} catch {
console.log("Failed to delete joke")
return null
}
}
export async function updatePassword(newPasswordHash) {
const session = await verifySession()
if (!session) return null
try {
users.updatePassword(session.userId, newPasswordHash)
return { success: true }
} catch {
console.log("Failed to update password")
return null
}
}
export async function deleteAccount() {
const session = await verifySession()
if (!session) return null
try {
users.deleteUser(session.userId)
return { success: true }
} catch {
console.log("Failed to delete account")
return null
}
}