relay/src/jobs/instance.rs

131 lines
3.4 KiB
Rust
Raw Normal View History

use crate::{
config::UrlKind,
jobs::{cache_media::CacheMedia, JobState},
};
2020-09-07 21:51:02 +00:00
use activitystreams::url::Url;
use anyhow::Error;
2020-04-21 00:56:50 +00:00
use background_jobs::ActixJob;
use std::{future::Future, pin::Pin};
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct QueryInstance {
2021-02-10 04:05:06 +00:00
actor_id: Url,
}
impl QueryInstance {
2021-02-10 04:05:06 +00:00
pub fn new(actor_id: Url) -> Self {
2020-06-20 04:11:02 +00:00
QueryInstance {
2021-02-10 04:05:06 +00:00
actor_id: actor_id.into(),
2020-06-20 04:11:02 +00:00
}
}
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(());
}
2021-02-10 04:05:06 +00:00
let mut instance_uri = self.actor_id.clone();
2020-06-20 15:06:01 +00:00
instance_uri.set_fragment(None);
instance_uri.set_query(None);
instance_uri.set_path("api/v1/instance");
let instance = state
.requests
.fetch_json::<Instance>(instance_uri.as_str())
.await?;
let description = if instance.description.is_empty() {
instance.short_description.unwrap_or(String::new())
} 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? {
2020-06-20 04:11:02 +00:00
contact.avatar = state.config.generate_url(UrlKind::Media(uuid)).into();
uuid
} else {
2021-02-10 04:05:06 +00:00
let uuid = state.media.store_url(contact.avatar.clone()).await?;
2020-06-20 04:11:02 +00:00
contact.avatar = state.config.generate_url(UrlKind::Media(uuid)).into();
uuid
};
state.job_server.queue(CacheMedia::new(uuid))?;
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;
2020-03-30 15:45:44 +00:00
type Future = Pin<Box<dyn Future<Output = Result<(), 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 {
2020-03-30 15:45:44 +00:00
Box::pin(self.perform(state))
}
}
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,
2020-06-27 22:29:23 +00:00
url: Url,
avatar: Url,
}