2021-08-02 20:33:40 +00:00
|
|
|
use crate::{
|
|
|
|
activities::{
|
|
|
|
community::announce::AnnouncableActivities,
|
|
|
|
generate_activity_id,
|
|
|
|
verify_activity,
|
|
|
|
verify_person_in_community,
|
|
|
|
voting::{
|
|
|
|
undo_vote_comment,
|
|
|
|
undo_vote_post,
|
|
|
|
vote::{Vote, VoteType},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
activity_queue::send_to_community_new,
|
|
|
|
extensions::context::lemmy_context,
|
|
|
|
fetcher::{
|
|
|
|
objects::get_or_fetch_and_insert_post_or_comment,
|
|
|
|
person::get_or_fetch_and_upsert_person,
|
|
|
|
},
|
|
|
|
ActorType,
|
|
|
|
PostOrComment,
|
|
|
|
};
|
2021-08-19 21:24:33 +00:00
|
|
|
use activitystreams::{
|
|
|
|
activity::kind::UndoType,
|
|
|
|
base::AnyBase,
|
|
|
|
primitives::OneOrMany,
|
|
|
|
unparsed::Unparsed,
|
|
|
|
};
|
2021-08-02 20:33:40 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2021-08-19 21:24:33 +00:00
|
|
|
use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityFields, ActivityHandler};
|
2021-08-02 20:33:40 +00:00
|
|
|
use lemmy_db_queries::Crud;
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{community::Community, person::Person},
|
|
|
|
CommunityId,
|
|
|
|
};
|
|
|
|
use lemmy_utils::LemmyError;
|
|
|
|
use lemmy_websocket::LemmyContext;
|
2021-08-19 21:24:33 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-08-02 20:33:40 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
use url::Url;
|
|
|
|
|
2021-08-19 21:24:33 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
|
2021-08-02 20:33:40 +00:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct UndoVote {
|
2021-08-19 21:24:33 +00:00
|
|
|
actor: Url,
|
2021-08-02 20:33:40 +00:00
|
|
|
to: PublicUrl,
|
|
|
|
object: Vote,
|
|
|
|
cc: [Url; 1],
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
kind: UndoType,
|
2021-08-19 21:24:33 +00:00
|
|
|
id: Url,
|
|
|
|
#[serde(rename = "@context")]
|
|
|
|
context: OneOrMany<AnyBase>,
|
2021-08-02 20:33:40 +00:00
|
|
|
#[serde(flatten)]
|
2021-08-19 21:24:33 +00:00
|
|
|
unparsed: Unparsed,
|
2021-08-02 20:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UndoVote {
|
|
|
|
pub async fn send(
|
|
|
|
object: &PostOrComment,
|
|
|
|
actor: &Person,
|
|
|
|
community_id: CommunityId,
|
|
|
|
kind: VoteType,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-08-19 21:24:33 +00:00
|
|
|
let object = Vote::new(object, actor, &community, kind.clone())?;
|
|
|
|
let id = generate_activity_id(UndoType::Undo)?;
|
2021-08-02 20:33:40 +00:00
|
|
|
let undo_vote = UndoVote {
|
2021-08-19 21:24:33 +00:00
|
|
|
actor: actor.actor_id(),
|
2021-08-02 20:33:40 +00:00
|
|
|
to: PublicUrl::Public,
|
2021-08-19 21:24:33 +00:00
|
|
|
object,
|
2021-08-02 20:33:40 +00:00
|
|
|
cc: [community.actor_id()],
|
|
|
|
kind: UndoType::Undo,
|
2021-08-19 21:24:33 +00:00
|
|
|
id: id.clone(),
|
|
|
|
context: lemmy_context(),
|
|
|
|
unparsed: Default::default(),
|
2021-08-02 20:33:40 +00:00
|
|
|
};
|
|
|
|
let activity = AnnouncableActivities::UndoVote(undo_vote);
|
|
|
|
send_to_community_new(activity, &id, actor, &community, vec![], context).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl ActivityHandler for UndoVote {
|
|
|
|
async fn verify(
|
|
|
|
&self,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2021-08-19 21:24:33 +00:00
|
|
|
verify_activity(self)?;
|
|
|
|
verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
|
|
|
|
verify_urls_match(&self.actor, self.object.actor())?;
|
2021-08-02 20:33:40 +00:00
|
|
|
self.object.verify(context, request_counter).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive(
|
2021-08-12 12:48:09 +00:00
|
|
|
self,
|
2021-08-02 20:33:40 +00:00
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2021-08-19 21:24:33 +00:00
|
|
|
let actor = get_or_fetch_and_upsert_person(&self.actor, context, request_counter).await?;
|
2021-08-02 20:33:40 +00:00
|
|
|
let object =
|
|
|
|
get_or_fetch_and_insert_post_or_comment(&self.object.object, context, request_counter)
|
|
|
|
.await?;
|
|
|
|
match object {
|
|
|
|
PostOrComment::Post(p) => undo_vote_post(actor, p.deref(), context).await,
|
|
|
|
PostOrComment::Comment(c) => undo_vote_comment(actor, c.deref(), context).await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|