1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use std::collections::HashSet;
use crate::db::kv::SortOrder;
use crate::types::{DynError, Pagination, StreamSorting, Timeframe};
use crate::models::{
post::{PostStream, StreamSource},
tag::TagDetails,
user::{Influencers, UserStream},
};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use super::user::UserDetails;
#[derive(PartialEq, Deserialize)]
pub enum ViewType {
Full,
Partial,
}
#[derive(Serialize, ToSchema, Deserialize, Default, Debug)]
pub struct Bootstrap {
pub users: UserStream,
pub posts: PostStream,
pub list: BootstrapList,
}
#[derive(Serialize, ToSchema, Deserialize, Default, Debug)]
pub struct BootstrapList {
pub stream: Vec<String>,
pub influencers: Vec<String>,
pub recommended: Vec<String>,
}
impl Bootstrap {
/// Builds an pubky.app bootstrap summary for the specified `user_id`, fetching posts, replies,
/// active influencers, and personalized suggestions
///
/// # Parameters
/// - `user_id: &str`
/// The ID of the user whose “ImAlive” stream is being built
/// - `view_type: ViewType`
/// Controls whether to fetch replies and include full stream entries (`Full`)
/// or only base posts (`Partial`)
pub async fn get_by_id(user_id: &str, view_type: ViewType) -> Result<Option<Self>, DynError> {
let mut bootstrap = Self::default();
let mut user_ids = HashSet::new();
// Boostrap guard: Early return if the user lookup fails, avoiding unnecessary work
let Some(_) = UserDetails::get_by_id(user_id).await? else {
return Ok(None);
};
user_ids.insert(user_id.to_string());
let is_full_view_type = view_type == ViewType::Full;
let post_stream_by_timeline =
get_post_stream_timeline(user_id, StreamSource::All, 20).await?;
let post_replies =
bootstrap.handle_post_stream(post_stream_by_timeline, &mut user_ids, view_type);
bootstrap.add_influencers(&mut user_ids).await?;
bootstrap
.add_recommended_users(&mut user_ids, user_id)
.await?;
// // TODO: Missing hot tags
// HotTags::get_hot_tags(None, None, &hot_tag_filter).await?;
if is_full_view_type {
bootstrap
.fetch_and_handle_replies(post_replies, &mut user_ids, user_id)
.await?;
}
// Merge all the users related with posts, post replies, influencers and recommended
bootstrap
.fetch_and_merge_users(&user_ids, Some(user_id))
.await?;
// UserViews has also taggers, fetch the missing users UserViews
if is_full_view_type {
let missing_taggers = bootstrap.collect_missing_taggers(&user_ids);
if !missing_taggers.is_empty() {
bootstrap
.fetch_and_merge_users(&missing_taggers, Some(user_id))
.await?;
}
}
Ok(Some(bootstrap))
}
/// Processes a stream of posts, collecting reply references, adding post taggers and populating the post stream
/// in the response object
///
/// # Parameters
/// - `post_stream`: The `PostStream` whose contained posts will be processed
/// - `user_ids`: A mutable set of user IDs; authors and taggers encountered will be inserted
/// - `view_type`: Indicates whether to operate in `Full` mode (recording stream entries and replies)
fn handle_post_stream(
&mut self,
post_stream: PostStream,
user_ids: &mut HashSet<String>,
view_type: ViewType,
) -> Vec<(String, String)> {
let is_full_view_type = view_type == ViewType::Full;
let mut post_replies = Vec::with_capacity(post_stream.0.len());
for post_view in post_stream.0.iter() {
let author_id = post_view.details.author.clone();
let post_id = post_view.details.id.clone();
if is_full_view_type && post_view.counts.replies > 0 {
post_replies.push((author_id.clone(), post_id.clone()))
}
// Add the author of the post
user_ids.insert(author_id.clone());
// Get all the taggers related with the post
Self::insert_taggers_id(&post_view.tags, user_ids);
// Include the post in the stream list
if is_full_view_type {
self.list.stream.push(format!("{author_id}:{post_id}"));
}
}
// After analyse the posts, authors and tags, push the stream
self.posts.extend(post_stream);
post_replies
}
/// Collects all tagger IDs from the current `users` view that are not yet present
/// in the given `user_ids` set
///
/// # Parameters
///
/// - `user_ids`: A set of user IDs that have already been fetched or seen
fn collect_missing_taggers(&self, user_ids: &HashSet<String>) -> HashSet<String> {
let mut missing_taggers = HashSet::new();
for user in self.users.0.iter() {
user.tags
.iter()
.flat_map(|tags| tags.taggers.iter())
.for_each(|tagger| {
if !user_ids.contains(tagger) {
missing_taggers.insert(tagger.clone());
}
});
}
missing_taggers
}
/// Appends each tagger’s user ID from the given post tag details into the provided set
///
/// # Parameters
/// - `tag_details_list: &Vec<TagDetails>`
/// A reference to a vector of `TagDetails`, each containing a list of tagger IDs
/// - `users_list: &mut HashSet<String>`
/// A mutable reference to a set of user IDs; each tagger ID will be inserted here
fn insert_taggers_id(tag_details_list: &[TagDetails], users_list: &mut HashSet<String>) {
for tag_details in tag_details_list.iter() {
for tagger_pk in tag_details.taggers.iter() {
users_list.insert(tagger_pk.to_string());
}
}
}
/// Fetches and appends user views for the given set of `user_ids`
///
/// # Parameters
/// - `user_ids: HashSet<String>`
/// A set of unique user IDs to fetch views for
/// - `viewer_id: Option<&str>`
/// Optional context user ID for personalized view generation
async fn fetch_and_merge_users(
&mut self,
user_ids: &HashSet<String>,
viewer_id: Option<&str>,
) -> Result<(), DynError> {
if user_ids.is_empty() {
return Ok(());
}
let user_ids_vec: Vec<String> = user_ids.iter().cloned().collect();
// TODO: If the user list is too big, we could do in batches
// for batch in user_ids.chunks(BATCH_SIZE) { ...
if let Some(user_stream) =
UserStream::from_listed_user_ids(&user_ids_vec, viewer_id, None).await?
{
self.users.extend(user_stream);
}
Ok(())
}
/// Fetches up to three replies for each post in `post_replies` and integrates their authors (and any taggers)
/// into both the internal user list
///
/// # Parameters
/// - `post_replies: Vec<(String, String)>`
/// A list of `(author_id, post_id)` tuples indicating which post replies to fetch
/// - `user_ids: &mut HashSet<String>`
/// A mutable reference to a set where each reply’s author ID (and any taggers) will be appended
/// - `viewer_id: &str`
/// The ID of the current viewer
async fn fetch_and_handle_replies(
&mut self,
post_replies: Vec<(String, String)>,
user_ids: &mut HashSet<String>,
viewer_id: &str,
) -> Result<(), DynError> {
// TODO: Might consider in the future to do in all the requests in parallel
// tokio::task::JoinSet or tokio::spawn(async move {...
for (author_id, post_id) in post_replies {
let reply_stream = get_post_stream_timeline(
viewer_id,
StreamSource::PostReplies { author_id, post_id },
3,
)
.await?;
self.handle_post_stream(reply_stream, user_ids, ViewType::Partial);
}
Ok(())
}
/// Fetches today’s active influencers and appends their IDs to both the internal `influencers` list
/// and the provided `user_ids` set
///
/// # Parameters
/// - `user_ids: &mut HashSet<String>` A mutable reference to a set of user IDs
///
async fn add_influencers(&mut self, user_ids: &mut HashSet<String>) -> Result<(), DynError> {
if let Some(influencers) =
Influencers::get_influencers(None, None, 0, 0, Timeframe::Today, true).await?
{
influencers.0.into_iter().for_each(|(id, _)| {
self.list.influencers.push(id.clone());
user_ids.insert(id);
});
}
Ok(())
}
/// Fetches recommended user IDs for the given `viewer_id` and appends them to both
/// the internal `active_users` list and the provided `user_ids` set
///
/// # Parameters
/// - `user_ids: &mut HashSet<String>` A mutable reference to a set of user IDs
/// - `viewer_id: &str` The ID of the user for whom recommended are being generated
async fn add_recommended_users(
&mut self,
user_ids: &mut HashSet<String>,
viewer_id: &str,
) -> Result<(), DynError> {
if let Some(recommended_users) = UserStream::get_recommended_ids(viewer_id, None).await? {
recommended_users.into_iter().for_each(|id| {
self.list.recommended.push(id.clone());
user_ids.insert(id);
});
}
Ok(())
}
}
async fn get_post_stream_timeline(
viewer_id: &str,
source: StreamSource,
limit: usize,
) -> Result<PostStream, DynError> {
let pagination = Pagination {
skip: Some(0),
limit: Some(limit),
start: None,
end: None,
};
Ok(PostStream::get_posts(
source,
pagination,
SortOrder::default(),
StreamSorting::Timeline,
Some(viewer_id.to_string()),
None,
None,
)
.await?
.unwrap_or_default())
}