2020-07-23 14:36:45 +00:00
|
|
|
use crate::{
|
|
|
|
apub::{
|
2020-07-29 11:46:11 +00:00
|
|
|
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
|
2020-07-28 16:08:28 +00:00
|
|
|
inbox::shared_inbox::{
|
2020-07-29 11:46:11 +00:00
|
|
|
announce_if_community_is_local,
|
|
|
|
get_user_from_activity,
|
|
|
|
receive_unhandled_activity,
|
2020-07-28 16:08:28 +00:00
|
|
|
},
|
2020-08-06 12:53:58 +00:00
|
|
|
ActorType,
|
2020-07-29 11:46:11 +00:00
|
|
|
FromApub,
|
|
|
|
PageExt,
|
2020-07-23 14:36:45 +00:00
|
|
|
},
|
|
|
|
websocket::{
|
2020-08-31 15:20:13 +00:00
|
|
|
messages::{SendComment, SendPost},
|
2020-07-23 14:36:45 +00:00
|
|
|
UserOperation,
|
|
|
|
},
|
2020-08-18 13:43:50 +00:00
|
|
|
LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
2020-08-01 13:25:17 +00:00
|
|
|
use activitystreams::{activity::Update, base::AnyBase, object::Note, prelude::*};
|
2020-08-18 13:43:50 +00:00
|
|
|
use actix_web::HttpResponse;
|
2020-08-11 14:31:05 +00:00
|
|
|
use anyhow::Context;
|
2020-09-14 15:29:50 +00:00
|
|
|
use lemmy_api_structs::{
|
|
|
|
blocking,
|
|
|
|
comment::CommentResponse,
|
|
|
|
post::PostResponse,
|
|
|
|
send_local_notifs,
|
|
|
|
};
|
2020-07-23 14:36:45 +00:00
|
|
|
use lemmy_db::{
|
|
|
|
comment::{Comment, CommentForm},
|
|
|
|
comment_view::CommentView,
|
|
|
|
post::{Post, PostForm},
|
|
|
|
post_view::PostView,
|
|
|
|
Crud,
|
|
|
|
};
|
2020-09-14 15:29:50 +00:00
|
|
|
use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError};
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
pub async fn receive_update(
|
|
|
|
activity: AnyBase,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-11 14:31:05 +00:00
|
|
|
let update = Update::from_any_base(activity)?.context(location_info!())?;
|
2020-08-06 12:53:58 +00:00
|
|
|
|
|
|
|
// ensure that update and actor come from the same instance
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_activity(&update, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
update.id(user.actor_id()?.domain().context(location_info!())?)?;
|
2020-08-06 12:53:58 +00:00
|
|
|
|
2020-07-23 14:36:45 +00:00
|
|
|
match update.object().as_single_kind_str() {
|
2020-08-18 13:43:50 +00:00
|
|
|
Some("Page") => receive_update_post(update, context).await,
|
|
|
|
Some("Note") => receive_update_comment(update, context).await,
|
2020-07-23 14:36:45 +00:00
|
|
|
_ => receive_unhandled_activity(update),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_update_post(
|
|
|
|
update: Update,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_activity(&update, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let page = PageExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = PostForm::from_apub(&page, context, Some(user.actor_id()?)).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let original_post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
|
2020-08-06 19:44:47 +00:00
|
|
|
.await?
|
|
|
|
.id;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2020-08-06 12:53:58 +00:00
|
|
|
Post::update(conn, original_post_id, &post)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
// Refetch the view
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2020-08-06 12:53:58 +00:00
|
|
|
PostView::read(conn, original_post_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::EditPost,
|
|
|
|
post: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(update, &user, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_update_comment(
|
|
|
|
update: Update,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-11 14:31:05 +00:00
|
|
|
let note = Note::from_any_base(update.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_activity(&update, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment = CommentForm::from_apub(¬e, context, Some(user.actor_id()?)).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let original_comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
|
2020-08-06 19:44:47 +00:00
|
|
|
.await?
|
|
|
|
.id;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let updated_comment = blocking(context.pool(), move |conn| {
|
2020-08-06 12:53:58 +00:00
|
|
|
Comment::update(conn, original_comment_id, &comment)
|
2020-07-23 14:36:45 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let post_id = updated_comment.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
let mentions = scrape_text_for_mentions(&updated_comment.content);
|
2020-08-18 13:43:50 +00:00
|
|
|
let recipient_ids = send_local_notifs(
|
|
|
|
mentions,
|
|
|
|
updated_comment,
|
|
|
|
&user,
|
|
|
|
post,
|
|
|
|
context.pool(),
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
// Refetch the view
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment_view = blocking(context.pool(), move |conn| {
|
2020-08-06 12:53:58 +00:00
|
|
|
CommentView::read(conn, original_comment_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
let res = CommentResponse {
|
|
|
|
comment: comment_view,
|
|
|
|
recipient_ids,
|
2020-07-28 16:08:28 +00:00
|
|
|
form_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendComment {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::EditComment,
|
|
|
|
comment: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(update, &user, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|