-
-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathlike.delete.ts
More file actions
45 lines (35 loc) · 1.49 KB
/
like.delete.ts
File metadata and controls
45 lines (35 loc) · 1.49 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
import * as v from 'valibot'
import { Client } from '@atproto/lex'
import * as dev from '#shared/types/lexicons/dev'
import { PackageLikeBodySchema } from '#shared/schemas/social'
import { throwOnMissingOAuthScope } from '#server/utils/atproto/oauth'
export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
const loggedInUsersDid = oAuthSession?.did.toString()
if (!oAuthSession || !loggedInUsersDid) {
throw createError({ statusCode: 401, statusMessage: 'Unauthorized' })
}
//Checks if the user has a scope to like packages
await throwOnMissingOAuthScope(oAuthSession, LIKES_SCOPE)
const body = v.parse(PackageLikeBodySchema, await readBody(event))
const likesUtil = new PackageLikesUtils()
const getTheUsersLikedRecord = await likesUtil.getTheUsersLikedRecord(
body.packageName,
loggedInUsersDid,
)
if (getTheUsersLikedRecord) {
const client = new Client(oAuthSession)
await client.deleteRecord(
dev.npmx.feed.like.$nsid,
getTheUsersLikedRecord.rkey,
)
const result = await likesUtil.unlikeAPackageAndReturnLikes(body.packageName, loggedInUsersDid)
return result
} else {
// Always unlike in the cache if this endpoint is called. May be a mismatch
await likesUtil.setUnlikeInCache(body.packageName, loggedInUsersDid)
}
console.warn(
`User ${loggedInUsersDid} tried to unlike a package ${body.packageName} but it was not liked by them.`,
)
return await likesUtil.getLikes(body.packageName, loggedInUsersDid)
})