Repository navigation
Expand file tree
/
Copy pathputPreferences.ts
More file actions
54 lines (48 loc) · 1.97 KB
/
Copy pathputPreferences.ts
File metadata and controls
54 lines (48 loc) · 1.97 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
import { InvalidRequestError } from '@atproto/xrpc-server'
import { AccountPreference } from '../../../../actor-store/preference/reader'
import { isAccessFull } from '../../../../auth-scope'
import { AppContext } from '../../../../context'
import { Server } from '../../../../lexicon'
import { ids } from '../../../../lexicon/lexicons'
import { computeProxyTo, pipethrough } from '../../../../pipethrough'
export default function (server: Server, ctx: AppContext) {
const { bskyAppView } = ctx
if (!bskyAppView) return
server.app.bsky.actor.putPreferences({
auth: ctx.authVerifier.authorization({
checkTakedown: true,
authorize: (permissions, { req }) => {
const lxm = ids.AppBskyActorPutPreferences
const aud = computeProxyTo(ctx, req, lxm)
permissions.assertRpc({ aud, lxm })
},
}),
handler: async ({ req, auth, input }) => {
const { did } = auth.credentials
// If the request has a proxy header different from the bsky app view,
// we need to proxy the request to the requested app view.
// @TODO This behavior should not be implemented as part of the XRPC framework
const lxm = ids.AppBskyActorPutPreferences
const aud = computeProxyTo(ctx, req, lxm)
if (aud !== `${bskyAppView.did}#bsky_appview`) {
return pipethrough(ctx, req, { iss: did, aud, lxm })
}
const checkedPreferences: AccountPreference[] = []
for (const pref of input.body.preferences) {
if (typeof pref.$type === 'string') {
checkedPreferences.push(pref as AccountPreference)
} else {
throw new InvalidRequestError('Preference is missing a $type')
}
}
const hasAccessFull =
auth.credentials.type === 'access' &&
isAccessFull(auth.credentials.scope)
await ctx.actorStore.transact(did, async (actorTxn) => {
await actorTxn.pref.putPreferences(checkedPreferences, 'app.bsky', {
hasAccessFull,
})
})
},
})
}