Skip to content

Commit

Permalink
Add account preferences APIs (bluesky-social#1013)
Browse files Browse the repository at this point in the history
* Add lexicons for account preferences

* Move prefs to app.bsky, codegen

* Setup model and services for user prefs

* Setup xrpc methods for prefs

* Test preferences, fixes

* Tidy

* Tidy

---------

Co-authored-by: Devin Ivy <[email protected]>
  • Loading branch information
pfrazee and devinivy authored May 11, 2023
1 parent 5fd5c86 commit df6ed7d
Show file tree
Hide file tree
Showing 28 changed files with 1,080 additions and 1 deletion.
25 changes: 25 additions & 0 deletions lexicons/app/bsky/actor/defs.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@
"following": {"type": "string", "format": "at-uri"},
"followedBy": {"type": "string", "format": "at-uri"}
}
},
"preferences": {
"type": "array",
"items": {
"type": "union",
"refs": [
"#adultContentPref",
"#contentLabelPref"
]
}
},
"adultContentPref": {
"type": "object",
"required": ["enabled"],
"properties": {
"enabled": {"type": "boolean", "default": false}
}
},
"contentLabelPref": {
"type": "object",
"required": ["label", "visibility"],
"properties": {
"label": {"type": "string"},
"visibility": {"type": "string", "knownValues": ["show", "warn", "hide"]}
}
}
}
}
28 changes: 28 additions & 0 deletions lexicons/app/bsky/actor/getPreferences.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"lexicon": 1,
"id": "app.bsky.actor.getPreferences",
"defs": {
"main": {
"type": "query",
"description": "Get private preferences attached to the account.",
"parameters": {
"type": "params",
"properties": {
}
},
"output": {
"encoding": "application/json",
"schema": {
"type": "object",
"required": ["preferences"],
"properties": {
"preferences": {
"type": "ref",
"ref": "app.bsky.actor.defs#preferences"
}
}
}
}
}
}
}
23 changes: 23 additions & 0 deletions lexicons/app/bsky/actor/putPreferences.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"lexicon": 1,
"id": "app.bsky.actor.putPreferences",
"defs": {
"main": {
"type": "procedure",
"description": "Sets the private preferences attached to the account.",
"input": {
"encoding": "application/json",
"schema": {
"type": "object",
"required": ["preferences"],
"properties": {
"preferences": {
"type": "ref",
"ref": "app.bsky.actor.defs#preferences"
}
}
}
}
}
}
}
26 changes: 26 additions & 0 deletions packages/api/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ import * as ComAtprotoSyncNotifyOfUpdate from './types/com/atproto/sync/notifyOf
import * as ComAtprotoSyncRequestCrawl from './types/com/atproto/sync/requestCrawl'
import * as ComAtprotoSyncSubscribeRepos from './types/com/atproto/sync/subscribeRepos'
import * as AppBskyActorDefs from './types/app/bsky/actor/defs'
import * as AppBskyActorGetPreferences from './types/app/bsky/actor/getPreferences'
import * as AppBskyActorGetProfile from './types/app/bsky/actor/getProfile'
import * as AppBskyActorGetProfiles from './types/app/bsky/actor/getProfiles'
import * as AppBskyActorGetSuggestions from './types/app/bsky/actor/getSuggestions'
import * as AppBskyActorProfile from './types/app/bsky/actor/profile'
import * as AppBskyActorPutPreferences from './types/app/bsky/actor/putPreferences'
import * as AppBskyActorSearchActors from './types/app/bsky/actor/searchActors'
import * as AppBskyActorSearchActorsTypeahead from './types/app/bsky/actor/searchActorsTypeahead'
import * as AppBskyEmbedExternal from './types/app/bsky/embed/external'
Expand Down Expand Up @@ -177,10 +179,12 @@ export * as ComAtprotoSyncNotifyOfUpdate from './types/com/atproto/sync/notifyOf
export * as ComAtprotoSyncRequestCrawl from './types/com/atproto/sync/requestCrawl'
export * as ComAtprotoSyncSubscribeRepos from './types/com/atproto/sync/subscribeRepos'
export * as AppBskyActorDefs from './types/app/bsky/actor/defs'
export * as AppBskyActorGetPreferences from './types/app/bsky/actor/getPreferences'
export * as AppBskyActorGetProfile from './types/app/bsky/actor/getProfile'
export * as AppBskyActorGetProfiles from './types/app/bsky/actor/getProfiles'
export * as AppBskyActorGetSuggestions from './types/app/bsky/actor/getSuggestions'
export * as AppBskyActorProfile from './types/app/bsky/actor/profile'
export * as AppBskyActorPutPreferences from './types/app/bsky/actor/putPreferences'
export * as AppBskyActorSearchActors from './types/app/bsky/actor/searchActors'
export * as AppBskyActorSearchActorsTypeahead from './types/app/bsky/actor/searchActorsTypeahead'
export * as AppBskyEmbedExternal from './types/app/bsky/embed/external'
Expand Down Expand Up @@ -1012,6 +1016,17 @@ export class ActorNS {
this.profile = new ProfileRecord(service)
}

