2 check_community_or_site_ban,
3 check_is_apub_id_valid,
4 fetcher::object_id::ObjectId,
5 generate_moderators_url,
8 use lemmy_api_common::blocking;
9 use lemmy_apub_lib::{traits::ActivityFields, verify::verify_domains_match};
10 use lemmy_db_schema::source::{community::Community, person::Person};
11 use lemmy_db_views_actor::community_view::CommunityView;
12 use lemmy_utils::{settings::structs::Settings, LemmyError};
13 use lemmy_websocket::LemmyContext;
14 use serde::{Deserialize, Serialize};
15 use strum_macros::ToString;
16 use url::{ParseError, Url};
24 pub mod private_message;
29 #[derive(Clone, Debug, ToString, Deserialize, Serialize)]
30 pub enum CreateOrUpdateType {
35 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
36 /// doesn't have a site ban.
37 async fn verify_person(
38 person_id: &ObjectId<Person>,
39 context: &LemmyContext,
40 request_counter: &mut i32,
41 ) -> Result<(), LemmyError> {
42 let person = person_id.dereference(context, request_counter).await?;
44 return Err(anyhow!("Person {} is banned", person_id).into());
49 pub(crate) async fn extract_community(
51 context: &LemmyContext,
52 request_counter: &mut i32,
53 ) -> Result<Community, LemmyError> {
54 let mut cc_iter = cc.iter();
56 if let Some(cid) = cc_iter.next() {
57 let cid = ObjectId::new(cid.clone());
58 if let Ok(c) = cid.dereference(context, request_counter).await {
62 return Err(anyhow!("No community found in cc").into());
67 /// Fetches the person and community to verify their type, then checks if person is banned from site
69 pub(crate) async fn verify_person_in_community(
70 person_id: &ObjectId<Person>,
71 community_id: &ObjectId<Community>,
72 context: &LemmyContext,
73 request_counter: &mut i32,
74 ) -> Result<(), LemmyError> {
75 let community = community_id.dereference(context, request_counter).await?;
76 let person = person_id.dereference(context, request_counter).await?;
77 check_community_or_site_ban(&person, community.id, context.pool()).await
80 /// Simply check that the url actually refers to a valid group.
81 async fn verify_community(
82 community_id: &ObjectId<Community>,
83 context: &LemmyContext,
84 request_counter: &mut i32,
85 ) -> Result<(), LemmyError> {
86 community_id.dereference(context, request_counter).await?;
90 fn verify_activity(activity: &dyn ActivityFields, settings: &Settings) -> Result<(), LemmyError> {
91 check_is_apub_id_valid(activity.actor(), false, settings)?;
92 verify_domains_match(activity.id_unchecked(), activity.actor())?;
96 /// Verify that the actor is a community mod. This check is only run if the community is local,
97 /// because in case of remote communities, admins can also perform mod actions. As admin status
98 /// is not federated, we cant verify their actions remotely.
99 pub(crate) async fn verify_mod_action(
100 actor_id: &ObjectId<Person>,
101 community_id: ObjectId<Community>,
102 context: &LemmyContext,
103 request_counter: &mut i32,
104 ) -> Result<(), LemmyError> {
105 let community = community_id.dereference_local(context).await?;
108 let actor = actor_id.dereference(context, request_counter).await?;
110 // Note: this will also return true for admins in addition to mods, but as we dont know about
111 // remote admins, it doesnt make any difference.
112 let community_id = community.id;
113 let actor_id = actor.id;
114 let is_mod_or_admin = blocking(context.pool(), move |conn| {
115 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
118 if !is_mod_or_admin {
119 return Err(anyhow!("Not a mod").into());
125 /// For Add/Remove community moderator activities, check that the target field actually contains
126 /// /c/community/moderators. Any different values are unsupported.
127 fn verify_add_remove_moderator_target(
129 community: &ObjectId<Community>,
130 ) -> Result<(), LemmyError> {
131 if target != &generate_moderators_url(&community.clone().into())?.into_inner() {
132 return Err(anyhow!("Unkown target url").into());
137 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
138 if community.deleted || community.removed {
139 Err(anyhow!("New post or comment cannot be created in deleted or removed community").into())
145 /// Generate a unique ID for an activity, in the format:
146 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
147 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
152 "{}/activities/{}/{}",
153 protocol_and_hostname,
154 kind.to_string().to_lowercase(),