2020-04-24 14:04:36 +00:00
|
|
|
use super::*;
|
2020-04-15 18:12:25 +00:00
|
|
|
|
|
|
|
#[serde(untagged)]
|
2020-04-17 14:55:28 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
2020-04-15 18:12:25 +00:00
|
|
|
pub enum UserAcceptedObjects {
|
|
|
|
Create(Create),
|
|
|
|
Update(Update),
|
|
|
|
Accept(Accept),
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Handler for all incoming activities to user inboxes.
|
2020-04-15 18:12:25 +00:00
|
|
|
pub async fn user_inbox(
|
2020-04-19 17:35:40 +00:00
|
|
|
request: HttpRequest,
|
2020-04-15 18:12:25 +00:00
|
|
|
input: web::Json<UserAcceptedObjects>,
|
2020-04-17 17:34:18 +00:00
|
|
|
path: web::Path<String>,
|
2020-04-24 14:04:36 +00:00
|
|
|
db: DbPoolParam,
|
2020-04-24 19:55:54 +00:00
|
|
|
_chat_server: ChatServerParam,
|
2020-04-15 18:12:25 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2020-04-19 17:35:40 +00:00
|
|
|
// TODO: would be nice if we could do the signature check here, but we cant access the actor property
|
2020-04-15 18:12:25 +00:00
|
|
|
let input = input.into_inner();
|
|
|
|
let conn = &db.get().unwrap();
|
2020-04-24 14:04:36 +00:00
|
|
|
let username = path.into_inner();
|
2020-04-24 19:55:54 +00:00
|
|
|
debug!("User {} received activity: {:?}", &username, &input);
|
2020-04-17 14:55:28 +00:00
|
|
|
|
2020-04-15 18:12:25 +00:00
|
|
|
match input {
|
2020-04-24 14:04:36 +00:00
|
|
|
UserAcceptedObjects::Create(c) => handle_create(&c, &request, &username, &conn),
|
|
|
|
UserAcceptedObjects::Update(u) => handle_update(&u, &request, &username, &conn),
|
|
|
|
UserAcceptedObjects::Accept(a) => handle_accept(&a, &request, &username, &conn),
|
2020-04-15 18:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Handle create activities and insert them in the database.
|
2020-04-19 17:35:40 +00:00
|
|
|
fn handle_create(
|
|
|
|
create: &Create,
|
|
|
|
request: &HttpRequest,
|
2020-04-24 19:55:54 +00:00
|
|
|
_username: &str,
|
2020-04-19 17:35:40 +00:00
|
|
|
conn: &PgConnection,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
2020-04-24 14:04:36 +00:00
|
|
|
// TODO before this even gets named, because we don't know what type of object it is, we need
|
|
|
|
// to parse this out
|
2020-04-24 19:55:54 +00:00
|
|
|
let user_uri = create
|
2020-04-19 17:35:40 +00:00
|
|
|
.create_props
|
|
|
|
.get_actor_xsd_any_uri()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
2020-04-24 19:55:54 +00:00
|
|
|
|
|
|
|
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
|
2020-04-19 17:35:40 +00:00
|
|
|
verify(request, &user.public_key.unwrap())?;
|
|
|
|
|
2020-04-15 18:12:25 +00:00
|
|
|
let page = create
|
|
|
|
.create_props
|
|
|
|
.get_object_base_box()
|
|
|
|
.to_owned()
|
|
|
|
.unwrap()
|
|
|
|
.to_owned()
|
|
|
|
.to_concrete::<Page>()?;
|
2020-04-24 19:55:54 +00:00
|
|
|
let post = PostForm::from_apub(&page, conn)?;
|
2020-04-15 18:12:25 +00:00
|
|
|
Post::create(conn, &post)?;
|
|
|
|
// TODO: send the new post out via websocket
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Handle update activities and insert them in the database.
|
2020-04-19 17:35:40 +00:00
|
|
|
fn handle_update(
|
|
|
|
update: &Update,
|
|
|
|
request: &HttpRequest,
|
2020-04-24 19:55:54 +00:00
|
|
|
_username: &str,
|
2020-04-19 17:35:40 +00:00
|
|
|
conn: &PgConnection,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
2020-04-24 19:55:54 +00:00
|
|
|
let user_uri = update
|
2020-04-19 17:35:40 +00:00
|
|
|
.update_props
|
|
|
|
.get_actor_xsd_any_uri()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
2020-04-24 19:55:54 +00:00
|
|
|
|
|
|
|
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
|
2020-04-19 17:35:40 +00:00
|
|
|
verify(request, &user.public_key.unwrap())?;
|
|
|
|
|
2020-04-15 18:12:25 +00:00
|
|
|
let page = update
|
|
|
|
.update_props
|
|
|
|
.get_object_base_box()
|
|
|
|
.to_owned()
|
|
|
|
.unwrap()
|
|
|
|
.to_owned()
|
|
|
|
.to_concrete::<Page>()?;
|
2020-04-24 19:55:54 +00:00
|
|
|
let post = PostForm::from_apub(&page, conn)?;
|
2020-04-15 18:12:25 +00:00
|
|
|
let id = Post::read_from_apub_id(conn, &post.ap_id)?.id;
|
|
|
|
Post::update(conn, id, &post)?;
|
|
|
|
// TODO: send the new post out via websocket
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Handle accepted follows.
|
2020-04-19 17:35:40 +00:00
|
|
|
fn handle_accept(
|
|
|
|
accept: &Accept,
|
|
|
|
request: &HttpRequest,
|
2020-04-24 14:04:36 +00:00
|
|
|
username: &str,
|
2020-04-19 17:35:40 +00:00
|
|
|
conn: &PgConnection,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let community_uri = accept
|
|
|
|
.accept_props
|
|
|
|
.get_actor_xsd_any_uri()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
2020-04-24 19:55:54 +00:00
|
|
|
|
2020-04-24 14:04:36 +00:00
|
|
|
let community = get_or_fetch_and_upsert_remote_community(&community_uri, conn)?;
|
2020-04-19 17:35:40 +00:00
|
|
|
verify(request, &community.public_key.unwrap())?;
|
|
|
|
|
2020-04-24 14:04:36 +00:00
|
|
|
let user = User_::read_from_name(&conn, username)?;
|
|
|
|
|
|
|
|
// Now you need to add this to the community follower
|
|
|
|
let community_follower_form = CommunityFollowerForm {
|
|
|
|
community_id: community.id,
|
|
|
|
user_id: user.id,
|
|
|
|
};
|
|
|
|
|
|
|
|
// This will fail if they're already a follower
|
|
|
|
CommunityFollower::follow(&conn, &community_follower_form)?;
|
|
|
|
|
2020-04-17 14:55:28 +00:00
|
|
|
// TODO: make sure that we actually requested a follow
|
2020-04-15 18:12:25 +00:00
|
|
|
// TODO: at this point, indicate to the user that they are following the community
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|