From a966f7fcb56f5b2b008b44c74a84e2f0e6208a67 Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 30 Apr 2024 14:17:29 +0200 Subject: [PATCH] fix: fetch remote users following/followers count --- src/server/fetcher.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/server/fetcher.rs b/src/server/fetcher.rs index c92aabf7..382b5e6d 100644 --- a/src/server/fetcher.rs +++ b/src/server/fetcher.rs @@ -1,6 +1,6 @@ use std::collections::BTreeMap; -use apb::{target::Addressed, Activity, Object}; +use apb::{target::Addressed, Activity, Collection, Object}; use base64::Engine; use reqwest::{header::{ACCEPT, CONTENT_TYPE, USER_AGENT}, Method, Response}; use sea_orm::{sea_query::Expr, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter}; @@ -84,7 +84,26 @@ impl Fetcher for Context { let user = Self::request( Method::GET, id, None, &format!("https://{}", self.domain()), &self.app().private_key, self.domain(), ).await?.json::().await?; - let user_model = model::user::Model::new(&user)?; + let mut user_model = model::user::Model::new(&user)?; + + // TODO try fetching these numbers from audience/generator fields to avoid making 2 more GETs + if let Some(followers_url) = &user_model.followers { + let user_followers = Self::request( + Method::GET, followers_url, None, &format!("https://{}", self.domain()), &self.app().private_key, self.domain(), + ).await?.json::().await?; + if let Some(total) = user_followers.total_items() { + user_model.followers_count = total as i64; + } + } + + if let Some(following_url) = &user_model.following { + let user_following = Self::request( + Method::GET, following_url, None, &format!("https://{}", self.domain()), &self.app().private_key, self.domain(), + ).await?.json::().await?; + if let Some(total) = user_following.total_items() { + user_model.following_count = total as i64; + } + } // TODO this may fail: while fetching, remote server may fetch our service actor. // if it does so with http signature, we will fetch that actor in background