From bd75b1f958d5696d5ceb7499357b7f92cc2b1e8a Mon Sep 17 00:00:00 2001 From: asonix Date: Mon, 23 Mar 2020 17:37:23 -0500 Subject: [PATCH] Add a crude re-seed for populating actors --- src/data/actor.rs | 30 +++++++++++++++++++++++++++++- src/main.rs | 4 ++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/data/actor.rs b/src/data/actor.rs index d69f773..b0c7aa3 100644 --- a/src/data/actor.rs +++ b/src/data/actor.rs @@ -1,6 +1,6 @@ use crate::{apub::AcceptedActors, db::Db, error::MyError, requests::Requests}; use activitystreams::primitives::XsdAnyUri; -use log::error; +use log::{error, info, warn}; use std::{collections::HashSet, sync::Arc, time::Duration}; use tokio::sync::RwLock; use ttl_cache::TtlCache; @@ -28,6 +28,34 @@ impl ActorCache { cache } + pub async fn re_seed( + &self, + listeners: &[XsdAnyUri], + requests: &Requests, + ) -> Result<(), MyError> { + info!("Seeding actors with {:?}", listeners); + for listener in listeners { + let mut listener = listener.clone(); + listener.as_url_mut().set_path("/actor"); + + let actor = match self.get(&listener, requests).await { + Ok(actor) => actor, + Err(e) => { + warn!("Couldn't seed {} due to {}, continuing", e, listener); + continue; + } + }; + + match self.save(actor).await { + Ok(_) => (), + Err(e) => { + warn!("Coudn't seed {} due to {}, continuing", listener, e); + } + } + } + Ok(()) + } + pub async fn is_following(&self, id: &XsdAnyUri) -> bool { self.following.read().await.contains(id) } diff --git a/src/main.rs b/src/main.rs index 9883404..2d5107d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,6 +66,10 @@ async fn main() -> Result<(), anyhow::Error> { let actors = ActorCache::new(db.clone()); let job_server = create_server(db.clone()); + actors + .re_seed(&state.listeners().await, &state.requests()) + .await?; + notify::spawn(state.clone(), actors.clone(), job_server.clone(), &config)?; if args.jobs_only() {