get it working (mostly)

This commit is contained in:
Felix 2020-05-07 19:07:33 +02:00 committed by Felix Ableitner
parent cac7011d53
commit ce0a37cdf1
3 changed files with 58 additions and 31 deletions

View File

@ -1,6 +1,11 @@
#!/bin/bash #!/bin/bash
set -e set -e
# already start rust build in the background
pushd ../../server/ || exit
cargo build &
popd || exit
if [ "$1" = "-yarn" ]; then if [ "$1" = "-yarn" ]; then
pushd ../../ui/ || exit pushd ../../ui/ || exit
yarn yarn
@ -8,6 +13,7 @@ if [ "$1" = "-yarn" ]; then
popd || exit popd || exit
fi fi
# wait for rust build to finish
pushd ../../server/ || exit pushd ../../server/ || exit
cargo build cargo build
popd || exit popd || exit

View File

@ -25,17 +25,14 @@ use crate::{
routes::DbPoolParam, routes::DbPoolParam,
Settings, Settings,
}; };
use activitystreams::{ use activitystreams::{activity::{Create, Delete, Dislike, Like, Remove, Undo, Update}, context, object::{kind::PageType, properties::ObjectProperties, AnyImage, Image, Page, Tombstone}, BaseBox, Activity, Base};
activity::{Create, Delete, Dislike, Like, Remove, Undo, Update},
context,
object::{kind::PageType, properties::ObjectProperties, AnyImage, Image, Page, Tombstone},
BaseBox,
};
use activitystreams_ext::Ext1; use activitystreams_ext::Ext1;
use actix_web::{body::Body, web::Path, HttpResponse, Result}; use actix_web::{body::Body, web::Path, HttpResponse, Result};
use diesel::PgConnection; use diesel::PgConnection;
use failure::Error; use failure::Error;
use serde::Deserialize; use serde::{Deserialize, Serialize};
use crate::apub::shared_inbox::do_announce;
use failure::_core::fmt::Debug;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct PostQuery { pub struct PostQuery {
@ -241,7 +238,7 @@ impl ApubObjectType for Post {
insert_activity(&conn, creator.id, &create, true)?; insert_activity(&conn, creator.id, &create, true)?;
send_activity(&create, creator, vec!(community.get_shared_inbox_url()))?; Post::send_post(creator, conn, &community, create)?;
Ok(()) Ok(())
} }
@ -264,7 +261,7 @@ impl ApubObjectType for Post {
insert_activity(&conn, creator.id, &update, true)?; insert_activity(&conn, creator.id, &update, true)?;
send_activity(&update, creator, vec!(community.get_shared_inbox_url()))?; Post::send_post(creator, conn, &community, update)?;
Ok(()) Ok(())
} }
@ -288,7 +285,8 @@ impl ApubObjectType for Post {
insert_activity(&conn, self.creator_id, &delete, true)?; insert_activity(&conn, self.creator_id, &delete, true)?;
let community = Community::read(conn, self.community_id)?; let community = Community::read(conn, self.community_id)?;
send_activity(&delete, creator, vec!(community.get_shared_inbox_url()))?;
Post::send_post(creator, conn, &community, delete)?;
Ok(()) Ok(())
} }
@ -328,7 +326,7 @@ impl ApubObjectType for Post {
insert_activity(&conn, self.creator_id, &undo, true)?; insert_activity(&conn, self.creator_id, &undo, true)?;
let community = Community::read(conn, self.community_id)?; let community = Community::read(conn, self.community_id)?;
send_activity(&undo, creator, vec!(community.get_shared_inbox_url()))?; Post::send_post(creator, conn, &community, undo)?;
Ok(()) Ok(())
} }
@ -352,7 +350,8 @@ impl ApubObjectType for Post {
insert_activity(&conn, mod_.id, &remove, true)?; insert_activity(&conn, mod_.id, &remove, true)?;
let community = Community::read(conn, self.community_id)?; let community = Community::read(conn, self.community_id)?;
send_activity(&remove, mod_, vec!(community.get_shared_inbox_url()))?;
Post::send_post(mod_, conn, &community, remove)?;
Ok(()) Ok(())
} }
fn send_undo_remove(&self, mod_: &User_, conn: &PgConnection) -> Result<(), Error> { fn send_undo_remove(&self, mod_: &User_, conn: &PgConnection) -> Result<(), Error> {
@ -390,11 +389,12 @@ impl ApubObjectType for Post {
insert_activity(&conn, mod_.id, &undo, true)?; insert_activity(&conn, mod_.id, &undo, true)?;
let community = Community::read(conn, self.community_id)?; let community = Community::read(conn, self.community_id)?;
send_activity(&undo, mod_, vec!(community.get_shared_inbox_url()))?; Post::send_post(mod_, conn, &community, undo)?;
Ok(()) Ok(())
} }
} }
impl ApubLikeableType for Post { impl ApubLikeableType for Post {
fn send_like(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error> { fn send_like(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
let page = self.to_apub(conn)?; let page = self.to_apub(conn)?;
@ -478,3 +478,23 @@ impl ApubLikeableType for Post {
Ok(()) Ok(())
} }
} }
impl Post {
fn send_post<A>(creator: &User_, conn: &PgConnection, community: &Community, activity: A) -> Result<(), Error>
where
A: Activity + Base + Serialize + Debug {
insert_activity(&conn, creator.id, &activity, true)?;
// if this is a local community, we need to do an announce from the community instead
if community.local {
do_announce(activity, &community.actor_id, &creator.actor_id, conn)?;
} else {
send_activity(
&activity,
creator,
vec![community.get_shared_inbox_url()],
)?;
}
Ok(())
}
}

View File

@ -37,7 +37,6 @@ use failure::{Error, _core::fmt::Debug};
use log::debug; use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::apub::fetcher::get_or_fetch_and_upsert_remote_community; use crate::apub::fetcher::get_or_fetch_and_upsert_remote_community;
use activitystreams_new::primitives::XsdAnyUri;
use crate::apub::activities::{populate_object_props, send_activity}; use crate::apub::activities::{populate_object_props, send_activity};
use crate::apub::ActorType; use crate::apub::ActorType;
use activitystreams::activity::Announce; use activitystreams::activity::Announce;
@ -68,7 +67,7 @@ impl SharedAcceptedObjects {
SharedAcceptedObjects::Announce(a) => a.announce_props.get_object_base_box(), SharedAcceptedObjects::Announce(a) => a.announce_props.get_object_base_box(),
} }
} }
fn sender(&self) -> XsdAnyUri { fn sender(&self) -> String {
let uri = match self { let uri = match self {
SharedAcceptedObjects::Create(c) => c.create_props.get_actor_xsd_any_uri(), SharedAcceptedObjects::Create(c) => c.create_props.get_actor_xsd_any_uri(),
SharedAcceptedObjects::Update(u) => u.update_props.get_actor_xsd_any_uri(), SharedAcceptedObjects::Update(u) => u.update_props.get_actor_xsd_any_uri(),
@ -79,9 +78,9 @@ impl SharedAcceptedObjects {
SharedAcceptedObjects::Remove(r) => r.remove_props.get_actor_xsd_any_uri(), SharedAcceptedObjects::Remove(r) => r.remove_props.get_actor_xsd_any_uri(),
SharedAcceptedObjects::Announce(a) => a.announce_props.get_actor_xsd_any_uri(), SharedAcceptedObjects::Announce(a) => a.announce_props.get_actor_xsd_any_uri(),
}; };
uri.unwrap().clone() uri.unwrap().clone().to_string()
} }
fn cc(&self) -> XsdAnyUri { fn cc(&self) -> String {
// TODO: there is probably an easier way to do this // TODO: there is probably an easier way to do this
let oprops = match self { let oprops = match self {
SharedAcceptedObjects::Create(c) => &c.object_props, SharedAcceptedObjects::Create(c) => &c.object_props,
@ -93,7 +92,7 @@ impl SharedAcceptedObjects {
SharedAcceptedObjects::Remove(r) => &r.object_props, SharedAcceptedObjects::Remove(r) => &r.object_props,
SharedAcceptedObjects::Announce(a) => &a.object_props, SharedAcceptedObjects::Announce(a) => &a.object_props,
}; };
oprops.get_cc_xsd_any_uri().unwrap().to_owned() oprops.get_cc_xsd_any_uri().unwrap().to_owned().to_string()
} }
} }
@ -111,10 +110,9 @@ pub async fn shared_inbox(
debug!("Shared inbox received activity: {}", json); debug!("Shared inbox received activity: {}", json);
let object = activity.object().cloned().unwrap(); let object = activity.object().cloned().unwrap();
let sender = activity.sender(); let sender = &activity.sender();
let cc = activity.cc(); let cc = &activity.cc();
// TODO: should make an enum Actor that contains user and community, and has methods like get_public_key()
match get_or_fetch_and_upsert_remote_user(&sender.to_string(), &conn) { match get_or_fetch_and_upsert_remote_user(&sender.to_string(), &conn) {
Ok(u) => verify(&request, &u), Ok(u) => verify(&request, &u),
Err(_) => { Err(_) => {
@ -127,7 +125,7 @@ pub async fn shared_inbox(
(SharedAcceptedObjects::Create(c), Some("Page")) => { (SharedAcceptedObjects::Create(c), Some("Page")) => {
// TODO: first check that it is addressed to a local community // TODO: first check that it is addressed to a local community
receive_create_post(&c, &conn, chat_server)?; receive_create_post(&c, &conn, chat_server)?;
do_announce(*c, &cc, &sender, conn) do_announce(*c, cc, sender, conn)
} }
(SharedAcceptedObjects::Update(u), Some("Page")) => { (SharedAcceptedObjects::Update(u), Some("Page")) => {
receive_update_post(&u, &conn, chat_server)?; receive_update_post(&u, &conn, chat_server)?;
@ -1499,22 +1497,22 @@ fn receive_undo_like_post(
Ok(HttpResponse::Ok().finish()) Ok(HttpResponse::Ok().finish())
} }
fn do_announce<A>( // TODO: move to community.rs
pub fn do_announce<A>(
activity: A, activity: A,
community_uri: &XsdAnyUri, community_uri: &str,
sender: &XsdAnyUri, sender: &str,
conn: &PgConnection, conn: &PgConnection,
) -> Result<HttpResponse, Error> ) -> Result<HttpResponse, Error>
where where
A: Activity + Base + Serialize, A: Activity + Base + Serialize,
{ {
// Note: signature check is done by receive_create_post() etc dbg!(&community_uri);
// TODO: this fails for some reason
let community = Community::read_from_actor_id(conn, &community_uri.to_string())?; let community = Community::read_from_actor_id(conn, &community_uri)?;
insert_activity(&conn, -1, &activity, false)?; insert_activity(&conn, -1, &activity, false)?;
// TODO: move the sending to community.rs
let mut announce = Announce::default(); let mut announce = Announce::default();
populate_object_props( populate_object_props(
&mut announce.object_props, &mut announce.object_props,
@ -1526,15 +1524,18 @@ where
.set_actor_xsd_any_uri(community.actor_id.to_owned())? .set_actor_xsd_any_uri(community.actor_id.to_owned())?
.set_object_base_box(BaseBox::from_concrete(activity)?)?; .set_object_base_box(BaseBox::from_concrete(activity)?)?;
insert_activity(&conn, -1, &announce, true)?; insert_activity(&conn, community.id, &announce, true)?;
// dont send to the instance where the activity originally came from, because that would result // dont send to the instance where the activity originally came from, because that would result
// in a database error (same data inserted twice) // in a database error (same data inserted twice)
let mut to = community.get_follower_inboxes(&conn)?; let mut to = community.get_follower_inboxes(&conn)?;
let sending_user = get_or_fetch_and_upsert_remote_user(&sender.to_string(), conn)?; let sending_user = get_or_fetch_and_upsert_remote_user(&sender, conn)?;
// this seems to be the "easiest" stable alternative for remove_item() // this seems to be the "easiest" stable alternative for remove_item()
to.retain(|x| *x != sending_user.get_shared_inbox_url()); to.retain(|x| *x != sending_user.get_shared_inbox_url());
dbg!(&announce);
dbg!(&to);
send_activity( send_activity(
&announce, &announce,
&community, &community,