diff options
author | Eric Bailey <git@esb.lol> | 2024-04-29 16:04:35 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-29 22:04:35 +0100 |
commit | a4e34537cee8e12a022238f054bee4fe22cc7325 (patch) | |
tree | e8ffbb4993441f64e4c112e9b046c566e577d661 /src/lib/api | |
parent | d893fe005d9d43e28b2926f8fed4f13165843d3b (diff) | |
download | voidsky-a4e34537cee8e12a022238f054bee4fe22cc7325.tar.zst |
Send Bluesky feeds and suggested follows more data (#3695)
* WIP * Fix constructors * Clean up * Tweak * Rm extra assignment * Narrow down the argument --------- Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
Diffstat (limited to 'src/lib/api')
-rw-r--r-- | src/lib/api/feed/custom.ts | 10 | ||||
-rw-r--r-- | src/lib/api/feed/home.ts | 11 | ||||
-rw-r--r-- | src/lib/api/feed/merge.ts | 14 | ||||
-rw-r--r-- | src/lib/api/feed/utils.ts | 21 |
4 files changed, 55 insertions, 1 deletions
diff --git a/src/lib/api/feed/custom.ts b/src/lib/api/feed/custom.ts index 75182c41f..87e45ceba 100644 --- a/src/lib/api/feed/custom.ts +++ b/src/lib/api/feed/custom.ts @@ -7,20 +7,25 @@ import { import {getContentLanguages} from '#/state/preferences/languages' import {FeedAPI, FeedAPIResponse} from './types' +import {createBskyTopicsHeader, isBlueskyOwnedFeed} from './utils' export class CustomFeedAPI implements FeedAPI { getAgent: () => BskyAgent params: GetCustomFeed.QueryParams + userInterests?: string constructor({ getAgent, feedParams, + userInterests, }: { getAgent: () => BskyAgent feedParams: GetCustomFeed.QueryParams + userInterests?: string }) { this.getAgent = getAgent this.params = feedParams + this.userInterests = userInterests } async peekLatest(): Promise<AppBskyFeedDefs.FeedViewPost> { @@ -44,6 +49,8 @@ export class CustomFeedAPI implements FeedAPI { }): Promise<FeedAPIResponse> { const contentLangs = getContentLanguages().join(',') const agent = this.getAgent() + const isBlueskyOwned = isBlueskyOwnedFeed(this.params.feed) + const res = agent.session ? await this.getAgent().app.bsky.feed.getFeed( { @@ -53,6 +60,9 @@ export class CustomFeedAPI implements FeedAPI { }, { headers: { + ...(isBlueskyOwned + ? createBskyTopicsHeader(this.userInterests) + : {}), 'Accept-Language': contentLangs, }, }, diff --git a/src/lib/api/feed/home.ts b/src/lib/api/feed/home.ts index 4a5308346..270f3aacb 100644 --- a/src/lib/api/feed/home.ts +++ b/src/lib/api/feed/home.ts @@ -32,14 +32,22 @@ export class HomeFeedAPI implements FeedAPI { discover: CustomFeedAPI usingDiscover = false itemCursor = 0 + userInterests?: string - constructor({getAgent}: {getAgent: () => BskyAgent}) { + constructor({ + userInterests, + getAgent, + }: { + userInterests?: string + getAgent: () => BskyAgent + }) { this.getAgent = getAgent this.following = new FollowingFeedAPI({getAgent}) this.discover = new CustomFeedAPI({ getAgent, feedParams: {feed: PROD_DEFAULT_FEED('whats-hot')}, }) + this.userInterests = userInterests } reset() { @@ -47,6 +55,7 @@ export class HomeFeedAPI implements FeedAPI { this.discover = new CustomFeedAPI({ getAgent: this.getAgent, feedParams: {feed: PROD_DEFAULT_FEED('whats-hot')}, + userInterests: this.userInterests, }) this.usingDiscover = false this.itemCursor = 0 diff --git a/src/lib/api/feed/merge.ts b/src/lib/api/feed/merge.ts index c85de0306..b7ac8bce1 100644 --- a/src/lib/api/feed/merge.ts +++ b/src/lib/api/feed/merge.ts @@ -9,11 +9,13 @@ import {feedUriToHref} from 'lib/strings/url-helpers' import {FeedTuner} from '../feed-manip' import {FeedTunerFn} from '../feed-manip' import {FeedAPI, FeedAPIResponse, ReasonFeedSource} from './types' +import {createBskyTopicsHeader, isBlueskyOwnedFeed} from './utils' const REQUEST_WAIT_MS = 500 // 500ms const POST_AGE_CUTOFF = 60e3 * 60 * 24 // 24hours export class MergeFeedAPI implements FeedAPI { + userInterests?: string getAgent: () => BskyAgent params: FeedParams feedTuners: FeedTunerFn[] @@ -27,14 +29,17 @@ export class MergeFeedAPI implements FeedAPI { getAgent, feedParams, feedTuners, + userInterests, }: { getAgent: () => BskyAgent feedParams: FeedParams feedTuners: FeedTunerFn[] + userInterests?: string }) { this.getAgent = getAgent this.params = feedParams this.feedTuners = feedTuners + this.userInterests = userInterests this.following = new MergeFeedSource_Following({ getAgent: this.getAgent, feedTuners: this.feedTuners, @@ -58,6 +63,7 @@ export class MergeFeedAPI implements FeedAPI { getAgent: this.getAgent, feedUri, feedTuners: this.feedTuners, + userInterests: this.userInterests, }), ), ) @@ -254,15 +260,18 @@ class MergeFeedSource_Custom extends MergeFeedSource { getAgent: () => BskyAgent minDate: Date feedUri: string + userInterests?: string constructor({ getAgent, feedUri, feedTuners, + userInterests, }: { getAgent: () => BskyAgent feedUri: string feedTuners: FeedTunerFn[] + userInterests?: string }) { super({ getAgent, @@ -270,6 +279,7 @@ class MergeFeedSource_Custom extends MergeFeedSource { }) this.getAgent = getAgent this.feedUri = feedUri + this.userInterests = userInterests this.sourceInfo = { $type: 'reasonFeedSource', uri: feedUri, @@ -284,6 +294,7 @@ class MergeFeedSource_Custom extends MergeFeedSource { ): Promise<AppBskyFeedGetTimeline.Response> { try { const contentLangs = getContentLanguages().join(',') + const isBlueskyOwned = isBlueskyOwnedFeed(this.feedUri) const res = await this.getAgent().app.bsky.feed.getFeed( { cursor, @@ -292,6 +303,9 @@ class MergeFeedSource_Custom extends MergeFeedSource { }, { headers: { + ...(isBlueskyOwned + ? createBskyTopicsHeader(this.userInterests) + : {}), 'Accept-Language': contentLangs, }, }, diff --git a/src/lib/api/feed/utils.ts b/src/lib/api/feed/utils.ts new file mode 100644 index 000000000..50162ed2a --- /dev/null +++ b/src/lib/api/feed/utils.ts @@ -0,0 +1,21 @@ +import {AtUri} from '@atproto/api' + +import {BSKY_FEED_OWNER_DIDS} from '#/lib/constants' +import {UsePreferencesQueryResponse} from '#/state/queries/preferences' + +export function createBskyTopicsHeader(userInterests?: string) { + return { + 'X-Bsky-Topics': userInterests || '', + } +} + +export function aggregateUserInterests( + preferences?: UsePreferencesQueryResponse, +) { + return preferences?.interests?.tags?.join(',') || '' +} + +export function isBlueskyOwnedFeed(feedUri: string) { + const uri = new AtUri(feedUri) + return BSKY_FEED_OWNER_DIDS.includes(uri.host) +} |