mirror of
https://git.asonix.dog/asonix/relay.git
synced 2024-11-28 20:41:33 +00:00
Add cache-media job to keep icons up-to-date
This commit is contained in:
parent
ce9de20471
commit
a45c204a5b
3 changed files with 58 additions and 4 deletions
44
src/jobs/cache_media.rs
Normal file
44
src/jobs/cache_media.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
use crate::jobs::JobState;
|
||||
use anyhow::Error;
|
||||
use background_jobs::ActixJob;
|
||||
use std::{future::Future, pin::Pin};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub struct CacheMedia {
|
||||
uuid: Uuid,
|
||||
}
|
||||
|
||||
impl CacheMedia {
|
||||
pub fn new(uuid: Uuid) -> Self {
|
||||
CacheMedia { uuid }
|
||||
}
|
||||
|
||||
async fn perform(self, state: JobState) -> Result<(), Error> {
|
||||
if state.media.get_bytes(self.uuid).await.is_some() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(url) = state.media.get_url(self.uuid).await? {
|
||||
let (content_type, bytes) = state.requests.fetch_bytes(url.as_str()).await?;
|
||||
|
||||
state
|
||||
.media
|
||||
.store_bytes(self.uuid, content_type, bytes)
|
||||
.await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl ActixJob for CacheMedia {
|
||||
type State = JobState;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
|
||||
|
||||
const NAME: &'static str = "relay::jobs::CacheMedia";
|
||||
|
||||
fn run(self, state: Self::State) -> Self::Future {
|
||||
Box::pin(self.perform(state))
|
||||
}
|
||||
}
|
|
@ -1,4 +1,7 @@
|
|||
use crate::{config::UrlKind, jobs::JobState};
|
||||
use crate::{
|
||||
config::UrlKind,
|
||||
jobs::{cache_media::CacheMedia, JobState},
|
||||
};
|
||||
use activitystreams_new::primitives::XsdAnyUri;
|
||||
use anyhow::Error;
|
||||
use background_jobs::ActixJob;
|
||||
|
@ -44,12 +47,16 @@ impl QueryInstance {
|
|||
};
|
||||
|
||||
if let Some(mut contact) = instance.contact {
|
||||
if let Some(uuid) = state.media.get_uuid(&contact.avatar).await? {
|
||||
let uuid = if let Some(uuid) = state.media.get_uuid(&contact.avatar).await? {
|
||||
contact.avatar = state.config.generate_url(UrlKind::Media(uuid)).parse()?;
|
||||
uuid
|
||||
} else {
|
||||
let uuid = state.media.store_url(&contact.avatar).await?;
|
||||
contact.avatar = state.config.generate_url(UrlKind::Media(uuid)).parse()?;
|
||||
}
|
||||
uuid
|
||||
};
|
||||
|
||||
state.job_server.queue(CacheMedia::new(uuid))?;
|
||||
|
||||
state
|
||||
.node_cache
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
pub mod apub;
|
||||
mod cache_media;
|
||||
mod deliver;
|
||||
mod deliver_many;
|
||||
mod instance;
|
||||
|
@ -7,7 +8,8 @@ mod process_listeners;
|
|||
mod storage;
|
||||
|
||||
pub use self::{
|
||||
deliver::Deliver, deliver_many::DeliverMany, instance::QueryInstance, nodeinfo::QueryNodeinfo,
|
||||
cache_media::CacheMedia, deliver::Deliver, deliver_many::DeliverMany, instance::QueryInstance,
|
||||
nodeinfo::QueryNodeinfo,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -54,6 +56,7 @@ pub fn create_workers(
|
|||
.register::<QueryNodeinfo>()
|
||||
.register::<QueryInstance>()
|
||||
.register::<Listeners>()
|
||||
.register::<CacheMedia>()
|
||||
.register::<apub::Announce>()
|
||||
.register::<apub::Follow>()
|
||||
.register::<apub::Forward>()
|
||||
|
|
Loading…
Reference in a new issue