3 community::announce::AnnouncableActivities,
5 post::send_websocket_message,
8 verify_person_in_community,
10 activity_queue::send_to_community_new,
11 extensions::context::lemmy_context,
12 fetcher::community::get_or_fetch_and_upsert_community,
13 objects::{post::Page, FromApub, ToApub},
16 use activitystreams::activity::kind::UpdateType;
17 use lemmy_api_common::blocking;
18 use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler};
19 use lemmy_db_queries::Crud;
20 use lemmy_db_schema::source::{community::Community, person::Person, post::Post};
21 use lemmy_utils::LemmyError;
22 use lemmy_websocket::{LemmyContext, UserOperationCrud};
25 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
26 #[serde(rename_all = "camelCase")]
27 pub struct UpdatePost {
33 common: ActivityCommonFields,
37 pub async fn send(post: &Post, actor: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
38 let community_id = post.community_id;
39 let community = blocking(context.pool(), move |conn| {
40 Community::read(conn, community_id)
44 let id = generate_activity_id(UpdateType::Update)?;
45 let update = UpdatePost {
46 to: PublicUrl::Public,
47 object: post.to_apub(context.pool()).await?,
48 cc: [community.actor_id()],
49 r#type: Default::default(),
50 common: ActivityCommonFields {
51 context: lemmy_context(),
53 actor: actor.actor_id(),
54 unparsed: Default::default(),
57 let activity = AnnouncableActivities::UpdatePost(update);
58 send_to_community_new(activity, &id, actor, &community, vec![], context).await
62 #[async_trait::async_trait(?Send)]
63 impl ActivityHandler for UpdatePost {
66 context: &LemmyContext,
67 request_counter: &mut i32,
68 ) -> Result<(), LemmyError> {
69 let community_id = get_or_fetch_and_upsert_community(&self.cc[0], context, request_counter)
72 let is_mod_action = self.object.is_mod_action(context.pool()).await?;
74 verify_activity(self.common())?;
75 verify_person_in_community(&self.common.actor, &community_id, context, request_counter).await?;
77 verify_mod_action(&self.common.actor, community_id, context).await?;
79 verify_urls_match(&self.common.actor, &self.object.attributed_to)?;
81 self.object.verify(context, request_counter).await?;
87 context: &LemmyContext,
88 request_counter: &mut i32,
89 ) -> Result<(), LemmyError> {
90 let post = Post::from_apub(
93 self.common.actor.clone(),
95 // TODO: we already check here if the mod action is valid, can remove that check param
100 send_websocket_message(post.id, UserOperationCrud::EditPost, context).await
103 fn common(&self) -> &ActivityCommonFields {