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

Fixes and updates to the preferences API #1575

Merged
merged 4 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 52 additions & 11 deletions packages/api/src/bsky-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,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 +273,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 +322,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 +348,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 +435,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
10 changes: 10 additions & 0 deletions packages/api/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export {}

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

Check warning on line 7 in packages/api/src/global.d.ts

View workflow job for this annotation

GitHub Actions / Verify

Unexpected any. Specify a different type
): T
}
}
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
Loading