Skip to content

Commit

Permalink
Add interest tags to preferences (bluesky-social#2086)
Browse files Browse the repository at this point in the history
* Add interestsPref

* Codegen

* Update lex

* Add method to agent, test

* Codegen

* Format

* Remove console

* Update lex, codegen, update tests

* Update tests

* Format

* Add changeset

* Update property name
  • Loading branch information
estrattonbailey authored Jan 25, 2024
1 parent 39fe6b5 commit 4171c04
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/real-bikes-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@atproto/api': patch
---

Add `setInterestsPref` method to BskyAgent, and `interests` prop to
`getPreferences` response.
15 changes: 14 additions & 1 deletion lexicons/app/bsky/actor/defs.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@
"#savedFeedsPref",
"#personalDetailsPref",
"#feedViewPref",
"#threadViewPref"
"#threadViewPref",
"#interestsPref"
]
}
},
Expand Down Expand Up @@ -199,6 +200,18 @@
"description": "Show followed users at the top of all replies."
}
}
},
"interestsPref": {
"type": "object",
"required": ["tags"],
"properties": {
"tags": {
"type": "array",
"maxLength": 100,
"items": { "type": "string", "maxLength": 640, "maxGraphemes": 64 },
"description": "A list of tags which describe the account owner's interests gathered during onboarding."
}
}
}
}
}
27 changes: 27 additions & 0 deletions packages/api/src/bsky-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
BskyLabelPreference,
BskyFeedViewPreference,
BskyThreadViewPreference,
BskyInterestsPreference,
} from './types'

const FEED_VIEW_PREF_DEFAULTS = {
Expand Down Expand Up @@ -323,6 +324,9 @@ export class BskyAgent extends AtpAgent {
adultContentEnabled: false,
contentLabels: {},
birthDate: undefined,
interests: {
tags: [],
},
}
const res = await this.app.bsky.actor.getPreferences({})
for (const pref of res.data.preferences) {
Expand Down Expand Up @@ -369,6 +373,13 @@ export class BskyAgent extends AtpAgent {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { $type, ...v } = pref
prefs.threadViewPrefs = { ...prefs.threadViewPrefs, ...v }
} else if (
AppBskyActorDefs.isInterestsPref(pref) &&
AppBskyActorDefs.validateInterestsPref(pref).success
) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { $type, ...v } = pref
prefs.interests = { ...prefs.interests, ...v }
}
}
return prefs
Expand Down Expand Up @@ -521,6 +532,22 @@ export class BskyAgent extends AtpAgent {
.concat([{ ...pref, $type: 'app.bsky.actor.defs#threadViewPref' }])
})
}

async setInterestsPref(pref: Partial<BskyInterestsPreference>) {
await updatePreferences(this, (prefs: AppBskyActorDefs.Preferences) => {
const existing = prefs.findLast(
(pref) =>
AppBskyActorDefs.isInterestsPref(pref) &&
AppBskyActorDefs.validateInterestsPref(pref).success,
)
if (existing) {
pref = { ...existing, ...pref }
}
return prefs
.filter((p) => !AppBskyActorDefs.isInterestsPref(p))
.concat([{ ...pref, $type: 'app.bsky.actor.defs#interestsPref' }])
})
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions packages/api/src/client/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4615,6 +4615,7 @@ export const schemaDict = {
'lex:app.bsky.actor.defs#personalDetailsPref',
'lex:app.bsky.actor.defs#feedViewPref',
'lex:app.bsky.actor.defs#threadViewPref',
'lex:app.bsky.actor.defs#interestsPref',
],
},
},
Expand Down Expand Up @@ -4718,6 +4719,23 @@ export const schemaDict = {
},
},
},
interestsPref: {
type: 'object',
required: ['tags'],
properties: {
tags: {
type: 'array',
maxLength: 100,
items: {
type: 'string',
maxLength: 640,
maxGraphemes: 64,
},
description:
"A list of tags which describe the account owner's interests gathered during onboarding.",
},
},
},
},
},
AppBskyActorGetPreferences: {
Expand Down
19 changes: 19 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 @@ -112,6 +112,7 @@ export type Preferences = (
| PersonalDetailsPref
| FeedViewPref
| ThreadViewPref
| InterestsPref
| { $type: string; [k: string]: unknown }
)[]

Expand Down Expand Up @@ -233,3 +234,21 @@ export function isThreadViewPref(v: unknown): v is ThreadViewPref {
export function validateThreadViewPref(v: unknown): ValidationResult {
return lexicons.validate('app.bsky.actor.defs#threadViewPref', v)
}

export interface InterestsPref {
/** A list of tags which describe the account owner's interests gathered during onboarding. */
tags: string[]
[k: string]: unknown
}

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

export function validateInterestsPref(v: unknown): ValidationResult {
return lexicons.validate('app.bsky.actor.defs#interestsPref', v)
}
9 changes: 9 additions & 0 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ export interface BskyThreadViewPreference {
[key: string]: any
}

/**
* Bluesky interests preferences
*/
export interface BskyInterestsPreference {
tags: string[]
[key: string]: any
}

/**
* Bluesky preferences
*/
Expand All @@ -110,4 +118,5 @@ export interface BskyPreferences {
adultContentEnabled: boolean
contentLabels: Record<string, BskyLabelPreference>
birthDate: Date | undefined
interests: BskyInterestsPreference
}
Loading

0 comments on commit 4171c04

Please sign in to comment.