2022-12-20 03:59:32 +01:00
|
|
|
use std::{sync::Arc, collections::HashSet, time::Instant};
|
|
|
|
use metrics::{increment_counter, histogram};
|
2022-12-19 21:20:13 +01:00
|
|
|
use serde::Deserialize;
|
2022-12-18 21:31:50 +01:00
|
|
|
use serde_json::json;
|
|
|
|
use sigh::PrivateKey;
|
|
|
|
use tokio::{
|
|
|
|
sync::mpsc::Receiver,
|
|
|
|
};
|
2022-12-19 21:20:13 +01:00
|
|
|
use crate::{db::Database, send, actor};
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Post<'a> {
|
2022-12-19 23:01:53 +01:00
|
|
|
pub url: Option<&'a str>,
|
2022-12-19 21:20:13 +01:00
|
|
|
pub uri: &'a str,
|
|
|
|
pub tags: Option<Vec<Tag<'a>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Post<'_> {
|
|
|
|
pub fn host(&self) -> Option<String> {
|
2022-12-19 23:01:53 +01:00
|
|
|
reqwest::Url::parse(self.url?)
|
2022-12-19 21:20:13 +01:00
|
|
|
.ok()
|
|
|
|
.and_then(|url| url.domain()
|
2022-12-20 00:15:00 +01:00
|
|
|
.map(str::to_lowercase)
|
2022-12-19 21:20:13 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tags(&self) -> Vec<String> {
|
|
|
|
match &self.tags {
|
|
|
|
None =>
|
|
|
|
vec![],
|
|
|
|
Some(tags) =>
|
|
|
|
tags.iter()
|
2023-03-03 02:19:48 +01:00
|
|
|
.map(|tag| tag.name.to_string())
|
2022-12-19 21:20:13 +01:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn relay_target_kinds(&self) -> impl Iterator<Item = actor::ActorKind> {
|
|
|
|
self.host()
|
|
|
|
.into_iter()
|
2022-12-20 00:15:00 +01:00
|
|
|
.map(actor::ActorKind::InstanceRelay)
|
2022-12-19 21:20:13 +01:00
|
|
|
.chain(
|
|
|
|
self.tags()
|
|
|
|
.into_iter()
|
2023-03-03 02:19:48 +01:00
|
|
|
.map(|ref s| actor::ActorKind::from_tag(s))
|
2022-12-19 21:20:13 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn relay_targets(&self, hostname: Arc<String>) -> impl Iterator<Item = actor::Actor> {
|
|
|
|
self.relay_target_kinds()
|
|
|
|
.map(move |kind| actor::Actor {
|
|
|
|
host: hostname.clone(),
|
|
|
|
kind,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Tag<'a> {
|
|
|
|
pub name: &'a str,
|
|
|
|
}
|
2022-12-18 21:31:50 +01:00
|
|
|
|
|
|
|
pub fn spawn(
|
|
|
|
client: Arc<reqwest::Client>,
|
2022-12-19 21:20:13 +01:00
|
|
|
hostname: Arc<String>,
|
|
|
|
database: Database,
|
2022-12-18 21:31:50 +01:00
|
|
|
private_key: PrivateKey,
|
2022-12-19 21:20:13 +01:00
|
|
|
mut stream_rx: Receiver<String>
|
2022-12-18 21:31:50 +01:00
|
|
|
) {
|
2022-12-20 00:04:56 +01:00
|
|
|
let private_key = Arc::new(private_key);
|
|
|
|
|
2022-12-18 21:31:50 +01:00
|
|
|
tokio::spawn(async move {
|
2022-12-19 21:20:13 +01:00
|
|
|
while let Some(data) = stream_rx.recv().await {
|
2022-12-20 03:59:32 +01:00
|
|
|
let t1 = Instant::now();
|
2022-12-19 21:20:13 +01:00
|
|
|
let post: Post = match serde_json::from_str(&data) {
|
|
|
|
Ok(post) => post,
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("parse error: {}", e);
|
|
|
|
tracing::trace!("data: {}", data);
|
|
|
|
continue;
|
|
|
|
}
|
2022-12-18 21:31:50 +01:00
|
|
|
};
|
2022-12-19 23:01:53 +01:00
|
|
|
let post_url = match post.url {
|
|
|
|
Some(url) => url,
|
|
|
|
// skip reposts
|
2022-12-20 03:13:44 +01:00
|
|
|
None => {
|
2022-12-20 04:10:45 +01:00
|
|
|
increment_counter!("relay_posts_total", "action" => "skip");
|
2022-12-20 03:13:44 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-12-19 23:01:53 +01:00
|
|
|
};
|
|
|
|
let mut seen_actors = HashSet::new();
|
|
|
|
let mut seen_inboxes = HashSet::new();
|
2022-12-19 21:20:13 +01:00
|
|
|
for actor in post.relay_targets(hostname.clone()) {
|
2022-12-19 23:01:53 +01:00
|
|
|
if seen_actors.contains(&actor) {
|
2022-12-19 21:20:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let actor_id = actor.uri();
|
|
|
|
let body = json!({
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"type": "Announce",
|
|
|
|
"actor": &actor_id,
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"object": &post.uri,
|
2022-12-19 23:01:53 +01:00
|
|
|
"id": &post_url,
|
2022-12-19 21:20:13 +01:00
|
|
|
});
|
|
|
|
let body = Arc::new(
|
|
|
|
serde_json::to_vec(&body)
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
for inbox in database.get_following_inboxes(&actor_id).await.unwrap() {
|
2022-12-19 23:01:53 +01:00
|
|
|
if seen_inboxes.contains(&inbox) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-12-20 00:04:56 +01:00
|
|
|
seen_inboxes.insert(inbox.clone());
|
|
|
|
let client_ = client.clone();
|
|
|
|
let body_ = body.clone();
|
|
|
|
let key_id = actor.key_id();
|
|
|
|
let private_key_ = private_key.clone();
|
2022-12-20 03:57:31 +01:00
|
|
|
tracing::debug!("relay {} from {} to {}", &post_url, actor_id, inbox);
|
2022-12-20 00:04:56 +01:00
|
|
|
tokio::spawn(async move {
|
|
|
|
if let Err(e) = send::send_raw(
|
|
|
|
&client_, &inbox,
|
|
|
|
&key_id, &private_key_, body_
|
|
|
|
).await {
|
|
|
|
tracing::error!("relay::send {:?}", e);
|
2022-12-20 00:19:21 +01:00
|
|
|
} else {
|
|
|
|
// success
|
|
|
|
systemd::daemon::notify(
|
|
|
|
false, [
|
|
|
|
(systemd::daemon::STATE_WATCHDOG, "1")
|
|
|
|
].iter()
|
|
|
|
).unwrap();
|
2022-12-20 00:04:56 +01:00
|
|
|
}
|
|
|
|
});
|
2022-12-19 21:20:13 +01:00
|
|
|
}
|
|
|
|
|
2022-12-19 23:01:53 +01:00
|
|
|
seen_actors.insert(actor);
|
2022-12-19 21:20:13 +01:00
|
|
|
}
|
2022-12-20 03:13:44 +01:00
|
|
|
if seen_inboxes.is_empty() {
|
2022-12-20 04:10:45 +01:00
|
|
|
increment_counter!("relay_posts_total", "action" => "no_relay");
|
2022-12-20 03:13:44 +01:00
|
|
|
} else {
|
2022-12-20 04:10:45 +01:00
|
|
|
increment_counter!("relay_posts_total", "action" => "relay");
|
2022-12-20 03:13:44 +01:00
|
|
|
}
|
2022-12-20 03:59:32 +01:00
|
|
|
let t2 = Instant::now();
|
2022-12-20 04:10:45 +01:00
|
|
|
histogram!("relay_post_duration", t2 - t1);
|
2022-12-18 21:31:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|