[READ-ONLY] a fast, modern browser for the npm registry
at main 46 lines 1.6 kB view raw
1import * as v from 'valibot' 2import { Client } from '@atproto/lex' 3import * as dev from '#shared/types/lexicons/dev' 4import type { UriString } from '@atproto/lex' 5import { LIKES_SCOPE } from '#shared/utils/constants' 6import { PackageLikeBodySchema } from '#shared/schemas/social' 7import { throwOnMissingOAuthScope } from '#server/utils/atproto/oauth' 8 9export default eventHandlerWithOAuthSession(async (event, oAuthSession) => { 10 const loggedInUsersDid = oAuthSession?.did.toString() 11 12 if (!oAuthSession || !loggedInUsersDid) { 13 throw createError({ statusCode: 401, statusMessage: 'Unauthorized' }) 14 } 15 16 //Checks if the user has a scope to like packages 17 await throwOnMissingOAuthScope(oAuthSession, LIKES_SCOPE) 18 19 const body = v.parse(PackageLikeBodySchema, await readBody(event)) 20 21 const likesUtil = new PackageLikesUtils() 22 23 // Checks to see if the user has liked the package already 24 const likesResult = await likesUtil.getLikes(body.packageName, loggedInUsersDid) 25 if (likesResult.userHasLiked) { 26 return likesResult 27 } 28 29 const subjectRef = PACKAGE_SUBJECT_REF(body.packageName) 30 const client = new Client(oAuthSession) 31 32 const like = dev.npmx.feed.like.$build({ 33 createdAt: new Date().toISOString(), 34 subjectRef: subjectRef as UriString, 35 }) 36 37 const result = await client.create(dev.npmx.feed.like, like) 38 if (!result) { 39 throw createError({ 40 status: 500, 41 message: 'Failed to create a like', 42 }) 43 } 44 45 return await likesUtil.likeAPackageAndReturnLikes(body.packageName, loggedInUsersDid, result.uri) 46})