2020-04-15 18:12:25 +00:00
|
|
|
use crate::apub::activities::accept_follow;
|
|
|
|
use crate::apub::fetcher::fetch_remote_user;
|
2020-04-19 17:35:40 +00:00
|
|
|
use crate::apub::signatures::verify;
|
2020-04-15 18:12:25 +00:00
|
|
|
use crate::db::community::{Community, CommunityFollower, CommunityFollowerForm};
|
|
|
|
use crate::db::Followable;
|
|
|
|
use activitystreams::activity::Follow;
|
2020-04-19 17:35:40 +00:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
2020-04-15 18:12:25 +00:00
|
|
|
use diesel::r2d2::{ConnectionManager, Pool};
|
|
|
|
use diesel::PgConnection;
|
|
|
|
use failure::Error;
|
2020-04-17 14:55:28 +00:00
|
|
|
use log::debug;
|
|
|
|
use serde::Deserialize;
|
2020-04-15 18:12:25 +00:00
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[serde(untagged)]
|
2020-04-17 14:55:28 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
2020-04-15 18:12:25 +00:00
|
|
|
pub enum CommunityAcceptedObjects {
|
|
|
|
Follow(Follow),
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Handler for all incoming activities to community inboxes.
|
2020-04-15 18:12:25 +00:00
|
|
|
pub async fn community_inbox(
|
2020-04-19 17:35:40 +00:00
|
|
|
request: HttpRequest,
|
2020-04-15 18:12:25 +00:00
|
|
|
input: web::Json<CommunityAcceptedObjects>,
|
2020-04-17 17:34:18 +00:00
|
|
|
path: web::Path<String>,
|
2020-04-15 18:12:25 +00:00
|
|
|
db: web::Data<Pool<ConnectionManager<PgConnection>>>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let input = input.into_inner();
|
|
|
|
let conn = &db.get().unwrap();
|
2020-04-17 14:55:28 +00:00
|
|
|
debug!(
|
|
|
|
"Community {} received activity {:?}",
|
2020-04-17 17:34:18 +00:00
|
|
|
&path.into_inner(),
|
|
|
|
&input
|
2020-04-17 14:55:28 +00:00
|
|
|
);
|
2020-04-15 18:12:25 +00:00
|
|
|
match input {
|
2020-04-19 17:35:40 +00:00
|
|
|
CommunityAcceptedObjects::Follow(f) => handle_follow(&f, &request, conn),
|
2020-04-15 18:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Handle a follow request from a remote user, adding it to the local database and returning an
|
|
|
|
/// Accept activity.
|
2020-04-19 17:35:40 +00:00
|
|
|
fn handle_follow(
|
|
|
|
follow: &Follow,
|
|
|
|
request: &HttpRequest,
|
|
|
|
conn: &PgConnection,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let user_uri = follow
|
|
|
|
.follow_props
|
|
|
|
.get_actor_xsd_any_uri()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
|
|
|
let user = fetch_remote_user(&Url::parse(&user_uri)?, conn)?;
|
|
|
|
verify(&request, &user.public_key.unwrap())?;
|
|
|
|
|
2020-04-15 18:12:25 +00:00
|
|
|
// TODO: make sure this is a local community
|
|
|
|
let community_uri = follow
|
|
|
|
.follow_props
|
|
|
|
.get_object_xsd_any_uri()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
|
|
|
let community = Community::read_from_actor_id(conn, &community_uri)?;
|
|
|
|
let community_follower_form = CommunityFollowerForm {
|
|
|
|
community_id: community.id,
|
|
|
|
user_id: user.id,
|
|
|
|
};
|
|
|
|
CommunityFollower::follow(&conn, &community_follower_form)?;
|
2020-04-18 18:54:20 +00:00
|
|
|
accept_follow(&follow, conn)?;
|
2020-04-15 18:12:25 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|