2020-05-21 23:04:12 +00:00
|
|
|
use crate::{
|
|
|
|
config::UrlKind,
|
2021-09-18 17:55:39 +00:00
|
|
|
error::Error,
|
2020-05-21 23:04:12 +00:00
|
|
|
jobs::{cache_media::CacheMedia, JobState},
|
|
|
|
};
|
2020-09-07 21:51:02 +00:00
|
|
|
use activitystreams::url::Url;
|
2020-04-21 00:56:50 +00:00
|
|
|
use background_jobs::ActixJob;
|
2020-03-23 03:52:42 +00:00
|
|
|
use std::{future::Future, pin::Pin};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) struct QueryInstance {
|
2021-02-10 04:05:06 +00:00
|
|
|
actor_id: Url,
|
2020-03-23 03:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl QueryInstance {
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn new(actor_id: Url) -> Self {
|
2021-02-11 19:41:03 +00:00
|
|
|
QueryInstance { actor_id }
|
2020-03-23 03:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 17:55:39 +00:00
|
|
|
#[tracing::instrument(name = "Query instance")]
|
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;
|
2020-03-25 22:10:10 +00:00
|
|
|
|
2021-02-10 04:05:06 +00:00
|
|
|
if !(contact_outdated || instance_outdated) {
|
2020-03-25 22:10:10 +00:00
|
|
|
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");
|
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 {
|
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();
|
2020-05-21 23:04:12 +00:00
|
|
|
uuid
|
2020-03-26 03:26:45 +00:00
|
|
|
} 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();
|
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(
|
2021-02-10 04:05:06 +00:00
|
|
|
self.actor_id.clone(),
|
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(
|
2021-02-10 04:05:06 +00:00
|
|
|
self.actor_id.clone(),
|
2020-03-23 03:52:42 +00:00
|
|
|
instance.title,
|
|
|
|
description,
|
|
|
|
instance.version,
|
|
|
|
instance.registrations,
|
2020-07-10 23:42:48 +00:00
|
|
|
instance.approval_required,
|
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;
|
2021-09-18 17:55:39 +00:00
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<(), anyhow::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 {
|
2021-09-18 17:55:39 +00:00
|
|
|
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
|
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-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
|
|
|
}
|