diff --git a/packages/api/src/agent.ts b/packages/api/src/agent.ts index 1e08c5db12..cb8e15575a 100644 --- a/packages/api/src/agent.ts +++ b/packages/api/src/agent.ts @@ -475,7 +475,7 @@ export class Agent extends XrpcClient { repo, collection: 'app.bsky.actor.profile', rkey: 'self', - record: updated, + record: validation.value, swapRecord: existing?.data.cid || null, }) } catch (e: unknown) { @@ -836,22 +836,18 @@ export class Agent extends XrpcClient { async setAdultContentEnabled(v: boolean) { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - let adultContentPref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isAdultContentPref(pref) && - AppBskyActorDefs.validateAdultContentPref(pref).success, - ) - if (adultContentPref) { - adultContentPref.enabled = v - } else { - adultContentPref = { - $type: 'app.bsky.actor.defs#adultContentPref', - enabled: v, - } + const adultContentPref = prefs.findLast( + AppBskyActorDefs.isValidAdultContentPref, + ) || { + $type: 'app.bsky.actor.defs#adultContentPref', + enabled: false, } + + adultContentPref.enabled = v + return prefs .filter((pref) => !AppBskyActorDefs.isAdultContentPref(pref)) - .concat([adultContentPref]) + .concat(adultContentPref) }) } @@ -864,13 +860,11 @@ export class Agent extends XrpcClient { ensureValidDid(labelerDid) } await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - const labelPref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isContentLabelPref(pref) && - AppBskyActorDefs.validateContentLabelPref(pref).success && - pref.label === key && - pref.labelerDid === labelerDid, - ) ?? { + const labelPref = prefs + .filter(AppBskyActorDefs.isValidContentLabelPref) + .findLast( + (pref) => pref.label === key && pref.labelerDid === labelerDid, + ) || { $type: 'app.bsky.actor.defs#contentLabelPref', label: key, labelerDid, @@ -893,20 +887,20 @@ export class Agent extends XrpcClient { // if it's a legacy label, double-write the legacy label if (legacyLabelValue) { - legacyLabelPref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isContentLabelPref(pref) && - AppBskyActorDefs.validateContentLabelPref(pref).success && - pref.label === legacyLabelValue && - pref.labelerDid === undefined, - ) ?? { + legacyLabelPref = prefs + .filter(AppBskyActorDefs.isValidContentLabelPref) + .findLast( + (pref) => + pref.label === legacyLabelValue && + pref.labelerDid === undefined, + ) || { $type: 'app.bsky.actor.defs#contentLabelPref', label: legacyLabelValue, labelerDid: undefined, visibility: value, } - legacyLabelPref.visibility = value + legacyLabelPref!.visibility = value } } } @@ -917,7 +911,7 @@ export class Agent extends XrpcClient { !AppBskyActorDefs.isContentLabelPref(pref) || !(pref.label === key && pref.labelerDid === labelerDid), ) - .concat([labelPref]) + .concat(labelPref) .filter((pref) => { if (!legacyLabelPref) return true return ( @@ -936,15 +930,13 @@ export class Agent extends XrpcClient { const prefs = await this.updatePreferences( (prefs: AppBskyActorDefs.Preferences) => { const labelersPref = prefs.findLast( - (pref) => - AppBskyActorDefs.isLabelersPref(pref) && - AppBskyActorDefs.validateLabelersPref(pref).success, - ) ?? { + AppBskyActorDefs.isValidLabelersPref, + ) || { $type: 'app.bsky.actor.defs#labelersPref', labelers: [], } - if (AppBskyActorDefs.isValidLabelersPref(labelersPref)) { + if (labelersPref) { let labelerPrefItem = labelersPref.labelers.find( (labeler) => labeler.did === did, ) @@ -958,7 +950,7 @@ export class Agent extends XrpcClient { return prefs .filter((pref) => !AppBskyActorDefs.isLabelersPref(pref)) - .concat([labelersPref]) + .concat(labelersPref) }, ) // automatically configure the client @@ -968,25 +960,20 @@ export class Agent extends XrpcClient { async removeLabeler(did: string) { const prefs = await this.updatePreferences( (prefs: AppBskyActorDefs.Preferences) => { - let labelersPref = prefs.findLast( - (pref) => - AppBskyActorDefs.isLabelersPref(pref) && - AppBskyActorDefs.validateLabelersPref(pref).success, - ) - if (!labelersPref) { - labelersPref = { - $type: 'app.bsky.actor.defs#labelersPref', - labelers: [], - } - } - if (AppBskyActorDefs.isLabelersPref(labelersPref)) { - labelersPref.labelers = labelersPref.labelers.filter( - (labeler) => labeler.did !== did, - ) + const labelersPref = prefs.findLast( + AppBskyActorDefs.isValidLabelersPref, + ) || { + $type: 'app.bsky.actor.defs#labelersPref', + labelers: [], } + + labelersPref.labelers = labelersPref.labelers.filter( + (labeler) => labeler.did !== did, + ) + return prefs .filter((pref) => !AppBskyActorDefs.isLabelersPref(pref)) - .concat([labelersPref]) + .concat(labelersPref) }, ) // automatically configure the client @@ -1000,76 +987,63 @@ export class Agent extends XrpcClient { }) { birthDate = birthDate instanceof Date ? birthDate.toISOString() : birthDate await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - let personalDetailsPref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isPersonalDetailsPref(pref) && - AppBskyActorDefs.validatePersonalDetailsPref(pref).success, - ) - if (personalDetailsPref) { - personalDetailsPref.birthDate = birthDate - } else { - personalDetailsPref = { - $type: 'app.bsky.actor.defs#personalDetailsPref', - birthDate, - } + const personalDetailsPref = prefs.findLast( + AppBskyActorDefs.isValidPersonalDetailsPref, + ) || { + $type: 'app.bsky.actor.defs#personalDetailsPref', + birthDate, } + + personalDetailsPref.birthDate = birthDate + return prefs .filter((pref) => !AppBskyActorDefs.isPersonalDetailsPref(pref)) - .concat([personalDetailsPref]) + .concat(personalDetailsPref) }) } async setFeedViewPrefs(feed: string, pref: Partial) { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - const existing = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isFeedViewPref(pref) && - AppBskyActorDefs.validateFeedViewPref(pref).success && - pref.feed === feed, - ) + const existing = prefs + .filter(AppBskyActorDefs.isValidFeedViewPref) + .findLast((pref) => pref.feed === feed) return prefs .filter((p) => !AppBskyActorDefs.isFeedViewPref(p) || p.feed !== feed) - .concat([ - { - ...existing, - ...pref, - $type: 'app.bsky.actor.defs#feedViewPref', - feed, - }, - ]) + .concat({ + ...existing, + ...pref, + $type: 'app.bsky.actor.defs#feedViewPref', + feed, + }) }) } async setThreadViewPrefs(pref: Partial) { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - const existing = prefs.findLast( - (pref) => - AppBskyActorDefs.isThreadViewPref(pref) && - AppBskyActorDefs.validateThreadViewPref(pref).success, - ) + const existing = prefs.findLast(AppBskyActorDefs.isValidThreadViewPref) return prefs .filter((p) => !AppBskyActorDefs.isThreadViewPref(p)) - .concat([ - { ...existing, ...pref, $type: 'app.bsky.actor.defs#threadViewPref' }, - ]) + .concat({ + ...existing, + ...pref, + $type: 'app.bsky.actor.defs#threadViewPref', + }) }) } async setInterestsPref(pref: Partial) { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - const existing = prefs.findLast( - (pref) => - AppBskyActorDefs.isInterestsPref(pref) && - AppBskyActorDefs.validateInterestsPref(pref).success, - ) + const existing = prefs.findLast(AppBskyActorDefs.isValidInterestsPref) return prefs .filter((p) => !AppBskyActorDefs.isInterestsPref(p)) - .concat([ - { ...existing, ...pref, $type: 'app.bsky.actor.defs#interestsPref' }, - ]) + .concat({ + ...existing, + ...pref, + $type: 'app.bsky.actor.defs#interestsPref', + }) }) } @@ -1088,9 +1062,7 @@ export class Agent extends XrpcClient { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { let mutedWordsPref = prefs.findLast( - (pref) => - AppBskyActorDefs.isMutedWordsPref(pref) && - AppBskyActorDefs.validateMutedWordsPref(pref).success, + AppBskyActorDefs.isValidMutedWordsPref, ) const newMutedWord: AppBskyActorDefs.MutedWord = { @@ -1120,7 +1092,7 @@ export class Agent extends XrpcClient { return prefs .filter((p) => !AppBskyActorDefs.isMutedWordsPref(p)) - .concat([mutedWordsPref]) + .concat(mutedWordsPref) }) } @@ -1149,9 +1121,7 @@ export class Agent extends XrpcClient { async updateMutedWord(mutedWord: AppBskyActorDefs.MutedWord) { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { const mutedWordsPref = prefs.findLast( - (pref) => - AppBskyActorDefs.isMutedWordsPref(pref) && - AppBskyActorDefs.validateMutedWordsPref(pref).success, + AppBskyActorDefs.isValidMutedWordsPref, ) if (mutedWordsPref && AppBskyActorDefs.isMutedWordsPref(mutedWordsPref)) { @@ -1185,9 +1155,10 @@ export class Agent extends XrpcClient { return prefs .filter((p) => !AppBskyActorDefs.isMutedWordsPref(p)) - .concat([ - { ...mutedWordsPref, $type: 'app.bsky.actor.defs#mutedWordsPref' }, - ]) + .concat({ + ...mutedWordsPref, + $type: 'app.bsky.actor.defs#mutedWordsPref', + }) } return prefs @@ -1200,9 +1171,7 @@ export class Agent extends XrpcClient { async removeMutedWord(mutedWord: AppBskyActorDefs.MutedWord) { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { const mutedWordsPref = prefs.findLast( - (pref) => - AppBskyActorDefs.isMutedWordsPref(pref) && - AppBskyActorDefs.validateMutedWordsPref(pref).success, + AppBskyActorDefs.isValidMutedWordsPref, ) if (mutedWordsPref && AppBskyActorDefs.isMutedWordsPref(mutedWordsPref)) { @@ -1224,9 +1193,10 @@ export class Agent extends XrpcClient { return prefs .filter((p) => !AppBskyActorDefs.isMutedWordsPref(p)) - .concat([ - { ...mutedWordsPref, $type: 'app.bsky.actor.defs#mutedWordsPref' }, - ]) + .concat({ + ...mutedWordsPref, + $type: 'app.bsky.actor.defs#mutedWordsPref', + }) } return prefs @@ -1250,37 +1220,30 @@ export class Agent extends XrpcClient { async bskyAppQueueNudges(nudges: string | string[]) { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - let bskyAppStatePref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isBskyAppStatePref(pref) && - AppBskyActorDefs.validateBskyAppStatePref(pref).success, - ) - - bskyAppStatePref = bskyAppStatePref || { + const bskyAppStatePref = prefs.findLast( + AppBskyActorDefs.isValidBskyAppStatePref, + ) || { $type: 'app.bsky.actor.defs#bskyAppStatePref', } - nudges = Array.isArray(nudges) ? nudges : [nudges] + bskyAppStatePref.queuedNudges = ( bskyAppStatePref.queuedNudges || [] ).concat(nudges) return prefs .filter((p) => !AppBskyActorDefs.isBskyAppStatePref(p)) - .concat([bskyAppStatePref]) + .concat(bskyAppStatePref) }) } async bskyAppDismissNudges(nudges: string | string[]) { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - let bskyAppStatePref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isBskyAppStatePref(pref) && - AppBskyActorDefs.validateBskyAppStatePref(pref).success, - ) - - bskyAppStatePref = bskyAppStatePref || { + const bskyAppStatePref = prefs.findLast( + AppBskyActorDefs.isValidBskyAppStatePref, + ) || { $type: 'app.bsky.actor.defs#bskyAppStatePref', } + nudges = Array.isArray(nudges) ? nudges : [nudges] bskyAppStatePref.queuedNudges = ( bskyAppStatePref.queuedNudges || [] @@ -1288,7 +1251,7 @@ export class Agent extends XrpcClient { return prefs .filter((p) => !AppBskyActorDefs.isBskyAppStatePref(p)) - .concat([bskyAppStatePref]) + .concat(bskyAppStatePref) }) } @@ -1303,20 +1266,17 @@ export class Agent extends XrpcClient { } await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - let bskyAppStatePref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isBskyAppStatePref(pref) && - AppBskyActorDefs.validateBskyAppStatePref(pref).success, - ) - - bskyAppStatePref = bskyAppStatePref || { + const bskyAppStatePref = prefs.findLast( + AppBskyActorDefs.isValidBskyAppStatePref, + ) || { $type: 'app.bsky.actor.defs#bskyAppStatePref', } + bskyAppStatePref.activeProgressGuide = guide return prefs .filter((p) => !AppBskyActorDefs.isBskyAppStatePref(p)) - .concat([bskyAppStatePref]) + .concat(bskyAppStatePref) }) } @@ -1327,15 +1287,12 @@ export class Agent extends XrpcClient { validateNux(nux) await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - let bskyAppStatePref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isBskyAppStatePref(pref) && - AppBskyActorDefs.validateBskyAppStatePref(pref).success, - ) - - bskyAppStatePref = bskyAppStatePref || { + const bskyAppStatePref = prefs.findLast( + AppBskyActorDefs.isValidBskyAppStatePref, + ) || { $type: 'app.bsky.actor.defs#bskyAppStatePref', } + bskyAppStatePref.nuxs = bskyAppStatePref.nuxs || [] const existing = bskyAppStatePref.nuxs?.find((n) => { @@ -1362,7 +1319,7 @@ export class Agent extends XrpcClient { return prefs .filter((p) => !AppBskyActorDefs.isBskyAppStatePref(p)) - .concat([bskyAppStatePref]) + .concat(bskyAppStatePref) }) } @@ -1371,22 +1328,19 @@ export class Agent extends XrpcClient { */ async bskyAppRemoveNuxs(ids: string[]) { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - let bskyAppStatePref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isBskyAppStatePref(pref) && - AppBskyActorDefs.validateBskyAppStatePref(pref).success, - ) - - bskyAppStatePref = bskyAppStatePref || { + const bskyAppStatePref = prefs.findLast( + AppBskyActorDefs.isValidBskyAppStatePref, + ) || { $type: 'app.bsky.actor.defs#bskyAppStatePref', } + bskyAppStatePref.nuxs = (bskyAppStatePref.nuxs || []).filter((nux) => { return !ids.includes(nux.id) }) return prefs .filter((p) => !AppBskyActorDefs.isBskyAppStatePref(p)) - .concat([bskyAppStatePref]) + .concat(bskyAppStatePref) }) } @@ -1425,27 +1379,21 @@ export class Agent extends XrpcClient { private async updateHiddenPost(postUri: string, action: 'hide' | 'unhide') { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - let pref = prefs.findLast( - (pref) => - AppBskyActorDefs.isHiddenPostsPref(pref) && - AppBskyActorDefs.validateHiddenPostsPref(pref).success, - ) - if (pref && AppBskyActorDefs.isHiddenPostsPref(pref)) { - pref.items = - action === 'hide' - ? Array.from(new Set([...pref.items, postUri])) - : pref.items.filter((uri) => uri !== postUri) - } else { - if (action === 'hide') { - pref = { - $type: 'app.bsky.actor.defs#hiddenPostsPref', - items: [postUri], - } - } + const pref = prefs.findLast(AppBskyActorDefs.isValidHiddenPostsPref) || { + $type: 'app.bsky.actor.defs#hiddenPostsPref', + items: [], } + + const hiddenItems = new Set(pref.items) + + if (action === 'hide') hiddenItems.add(postUri) + else hiddenItems.delete(postUri) + + pref.items = [...hiddenItems] + return prefs .filter((p) => !AppBskyActorDefs.isInterestsPref(p)) - .concat([{ ...pref, $type: 'app.bsky.actor.defs#hiddenPostsPref' }]) + .concat(pref) }) } @@ -1460,26 +1408,21 @@ export class Agent extends XrpcClient { ): Promise<{ saved: string[]; pinned: string[] }> { let res await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { - let feedsPref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isSavedFeedsPref(pref) && - AppBskyActorDefs.validateSavedFeedsPref(pref).success, - ) - if (feedsPref) { - res = cb(feedsPref.saved, feedsPref.pinned) - feedsPref.saved = res.saved - feedsPref.pinned = res.pinned - } else { - res = cb([], []) - feedsPref = { - $type: 'app.bsky.actor.defs#savedFeedsPref', - saved: res.saved, - pinned: res.pinned, - } + const feedsPref = prefs.findLast( + AppBskyActorDefs.isValidSavedFeedsPref, + ) || { + $type: 'app.bsky.actor.defs#savedFeedsPref', + saved: [], + pinned: [], } + + res = cb(feedsPref.saved, feedsPref.pinned) + feedsPref.saved = res.saved + feedsPref.pinned = res.pinned + return prefs .filter((pref) => !AppBskyActorDefs.isSavedFeedsPref(pref)) - .concat([feedsPref]) + .concat(feedsPref) }) return res } @@ -1493,14 +1436,10 @@ export class Agent extends XrpcClient { await this.updatePreferences((prefs: AppBskyActorDefs.Preferences) => { let existingV2Pref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isSavedFeedsPrefV2(pref) && - AppBskyActorDefs.validateSavedFeedsPrefV2(pref).success, + AppBskyActorDefs.isValidSavedFeedsPrefV2, ) let existingV1Pref = prefs.findLast( - (pref): pref is $Typed => - AppBskyActorDefs.isSavedFeedsPref(pref) && - AppBskyActorDefs.validateSavedFeedsPref(pref).success, + AppBskyActorDefs.isValidSavedFeedsPref, ) if (existingV2Pref) { @@ -1602,11 +1541,7 @@ function remapLegacyLabels( function prefsArrayToLabelerDids( prefs: AppBskyActorDefs.Preferences, ): string[] { - const labelersPref = prefs.findLast( - (pref) => - AppBskyActorDefs.isLabelersPref(pref) && - AppBskyActorDefs.validateLabelersPref(pref).success, - ) + const labelersPref = prefs.findLast(AppBskyActorDefs.isValidLabelersPref) let dids: string[] = [] if (labelersPref) { dids = (labelersPref as AppBskyActorDefs.LabelersPref).labelers.map(