1 use crate::apub::activities::accept_follow;
2 use crate::apub::fetcher::fetch_remote_user;
3 use crate::db::community::{Community, CommunityFollower, CommunityFollowerForm};
4 use crate::db::post::{Post, PostForm};
6 use crate::db::Followable;
7 use activitystreams::activity::{Accept, Create, Follow, Update};
8 use activitystreams::object::Page;
9 use actix_web::{web, HttpResponse};
10 use diesel::r2d2::{ConnectionManager, Pool};
11 use diesel::PgConnection;
15 // TODO: need a proper actor that has this inbox
18 input: web::Json<AcceptedObjects>,
19 db: web::Data<Pool<ConnectionManager<PgConnection>>>,
20 ) -> Result<HttpResponse, Error> {
21 // TODO: make sure that things are received in the correct inbox
22 // (by using seperate handler functions and checking the user/community name in the path)
23 let input = input.into_inner();
24 let conn = &db.get().unwrap();
26 AcceptedObjects::Create(c) => handle_create(&c, conn),
27 AcceptedObjects::Update(u) => handle_update(&u, conn),
28 AcceptedObjects::Follow(f) => handle_follow(&f, conn),
29 AcceptedObjects::Accept(a) => handle_accept(&a, conn),
33 fn handle_create(create: &Create, conn: &PgConnection) -> Result<HttpResponse, Error> {
36 .get_object_base_box()
40 .to_concrete::<Page>()?;
41 let post = PostForm::from_page(&page, conn)?;
42 Post::create(conn, &post)?;
43 // TODO: send the new post out via websocket
44 Ok(HttpResponse::Ok().finish())
47 fn handle_update(update: &Update, conn: &PgConnection) -> Result<HttpResponse, Error> {
50 .get_object_base_box()
54 .to_concrete::<Page>()?;
55 let post = PostForm::from_page(&page, conn)?;
56 let id = Post::read_from_apub_id(conn, &post.ap_id)?.id;
57 Post::update(conn, id, &post)?;
58 // TODO: send the new post out via websocket
59 Ok(HttpResponse::Ok().finish())
62 fn handle_follow(follow: &Follow, conn: &PgConnection) -> Result<HttpResponse, Error> {
63 println!("received follow: {:?}", &follow);
65 // TODO: make sure this is a local community
66 let community_uri = follow
68 .get_object_xsd_any_uri()
71 let community = Community::read_from_actor_id(conn, &community_uri)?;
74 .get_actor_xsd_any_uri()
77 let user = fetch_remote_user(&Url::parse(&user_uri)?, conn)?;
78 // TODO: insert ID of the user into follows of the community
79 let community_follower_form = CommunityFollowerForm {
80 community_id: community.id,
83 CommunityFollower::follow(&conn, &community_follower_form)?;
84 accept_follow(&follow)?;
85 Ok(HttpResponse::Ok().finish())
88 fn handle_accept(accept: &Accept, _conn: &PgConnection) -> Result<HttpResponse, Error> {
89 println!("received accept: {:?}", &accept);
90 // TODO: at this point, indicate to the user that they are following the community
91 Ok(HttpResponse::Ok().finish())
95 #[derive(serde::Deserialize)]
96 pub enum AcceptedObjects {