relay/src/jobs/instance.rs

140 lines
3.8 KiB
Rust
Raw Normal View History

use crate::{
config::UrlKind,
2022-01-17 22:54:45 +00:00
error::{Error, ErrorKind},
jobs::{cache_media::CacheMedia, JobState},
};
2022-01-17 22:54:45 +00:00
use activitystreams::{iri, iri_string::types::IriString};
2020-04-21 00:56:50 +00:00
use background_jobs::ActixJob;
use std::{future::Future, pin::Pin};
2021-09-21 19:32:25 +00:00
#[derive(Clone, serde::Deserialize, serde::Serialize)]
2021-02-10 04:17:20 +00:00
pub(crate) struct QueryInstance {
2022-01-17 22:54:45 +00:00
actor_id: IriString,
}
2021-09-21 19:32:25 +00:00
impl std::fmt::Debug for QueryInstance {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("QueryInstance")
.field("actor_id", &self.actor_id.to_string())
.finish()
}
}
impl QueryInstance {
2022-01-17 22:54:45 +00:00
pub(crate) fn new(actor_id: IriString) -> Self {
QueryInstance { actor_id }
}
2022-11-01 20:57:33 +00:00
#[tracing::instrument(name = "Query instance", skip(state))]
2020-06-20 15:06:01 +00:00
async fn perform(self, state: JobState) -> Result<(), Error> {
2021-02-10 04:05:06 +00:00
let contact_outdated = state
.node_cache
.is_contact_outdated(self.actor_id.clone())
.await;
let instance_outdated = state
.node_cache
.is_instance_outdated(self.actor_id.clone())
.await;
2021-02-10 04:05:06 +00:00
if !(contact_outdated || instance_outdated) {
return Ok(());
}
2022-01-17 22:54:45 +00:00
let authority = self
.actor_id
.authority_str()
.ok_or(ErrorKind::MissingDomain)?;
let scheme = self.actor_id.scheme_str();
let instance_uri = iri!(format!("{}://{}/api/v1/instance", scheme, authority));
let instance = state
.requests
.fetch_json::<Instance>(instance_uri.as_str())
.await?;
let description = if instance.description.is_empty() {
2021-11-23 22:19:59 +00:00
instance.short_description.unwrap_or_default()
} else {
instance.description
};
if let Some(mut contact) = instance.contact {
2021-02-10 04:05:06 +00:00
let uuid = if let Some(uuid) = state.media.get_uuid(contact.avatar.clone()).await? {
2021-11-23 22:19:59 +00:00
contact.avatar = state.config.generate_url(UrlKind::Media(uuid));
uuid
} else {
2021-02-10 04:05:06 +00:00
let uuid = state.media.store_url(contact.avatar.clone()).await?;
2021-11-23 22:19:59 +00:00
contact.avatar = state.config.generate_url(UrlKind::Media(uuid));
uuid
};
2021-10-11 19:19:32 +00:00
state.job_server.queue(CacheMedia::new(uuid)).await?;
state
.node_cache
.set_contact(
2021-02-10 04:05:06 +00:00
self.actor_id.clone(),
contact.username,
contact.display_name,
2020-06-27 22:29:23 +00:00
contact.url,
contact.avatar,
)
.await?;
}
let description = ammonia::clean(&description);
state
.node_cache
.set_instance(
2021-02-10 04:05:06 +00:00
self.actor_id.clone(),
instance.title,
description,
instance.version,
instance.registrations,
instance.approval_required,
)
.await?;
Ok(())
}
}
2020-03-30 15:45:44 +00:00
impl ActixJob for QueryInstance {
type State = JobState;
2021-09-18 17:55:39 +00:00
type Future = Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>>;
2020-04-21 01:03:46 +00:00
const NAME: &'static str = "relay::jobs::QueryInstance";
2020-04-21 00:56:50 +00:00
fn run(self, state: Self::State) -> Self::Future {
2021-09-18 17:55:39 +00:00
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
}
}
2020-07-10 23:18:05 +00:00
fn default_approval() -> bool {
false
}
#[derive(serde::Deserialize)]
struct Instance {
title: String,
short_description: Option<String>,
description: String,
version: String,
registrations: bool,
2020-07-10 23:18:05 +00:00
#[serde(default = "default_approval")]
approval_required: bool,
2020-03-25 22:44:29 +00:00
#[serde(rename = "contact_account")]
contact: Option<Contact>,
}
#[derive(serde::Deserialize)]
struct Contact {
username: String,
display_name: String,
2022-01-17 22:54:45 +00:00
url: IriString,
avatar: IriString,
}