2020-05-16 14:04:08 +00:00
|
|
|
use crate::{
|
|
|
|
api::user::PrivateMessageResponse,
|
|
|
|
apub::{
|
|
|
|
extensions::signatures::verify,
|
|
|
|
fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user},
|
2020-07-28 16:47:26 +00:00
|
|
|
insert_activity, FromApub,
|
2020-05-16 14:04:08 +00:00
|
|
|
},
|
2020-07-01 12:54:29 +00:00
|
|
|
blocking,
|
2020-05-16 14:04:08 +00:00
|
|
|
routes::{ChatServerParam, DbPoolParam},
|
|
|
|
websocket::{server::SendUserRoomMessage, UserOperation},
|
2020-07-28 16:47:26 +00:00
|
|
|
DbPool, LemmyError,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-07-17 21:11:07 +00:00
|
|
|
use activitystreams_new::{
|
2020-05-16 14:04:08 +00:00
|
|
|
activity::{Accept, Create, Delete, Undo, Update},
|
|
|
|
object::Note,
|
2020-07-17 21:11:07 +00:00
|
|
|
prelude::*,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-07-01 12:54:29 +00:00
|
|
|
use actix_web::{client::Client, web, HttpRequest, HttpResponse};
|
2020-07-10 18:15:41 +00:00
|
|
|
use lemmy_db::{
|
|
|
|
community::{CommunityFollower, CommunityFollowerForm},
|
|
|
|
naive_now,
|
|
|
|
private_message::{PrivateMessage, PrivateMessageForm},
|
|
|
|
private_message_view::PrivateMessageView,
|
|
|
|
user::User_,
|
2020-07-28 16:47:26 +00:00
|
|
|
Crud, Followable,
|
2020-07-10 18:15:41 +00:00
|
|
|
};
|
2020-05-16 14:04:08 +00:00
|
|
|
use log::debug;
|
|
|
|
use serde::Deserialize;
|
2020-07-01 12:54:29 +00:00
|
|
|
use std::fmt::Debug;
|
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 {
|
2020-05-06 02:06:24 +00:00
|
|
|
Accept(Box<Accept>),
|
|
|
|
Create(Box<Create>),
|
|
|
|
Update(Box<Update>),
|
|
|
|
Delete(Box<Delete>),
|
|
|
|
Undo(Box<Undo>),
|
2020-04-15 18:12:25 +00:00
|
|
|
}
|
|
|
|
|
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-07-01 12:54:29 +00:00
|
|
|
client: web::Data<Client>,
|
2020-04-24 14:04:36 +00:00
|
|
|
db: DbPoolParam,
|
2020-05-06 02:06:24 +00:00
|
|
|
chat_server: ChatServerParam,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
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();
|
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-07-01 12:54:29 +00:00
|
|
|
UserAcceptedObjects::Accept(a) => receive_accept(*a, &request, &username, &client, &db).await,
|
2020-05-06 02:06:24 +00:00
|
|
|
UserAcceptedObjects::Create(c) => {
|
2020-07-01 12:54:29 +00:00
|
|
|
receive_create_private_message(*c, &request, &client, &db, chat_server).await
|
2020-05-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
UserAcceptedObjects::Update(u) => {
|
2020-07-01 12:54:29 +00:00
|
|
|
receive_update_private_message(*u, &request, &client, &db, chat_server).await
|
2020-05-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
UserAcceptedObjects::Delete(d) => {
|
2020-07-01 12:54:29 +00:00
|
|
|
receive_delete_private_message(*d, &request, &client, &db, chat_server).await
|
2020-05-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
UserAcceptedObjects::Undo(u) => {
|
2020-07-01 12:54:29 +00:00
|
|
|
receive_undo_delete_private_message(*u, &request, &client, &db, chat_server).await
|
2020-05-06 02:06:24 +00:00
|
|
|
}
|
2020-04-15 18:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Handle accepted follows.
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn receive_accept(
|
|
|
|
accept: Accept,
|
2020-04-19 17:35:40 +00:00
|
|
|
request: &HttpRequest,
|
2020-04-24 14:04:36 +00:00
|
|
|
username: &str,
|
2020-07-01 12:54:29 +00:00
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-07-17 21:11:07 +00:00
|
|
|
let community_uri = accept.actor()?.to_owned().single_xsd_any_uri().unwrap();
|
2020-04-24 19:55:54 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let community = get_or_fetch_and_upsert_remote_community(&community_uri, client, pool).await?;
|
2020-05-14 13:38:07 +00:00
|
|
|
verify(request, &community)?;
|
2020-04-19 17:35:40 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let username = username.to_owned();
|
|
|
|
let user = blocking(pool, move |conn| User_::read_from_name(conn, &username)).await??;
|
2020-04-24 14:04:36 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
insert_activity(community.creator_id, accept, false, pool).await?;
|
2020-04-27 22:17:02 +00:00
|
|
|
|
2020-04-24 14:04:36 +00:00
|
|
|
// 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
|
2020-07-01 12:54:29 +00:00
|
|
|
blocking(pool, move |conn| {
|
|
|
|
CommunityFollower::follow(conn, &community_follower_form)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-04-24 14:04:36 +00:00
|
|
|
|
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
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn receive_create_private_message(
|
|
|
|
create: Create,
|
2020-05-06 02:06:24 +00:00
|
|
|
request: &HttpRequest,
|
2020-07-01 12:54:29 +00:00
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
2020-05-06 02:06:24 +00:00
|
|
|
chat_server: ChatServerParam,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-07-17 21:11:07 +00:00
|
|
|
let user_uri = &create.actor()?.to_owned().single_xsd_any_uri().unwrap();
|
|
|
|
let note = Note::from_any_base(create.object().as_one().unwrap().to_owned())?.unwrap();
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-17 21:11:07 +00:00
|
|
|
let user = get_or_fetch_and_upsert_remote_user(user_uri, client, pool).await?;
|
2020-05-14 13:38:07 +00:00
|
|
|
verify(request, &user)?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
insert_activity(user.id, create, false, pool).await?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-17 21:11:07 +00:00
|
|
|
let private_message = PrivateMessageForm::from_apub(¬e, client, pool, user_uri).await?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let inserted_private_message = blocking(pool, move |conn| {
|
|
|
|
PrivateMessage::create(conn, &private_message)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let message = blocking(pool, move |conn| {
|
|
|
|
PrivateMessageView::read(conn, inserted_private_message.id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let res = PrivateMessageResponse { message };
|
|
|
|
|
|
|
|
let recipient_id = res.message.recipient_id;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
chat_server.do_send(SendUserRoomMessage {
|
|
|
|
op: UserOperation::CreatePrivateMessage,
|
|
|
|
response: res,
|
2020-07-01 12:54:29 +00:00
|
|
|
recipient_id,
|
2020-05-06 02:06:24 +00:00
|
|
|
my_id: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn receive_update_private_message(
|
|
|
|
update: Update,
|
2020-05-06 02:06:24 +00:00
|
|
|
request: &HttpRequest,
|
2020-07-01 12:54:29 +00:00
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
2020-05-06 02:06:24 +00:00
|
|
|
chat_server: ChatServerParam,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-07-17 21:11:07 +00:00
|
|
|
let user_uri = &update.actor()?.to_owned().single_xsd_any_uri().unwrap();
|
|
|
|
let note = Note::from_any_base(update.object().as_one().unwrap().to_owned())?.unwrap();
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?;
|
2020-05-14 13:38:07 +00:00
|
|
|
verify(request, &user)?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
insert_activity(user.id, update, false, pool).await?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-17 21:11:07 +00:00
|
|
|
let private_message_form = PrivateMessageForm::from_apub(¬e, client, pool, user_uri).await?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let private_message_ap_id = private_message_form.ap_id.clone();
|
|
|
|
let private_message = blocking(pool, move |conn| {
|
|
|
|
PrivateMessage::read_from_apub_id(conn, &private_message_ap_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let private_message_id = private_message.id;
|
|
|
|
blocking(pool, move |conn| {
|
|
|
|
PrivateMessage::update(conn, private_message_id, &private_message_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let private_message_id = private_message.id;
|
|
|
|
let message = blocking(pool, move |conn| {
|
|
|
|
PrivateMessageView::read(conn, private_message_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let res = PrivateMessageResponse { message };
|
|
|
|
|
|
|
|
let recipient_id = res.message.recipient_id;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
chat_server.do_send(SendUserRoomMessage {
|
|
|
|
op: UserOperation::EditPrivateMessage,
|
|
|
|
response: res,
|
2020-07-01 12:54:29 +00:00
|
|
|
recipient_id,
|
2020-05-06 02:06:24 +00:00
|
|
|
my_id: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn receive_delete_private_message(
|
|
|
|
delete: Delete,
|
2020-05-06 02:06:24 +00:00
|
|
|
request: &HttpRequest,
|
2020-07-01 12:54:29 +00:00
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
2020-05-06 02:06:24 +00:00
|
|
|
chat_server: ChatServerParam,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-07-17 21:11:07 +00:00
|
|
|
let user_uri = &delete.actor()?.to_owned().single_xsd_any_uri().unwrap();
|
|
|
|
let note = Note::from_any_base(delete.object().as_one().unwrap().to_owned())?.unwrap();
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?;
|
2020-05-14 13:38:07 +00:00
|
|
|
verify(request, &user)?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
insert_activity(user.id, delete, false, pool).await?;
|
|
|
|
|
2020-07-17 21:11:07 +00:00
|
|
|
let private_message_form = PrivateMessageForm::from_apub(¬e, client, pool, user_uri).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let private_message_ap_id = private_message_form.ap_id;
|
|
|
|
let private_message = blocking(pool, move |conn| {
|
|
|
|
PrivateMessage::read_from_apub_id(conn, &private_message_ap_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
let private_message_form = PrivateMessageForm {
|
2020-07-01 12:54:29 +00:00
|
|
|
content: private_message_form.content,
|
2020-05-06 02:06:24 +00:00
|
|
|
recipient_id: private_message.recipient_id,
|
|
|
|
creator_id: private_message.creator_id,
|
|
|
|
deleted: Some(true),
|
|
|
|
read: None,
|
|
|
|
ap_id: private_message.ap_id,
|
|
|
|
local: private_message.local,
|
|
|
|
published: None,
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
};
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let private_message_id = private_message.id;
|
|
|
|
blocking(pool, move |conn| {
|
|
|
|
PrivateMessage::update(conn, private_message_id, &private_message_form)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let private_message_id = private_message.id;
|
|
|
|
let message = blocking(pool, move |conn| {
|
|
|
|
PrivateMessageView::read(&conn, private_message_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let res = PrivateMessageResponse { message };
|
|
|
|
|
|
|
|
let recipient_id = res.message.recipient_id;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
chat_server.do_send(SendUserRoomMessage {
|
|
|
|
op: UserOperation::EditPrivateMessage,
|
|
|
|
response: res,
|
2020-07-01 12:54:29 +00:00
|
|
|
recipient_id,
|
2020-05-06 02:06:24 +00:00
|
|
|
my_id: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn receive_undo_delete_private_message(
|
|
|
|
undo: Undo,
|
2020-05-06 02:06:24 +00:00
|
|
|
request: &HttpRequest,
|
2020-07-01 12:54:29 +00:00
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
2020-05-06 02:06:24 +00:00
|
|
|
chat_server: ChatServerParam,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-07-17 21:11:07 +00:00
|
|
|
let delete = Delete::from_any_base(undo.object().as_one().unwrap().to_owned())?.unwrap();
|
|
|
|
let note = Note::from_any_base(delete.object().as_one().unwrap().to_owned())?.unwrap();
|
|
|
|
let user_uri = &delete.actor()?.to_owned().single_xsd_any_uri().unwrap();
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?;
|
2020-05-14 13:38:07 +00:00
|
|
|
verify(request, &user)?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
insert_activity(user.id, delete, false, pool).await?;
|
|
|
|
|
2020-07-17 21:11:07 +00:00
|
|
|
let private_message = PrivateMessageForm::from_apub(¬e, client, pool, user_uri).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let private_message_ap_id = private_message.ap_id.clone();
|
|
|
|
let private_message_id = blocking(pool, move |conn| {
|
|
|
|
PrivateMessage::read_from_apub_id(conn, &private_message_ap_id).map(|pm| pm.id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
let private_message_form = PrivateMessageForm {
|
|
|
|
content: private_message.content,
|
|
|
|
recipient_id: private_message.recipient_id,
|
|
|
|
creator_id: private_message.creator_id,
|
|
|
|
deleted: Some(false),
|
|
|
|
read: None,
|
|
|
|
ap_id: private_message.ap_id,
|
|
|
|
local: private_message.local,
|
|
|
|
published: None,
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
};
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
blocking(pool, move |conn| {
|
|
|
|
PrivateMessage::update(conn, private_message_id, &private_message_form)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let message = blocking(pool, move |conn| {
|
|
|
|
PrivateMessageView::read(&conn, private_message_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let res = PrivateMessageResponse { message };
|
|
|
|
|
|
|
|
let recipient_id = res.message.recipient_id;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
chat_server.do_send(SendUserRoomMessage {
|
|
|
|
op: UserOperation::EditPrivateMessage,
|
|
|
|
response: res,
|
2020-07-01 12:54:29 +00:00
|
|
|
recipient_id,
|
2020-05-06 02:06:24 +00:00
|
|
|
my_id: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|