Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interest tags to preferences #2086

Merged
merged 13 commits into from
Jan 25, 2024
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
Loading