Use shared inbox when available
But it is not yet stored in the database, so it means never
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
use diesel::PgConnection;
|
||||
use reqwest::Client;
|
||||
use serde_json;
|
||||
|
||||
use BASE_URL;
|
||||
use activity_pub::{activity_pub, ActivityPub, context, ap_url};
|
||||
use activity_pub::activity::Activity;
|
||||
use activity_pub::request;
|
||||
use activity_pub::sign::*;
|
||||
use models::instance::Instance;
|
||||
|
||||
pub enum ActorType {
|
||||
@@ -38,6 +34,8 @@ pub trait Actor: Sized {
|
||||
|
||||
fn get_inbox_url(&self) -> String;
|
||||
|
||||
fn get_shared_inbox_url(&self) -> Option<String>;
|
||||
|
||||
fn custom_props(&self, _conn: &PgConnection) -> serde_json::Map<String, serde_json::Value> {
|
||||
serde_json::Map::new()
|
||||
}
|
||||
@@ -84,23 +82,5 @@ pub trait Actor: Sized {
|
||||
))
|
||||
}
|
||||
|
||||
fn send_to_inbox<A: Activity, S: Actor + Signer>(&self, conn: &PgConnection, sender: &S, act: A) {
|
||||
let mut act = act.serialize();
|
||||
act["@context"] = context();
|
||||
let signed = act.sign(sender, conn);
|
||||
|
||||
let res = Client::new()
|
||||
.post(&self.get_inbox_url()[..])
|
||||
.headers(request::headers())
|
||||
.header(request::signature(sender, request::headers(), conn))
|
||||
.header(request::digest(signed.to_string()))
|
||||
.body(signed.to_string())
|
||||
.send();
|
||||
match res {
|
||||
Ok(mut r) => println!("Successfully sent activity to inbox ({})\n\n{:?}", self.get_inbox_url(), r.text().unwrap()),
|
||||
Err(e) => println!("Error while sending to inbox ({:?})", e)
|
||||
}
|
||||
}
|
||||
|
||||
fn from_url(conn: &PgConnection, url: String) -> Option<Self>;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ use serde_json;
|
||||
|
||||
use activity_pub::activity;
|
||||
use activity_pub::actor::Actor;
|
||||
use activity_pub::outbox::broadcast;
|
||||
use activity_pub::sign::*;
|
||||
use models::blogs::Blog;
|
||||
use models::comments::*;
|
||||
@@ -79,7 +80,7 @@ pub trait Inbox: Actor + Sized {
|
||||
}
|
||||
}
|
||||
|
||||
fn accept_follow<A: Actor, B: Actor + Signer, T: activity::Activity>(
|
||||
fn accept_follow<A: Actor + Signer, B: Actor + Clone, T: activity::Activity>(
|
||||
&self,
|
||||
conn: &PgConnection,
|
||||
from: &A,
|
||||
@@ -94,6 +95,6 @@ pub trait Inbox: Actor + Sized {
|
||||
});
|
||||
|
||||
let accept = activity::Accept::new(target, follow, conn);
|
||||
from.send_to_inbox(conn, target, accept)
|
||||
broadcast(conn, from, accept, vec![target.clone()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use array_tool::vec::Uniq;
|
||||
use diesel::PgConnection;
|
||||
use reqwest::Client;
|
||||
use rocket::http::Status;
|
||||
use rocket::response::{Response, Responder};
|
||||
use rocket::request::Request;
|
||||
@@ -8,8 +10,8 @@ use std::sync::Arc;
|
||||
use activity_pub::{activity_pub, ActivityPub, context};
|
||||
use activity_pub::activity::Activity;
|
||||
use activity_pub::actor::Actor;
|
||||
use activity_pub::sign::Signer;
|
||||
use models::users::User;
|
||||
use activity_pub::request;
|
||||
use activity_pub::sign::*;
|
||||
|
||||
pub struct Outbox {
|
||||
id: String,
|
||||
@@ -42,8 +44,28 @@ impl<'r> Responder<'r> for Outbox {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn broadcast<A: Activity + Clone, S: Actor + Signer>(conn: &PgConnection, sender: &S, act: A, to: Vec<User>) {
|
||||
for user in to {
|
||||
user.send_to_inbox(conn, sender, act.clone()); // TODO: run it in Sidekiq or something like that
|
||||
pub fn broadcast<A: Activity + Clone, S: Actor + Signer, T: Actor>(conn: &PgConnection, sender: &S, act: A, to: Vec<T>) {
|
||||
let boxes = to.into_iter()
|
||||
.map(|u| u.get_shared_inbox_url().unwrap_or(u.get_inbox_url()))
|
||||
.collect::<Vec<String>>()
|
||||
.unique();
|
||||
for inbox in boxes {
|
||||
// TODO: run it in Sidekiq or something like that
|
||||
|
||||
let mut act = act.serialize();
|
||||
act["@context"] = context();
|
||||
let signed = act.sign(sender, conn);
|
||||
|
||||
let res = Client::new()
|
||||
.post(&inbox[..])
|
||||
.headers(request::headers())
|
||||
.header(request::signature(sender, request::headers(), conn))
|
||||
.header(request::digest(signed.to_string()))
|
||||
.body(signed.to_string())
|
||||
.send();
|
||||
match res {
|
||||
Ok(mut r) => println!("Successfully sent activity to inbox ({})\n\n{:?}", inbox, r.text().unwrap()),
|
||||
Err(e) => println!("Error while sending to inbox ({:?})", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user