mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-01-23 10:25:56 +00:00
Rework and fix (un)block user, announce, update post
This commit is contained in:
parent
caba7e4cf0
commit
e88ab170ef
5 changed files with 64 additions and 29 deletions
|
@ -19,6 +19,7 @@ use crate::{
|
||||||
http::is_activity_already_known,
|
http::is_activity_already_known,
|
||||||
};
|
};
|
||||||
use activitystreams::activity::kind::AnnounceType;
|
use activitystreams::activity::kind::AnnounceType;
|
||||||
|
use lemmy_apub::insert_activity;
|
||||||
use lemmy_apub_lib::{ActivityCommonFields, ActivityHandlerNew, PublicUrl};
|
use lemmy_apub_lib::{ActivityCommonFields, ActivityHandlerNew, PublicUrl};
|
||||||
use lemmy_utils::LemmyError;
|
use lemmy_utils::LemmyError;
|
||||||
use lemmy_websocket::LemmyContext;
|
use lemmy_websocket::LemmyContext;
|
||||||
|
@ -49,7 +50,7 @@ pub enum AnnouncableActivities {
|
||||||
pub struct AnnounceActivity {
|
pub struct AnnounceActivity {
|
||||||
to: PublicUrl,
|
to: PublicUrl,
|
||||||
object: AnnouncableActivities,
|
object: AnnouncableActivities,
|
||||||
cc: [Url; 1],
|
cc: Vec<Url>,
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
kind: AnnounceType,
|
kind: AnnounceType,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
|
@ -65,6 +66,7 @@ impl ActivityHandlerNew for AnnounceActivity {
|
||||||
) -> Result<(), LemmyError> {
|
) -> Result<(), LemmyError> {
|
||||||
verify_activity(self.common())?;
|
verify_activity(self.common())?;
|
||||||
verify_community(&self.common.actor, context, request_counter).await?;
|
verify_community(&self.common.actor, context, request_counter).await?;
|
||||||
|
self.object.verify(context, request_counter).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,6 +78,14 @@ impl ActivityHandlerNew for AnnounceActivity {
|
||||||
if is_activity_already_known(context.pool(), self.object.common().id_unchecked()).await? {
|
if is_activity_already_known(context.pool(), self.object.common().id_unchecked()).await? {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
insert_activity(
|
||||||
|
self.object.common().id_unchecked(),
|
||||||
|
self.object.clone(),
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
context.pool(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
self.object.receive(context, request_counter).await
|
self.object.receive(context, request_counter).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use crate::activities::verify_mod_action;
|
use crate::activities::{verify_activity, verify_mod_action, verify_person_in_community};
|
||||||
use activitystreams::activity::kind::BlockType;
|
use activitystreams::activity::kind::BlockType;
|
||||||
use lemmy_api_common::blocking;
|
use lemmy_api_common::blocking;
|
||||||
use lemmy_apub::{
|
use lemmy_apub::fetcher::{
|
||||||
check_is_apub_id_valid,
|
community::get_or_fetch_and_upsert_community,
|
||||||
fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
|
person::get_or_fetch_and_upsert_person,
|
||||||
};
|
};
|
||||||
use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandlerNew, PublicUrl};
|
use lemmy_apub_lib::{ActivityCommonFields, ActivityHandlerNew, PublicUrl};
|
||||||
use lemmy_db_queries::{Bannable, Followable};
|
use lemmy_db_queries::{Bannable, Followable};
|
||||||
use lemmy_db_schema::source::community::{
|
use lemmy_db_schema::source::community::{
|
||||||
CommunityFollower,
|
CommunityFollower,
|
||||||
|
@ -31,10 +31,15 @@ pub struct BlockUserFromCommunity {
|
||||||
|
|
||||||
#[async_trait::async_trait(?Send)]
|
#[async_trait::async_trait(?Send)]
|
||||||
impl ActivityHandlerNew for BlockUserFromCommunity {
|
impl ActivityHandlerNew for BlockUserFromCommunity {
|
||||||
async fn verify(&self, context: &LemmyContext, _: &mut i32) -> Result<(), LemmyError> {
|
async fn verify(
|
||||||
verify_domains_match(&self.common.actor, self.common.id_unchecked())?;
|
&self,
|
||||||
check_is_apub_id_valid(&self.common.actor, false)?;
|
context: &LemmyContext,
|
||||||
verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await
|
request_counter: &mut i32,
|
||||||
|
) -> Result<(), LemmyError> {
|
||||||
|
verify_activity(self.common())?;
|
||||||
|
verify_person_in_community(&self.common.actor, &self.cc, context, request_counter).await?;
|
||||||
|
verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receive(
|
async fn receive(
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
use crate::activities::{community::block_user::BlockUserFromCommunity, verify_mod_action};
|
use crate::activities::{
|
||||||
use activitystreams::activity::kind::BlockType;
|
community::block_user::BlockUserFromCommunity,
|
||||||
use lemmy_api_common::blocking;
|
verify_activity,
|
||||||
use lemmy_apub::{
|
verify_mod_action,
|
||||||
check_is_apub_id_valid,
|
verify_person_in_community,
|
||||||
fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
|
|
||||||
};
|
};
|
||||||
use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandlerNew, PublicUrl};
|
use activitystreams::activity::kind::UndoType;
|
||||||
|
use lemmy_api_common::blocking;
|
||||||
|
use lemmy_apub::fetcher::{
|
||||||
|
community::get_or_fetch_and_upsert_community,
|
||||||
|
person::get_or_fetch_and_upsert_person,
|
||||||
|
};
|
||||||
|
use lemmy_apub_lib::{ActivityCommonFields, ActivityHandlerNew, PublicUrl};
|
||||||
use lemmy_db_queries::Bannable;
|
use lemmy_db_queries::Bannable;
|
||||||
use lemmy_db_schema::source::community::{CommunityPersonBan, CommunityPersonBanForm};
|
use lemmy_db_schema::source::community::{CommunityPersonBan, CommunityPersonBanForm};
|
||||||
use lemmy_utils::LemmyError;
|
use lemmy_utils::LemmyError;
|
||||||
|
@ -19,7 +24,7 @@ pub struct UndoBlockUserFromCommunity {
|
||||||
object: BlockUserFromCommunity,
|
object: BlockUserFromCommunity,
|
||||||
cc: [Url; 1],
|
cc: [Url; 1],
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
kind: BlockType,
|
kind: UndoType,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
common: ActivityCommonFields,
|
common: ActivityCommonFields,
|
||||||
}
|
}
|
||||||
|
@ -31,10 +36,11 @@ impl ActivityHandlerNew for UndoBlockUserFromCommunity {
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
) -> Result<(), LemmyError> {
|
) -> Result<(), LemmyError> {
|
||||||
verify_domains_match(&self.common.actor, self.common.id_unchecked())?;
|
verify_activity(self.common())?;
|
||||||
check_is_apub_id_valid(&self.common.actor, false)?;
|
verify_person_in_community(&self.common.actor, &self.cc, context, request_counter).await?;
|
||||||
verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await?;
|
verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await?;
|
||||||
self.object.verify(context, request_counter).await
|
self.object.verify(context, request_counter).await?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receive(
|
async fn receive(
|
||||||
|
|
|
@ -49,14 +49,13 @@ impl ActivityHandlerNew for UpdatePost {
|
||||||
verify_activity(self.common())?;
|
verify_activity(self.common())?;
|
||||||
let community =
|
let community =
|
||||||
verify_person_in_community(&self.common.actor, &self.cc, context, request_counter).await?;
|
verify_person_in_community(&self.common.actor, &self.cc, context, request_counter).await?;
|
||||||
verify_domains_match_opt(&self.common.actor, self.object.id_unchecked())?;
|
|
||||||
|
|
||||||
let temp_post = PostForm::from_apub(
|
let temp_post = PostForm::from_apub(
|
||||||
&self.object,
|
&self.object,
|
||||||
context,
|
context,
|
||||||
self.common.actor.clone(),
|
self.common.actor.clone(),
|
||||||
request_counter,
|
request_counter,
|
||||||
false,
|
true,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
let post_id: DbUrl = temp_post.ap_id.context(location_info!())?;
|
let post_id: DbUrl = temp_post.ap_id.context(location_info!())?;
|
||||||
|
@ -66,9 +65,14 @@ impl ActivityHandlerNew for UpdatePost {
|
||||||
.await??;
|
.await??;
|
||||||
let stickied = temp_post.stickied.context(location_info!())?;
|
let stickied = temp_post.stickied.context(location_info!())?;
|
||||||
let locked = temp_post.locked.context(location_info!())?;
|
let locked = temp_post.locked.context(location_info!())?;
|
||||||
|
// community mod changed locked/sticky status
|
||||||
if (stickied != old_post.stickied) || (locked != old_post.locked) {
|
if (stickied != old_post.stickied) || (locked != old_post.locked) {
|
||||||
verify_mod_action(&self.common.actor, community.actor_id(), context).await?;
|
verify_mod_action(&self.common.actor, community.actor_id(), context).await?;
|
||||||
}
|
}
|
||||||
|
// user edited their own post
|
||||||
|
else {
|
||||||
|
verify_domains_match_opt(&self.common.actor, self.object.id_unchecked())?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,8 @@ async fn payload_to_string(mut payload: Payload) -> Result<String, LemmyError> {
|
||||||
Bytes::from(bytes).as_ref().read_to_string(&mut unparsed)?;
|
Bytes::from(bytes).as_ref().read_to_string(&mut unparsed)?;
|
||||||
Ok(unparsed)
|
Ok(unparsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: move most of this code to library
|
||||||
async fn receive_activity<'a, T>(
|
async fn receive_activity<'a, T>(
|
||||||
request: HttpRequest,
|
request: HttpRequest,
|
||||||
activity: &'a str,
|
activity: &'a str,
|
||||||
|
@ -60,19 +62,23 @@ where
|
||||||
{
|
{
|
||||||
let activity = serde_json::from_str::<T>(activity)?;
|
let activity = serde_json::from_str::<T>(activity)?;
|
||||||
let activity_data = activity.common();
|
let activity_data = activity.common();
|
||||||
// TODO: which order to check things?
|
|
||||||
// Do nothing if we received the same activity before
|
|
||||||
if is_activity_already_known(context.pool(), activity_data.id_unchecked()).await? {
|
|
||||||
return Ok(HttpResponse::Ok().finish());
|
|
||||||
}
|
|
||||||
|
|
||||||
let request_counter = &mut 0;
|
let request_counter = &mut 0;
|
||||||
let actor =
|
let actor =
|
||||||
get_or_fetch_and_upsert_actor(&activity_data.actor, &context, request_counter).await?;
|
get_or_fetch_and_upsert_actor(&activity_data.actor, &context, request_counter).await?;
|
||||||
verify_signature(&request, &actor.public_key().context(location_info!())?)?;
|
verify_signature(&request, &actor.public_key().context(location_info!())?)?;
|
||||||
|
|
||||||
|
// Do nothing if we received the same activity before
|
||||||
|
if is_activity_already_known(context.pool(), activity_data.id_unchecked()).await? {
|
||||||
|
return Ok(HttpResponse::Ok().finish());
|
||||||
|
}
|
||||||
|
check_is_apub_id_valid(&activity_data.actor, false)?;
|
||||||
|
println!(
|
||||||
|
"Verifying activity {}",
|
||||||
|
activity_data.id_unchecked().to_string()
|
||||||
|
);
|
||||||
activity.verify(&context, request_counter).await?;
|
activity.verify(&context, request_counter).await?;
|
||||||
assert_activity_not_local(&activity)?;
|
assert_activity_not_local(&activity)?;
|
||||||
check_is_apub_id_valid(&activity_data.actor, false)?;
|
|
||||||
|
|
||||||
// Log the activity, so we avoid receiving and parsing it twice. Note that this could still happen
|
// Log the activity, so we avoid receiving and parsing it twice. Note that this could still happen
|
||||||
// if we receive the same activity twice in very quick succession.
|
// if we receive the same activity twice in very quick succession.
|
||||||
|
@ -85,6 +91,10 @@ where
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"Receiving activity {}",
|
||||||
|
activity_data.id_unchecked().to_string()
|
||||||
|
);
|
||||||
activity.receive(&context, request_counter).await?;
|
activity.receive(&context, request_counter).await?;
|
||||||
Ok(HttpResponse::Ok().finish())
|
Ok(HttpResponse::Ok().finish())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue