lemmy/server/src/apub/activities.rs

85 lines
2.4 KiB
Rust
Raw Normal View History

2020-04-09 19:04:31 +00:00
use crate::apub::{get_apub_protocol_string, get_following_instances};
2020-04-10 11:26:06 +00:00
use crate::db::community::Community;
2020-04-09 19:04:31 +00:00
use crate::db::post::Post;
use crate::db::user::User_;
2020-04-10 11:26:06 +00:00
use crate::db::Crud;
2020-04-13 13:06:41 +00:00
use activitystreams::activity::{Create, Update};
use activitystreams::object::properties::ObjectProperties;
use activitystreams::{context, public};
2020-04-09 19:04:31 +00:00
use diesel::PgConnection;
use failure::Error;
2020-04-13 13:06:41 +00:00
use failure::_core::fmt::Debug;
2020-04-09 19:04:31 +00:00
use isahc::prelude::*;
2020-04-13 13:06:41 +00:00
use serde::Serialize;
2020-04-09 19:04:31 +00:00
2020-04-13 13:06:41 +00:00
fn populate_object_props(
props: &mut ObjectProperties,
addressed_to: &str,
object_id: &str,
) -> Result<(), Error> {
props
.set_context_xsd_any_uri(context())?
// TODO: the activity needs a seperate id from the object
.set_id(object_id)?
2020-04-10 11:26:06 +00:00
// TODO: should to/cc go on the Create, or on the Post? or on both?
// TODO: handle privacy on the receiving side (at least ignore anything thats not public)
2020-04-13 13:06:41 +00:00
.set_to_xsd_any_uri(public())?
.set_cc_xsd_any_uri(addressed_to)?;
Ok(())
}
fn send_activity<A>(activity: &A) -> Result<(), Error>
where
A: Serialize + Debug,
{
let json = serde_json::to_string(&activity)?;
2020-04-09 19:04:31 +00:00
for i in get_following_instances() {
2020-04-10 11:26:06 +00:00
// TODO: need to send this to the inbox of following users
2020-04-09 19:04:31 +00:00
let inbox = format!(
"{}://{}/federation/inbox",
get_apub_protocol_string(),
i.domain
);
let res = Request::post(inbox)
.header("Content-Type", "application/json")
.body(json.to_owned())?
.send()?;
dbg!(res);
}
Ok(())
}
2020-04-13 13:06:41 +00:00
pub fn post_create(post: &Post, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
let page = post.as_page(conn)?;
let community = Community::read(conn, post.community_id)?;
let mut create = Create::new();
populate_object_props(
&mut create.object_props,
&community.get_followers_url(),
&post.ap_id,
)?;
create
.create_props
.set_actor_xsd_any_uri(creator.actor_id.to_owned())?
.set_object_base_box(page)?;
send_activity(&create)?;
Ok(())
}
pub fn post_update(post: &Post, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
let page = post.as_page(conn)?;
let community = Community::read(conn, post.community_id)?;
let mut update = Update::new();
populate_object_props(
&mut update.object_props,
&community.get_followers_url(),
&post.ap_id,
)?;
update
.update_props
.set_actor_xsd_any_uri(creator.actor_id.to_owned())?
.set_object_base_box(page)?;
send_activity(&update)?;
Ok(())
}