Skip to content

Commit 27646d1

Browse files
wip(client): connected the classes
1 parent 3ccc17a commit 27646d1

File tree

10 files changed

+319
-119
lines changed

10 files changed

+319
-119
lines changed

packages/client/src/actor/actor.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import type {
77
ComAtprotoRepoStrongRef,
88
} from '@tsky/lexicons';
99
import type { Client } from '~/agent/client';
10-
import { List } from '~/list';
10+
import { FeedGeneratorView } from '~/feed/generator';
11+
import { ListView } from '~/list';
1112
import type { RPCOptions } from '~/types';
1213
import { Paginator } from '~/utils';
1314

@@ -98,8 +99,7 @@ export class Actor {
9899

99100
return {
100101
...res.data,
101-
// TODO: Solve this
102-
// lists: res.data.lists.map((list) => new List(this.client, list)),
102+
lists: res.data.lists.map((list) => new ListView(this.client, list)),
103103
};
104104
});
105105
}
@@ -129,12 +129,19 @@ export class Actor {
129129
*/
130130
feeds(limit?: number, options?: RPCOptions) {
131131
return Paginator.init(async (cursor) => {
132-
const res = await this.client.get('app.bsky.feed.getActorFeeds', {
133-
params: { cursor, actor: this.did, limit },
134-
...options,
135-
});
132+
const res = await this.client
133+
.get('app.bsky.feed.getActorFeeds', {
134+
params: { cursor, actor: this.did, limit },
135+
...options,
136+
})
137+
.then((res) => res.data);
136138

137-
return res.data;
139+
return {
140+
...res,
141+
feeds: res.feeds.map(
142+
(feed) => new FeedGeneratorView(this.client, feed),
143+
),
144+
};
138145
});
139146
}
140147

@@ -183,8 +190,7 @@ export class BasicActorProfile
183190
extends Actor
184191
implements AppBskyActorDefs.ProfileViewBasic
185192
{
186-
// @ts-expect-error - We added this property with Object.assign
187-
handle: string;
193+
handle!: string;
188194
associated?: AppBskyActorDefs.ProfileAssociated;
189195
avatar?: string;
190196
createdAt?: string;

packages/client/src/agent/agent.ts

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { type CredentialManager, XRPC } from '@atcute/client';
22
import type {
3+
AppBskyFeedGetFeed,
4+
AppBskyFeedSendInteractions,
35
AppBskyGraphGetStarterPack,
46
AppBskyGraphGetStarterPacks,
57
At,
68
Queries,
79
} from '@tsky/lexicons';
810
import { ActorWithProfileFunction } from '~/actor';
9-
import { Feed } from '~/feed';
10-
import { List } from '~/list';
11+
import { FeedViewPost } from '~/feed';
12+
import { Post } from '~/post';
1113
import { Search } from '~/search';
1214
import type { RPCOptions } from '~/types';
1315
import { User } from '~/user';
16+
import { Paginator } from '~/utils';
1417
import { Video } from '~/video';
1518
import { Client } from './client';
1619

@@ -34,12 +37,42 @@ export class Agent {
3437
return new ActorWithProfileFunction(this.client, identifier);
3538
}
3639

37-
list(uri: string) {
38-
return new List(this.client, uri);
40+
/**
41+
* Get a hydrated feed from an actor's selected feed generator. Implemented by App View.
42+
*/
43+
async feed(
44+
params: AppBskyFeedGetFeed.Params,
45+
options?: AppBskyFeedGetFeed.Input,
46+
): Promise<Paginator<AppBskyFeedGetFeed.Output>> {
47+
return Paginator.init(async (cursor) => {
48+
const res = await this.client.get('app.bsky.feed.getFeed', {
49+
...(options ?? {}),
50+
params: {
51+
cursor,
52+
...params,
53+
},
54+
});
55+
56+
return {
57+
...res.data,
58+
feed: res.data.feed.map((item) => new FeedViewPost(this.client, item)),
59+
};
60+
});
3961
}
4062

41-
get feed() {
42-
return new Feed(this.client);
63+
/**
64+
* Send information about interactions with feed items back to the feed generator that served them.
65+
*/
66+
async sendInteractions(
67+
interactions: AppBskyFeedSendInteractions.Input['interactions'],
68+
options: RPCOptions = {},
69+
) {
70+
const res = await this.client.call('app.bsky.feed.sendInteractions', {
71+
data: { interactions },
72+
...options,
73+
});
74+
75+
return res.data;
4376
}
4477

4578
get search() {
@@ -62,6 +95,17 @@ export class Agent {
6295
return new Video(this.client);
6396
}
6497

98+
async posts(uris: string[], options?: RPCOptions) {
99+
const data = await this.client
100+
.get('app.bsky.feed.getPosts', {
101+
params: { uris },
102+
...options,
103+
})
104+
.then((res) => res.data);
105+
106+
return data.posts.map((post) => new Post(this.client, post));
107+
}
108+
65109
/**
66110
* Gets a view of a starter pack.
67111
*/

packages/client/src/feed/feed.ts

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,34 @@
1-
import type {
2-
AppBskyFeedGetFeed,
3-
AppBskyFeedSendInteractions,
4-
} from '@tsky/lexicons';
1+
import type { AppBskyFeedDefs, Typed } from '@tsky/lexicons';
2+
import { BasicActorProfile } from '~/actor';
53
import type { Client } from '~/agent/client';
6-
import type { RPCOptions } from '~/types';
7-
import { Paginator } from '~/utils';
8-
import { FeedGenerator } from './generator';
4+
import { Post } from '~/post';
95

10-
export class Feed {
11-
constructor(private client: Client) {}
6+
export class FeedViewPost implements AppBskyFeedDefs.FeedViewPost {
7+
post: AppBskyFeedDefs.PostView;
8+
feedContext?: string | undefined;
9+
reason?:
10+
| Typed<AppBskyFeedDefs.ReasonPin, string>
11+
| Typed<AppBskyFeedDefs.ReasonRepost, string>
12+
| undefined;
13+
reply?: AppBskyFeedDefs.ReplyRef | undefined;
14+
$type?: string | undefined;
1215

13-
/**
14-
* Get a hydrated feed from an actor's selected feed generator. Implemented by App View.
15-
*/
16-
async get(
17-
params: AppBskyFeedGetFeed.Params,
18-
options?: AppBskyFeedGetFeed.Input,
19-
): Promise<Paginator<AppBskyFeedGetFeed.Output>> {
20-
return Paginator.init(async (cursor) => {
21-
const res = await this.client.get('app.bsky.feed.getFeed', {
22-
...(options ?? {}),
23-
params: {
24-
cursor,
25-
...params,
26-
},
27-
});
28-
29-
return res.data;
30-
});
31-
}
32-
33-
/**
34-
* Send information about interactions with feed items back to the feed generator that served them.
35-
*/
36-
async sendInteractions(
37-
interactions: AppBskyFeedSendInteractions.Input['interactions'],
38-
options: RPCOptions = {},
16+
constructor(
17+
private client: Client,
18+
payload: AppBskyFeedDefs.FeedViewPost,
3919
) {
40-
const res = await this.client.call('app.bsky.feed.sendInteractions', {
41-
data: { interactions },
42-
...options,
43-
});
44-
45-
return res.data;
46-
}
20+
this.$type = payload.$type;
21+
this.feedContext = payload.feedContext;
22+
this.reason = payload.reason;
23+
this.post = new Post(this.client, payload.post);
4724

48-
generator() {
49-
return new FeedGenerator(this.client);
25+
if (payload.reply) {
26+
this.reply = {
27+
...payload.reply,
28+
grandparentAuthor: payload.reply.grandparentAuthor
29+
? new BasicActorProfile(this.client, payload.reply.grandparentAuthor)
30+
: undefined,
31+
};
32+
}
5033
}
5134
}

packages/client/src/feed/generator.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import type {
2+
AppBskyActorDefs,
3+
AppBskyFeedDefs,
24
AppBskyFeedGetFeedGenerator,
35
AppBskyFeedGetFeedGenerators,
46
AppBskyFeedGetFeedSkeleton,
7+
AppBskyRichtextFacet,
8+
ComAtprotoLabelDefs,
59
} from '@tsky/lexicons';
10+
import { BasicActorProfile } from '~/actor';
611
import type { Client } from '~/agent/client';
712
import type { RPCOptions } from '~/types';
813
import { Paginator } from '~/utils';
@@ -74,3 +79,33 @@ export class FeedGenerator {
7479
});
7580
}
7681
}
82+
83+
export class FeedGeneratorView implements AppBskyFeedDefs.GeneratorView {
84+
cid!: string;
85+
creator: AppBskyActorDefs.ProfileView;
86+
did!: `did:${string}`;
87+
displayName!: string;
88+
indexedAt!: string;
89+
uri!: string;
90+
acceptsInteractions?: boolean | undefined;
91+
avatar?: string | undefined;
92+
contentMode?:
93+
| (string & {})
94+
| 'app.bsky.feed.defs#contentModeUnspecified'
95+
| 'app.bsky.feed.defs#contentModeVideo'
96+
| undefined;
97+
description?: string | undefined;
98+
descriptionFacets?: AppBskyRichtextFacet.Main[] | undefined;
99+
labels?: ComAtprotoLabelDefs.Label[] | undefined;
100+
likeCount?: number | undefined;
101+
viewer?: AppBskyFeedDefs.GeneratorViewerState | undefined;
102+
$type?: string | undefined;
103+
104+
constructor(
105+
private client: Client,
106+
payload: AppBskyFeedDefs.GeneratorView,
107+
) {
108+
Object.assign(this, payload);
109+
this.creator = new BasicActorProfile(this.client, payload.creator);
110+
}
111+
}

packages/client/src/list/list.ts

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import { ActorProfile } from '~/actor';
1+
import type {
2+
AppBskyActorDefs,
3+
AppBskyGraphDefs,
4+
AppBskyRichtextFacet,
5+
ComAtprotoLabelDefs,
6+
} from '@tsky/lexicons';
7+
import { ActorProfile, BasicActorProfile } from '~/actor';
28
import type { Client } from '~/agent/client';
9+
import { Post } from '~/post';
310
import type { RPCOptions } from '~/types';
411
import { Paginator } from '~/utils';
512

613
export class List {
714
constructor(
815
private client: Client,
9-
private uri: string,
16+
public uri: string,
1017
) {}
1118

1219
/**
@@ -29,6 +36,7 @@ export class List {
2936
...item,
3037
subject: new ActorProfile(this.client, item.subject),
3138
})),
39+
list: new ListView(this.client, res.data.list),
3240
};
3341
});
3442
}
@@ -47,7 +55,60 @@ export class List {
4755
...options,
4856
});
4957

50-
return res.data;
58+
return {
59+
...res.data,
60+
feed: res.data.feed.map((item) => ({
61+
...item,
62+
post: new Post(this.client, item.post),
63+
reply: item.reply
64+
? {
65+
...item.reply,
66+
grandparentAuthor: item.reply.grandparentAuthor
67+
? new BasicActorProfile(
68+
this.client,
69+
item.reply.grandparentAuthor,
70+
)
71+
: undefined,
72+
}
73+
: undefined,
74+
})),
75+
};
5176
});
5277
}
5378
}
79+
80+
export class ListBasicView
81+
extends List
82+
implements AppBskyGraphDefs.ListViewBasic
83+
{
84+
cid!: string;
85+
name!: string;
86+
purpose!: AppBskyGraphDefs.ListPurpose;
87+
avatar?: string | undefined;
88+
indexedAt?: string | undefined;
89+
labels?: ComAtprotoLabelDefs.Label[] | undefined;
90+
listItemCount?: number | undefined;
91+
viewer?: AppBskyGraphDefs.ListViewerState | undefined;
92+
$type?: string | undefined;
93+
94+
constructor(client: Client, payload: AppBskyGraphDefs.ListViewBasic) {
95+
super(client, payload.uri);
96+
Object.assign(this, payload);
97+
}
98+
}
99+
100+
export class ListView
101+
extends ListBasicView
102+
implements AppBskyGraphDefs.ListView
103+
{
104+
override indexedAt: string;
105+
creator: AppBskyActorDefs.ProfileView;
106+
description?: string | undefined;
107+
descriptionFacets?: AppBskyRichtextFacet.Main[] | undefined;
108+
109+
constructor(client: Client, payload: AppBskyGraphDefs.ListView) {
110+
super(client, payload);
111+
this.indexedAt = payload.indexedAt;
112+
this.creator = new ActorProfile(client, payload.creator);
113+
}
114+
}

packages/client/src/post/post.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import type { RPCOptions } from '~/types';
1818
import { Paginator, parseAtUri } from '~/utils';
1919

2020
export class Post implements AppBskyFeedDefs.PostView {
21-
uri: string;
21+
uri!: string;
2222
author: BasicActorProfile;
23-
cid: string;
24-
indexedAt: string;
23+
cid!: string;
24+
indexedAt!: string;
2525
record: unknown;
2626
embed?:
2727
| Typed<AppBskyEmbedExternal.View, string>

0 commit comments

Comments
 (0)