lemmy/server/src/apub/inbox/activities/update.rs

123 lines
3.6 KiB
Rust

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},
inbox::shared_inbox::{
announce_if_community_is_local, get_user_from_activity, receive_unhandled_activity,
},
ActorType, FromApub, PageExt,
},
blocking,
routes::ChatServerParam,
websocket::{
server::{SendComment, SendPost},
UserOperation,
},
DbPool, LemmyError,
};
use activitystreams_new::{activity::Update, base::AnyBase, object::Note, prelude::*};
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(&note, 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);
let recipient_ids =
send_local_notifs(mentions, updated_comment, &user, post, pool, false).await?;
// 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,
form_id: None,
};
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())
}