2 check_is_apub_id_valid,
3 fetcher::object_id::ObjectId,
4 generate_moderators_url,
5 objects::{community::ApubCommunity, person::ApubPerson},
7 use activitystreams::public;
9 use lemmy_api_common::blocking;
10 use lemmy_apub_lib::{traits::ActivityFields, verify::verify_domains_match};
11 use lemmy_db_schema::source::community::Community;
12 use lemmy_db_views_actor::{
13 community_person_ban_view::CommunityPersonBanView,
14 community_view::CommunityView,
16 use lemmy_utils::{settings::structs::Settings, LemmyError};
17 use lemmy_websocket::LemmyContext;
18 use serde::{Deserialize, Serialize};
19 use strum_macros::ToString;
20 use url::{ParseError, Url};
28 pub mod private_message;
32 #[derive(Clone, Debug, ToString, Deserialize, Serialize)]
33 pub enum CreateOrUpdateType {
38 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
39 /// doesn't have a site ban.
40 async fn verify_person(
41 person_id: &ObjectId<ApubPerson>,
42 context: &LemmyContext,
43 request_counter: &mut i32,
44 ) -> Result<(), LemmyError> {
45 let person = person_id.dereference(context, request_counter).await?;
47 return Err(anyhow!("Person {} is banned", person_id).into());
52 /// Fetches the person and community to verify their type, then checks if person is banned from site
54 pub(crate) async fn verify_person_in_community(
55 person_id: &ObjectId<ApubPerson>,
56 community: &ApubCommunity,
57 context: &LemmyContext,
58 request_counter: &mut i32,
59 ) -> Result<(), LemmyError> {
60 let person = person_id.dereference(context, request_counter).await?;
62 return Err(anyhow!("Person is banned from site").into());
64 let person_id = person.id;
65 let community_id = community.id;
67 move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
68 if blocking(context.pool(), is_banned).await? {
69 return Err(anyhow!("Person is banned from community").into());
75 fn verify_activity(activity: &dyn ActivityFields, settings: &Settings) -> Result<(), LemmyError> {
76 check_is_apub_id_valid(activity.actor(), false, settings)?;
77 verify_domains_match(activity.id_unchecked(), activity.actor())?;
81 /// Verify that the actor is a community mod. This check is only run if the community is local,
82 /// because in case of remote communities, admins can also perform mod actions. As admin status
83 /// is not federated, we cant verify their actions remotely.
84 pub(crate) async fn verify_mod_action(
85 actor_id: &ObjectId<ApubPerson>,
86 community: &ApubCommunity,
87 context: &LemmyContext,
88 request_counter: &mut i32,
89 ) -> Result<(), LemmyError> {
91 let actor = actor_id.dereference(context, request_counter).await?;
93 // Note: this will also return true for admins in addition to mods, but as we dont know about
94 // remote admins, it doesnt make any difference.
95 let community_id = community.id;
96 let actor_id = actor.id;
97 let is_mod_or_admin = blocking(context.pool(), move |conn| {
98 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
101 if !is_mod_or_admin {
102 return Err(anyhow!("Not a mod").into());
108 /// For Add/Remove community moderator activities, check that the target field actually contains
109 /// /c/community/moderators. Any different values are unsupported.
110 fn verify_add_remove_moderator_target(
112 community: &ApubCommunity,
113 ) -> Result<(), LemmyError> {
114 if target != &generate_moderators_url(&community.actor_id)?.into_inner() {
115 return Err(anyhow!("Unkown target url").into());
120 pub(crate) fn verify_is_public(to: &[Url]) -> Result<(), LemmyError> {
121 if !to.contains(&public()) {
122 return Err(anyhow!("Object is not public").into());
127 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
128 if community.deleted || community.removed {
129 Err(anyhow!("New post or comment cannot be created in deleted or removed community").into())
135 /// Generate a unique ID for an activity, in the format:
136 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
137 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
142 "{}/activities/{}/{}",
143 protocol_and_hostname,
144 kind.to_string().to_lowercase(),