getPreferences(
params?: AppBskyActorGetPreferences.QueryParams,
opts?: AppBskyActorGetPreferences.CallOptions,
): Promise<AppBskyActorGetPreferences.Response> {
return this._service.xrpc
.call('app.bsky.actor.getPreferences', params, undefined, opts)
.catch((e) => {
throw AppBskyActorGetPreferences.toKnownErr(e)
})
}

getProfile(
params?: AppBskyActorGetProfile.QueryParams,
opts?: AppBskyActorGetProfile.CallOptions,
Expand Down Expand Up @@ -1045,6 +1060,17 @@ export class ActorNS {
})
}

putPreferences(
data?: AppBskyActorPutPreferences.InputSchema,
opts?: AppBskyActorPutPreferences.CallOptions,
): Promise<AppBskyActorPutPreferences.Response> {
return this._service.xrpc
.call('app.bsky.actor.putPreferences', opts?.qp, data, opts)
.catch((e) => {
throw AppBskyActorPutPreferences.toKnownErr(e)
})
}

searchActors(
params?: AppBskyActorSearchActors.QueryParams,
opts?: AppBskyActorSearchActors.CallOptions,
Expand Down
85 changes: 85 additions & 0 deletions packages/api/src/client/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3509,6 +3509,66 @@ export const schemaDict = {
},
},
},
preferences: {
type: 'array',
items: {
type: 'union',
refs: [
'lex:app.bsky.actor.defs#adultContentPref',
'lex:app.bsky.actor.defs#contentLabelPref',
],
},
},
adultContentPref: {
type: 'object',
required: ['enabled'],
properties: {
enabled: {
type: 'boolean',
default: false,
},
},
},
contentLabelPref: {
type: 'object',
required: ['label', 'visibility'],
properties: {
label: {
type: 'string',
},
visibility: {
type: 'string',
knownValues: ['show', 'warn', 'hide'],
},
},
},
},
},
AppBskyActorGetPreferences: {
lexicon: 1,
id: 'app.bsky.actor.getPreferences',
defs: {
main: {
type: 'query',
description: 'Get private preferences attached to the account.',
parameters: {
type: 'params',
properties: {},
},
output: {
encoding: 'application/json',
schema: {
type: 'object',
required: ['preferences'],
properties: {
preferences: {
type: 'ref',
ref: 'lex:app.bsky.actor.defs#preferences',
},
},
},
},
},
},
},
AppBskyActorGetProfile: {
Expand Down Expand Up @@ -3655,6 +3715,29 @@ export const schemaDict = {
},
},
},
AppBskyActorPutPreferences: {
lexicon: 1,
id: 'app.bsky.actor.putPreferences',
defs: {
main: {
type: 'procedure',
description: 'Sets the private preferences attached to the account.',
input: {
encoding: 'application/json',
schema: {
type: 'object',
required: ['preferences'],
properties: {
preferences: {
type: 'ref',
ref: 'lex:app.bsky.actor.defs#preferences',
},
},
},
},
},
},
},
AppBskyActorSearchActors: {
lexicon: 1,
id: 'app.bsky.actor.searchActors',
Expand Down Expand Up @@ -5659,10 +5742,12 @@ export const ids = {
ComAtprotoSyncRequestCrawl: 'com.atproto.sync.requestCrawl',
ComAtprotoSyncSubscribeRepos: 'com.atproto.sync.subscribeRepos',
AppBskyActorDefs: 'app.bsky.actor.defs',
AppBskyActorGetPreferences: 'app.bsky.actor.getPreferences',
AppBskyActorGetProfile: 'app.bsky.actor.getProfile',
AppBskyActorGetProfiles: 'app.bsky.actor.getProfiles',
AppBskyActorGetSuggestions: 'app.bsky.actor.getSuggestions',
AppBskyActorProfile: 'app.bsky.actor.profile',
AppBskyActorPutPreferences: 'app.bsky.actor.putPreferences',
AppBskyActorSearchActors: 'app.bsky.actor.searchActors',
AppBskyActorSearchActorsTypeahead: 'app.bsky.actor.searchActorsTypeahead',
AppBskyEmbedExternal: 'app.bsky.embed.external',
Expand Down
41 changes: 41 additions & 0 deletions packages/api/src/client/types/app/bsky/actor/defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,44 @@ export function isViewerState(v: unknown): v is ViewerState {
export function validateViewerState(v: unknown): ValidationResult {
return lexicons.validate('app.bsky.actor.defs#viewerState', v)
}

export type Preferences = (
| AdultContentPref
| ContentLabelPref
| { $type: string; [k: string]: unknown }
)[]

export interface AdultContentPref {
enabled: boolean
[k: string]: unknown
}

export function isAdultContentPref(v: unknown): v is AdultContentPref {
return (
isObj(v) &&
hasProp(v, '$type') &&
v.$type === 'app.bsky.actor.defs#adultContentPref'
)
}

export function validateAdultContentPref(v: unknown): ValidationResult {
return lexicons.validate('app.bsky.actor.defs#adultContentPref', v)
}

export interface ContentLabelPref {
label: string
visibility: 'show' | 'warn' | 'hide' | (string & {})
[k: string]: unknown
}

export function isContentLabelPref(v: unknown): v is ContentLabelPref {
return (
isObj(v) &&
hasProp(v, '$type') &&
v.$type === 'app.bsky.actor.defs#contentLabelPref'
)
}

export function validateContentLabelPref(v: unknown): ValidationResult {
return lexicons.validate('app.bsky.actor.defs#contentLabelPref', v)
}
34 changes: 34 additions & 0 deletions packages/api/src/client/types/app/bsky/actor/getPreferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* GENERATED CODE - DO NOT MODIFY
*/
import { Headers, XRPCError } from '@atproto/xrpc'
import { ValidationResult, BlobRef } from '@atproto/lexicon'
import { isObj, hasProp } from '../../../../util'
import { lexicons } from '../../../../lexicons'
import { CID } from 'multiformats/cid'
import * as AppBskyActorDefs from './defs'

export interface QueryParams {}

export type InputSchema = undefined

export interface OutputSchema {
preferences: AppBskyActorDefs.Preferences
[k: string]: unknown
}

export interface CallOptions {
headers?: Headers
}

export interface Response {
success: boolean
headers: Headers
data: OutputSchema
}

export function toKnownErr(e: any) {
if (e instanceof XRPCError) {
}
return e
}
33 changes: 33 additions & 0 deletions packages/api/src/client/types/app/bsky/actor/putPreferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* GENERATED CODE - DO NOT MODIFY
*/
import { Headers, XRPCError } from '@atproto/xrpc'
import { ValidationResult, BlobRef } from '@atproto/lexicon'
import { isObj, hasProp } from '../../../../util'
import { lexicons } from '../../../../lexicons'
import { CID } from 'multiformats/cid'
import * as AppBskyActorDefs from './defs'

export interface QueryParams {}

export interface InputSchema {
preferences: AppBskyActorDefs.Preferences
[k: string]: unknown
}

export interface CallOptions {
headers?: Headers
qp?: QueryParams
encoding: 'application/json'
}

export interface Response {
success: boolean
headers: Headers
}

export function toKnownErr(e: any) {
if (e instanceof XRPCError) {
}
return e
}
Loading

0 comments on commit df6ed7d

Please sign in to comment.