3 objects::{community::ApubCommunity, person::ApubPerson},
6 use activitypub_federation::{
7 activity_queue::send_activity,
9 fetch::object_id::ObjectId,
11 protocol::context::WithContext,
12 traits::{ActivityHandler, Actor},
15 use lemmy_api_common::context::LemmyContext;
16 use lemmy_db_schema::{newtypes::CommunityId, source::community::Community};
17 use lemmy_db_views_actor::structs::{CommunityPersonBanView, CommunityView};
18 use lemmy_utils::error::LemmyError;
22 use url::{ParseError, Url};
27 pub mod create_or_update;
33 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
34 /// doesn't have a site ban.
35 #[tracing::instrument(skip_all)]
36 async fn verify_person(
37 person_id: &ObjectId<ApubPerson>,
38 context: &Data<LemmyContext>,
39 ) -> Result<(), LemmyError> {
40 let person = person_id.dereference(context).await?;
42 let err = anyhow!("Person {} is banned", person_id);
43 return Err(LemmyError::from_error_message(err, "banned"));
48 /// Fetches the person and community to verify their type, then checks if person is banned from site
50 #[tracing::instrument(skip_all)]
51 pub(crate) async fn verify_person_in_community(
52 person_id: &ObjectId<ApubPerson>,
53 community: &ApubCommunity,
54 context: &Data<LemmyContext>,
55 ) -> Result<(), LemmyError> {
56 let person = person_id.dereference(context).await?;
58 return Err(LemmyError::from_message("Person is banned from site"));
60 let person_id = person.id;
61 let community_id = community.id;
62 let is_banned = CommunityPersonBanView::get(context.pool(), person_id, community_id)
66 return Err(LemmyError::from_message("Person is banned from community"));
72 /// Verify that mod action in community was performed by a moderator.
74 /// * `mod_id` - Activitypub ID of the mod or admin who performed the action
75 /// * `object_id` - Activitypub ID of the actor or object that is being moderated
76 /// * `community` - The community inside which moderation is happening
77 #[tracing::instrument(skip_all)]
78 pub(crate) async fn verify_mod_action(
79 mod_id: &ObjectId<ApubPerson>,
81 community_id: CommunityId,
82 context: &Data<LemmyContext>,
83 ) -> Result<(), LemmyError> {
84 let mod_ = mod_id.dereference(context).await?;
87 CommunityView::is_mod_or_admin(context.pool(), mod_.id, community_id).await?;
92 // mod action comes from the same instance as the moderated object, so it was presumably done
93 // by an instance admin.
94 // TODO: federate instance admin status and check it here
95 if mod_id.inner().domain() == object_id.domain() {
99 Err(LemmyError::from_message("Not a mod"))
102 pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
103 if ![to, cc].iter().any(|set| set.contains(&public())) {
104 return Err(LemmyError::from_message("Object is not public"));
109 pub(crate) fn verify_community_matches<T>(
110 a: &ObjectId<ApubCommunity>,
112 ) -> Result<(), LemmyError>
114 T: Into<ObjectId<ApubCommunity>>,
116 let b: ObjectId<ApubCommunity> = b.into();
118 return Err(LemmyError::from_message("Invalid community"));
123 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
124 if community.deleted || community.removed {
125 Err(LemmyError::from_message(
126 "New post or comment cannot be created in deleted or removed community",
133 /// Generate a unique ID for an activity, in the format:
134 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
135 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
140 "{}/activities/{}/{}",
141 protocol_and_hostname,
142 kind.to_string().to_lowercase(),
148 #[tracing::instrument(skip_all)]
149 async fn send_lemmy_activity<Activity, ActorT>(
150 data: &Data<LemmyContext>,
155 ) -> Result<(), LemmyError>
157 Activity: ActivityHandler + Serialize + Send + Sync + Clone,
159 Activity: ActivityHandler<Error = LemmyError>,
161 info!("Sending activity {}", activity.id().to_string());
162 let activity = WithContext::new(activity, CONTEXT.deref().clone());
164 insert_activity(activity.id(), &activity, true, sensitive, data).await?;
165 send_activity(activity, actor, inbox, data).await?;