lemmy/server/src/apub/activities.rs

45 lines
1.6 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-09 19:04:31 +00:00
use activitystreams::activity::Create;
use activitystreams::context;
use diesel::PgConnection;
use failure::Error;
use isahc::prelude::*;
2020-04-09 19:26:22 +00:00
pub fn post_create(post: &Post, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
2020-04-09 19:04:31 +00:00
let page = post.as_page(conn)?;
2020-04-10 11:26:06 +00:00
let community = Community::read(conn, post.community_id)?;
2020-04-09 19:04:31 +00:00
let mut create = Create::new();
create.object_props.set_context_xsd_any_uri(context())?;
create
.object_props
2020-04-10 11:26:06 +00:00
// TODO: seems like the create activity needs its own id (and be fetchable there)
.set_id(page.object_props.get_id().unwrap().to_string())?
// 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)
.set_to_xsd_any_uri("https://www.w3.org/ns/activitystreams#Public")?
.set_cc_xsd_any_uri(format!("{}/followers", community.actor_id))?;
2020-04-09 19:26:22 +00:00
create
.create_props
.set_actor_xsd_any_uri(creator.actor_id.to_owned())?;
2020-04-09 19:04:31 +00:00
create.create_props.set_object_base_box(page)?;
let json = serde_json::to_string(&create)?;
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(())
}