Skip to content

Commit 53b3268

Browse files
committed
Some small changes
1 parent a282987 commit 53b3268

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

app/composables/useAtproto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ERROR_NEED_REAUTH } from '#imports'
22
import type { FetchError } from 'ofetch'
33
import type { UserSession } from '#shared/schemas/userSession'
4-
import type { PackageLikes } from '~~/server/utils/atproto/utils/likes'
4+
import type { PackageLikes } from '#server/utils/atproto/utils/likes'
55

66
export async function authRedirect(identifier: string, create: boolean = false) {
77
let query = { handle: identifier } as {}

server/api/auth/social/like.delete.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Client } from '@atproto/lex'
22
import * as dev from '#shared/types/lexicons/dev'
3-
import { LIKES_SCOPE } from '~~/shared/utils/constants'
4-
import { checkOAuthScope } from '~~/server/utils/atproto/oauth'
3+
import { throwOnMissingOAuthScope } from '#server/utils/atproto/oauth'
54

65
export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
76
const loggedInUsersDid = oAuthSession?.did.toString()
@@ -10,6 +9,9 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
109
throw createError({ statusCode: 401, statusMessage: 'Unauthorized' })
1110
}
1211

12+
//Checks if the user has a scope to like packages
13+
await throwOnMissingOAuthScope(oAuthSession, LIKES_SCOPE)
14+
1315
const body = await readBody<{ packageName: string }>(event)
1416

1517
if (!body.packageName) {
@@ -25,9 +27,8 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
2527
body.packageName,
2628
loggedInUsersDid,
2729
)
30+
2831
if (getTheUsersLikedRecord) {
29-
//Checks if the user has a scope to like packages
30-
await checkOAuthScope(oAuthSession, LIKES_SCOPE)
3132
const client = new Client(oAuthSession)
3233

3334
await client.delete(dev.npmx.feed.like, {

server/api/auth/social/like.post.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Client } from '@atproto/lex'
22
import * as dev from '#shared/types/lexicons/dev'
33
import type { UriString } from '@atproto/lex'
4-
import { LIKES_SCOPE } from '~~/shared/utils/constants'
5-
import { checkOAuthScope } from '~~/server/utils/atproto/oauth'
4+
import { LIKES_SCOPE } from '#shared/utils/constants'
5+
import { throwOnMissingOAuthScope } from '~~/server/utils/atproto/oauth'
66

77
export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
88
const loggedInUsersDid = oAuthSession?.did.toString()
@@ -11,6 +11,9 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
1111
throw createError({ statusCode: 401, statusMessage: 'Unauthorized' })
1212
}
1313

14+
//Checks if the user has a scope to like packages
15+
await throwOnMissingOAuthScope(oAuthSession, LIKES_SCOPE)
16+
1417
const body = await readBody<{ packageName: string }>(event)
1518

1619
if (!body.packageName) {
@@ -30,9 +33,6 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
3033
})
3134
}
3235

33-
//Checks if the user has a scope to like packages
34-
await checkOAuthScope(oAuthSession, LIKES_SCOPE)
35-
3636
const subjectRef = PACKAGE_SUBJECT_REF(body.packageName)
3737
const client = new Client(oAuthSession)
3838

@@ -49,5 +49,5 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
4949
})
5050
}
5151

52-
return await likesUtil.likeAPackageAndRetunLikes(body.packageName, loggedInUsersDid, result.uri)
52+
return await likesUtil.likeAPackageAndReturnLikes(body.packageName, loggedInUsersDid, result.uri)
5353
})

server/utils/atproto/oauth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ async function getOAuthSession(event: H3Event): Promise<OAuthSession | undefined
6161
}
6262

6363
/**
64+
* Throws if the logged in OAuth Session does not have the required scopes.
6465
* As we add new scopes we need to check if the client has the ability to use it.
6566
* If not need to let the client know to redirect the user to the PDS to upgrade their scopes.
6667
* @param oAuthSession - The current OAuth session from the event
6768
* @param requiredScopes - The required scope you are checking if you can use
6869
*/
69-
export async function checkOAuthScope(oAuthSession: OAuthSession, requiredScopes: string) {
70+
export async function throwOnMissingOAuthScope(oAuthSession: OAuthSession, requiredScopes: string) {
7071
const tokenInfo = await oAuthSession.getTokenInfo()
7172
if (!tokenInfo.scope.includes(requiredScopes)) {
7273
throw createError({

server/utils/atproto/utils/likes.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getCacheAdatper } from '../../cache'
22
import { $nsid as likeNsid } from '#shared/types/lexicons/dev/npmx/feed/like.defs'
3-
import type { Backlink } from '~~/shared/utils/constellation'
3+
import type { Backlink } from '#shared/utils/constellation'
44

55
/**
66
* Likes for a npm package on npmx
@@ -123,7 +123,7 @@ export class PackageLikesUtils {
123123
return {
124124
totalLikes: totalLikes,
125125
userHasLiked,
126-
} as PackageLikes
126+
}
127127
}
128128

129129
/**
@@ -151,7 +151,7 @@ export class PackageLikesUtils {
151151
* @param usersDid
152152
* @param atUri - The URI of the like record
153153
*/
154-
async likeAPackageAndRetunLikes(
154+
async likeAPackageAndReturnLikes(
155155
packageName: string,
156156
usersDid: string,
157157
atUri: string,
@@ -181,7 +181,7 @@ export class PackageLikesUtils {
181181
return {
182182
totalLikes: totalLikes,
183183
userHasLiked: true,
184-
} as PackageLikes
184+
}
185185
}
186186

187187
/**
@@ -241,6 +241,6 @@ export class PackageLikesUtils {
241241
return {
242242
totalLikes: totalLikes,
243243
userHasLiked: false,
244-
} as PackageLikes
244+
}
245245
}
246246
}

0 commit comments

Comments
 (0)