lemmy/crates/apub/src/activities/comment/remove.rs
2021-07-30 23:16:33 +02:00

65 lines
1.8 KiB
Rust

use crate::{
activities::{comment::send_websocket_message, verify_mod_action},
check_is_apub_id_valid,
fetcher::objects::get_or_fetch_and_insert_comment,
};
use activitystreams::activity::kind::RemoveType;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
values::PublicUrl,
verify_domains_match,
ActivityCommonFields,
ActivityHandlerNew,
};
use lemmy_db_queries::source::comment::Comment_;
use lemmy_db_schema::source::comment::Comment;
use lemmy_utils::LemmyError;
use lemmy_websocket::{LemmyContext, UserOperationCrud};
use url::Url;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RemoveComment {
to: PublicUrl,
pub(in crate::activities::comment) object: Url,
cc: [Url; 1],
#[serde(rename = "type")]
kind: RemoveType,
#[serde(flatten)]
common: ActivityCommonFields,
}
#[async_trait::async_trait(?Send)]
impl ActivityHandlerNew for RemoveComment {
async fn verify(&self, context: &LemmyContext, _: &mut i32) -> Result<(), LemmyError> {
verify_domains_match(&self.common.actor, self.common.id_unchecked())?;
check_is_apub_id_valid(&self.common.actor, false)?;
verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await
}
async fn receive(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let comment = get_or_fetch_and_insert_comment(&self.object, context, request_counter).await?;
let removed_comment = blocking(context.pool(), move |conn| {
Comment::update_removed(conn, comment.id, true)
})
.await??;
send_websocket_message(
removed_comment.id,
vec![],
UserOperationCrud::EditComment,
context,
)
.await
}
fn common(&self) -> &ActivityCommonFields {
&self.common
}
}