oku_fs/fs/net.rs
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
use std::{collections::HashSet, path::PathBuf, time::SystemTime};
use crate::{
config::OkuFsConfig,
database::{OkuIdentity, OkuNote, OkuPost, OkuUser, DATABASE},
discovery::REPUBLISH_DELAY,
fs::{merge_tickets, OkuFs},
};
use anyhow::anyhow;
use futures::StreamExt;
use iroh::{
base::ticket::Ticket,
blobs::Hash,
client::docs::Entry,
docs::{Author, AuthorId, CapabilityKind, DocTicket, NamespaceId},
};
use miette::IntoDiagnostic;
use rayon::iter::{
FromParallelIterator, IntoParallelIterator, IntoParallelRefIterator, ParallelIterator,
};
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Serialize, Deserialize, Debug, Clone)]
/// An Oku user's credentials, which are sensitive, exported from a node, able to be imported into another.
pub struct ExportedUser {
author: Author,
home_replica: Option<NamespaceId>,
home_replica_ticket: Option<DocTicket>,
}
impl OkuFs {
/// Retrieve the content authorship ID used by the node.
///
/// # Returns
///
/// The content authorship ID used by the node.
pub async fn default_author(&self) -> anyhow::Result<AuthorId> {
self.node.authors().default().await
}
/// Exports the local Oku user's credentials.
///
/// # Returns
///
/// The local Oku user's credentials, containing sensitive information.
pub async fn export_user(&self) -> Option<ExportedUser> {
let default_author = self.get_author().await.ok();
let home_replica = self.home_replica().await;
let home_replica_ticket = match home_replica {
Some(home_replica_id) => self
.create_document_ticket(home_replica_id, iroh::client::docs::ShareMode::Write)
.await
.ok(),
None => None,
};
default_author.map(|author| ExportedUser {
author,
home_replica,
home_replica_ticket,
})
}
/// Imports Oku user credentials that were exported from another node.
///
/// # Arguments
///
/// * `exported_user` - Oku user credentials, which contain sensitive information.
pub async fn import_user(&self, exported_user: ExportedUser) -> miette::Result<()> {
self.node
.authors()
.import(exported_user.author.clone())
.await
.map_err(|e| miette::miette!("{}", e))?;
self.node
.authors()
.set_default(exported_user.author.id())
.await
.map_err(|e| miette::miette!("{}", e))?;
match (
exported_user.home_replica,
exported_user.home_replica_ticket,
) {
(Some(home_replica), Some(home_replica_ticket)) => match self
.fetch_replica_by_ticket(&home_replica_ticket, None)
.await
{
Ok(_) => (),
Err(_e) => self
.fetch_replica_by_id(home_replica, None)
.await
.map_err(|e| miette::miette!("{}", e))?,
},
(Some(home_replica), None) => self
.fetch_replica_by_id(home_replica, None)
.await
.map_err(|e| miette::miette!("{}", e))?,
_ => (),
}
self.set_home_replica(exported_user.home_replica)
}
/// Exports the local Oku user's credentials in TOML format.
///
/// # Returns
///
/// The local Oku user's credentials, containing sensitive information.
pub async fn export_user_toml(&self) -> miette::Result<String> {
toml::to_string(
&self
.export_user()
.await
.ok_or(miette::miette!("No authorship credentials to export … "))?,
)
.into_diagnostic()
}
/// Imports Oku user credentials that were exported from another node.
///
/// # Arguments
///
/// * `exported_user` - Oku user credentials, encoded in TOML format. They contain sensitive information.
pub async fn import_user_toml(&self, exported_user_toml: String) -> miette::Result<()> {
let exported_user: ExportedUser = toml::from_str(&exported_user_toml).into_diagnostic()?;
self.import_user(exported_user).await
}
/// Retrieve the home replica of the Oku user.
///
/// # Returns
///
/// The home replica of the Oku user.
pub async fn home_replica(&self) -> Option<NamespaceId> {
let config = OkuFsConfig::load_or_create_config().ok()?;
let home_replica = config.home_replica().ok().flatten()?;
let home_replica_capability = self.get_replica_capability(home_replica).await.ok()?;
match home_replica_capability {
CapabilityKind::Write => Some(home_replica),
CapabilityKind::Read => None,
}
}
/// Set the home replica of the Oku user.
///
/// # Arguments
///
/// * `home_replica` - The ID of the intended new home replica.
pub fn set_home_replica(&self, home_replica: Option<NamespaceId>) -> miette::Result<()> {
let config = OkuFsConfig::load_or_create_config()?;
config.set_home_replica(home_replica)?;
config.save()?;
self.replica_sender.send_replace(());
Ok(())
}
/// Retrieves the OkuNet posts by the local user, if any.
///
/// # Returns
///
/// A list of the OkuNet posts by the local user.
pub async fn posts(&self) -> Option<Vec<OkuPost>> {
let post_files = self
.read_directory(self.home_replica().await?, "/posts/".into())
.await
.ok()
.unwrap_or_default();
Some(
post_files
.par_iter()
.filter_map(|(entry, bytes)| {
toml::from_str::<OkuNote>(String::from_utf8_lossy(bytes).as_ref())
.ok()
.map(|x| OkuPost {
entry: entry.clone(),
note: x,
})
})
.collect(),
)
}
/// Retrieves all posts containing a given tag, whether they are authored by the local user or an OkuNet user.
///
/// # Arguments
///
/// * `tag` - A tag.
///
/// # Returns
///
/// A list of OkuNet posts with the given tag.
pub async fn all_posts_with_tag(&self, tag: String) -> Vec<OkuPost> {
let mut posts = HashSet::<_>::from_par_iter(self.posts().await.unwrap_or_default());
posts.extend(DATABASE.get_posts().unwrap_or_default());
posts
.into_par_iter()
.filter(|x| x.note.tags.contains(&tag))
.collect()
}
/// Retrieves the set of all tags that appear in posts authored by the local user or an OkuNet user.
///
/// # Returns
///
/// All tags that appear across all posts on the local machine.
pub async fn all_tags(&self) -> HashSet<String> {
let mut tags = HashSet::<_>::from_par_iter(
self.posts()
.await
.unwrap_or_default()
.into_par_iter()
.flat_map(|x| x.note.tags),
);
tags.extend(
DATABASE
.get_posts()
.unwrap_or_default()
.into_iter()
.flat_map(|x| x.note.tags),
);
tags
}
/// Retrieves an OkuNet post authored by the local user using its path.
///
/// # Arguments
///
/// * `path` - A path to a post in the user's home replica.
///
/// # Returns
///
/// The OkuNet post at the given path.
pub async fn post(&self, path: PathBuf) -> miette::Result<OkuPost> {
let namespace_id = self
.home_replica()
.await
.ok_or(miette::miette!("Home replica not set … "))?;
match self.read_file(namespace_id, path.clone()).await {
Ok(bytes) => {
let note = toml::from_str::<OkuNote>(String::from_utf8_lossy(&bytes).as_ref())
.into_diagnostic()?;
Ok(OkuPost {
entry: self.get_entry(namespace_id, path).await?,
note,
})
}
Err(e) => Err(miette::miette!("{}", e)),
}
}
/// Retrieves the OkuNet identity of the local user.
///
/// # Returns
///
/// The local user's OkuNet identity, if they have one.
pub async fn identity(&self) -> Option<OkuIdentity> {
let profile_bytes = self
.read_file(self.home_replica().await?, "/profile.toml".into())
.await
.ok()?;
toml::from_str(String::from_utf8_lossy(&profile_bytes).as_ref()).ok()
}
/// Replaces the current OkuNet identity of the local user.
///
/// # Arguments
///
/// * `identity` - The new OkuNet identity.
///
/// # Returns
///
/// The hash of the new identity file in the local user's home replica.
pub async fn set_identity(&self, identity: OkuIdentity) -> miette::Result<Hash> {
// It is not valid to follow or unfollow yourself.
let mut validated_identity = identity.clone();
let me = self.default_author().await.ok();
validated_identity
.following
.retain(|y| !matches!(me, Some(x) if &x == y));
validated_identity
.blocked
.retain(|y| !matches!(me, Some(x) if &x == y));
// It is not valid to follow blocked people.
validated_identity.following = validated_identity
.following
.difference(&validated_identity.blocked)
.copied()
.collect();
self.create_or_modify_file(
self.home_replica()
.await
.ok_or(miette::miette!("No home replica set … "))?,
"/profile.toml".into(),
toml::to_string_pretty(&validated_identity).into_diagnostic()?,
)
.await
}
/// Replaces the current display name of the local user.
///
/// # Arguments
///
/// * `display_name` - The new display name.
///
/// # Returns
///
/// # The hash of the new identity file in the local user's home replica.
pub async fn set_display_name(&self, display_name: String) -> miette::Result<Hash> {
let mut identity = self.identity().await.unwrap_or_default();
identity.name = display_name;
self.set_identity(identity).await
}
/// Follow or unfollow a user.
///
/// # Arguments
///
/// * `author_id` - The user to follow or unfollow's content authorship ID.
///
/// # Returns
///
/// The hash of the new identity file in the local user's home replica.
pub async fn toggle_follow(&self, author_id: AuthorId) -> miette::Result<Hash> {
let mut identity = self.identity().await.unwrap_or_default();
match identity.following.contains(&author_id) {
true => identity.following.remove(&author_id),
false => identity.following.insert(author_id),
};
self.set_identity(identity).await
}
/// Block or unblock a user.
///
/// # Arguments
///
/// * `author_id` - The user to block or unblock's content authorship ID.
///
/// # Returns
///
/// The hash of the new identity file in the local user's home replica.
pub async fn toggle_block(&self, author_id: AuthorId) -> miette::Result<Hash> {
let mut identity = self.identity().await.unwrap_or_default();
match identity.blocked.contains(&author_id) {
true => identity.blocked.remove(&author_id),
false => identity.blocked.insert(author_id),
};
self.set_identity(identity).await
}
/// Check if a user is followed.
///
/// # Arguments
///
/// * `author_id` - The user's content authorship ID.
///
/// # Returns
///
/// Whether or not the user is followed.
pub async fn is_followed(&self, author_id: &AuthorId) -> bool {
self.identity()
.await
.map(|x| x.following.contains(author_id))
.unwrap_or(false)
}
/// Check if a user is blocked.
///
/// # Arguments
///
/// * `author_id` - The user's content authorship ID.
///
/// # Returns
///
/// Whether or not the user is blocked.
pub async fn is_blocked(&self, author_id: &AuthorId) -> bool {
self.identity()
.await
.map(|x| x.blocked.contains(author_id))
.unwrap_or(false)
}
/// Check whether or not an author ID is the local user's.
///
/// # Arguments
///
/// * `author_id` - A user's content authorship ID.
///
/// # Returns
///
/// Whether or not the user's authorship ID is the local user's.
pub async fn is_me(&self, author_id: &AuthorId) -> bool {
matches!(self.default_author().await.ok(), Some(x) if &x == author_id)
}
/// Retrieves an [`OkuUser`] representing the local user.
///
/// # Returns
///
/// An [`OkuUser`] representing the current user, as if it were retrieved from another Oku user's database.
pub async fn user(&self) -> miette::Result<OkuUser> {
Ok(OkuUser {
author_id: self
.default_author()
.await
.map_err(|e| miette::miette!("{}", e))?,
last_fetched: SystemTime::now(),
posts: self
.posts()
.await
.map(|x| x.into_par_iter().map(|y| y.entry).collect())
.unwrap_or_default(),
identity: self.identity().await,
})
}
/// Attempts to retrieve an OkuNet post from a file entry.
///
/// # Arguments
///
/// * `entry` - The file entry to parse.
///
/// # Returns
///
/// An OkuNet post, if the entry represents one.
pub async fn post_from_entry(&self, entry: &Entry) -> miette::Result<OkuPost> {
let bytes = entry
.content_bytes(&self.node)
.await
.map_err(|e| miette::miette!("{}", e))?;
let note = toml::from_str::<OkuNote>(String::from_utf8_lossy(&bytes).as_ref())
.into_diagnostic()?;
Ok(OkuPost {
entry: entry.clone(),
note,
})
}
/// Retrieves OkuNet posts from the file entries in an [`OkuUser`].
///
/// # Arguments
///
/// * `user` - The OkuNet user record containing the file entries.
///
/// # Returns
///
/// A list of OkuNet posts contained within the user record.
pub async fn posts_from_user(&self, user: &OkuUser) -> miette::Result<Vec<OkuPost>> {
let mut posts: Vec<_> = Vec::new();
for post in user.posts.clone() {
posts.push(self.post_from_entry(&post).await?);
}
Ok(posts)
}
/// Create or modify an OkuNet post in the user's home replica.
///
/// # Arguments
///
/// * `path` - The path to create, or modify, the post at; a suggested path is generated if none is provided.
///
/// * `url` - The URL the post is regarding.
///
/// * `title` - The title of the post.
///
/// * `body` - The body of the post.
///
/// * `tags` - A list of tags associated with the post.
///
/// # Returns
///
/// A hash of the post's content.
pub async fn create_or_modify_post(
&self,
path: Option<PathBuf>,
url: Url,
title: String,
body: String,
tags: HashSet<String>,
) -> miette::Result<Hash> {
let home_replica_id = self
.home_replica()
.await
.ok_or(miette::miette!("No home replica set … "))?;
let new_note = OkuNote {
url,
title,
body,
tags,
};
let post_path = match path {
Some(given_path) => given_path,
None => new_note.suggested_post_path().into(),
};
self.create_or_modify_file(
home_replica_id,
post_path,
toml::to_string_pretty(&new_note).into_diagnostic()?,
)
.await
}
/// Delete an OkuNet post in the user's home replica.
///
/// # Arguments
///
/// * `path` - A path to a post in the user's home replica.
///
/// # Returns
///
/// The number of entries deleted in the replica, which should be 1 if the file was successfully deleted.
pub async fn delete_post(&self, path: PathBuf) -> miette::Result<usize> {
let home_replica_id = self
.home_replica()
.await
.ok_or(miette::miette!("No home replica set … "))?;
self.delete_file(home_replica_id, path).await
}
/// Refreshes any user data last retrieved longer than [`REPUBLISH_DELAY`] ago according to the system time; the users one is following, and the users they're following, are recorded locally.
/// Blocked users are not recorded.
pub async fn refresh_users(&self) -> miette::Result<()> {
// Wanted users: followed users
// Unwanted users: blocked users, unfollowed users
let (followed_users, blocked_users) = match self.identity().await {
Some(identity) => (identity.following, identity.blocked),
None => (HashSet::new(), HashSet::new()),
};
// In case a user is somehow followed and blocked (additional checks should already prevent this)
let users_to_add: HashSet<_> = followed_users
.difference(&blocked_users)
.map(|x| x.to_owned())
.collect();
let local_users: HashSet<_> = DATABASE.all_local_users().into_par_iter().collect();
let users_to_delete: HashSet<_> = local_users
.difference(&users_to_add)
.map(|x| x.to_owned())
.collect();
for user_id in users_to_add {
let user = self.get_or_fetch_user(user_id).await?;
let (user_followed_users, user_blocked_users) = match user.identity {
Some(identity) => (identity.following, identity.blocked),
None => (HashSet::new(), HashSet::new()),
};
for user_user in user_followed_users.difference(&user_blocked_users) {
self.get_or_fetch_user(*user_user).await?;
}
}
DATABASE.delete_by_author_ids(Vec::from_par_iter(users_to_delete))?;
Ok(())
}
/// Retrieves user data regardless of when last retrieved; the users one is following, and the users they're following, are recorded locally.
/// Blocked users are not recorded.
pub async fn fetch_users(&self) -> miette::Result<()> {
// Wanted users: followed users
// Unwanted users: blocked users, unfollowed users
let (followed_users, blocked_users) = match self.identity().await {
Some(identity) => (identity.following, identity.blocked),
None => (HashSet::new(), HashSet::new()),
};
// In case a user is somehow followed and blocked (additional checks should already prevent this)
let users_to_add: HashSet<_> = followed_users
.difference(&blocked_users)
.map(|x| x.to_owned())
.collect();
let local_users: HashSet<_> = DATABASE.all_local_users().into_par_iter().collect();
let users_to_delete: HashSet<_> = local_users
.difference(&users_to_add)
.map(|x| x.to_owned())
.collect();
for user_id in users_to_add {
let user = self.fetch_user(user_id).await?;
let (user_followed_users, user_blocked_users) = match user.identity {
Some(identity) => (identity.following, identity.blocked),
None => (HashSet::new(), HashSet::new()),
};
for user_user in user_followed_users.difference(&user_blocked_users) {
self.fetch_user(*user_user).await?;
}
}
DATABASE.delete_by_author_ids(Vec::from_par_iter(users_to_delete))?;
Ok(())
}
/// Use the mainline DHT to obtain a ticket for the home replica of the user with the given content authorship ID.
///
/// # Arguments
///
/// * `author_id` - A content authorship ID.
///
/// # Returns
///
/// A ticket for the home replica of the user with the given content authorship ID.
pub async fn resolve_author_id(&self, author_id: AuthorId) -> anyhow::Result<DocTicket> {
let get_stream = self.dht.get_mutable(author_id.as_bytes(), None, None)?;
tokio::pin!(get_stream);
let mut tickets = Vec::new();
while let Some(mutable_item) = get_stream.next().await {
tickets.push(DocTicket::from_bytes(mutable_item.value())?)
}
merge_tickets(tickets).ok_or(anyhow!(
"Could not find tickets for {} … ",
author_id.to_string()
))
}
/// Join a swarm to fetch the latest version of an OkuNet post.
///
/// # Arguments
///
/// * `author_id` - The authorship ID of the post's author.
///
/// * `path` - The path to the post in the author's home replica.
///
/// # Returns
///
/// The requested OkuNet post.
pub async fn fetch_post(&self, author_id: AuthorId, path: PathBuf) -> miette::Result<OkuPost> {
let ticket = self
.resolve_author_id(author_id)
.await
.map_err(|e| miette::miette!("{}", e))?;
let namespace_id = ticket.capability.id();
match self.fetch_file_with_ticket(&ticket, path.clone()).await {
Ok(bytes) => {
let note = toml::from_str::<OkuNote>(String::from_utf8_lossy(&bytes).as_ref())
.into_diagnostic()?;
Ok(OkuPost {
entry: self.get_entry(namespace_id, path).await?,
note,
})
}
Err(e) => Err(miette::miette!("{}", e)),
}
}
/// Retrieves an OkuNet post from the database, or from the mainline DHT if not found locally.
///
/// # Arguments
///
/// * `author_id` - The authorship ID of the post's author.
///
/// * `path` - The path to the post in the author's home replica.
///
/// # Returns
///
/// The requested OkuNet post.
pub async fn get_or_fetch_post(
&self,
author_id: AuthorId,
path: PathBuf,
) -> miette::Result<OkuPost> {
match DATABASE.get_post(author_id, path.clone()).ok().flatten() {
Some(post) => Ok(post),
None => self.fetch_post(author_id, path).await,
}
}
/// Join a swarm to fetch the latest version of a home replica and obtain the OkuNet identity within it.
///
/// # Arguments
///
/// * `author_id` - A content authorship ID.
///
/// # Returns
///
/// The OkuNet identity within the home replica of the user with the given content authorship ID.
pub async fn fetch_profile(&self, ticket: &DocTicket) -> miette::Result<OkuIdentity> {
match self
.fetch_file_with_ticket(ticket, "/profile.toml".into())
.await
{
Ok(profile_bytes) => Ok(toml::from_str(
String::from_utf8_lossy(&profile_bytes).as_ref(),
)
.into_diagnostic()?),
Err(e) => Err(miette::miette!("{}", e)),
}
}
/// Join a swarm to fetch the latest version of a home replica and obtain the OkuNet posts within it.
///
/// # Arguments
///
/// * `author_id` - A content authorship ID.
///
/// # Returns
///
/// The OkuNet posts within the home replica of the user with the given content authorship ID.
pub async fn fetch_posts(&self, ticket: &DocTicket) -> miette::Result<Vec<OkuPost>> {
match self
.fetch_directory_with_ticket(ticket, "/posts/".into())
.await
{
Ok(post_files) => Ok(post_files
.par_iter()
.filter_map(|(entry, bytes)| {
toml::from_str::<OkuNote>(String::from_utf8_lossy(bytes).as_ref())
.ok()
.map(|x| OkuPost {
entry: entry.clone(),
note: x,
})
})
.collect()),
Err(e) => Err(miette::miette!("{}", e)),
}
}
/// Obtain an OkuNet user's content, identified by their content authorship ID.
///
/// If last retrieved longer than [`REPUBLISH_DELAY`] ago according to the system time, a known user's content will be re-fetched.
///
/// # Arguments
///
/// * `author_id` - A content authorship ID.
///
/// # Returns
///
/// An OkuNet user's content.
pub async fn get_or_fetch_user(&self, author_id: AuthorId) -> miette::Result<OkuUser> {
match DATABASE.get_user(author_id).ok().flatten() {
Some(user) => {
match SystemTime::now()
.duration_since(user.last_fetched)
.into_diagnostic()?
> REPUBLISH_DELAY
{
true => self.fetch_user(author_id).await,
false => Ok(user),
}
}
None => self.fetch_user(author_id).await,
}
}
/// Fetch the latest version of an OkuNet user's content, identified by their content authorship ID.
///
/// # Arguments
///
/// * `author_id` - A content authorship ID.
///
/// # Returns
///
/// The latest version of an OkuNet user's content.
pub async fn fetch_user(&self, author_id: AuthorId) -> miette::Result<OkuUser> {
let ticket = self
.resolve_author_id(author_id)
.await
.map_err(|e| miette::miette!("{}", e))?;
let profile = self.fetch_profile(&ticket).await.ok();
let posts = self.fetch_posts(&ticket).await.ok();
if let Some(posts) = posts.clone() {
DATABASE.upsert_posts(posts)?;
}
DATABASE.upsert_user(OkuUser {
author_id,
last_fetched: SystemTime::now(),
posts: posts
.map(|x| x.into_par_iter().map(|y| y.entry).collect())
.unwrap_or_default(),
identity: profile,
})?;
DATABASE
.get_user(author_id)?
.ok_or(miette::miette!("User {} not found … ", author_id))
}
}