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_id: &ObjectId<ApubCommunity>,
87 context: &LemmyContext,
88 request_counter: &mut i32,
89 ) -> Result<(), LemmyError> {
90 let community = community_id.dereference_local(context).await?;
93 let actor = actor_id.dereference(context, request_counter).await?;
95 // Note: this will also return true for admins in addition to mods, but as we dont know about
96 // remote admins, it doesnt make any difference.
97 let community_id = community.id;
98 let actor_id = actor.id;
99 let is_mod_or_admin = blocking(context.pool(), move |conn| {
100 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
103 if !is_mod_or_admin {
104 return Err(anyhow!("Not a mod").into());
110 /// For Add/Remove community moderator activities, check that the target field actually contains
111 /// /c/community/moderators. Any different values are unsupported.
112 fn verify_add_remove_moderator_target(
114 community: &ObjectId<ApubCommunity>,
115 ) -> Result<(), LemmyError> {
116 if target != &generate_moderators_url(&community.clone().into())?.into_inner() {
117 return Err(anyhow!("Unkown target url").into());
122 pub(crate) fn verify_is_public(to: &[Url]) -> Result<(), LemmyError> {
123 if !to.contains(&public()) {
124 return Err(anyhow!("Object is not public").into());
129 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
130 if community.deleted || community.removed {
131 Err(anyhow!("New post or comment cannot be created in deleted or removed community").into())
137 /// Generate a unique ID for an activity, in the format:
138 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
139 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
144 "{}/activities/{}/{}",
145 protocol_and_hostname,
146 kind.to_string().to_lowercase(),