2021-07-17 16:20:44 +00:00
|
|
|
use crate::{
|
2021-07-27 22:18:50 +00:00
|
|
|
activities::{
|
2021-10-14 16:33:19 +00:00
|
|
|
check_community_deleted_or_removed,
|
2022-12-01 20:52:49 +00:00
|
|
|
community::send_activity_in_community,
|
2021-07-27 22:18:50 +00:00
|
|
|
generate_activity_id,
|
2021-10-25 17:22:34 +00:00
|
|
|
verify_is_public,
|
2021-07-31 17:26:17 +00:00
|
|
|
verify_mod_action,
|
2021-07-27 22:18:50 +00:00
|
|
|
verify_person_in_community,
|
|
|
|
},
|
2021-10-29 10:32:42 +00:00
|
|
|
activity_lists::AnnouncableActivities,
|
2023-03-21 15:03:05 +00:00
|
|
|
insert_activity,
|
2021-10-28 21:17:59 +00:00
|
|
|
objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
|
2022-12-01 20:52:49 +00:00
|
|
|
protocol::{
|
|
|
|
activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
|
|
|
|
InCommunity,
|
|
|
|
},
|
2022-11-28 14:29:33 +00:00
|
|
|
SendActivity,
|
2021-07-27 22:18:50 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
kinds::public,
|
|
|
|
protocol::verification::{verify_domains_match, verify_urls_match},
|
|
|
|
traits::{ActivityHandler, Actor, Object},
|
2021-11-03 17:33:51 +00:00
|
|
|
};
|
2022-11-26 02:04:46 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2023-02-18 14:50:28 +00:00
|
|
|
post::{CreatePost, EditPost, PostResponse},
|
2022-11-26 02:04:46 +00:00
|
|
|
};
|
2022-04-07 20:46:10 +00:00
|
|
|
use lemmy_db_schema::{
|
2023-06-20 08:52:51 +00:00
|
|
|
aggregates::structs::PostAggregates,
|
2022-11-28 14:29:33 +00:00
|
|
|
newtypes::PersonId,
|
2022-04-07 20:46:10 +00:00
|
|
|
source::{
|
|
|
|
community::Community,
|
2022-11-28 14:29:33 +00:00
|
|
|
person::Person,
|
|
|
|
post::{Post, PostLike, PostLikeForm},
|
2022-04-07 20:46:10 +00:00
|
|
|
},
|
|
|
|
traits::{Crud, Likeable},
|
|
|
|
};
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorType};
|
2022-06-02 14:33:41 +00:00
|
|
|
use url::Url;
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-11-28 14:29:33 +00:00
|
|
|
impl SendActivity for CreatePost {
|
|
|
|
type Response = PostResponse;
|
|
|
|
|
|
|
|
async fn send_activity(
|
|
|
|
_request: &Self,
|
|
|
|
response: &Self::Response,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2022-11-28 14:29:33 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
CreateOrUpdatePage::send(
|
|
|
|
&response.post_view.post,
|
|
|
|
response.post_view.creator.id,
|
|
|
|
CreateOrUpdateType::Create,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-11-28 14:29:33 +00:00
|
|
|
impl SendActivity for EditPost {
|
|
|
|
type Response = PostResponse;
|
|
|
|
|
|
|
|
async fn send_activity(
|
|
|
|
_request: &Self,
|
|
|
|
response: &Self::Response,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2022-11-28 14:29:33 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
CreateOrUpdatePage::send(
|
|
|
|
&response.post_view.post,
|
|
|
|
response.post_view.creator.id,
|
|
|
|
CreateOrUpdateType::Update,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 20:52:49 +00:00
|
|
|
impl CreateOrUpdatePage {
|
2021-10-27 16:03:07 +00:00
|
|
|
pub(crate) async fn new(
|
2021-11-06 12:37:55 +00:00
|
|
|
post: ApubPost,
|
2021-10-18 21:36:44 +00:00
|
|
|
actor: &ApubPerson,
|
2021-10-27 16:03:07 +00:00
|
|
|
community: &ApubCommunity,
|
2021-07-31 17:26:17 +00:00
|
|
|
kind: CreateOrUpdateType,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2022-12-01 20:52:49 +00:00
|
|
|
) -> Result<CreateOrUpdatePage, LemmyError> {
|
2021-09-22 15:57:09 +00:00
|
|
|
let id = generate_activity_id(
|
|
|
|
kind.clone(),
|
|
|
|
&context.settings().get_protocol_and_hostname(),
|
|
|
|
)?;
|
2022-12-01 20:52:49 +00:00
|
|
|
Ok(CreateOrUpdatePage {
|
2023-03-21 15:03:05 +00:00
|
|
|
actor: actor.id().into(),
|
2021-10-25 17:22:34 +00:00
|
|
|
to: vec![public()],
|
2023-03-21 15:03:05 +00:00
|
|
|
object: post.into_json(context).await?,
|
|
|
|
cc: vec![community.id()],
|
2021-07-31 17:26:17 +00:00
|
|
|
kind,
|
2021-08-19 21:24:33 +00:00
|
|
|
id: id.clone(),
|
2023-03-21 15:03:05 +00:00
|
|
|
audience: Some(community.id().into()),
|
2021-10-27 16:03:07 +00:00
|
|
|
})
|
|
|
|
}
|
2021-12-06 14:54:47 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2023-02-18 14:50:28 +00:00
|
|
|
pub(crate) async fn send(
|
2022-11-28 14:29:33 +00:00
|
|
|
post: &Post,
|
|
|
|
person_id: PersonId,
|
2021-10-27 16:03:07 +00:00
|
|
|
kind: CreateOrUpdateType,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2021-10-27 16:03:07 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2022-11-28 14:29:33 +00:00
|
|
|
let post = ApubPost(post.clone());
|
2021-10-27 16:03:07 +00:00
|
|
|
let community_id = post.community_id;
|
2022-11-28 14:29:33 +00:00
|
|
|
let person: ApubPerson = Person::read(context.pool(), person_id).await?.into();
|
2022-11-09 10:05:00 +00:00
|
|
|
let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
|
2021-11-11 19:49:15 +00:00
|
|
|
|
2022-11-28 14:29:33 +00:00
|
|
|
let create_or_update =
|
|
|
|
CreateOrUpdatePage::new(post, &person, &community, kind, context).await?;
|
2022-11-23 23:40:47 +00:00
|
|
|
let is_mod_action = create_or_update.object.is_mod_action(context).await?;
|
2022-11-16 22:51:05 +00:00
|
|
|
let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
|
2022-11-28 14:29:33 +00:00
|
|
|
send_activity_in_community(
|
|
|
|
activity,
|
|
|
|
&person,
|
|
|
|
&community,
|
|
|
|
vec![],
|
|
|
|
is_mod_action,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-11-12 13:52:57 +00:00
|
|
|
Ok(())
|
2021-07-27 22:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-12-01 20:52:49 +00:00
|
|
|
impl ActivityHandler for CreateOrUpdatePage {
|
2021-10-06 20:20:05 +00:00
|
|
|
type DataType = LemmyContext;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
|
|
|
|
|
|
|
fn id(&self) -> &Url {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn actor(&self) -> &Url {
|
|
|
|
self.actor.inner()
|
|
|
|
}
|
2021-12-06 14:54:47 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
|
2021-11-06 17:44:34 +00:00
|
|
|
verify_is_public(&self.to, &self.cc)?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let community = self.community(context).await?;
|
|
|
|
verify_person_in_community(&self.actor, &community, context).await?;
|
2021-10-14 16:33:19 +00:00
|
|
|
check_community_deleted_or_removed(&community)?;
|
|
|
|
|
2021-07-31 17:26:17 +00:00
|
|
|
match self.kind {
|
|
|
|
CreateOrUpdateType::Create => {
|
2021-11-03 16:26:09 +00:00
|
|
|
verify_domains_match(self.actor.inner(), self.object.id.inner())?;
|
2022-05-06 23:53:33 +00:00
|
|
|
verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
|
2023-06-07 19:18:17 +00:00
|
|
|
// Check that the post isnt locked, as that isnt possible for newly created posts.
|
2021-07-31 17:26:17 +00:00
|
|
|
// However, when fetching a remote post we generate a new create activity with the current
|
2023-06-07 19:18:17 +00:00
|
|
|
// locked value, so this check may fail. So only check if its a local community,
|
2021-07-31 17:26:17 +00:00
|
|
|
// because then we will definitely receive all create and update activities separately.
|
2023-06-07 19:18:17 +00:00
|
|
|
let is_locked = self.object.comments_enabled == Some(false);
|
|
|
|
if community.local && is_locked {
|
2023-07-10 14:50:07 +00:00
|
|
|
return Err(LemmyErrorType::NewPostCannotBeLocked)?;
|
2021-07-31 17:26:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CreateOrUpdateType::Update => {
|
2021-10-06 20:20:05 +00:00
|
|
|
let is_mod_action = self.object.is_mod_action(context).await?;
|
2021-07-31 17:26:17 +00:00
|
|
|
if is_mod_action {
|
2023-03-21 15:03:05 +00:00
|
|
|
verify_mod_action(&self.actor, self.object.id.inner(), community.id, context).await?;
|
2021-07-31 17:26:17 +00:00
|
|
|
} else {
|
2021-11-03 16:26:09 +00:00
|
|
|
verify_domains_match(self.actor.inner(), self.object.id.inner())?;
|
2022-05-06 23:53:33 +00:00
|
|
|
verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
|
2021-07-31 17:26:17 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-27 22:18:50 +00:00
|
|
|
}
|
2023-03-21 15:03:05 +00:00
|
|
|
ApubPost::verify(&self.object, self.actor.inner(), context).await?;
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
|
|
|
|
insert_activity(&self.id, &self, false, false, context).await?;
|
|
|
|
let post = ApubPost::from_json(self.object, context).await?;
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
|
2022-04-07 20:46:10 +00:00
|
|
|
// author likes their own post by default
|
|
|
|
let like_form = PostLikeForm {
|
|
|
|
post_id: post.id,
|
|
|
|
person_id: post.creator_id,
|
|
|
|
score: 1,
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
PostLike::like(context.pool(), &like_form).await?;
|
2023-06-20 08:52:51 +00:00
|
|
|
|
|
|
|
// Calculate initial hot_rank for post
|
|
|
|
PostAggregates::update_hot_rank(context.pool(), post.id).await?;
|
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
Ok(())
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
}
|
|
|
|
}
|