Skip to content

Commit

Permalink
Fixes and updates to the preferences API (bluesky-social#1575)
Browse files Browse the repository at this point in the history
* Fix to handle duplicate preference key entries

* Add personal details preference API to sdk

* Add Array.prototype.findLast() type declaration

* Move interface declaration to ensure it's included in other package builds
  • Loading branch information
pfrazee authored and mloar committed Sep 25, 2023
1 parent 64cf758 commit 4df395e
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 11 deletions.
72 changes: 61 additions & 11 deletions packages/api/src/bsky-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import {
} from './client'
import { BskyPreferences, BskyLabelPreference } from './types'

declare global {
interface Array<T> {
findLast(
predicate: (value: T, index: number, obj: T[]) => unknown,
thisArg?: any,
): T
}
}

export class BskyAgent extends AtpAgent {
get app() {
return this.api.app
Expand Down Expand Up @@ -247,6 +256,7 @@ export class BskyAgent extends AtpAgent {
},
adultContentEnabled: false,
contentLabels: {},
birthDate: undefined,
}
const res = await this.app.bsky.actor.getPreferences({})
for (const pref of res.data.preferences) {
Expand All @@ -272,6 +282,13 @@ export class BskyAgent extends AtpAgent {
) {
prefs.feeds.saved = pref.saved
prefs.feeds.pinned = pref.pinned
} else if (
AppBskyActorDefs.isPersonalDetailsPref(pref) &&
AppBskyActorDefs.validatePersonalDetailsPref(pref).success
) {
if (pref.birthDate) {
prefs.birthDate = new Date(pref.birthDate)
}
}
}
return prefs
Expand Down Expand Up @@ -314,20 +331,22 @@ export class BskyAgent extends AtpAgent {

async setAdultContentEnabled(v: boolean) {
await updatePreferences(this, (prefs: AppBskyActorDefs.Preferences) => {
const existing = prefs.find(
let adultContentPref = prefs.findLast(
(pref) =>
AppBskyActorDefs.isAdultContentPref(pref) &&
AppBskyActorDefs.validateAdultContentPref(pref).success,
)
if (existing) {
existing.enabled = v
if (adultContentPref) {
adultContentPref.enabled = v
} else {
prefs.push({
adultContentPref = {
$type: 'app.bsky.actor.defs#adultContentPref',
enabled: v,
})
}
}
return prefs
.filter((pref) => !AppBskyActorDefs.isAdultContentPref(pref))
.concat([adultContentPref])
})
}

Expand All @@ -338,22 +357,53 @@ export class BskyAgent extends AtpAgent {
}

await updatePreferences(this, (prefs: AppBskyActorDefs.Preferences) => {
const existing = prefs.find(
let labelPref = prefs.findLast(
(pref) =>
AppBskyActorDefs.isContentLabelPref(pref) &&
AppBskyActorDefs.validateAdultContentPref(pref).success &&
pref.label === key,
)
if (existing) {
existing.visibility = value
if (labelPref) {
labelPref.visibility = value
} else {
prefs.push({
labelPref = {
$type: 'app.bsky.actor.defs#contentLabelPref',
label: key,
visibility: value,
})
}
}
return prefs
.filter(
(pref) =>
!AppBskyActorDefs.isContentLabelPref(pref) || pref.label !== key,
)
.concat([labelPref])
})
}

async setPersonalDetails({
birthDate,
}: {
birthDate: string | Date | undefined
}) {
birthDate = birthDate instanceof Date ? birthDate.toISOString() : birthDate
await updatePreferences(this, (prefs: AppBskyActorDefs.Preferences) => {
let personalDetailsPref = prefs.findLast(
(pref) =>
AppBskyActorDefs.isPersonalDetailsPref(pref) &&
AppBskyActorDefs.validatePersonalDetailsPref(pref).success,
)
if (personalDetailsPref) {
personalDetailsPref.birthDate = birthDate
} else {
personalDetailsPref = {
$type: 'app.bsky.actor.defs#personalDetailsPref',
birthDate,
}
}
return prefs
.filter((pref) => !AppBskyActorDefs.isPersonalDetailsPref(pref))
.concat([personalDetailsPref])
})
}
}
Expand Down Expand Up @@ -394,7 +444,7 @@ async function updateFeedPreferences(
): Promise<{ saved: string[]; pinned: string[] }> {
let res
await updatePreferences(agent, (prefs: AppBskyActorDefs.Preferences) => {
let feedsPref = prefs.find(
let feedsPref = prefs.findLast(
(pref) =>
AppBskyActorDefs.isSavedFeedsPref(pref) &&
AppBskyActorDefs.validateSavedFeedsPref(pref).success,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ export interface BskyPreferences {
}
adultContentEnabled: boolean
contentLabels: Record<string, BskyLabelPreference>
birthDate: Date | undefined
}
Loading

0 comments on commit 4df395e

Please sign in to comment.