2020-05-21 23:04:12 +00:00
|
|
|
use crate::{
|
|
|
|
config::UrlKind,
|
|
|
|
jobs::{cache_media::CacheMedia, JobState},
|
|
|
|
};
|
2020-06-27 22:29:23 +00:00
|
|
|
use activitystreams_new::url::Url;
|
2020-03-23 03:52:42 +00:00
|
|
|
use anyhow::Error;
|
2020-04-21 00:56:50 +00:00
|
|
|
use background_jobs::ActixJob;
|
2020-03-25 22:10:10 +00:00
|
|
|
use futures::join;
|
2020-03-23 03:52:42 +00:00
|
|
|
use std::{future::Future, pin::Pin};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
|
|
|
pub struct QueryInstance {
|
2020-06-27 22:29:23 +00:00
|
|
|
listener: Url,
|
2020-03-23 03:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl QueryInstance {
|
2020-06-20 04:11:02 +00:00
|
|
|
pub fn new(listener: Url) -> Self {
|
|
|
|
QueryInstance {
|
|
|
|
listener: listener.into(),
|
|
|
|
}
|
2020-03-23 03:52:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 15:06:01 +00:00
|
|
|
async fn perform(self, state: JobState) -> Result<(), Error> {
|
2020-03-25 22:10:10 +00:00
|
|
|
let (o1, o2) = join!(
|
2020-06-20 04:11:02 +00:00
|
|
|
state.node_cache.is_contact_outdated(&self.listener),
|
|
|
|
state.node_cache.is_instance_outdated(&self.listener),
|
2020-03-25 22:10:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if !(o1 || o2) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-06-20 15:06:01 +00:00
|
|
|
let mut instance_uri = self.listener.clone();
|
|
|
|
instance_uri.set_fragment(None);
|
|
|
|
instance_uri.set_query(None);
|
|
|
|
instance_uri.set_path("api/v1/instance");
|
2020-03-23 03:52:42 +00:00
|
|
|
|
|
|
|
let instance = state
|
|
|
|
.requests
|
2020-07-10 22:24:47 +00:00
|
|
|
.fetch_json::<Instance>(instance_uri.as_str())
|
2020-03-23 03:52:42 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
let description = if instance.description.is_empty() {
|
2020-07-10 22:47:41 +00:00
|
|
|
instance.short_description.unwrap_or(String::new())
|
2020-03-23 03:52:42 +00:00
|
|
|
} else {
|
|
|
|
instance.description
|
|
|
|
};
|
|
|
|
|
2020-03-26 03:26:45 +00:00
|
|
|
if let Some(mut contact) = instance.contact {
|
2020-05-21 23:04:12 +00:00
|
|
|
let uuid = if let Some(uuid) = state.media.get_uuid(&contact.avatar).await? {
|
2020-06-20 04:11:02 +00:00
|
|
|
contact.avatar = state.config.generate_url(UrlKind::Media(uuid)).into();
|
2020-05-21 23:04:12 +00:00
|
|
|
uuid
|
2020-03-26 03:26:45 +00:00
|
|
|
} else {
|
2020-03-26 18:21:05 +00:00
|
|
|
let uuid = state.media.store_url(&contact.avatar).await?;
|
2020-06-20 04:11:02 +00:00
|
|
|
contact.avatar = state.config.generate_url(UrlKind::Media(uuid)).into();
|
2020-05-21 23:04:12 +00:00
|
|
|
uuid
|
|
|
|
};
|
|
|
|
|
|
|
|
state.job_server.queue(CacheMedia::new(uuid))?;
|
2020-03-26 03:26:45 +00:00
|
|
|
|
2020-03-23 03:52:42 +00:00
|
|
|
state
|
|
|
|
.node_cache
|
|
|
|
.set_contact(
|
2020-06-20 04:11:02 +00:00
|
|
|
&self.listener,
|
2020-03-23 03:52:42 +00:00
|
|
|
contact.username,
|
|
|
|
contact.display_name,
|
2020-06-27 22:29:23 +00:00
|
|
|
contact.url,
|
|
|
|
contact.avatar,
|
2020-03-23 03:52:42 +00:00
|
|
|
)
|
2020-03-25 22:10:10 +00:00
|
|
|
.await?;
|
2020-03-23 03:52:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 03:26:45 +00:00
|
|
|
let description = ammonia::clean(&description);
|
|
|
|
|
2020-03-23 03:52:42 +00:00
|
|
|
state
|
|
|
|
.node_cache
|
|
|
|
.set_instance(
|
2020-06-20 04:11:02 +00:00
|
|
|
&self.listener,
|
2020-03-23 03:52:42 +00:00
|
|
|
instance.title,
|
|
|
|
description,
|
|
|
|
instance.version,
|
|
|
|
instance.registrations,
|
2020-07-10 23:34:24 +00:00
|
|
|
instance.approval_required || instance.registrations,
|
2020-03-23 03:52:42 +00:00
|
|
|
)
|
2020-03-25 22:10:10 +00:00
|
|
|
.await?;
|
2020-03-23 03:52:42 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:45:44 +00:00
|
|
|
impl ActixJob for QueryInstance {
|
2020-03-23 03:52:42 +00:00
|
|
|
type State = JobState;
|
2020-03-30 15:45:44 +00:00
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
|
2020-03-23 03:52:42 +00:00
|
|
|
|
2020-04-21 01:03:46 +00:00
|
|
|
const NAME: &'static str = "relay::jobs::QueryInstance";
|
2020-04-21 00:56:50 +00:00
|
|
|
|
2020-03-23 03:52:42 +00:00
|
|
|
fn run(self, state: Self::State) -> Self::Future {
|
2020-03-30 15:45:44 +00:00
|
|
|
Box::pin(self.perform(state))
|
2020-03-23 03:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 23:18:05 +00:00
|
|
|
fn default_approval() -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-03-23 03:52:42 +00:00
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
struct Instance {
|
|
|
|
title: String,
|
2020-07-10 22:47:41 +00:00
|
|
|
short_description: Option<String>,
|
2020-03-23 03:52:42 +00:00
|
|
|
description: String,
|
|
|
|
version: String,
|
|
|
|
registrations: bool,
|
2020-07-10 23:18:05 +00:00
|
|
|
|
|
|
|
#[serde(default = "default_approval")]
|
2020-03-23 03:52:42 +00:00
|
|
|
approval_required: bool,
|
|
|
|
|
2020-07-10 23:34:24 +00:00
|
|
|
#[serde(default = "default_approval")]
|
|
|
|
registrations: bool,
|
|
|
|
|
2020-03-25 22:44:29 +00:00
|
|
|
#[serde(rename = "contact_account")]
|
2020-03-23 03:52:42 +00:00
|
|
|
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,
|
2020-03-23 03:52:42 +00:00
|
|
|
}
|