2020-07-23 14:36:45 +00:00
|
|
|
use crate::{
|
|
|
|
api::{
|
|
|
|
comment::{send_local_notifs, CommentResponse},
|
|
|
|
post::PostResponse,
|
|
|
|
},
|
|
|
|
apub::{
|
|
|
|
fetcher::{get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post},
|
2020-07-28 16:08:28 +00:00
|
|
|
inbox::shared_inbox::{
|
2020-07-28 16:47:26 +00:00
|
|
|
announce_if_community_is_local, get_user_from_activity, receive_unhandled_activity,
|
2020-07-28 16:08:28 +00:00
|
|
|
},
|
2020-07-28 16:47:26 +00:00
|
|
|
ActorType, FromApub, PageExt,
|
2020-07-23 14:36:45 +00:00
|
|
|
},
|
|
|
|
blocking,
|
|
|
|
routes::ChatServerParam,
|
|
|
|
websocket::{
|
|
|
|
server::{SendComment, SendPost},
|
|
|
|
UserOperation,
|
|
|
|
},
|
2020-07-28 16:47:26 +00:00
|
|
|
DbPool, LemmyError,
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
2020-07-28 16:08:28 +00:00
|
|
|
use activitystreams_new::{activity::Update, base::AnyBase, object::Note, prelude::*};
|
2020-07-23 14:36:45 +00:00
|
|
|
use actix_web::{client::Client, HttpResponse};
|
|
|
|
use lemmy_db::{
|
|
|
|
comment::{Comment, CommentForm},
|
|
|
|
comment_view::CommentView,
|
|
|
|
post::{Post, PostForm},
|
|
|
|
post_view::PostView,
|
|
|
|
Crud,
|
|
|
|
};
|
|
|
|
use lemmy_utils::scrape_text_for_mentions;
|
|
|
|
|
|
|
|
pub async fn receive_update(
|
|
|
|
activity: AnyBase,
|
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
|
|
|
chat_server: ChatServerParam,
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
|
|
|
let update = Update::from_any_base(activity)?.unwrap();
|
|
|
|
match update.object().as_single_kind_str() {
|
|
|
|
Some("Page") => receive_update_post(update, client, pool, chat_server).await,
|
|
|
|
Some("Note") => receive_update_comment(update, client, pool, chat_server).await,
|
|
|
|
_ => receive_unhandled_activity(update),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_update_post(
|
|
|
|
update: Update,
|
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
|
|
|
chat_server: ChatServerParam,
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
|
|
|
let user = get_user_from_activity(&update, client, pool).await?;
|
|
|
|
let page = PageExt::from_any_base(update.object().to_owned().one().unwrap())?.unwrap();
|
|
|
|
|
|
|
|
let post = PostForm::from_apub(&page, client, pool, &user.actor_id()?).await?;
|
|
|
|
|
|
|
|
let post_id = get_or_fetch_and_insert_remote_post(&post.get_ap_id()?, client, pool)
|
|
|
|
.await?
|
|
|
|
.id;
|
|
|
|
|
|
|
|
blocking(pool, move |conn| Post::update(conn, post_id, &post)).await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
|
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
|
|
|
chat_server.do_send(SendPost {
|
|
|
|
op: UserOperation::EditPost,
|
|
|
|
post: res,
|
|
|
|
my_id: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
announce_if_community_is_local(update, &user, client, pool).await?;
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_update_comment(
|
|
|
|
update: Update,
|
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
|
|
|
chat_server: ChatServerParam,
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
|
|
|
let note = Note::from_any_base(update.object().to_owned().one().unwrap())?.unwrap();
|
|
|
|
let user = get_user_from_activity(&update, client, pool).await?;
|
|
|
|
|
|
|
|
let comment = CommentForm::from_apub(¬e, client, pool, &user.actor_id()?).await?;
|
|
|
|
|
|
|
|
let comment_id = get_or_fetch_and_insert_remote_comment(&comment.get_ap_id()?, client, pool)
|
|
|
|
.await?
|
|
|
|
.id;
|
|
|
|
|
|
|
|
let updated_comment = blocking(pool, move |conn| {
|
|
|
|
Comment::update(conn, comment_id, &comment)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let post_id = updated_comment.post_id;
|
|
|
|
let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
|
|
|
|
|
|
|
|
let mentions = scrape_text_for_mentions(&updated_comment.content);
|
2020-07-28 16:08:28 +00:00
|
|
|
let recipient_ids =
|
|
|
|
send_local_notifs(mentions, updated_comment, &user, post, pool, false).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let comment_view =
|
|
|
|
blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
chat_server.do_send(SendComment {
|
|
|
|
op: UserOperation::EditComment,
|
|
|
|
comment: res,
|
|
|
|
my_id: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
announce_if_community_is_local(update, &user, client, pool).await?;
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|