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::{verify_domains_match, ActivityFields};
10 use lemmy_db_queries::ApubObject;
11 use lemmy_db_schema::{
12 source::{community::Community, person::Person},
15 use lemmy_db_views_actor::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;
33 #[derive(Clone, Debug, ToString, Deserialize, Serialize)]
34 pub enum CreateOrUpdateType {
39 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
40 /// doesn't have a site ban.
41 async fn verify_person(
42 person_id: &ObjectId<Person>,
43 context: &LemmyContext,
44 request_counter: &mut i32,
45 ) -> Result<(), LemmyError> {
46 let person = person_id.dereference(context, request_counter).await?;
48 return Err(anyhow!("Person {} is banned", person_id).into());
53 pub(crate) async fn extract_community(
55 context: &LemmyContext,
56 request_counter: &mut i32,
57 ) -> Result<Community, LemmyError> {
58 let mut cc_iter = cc.iter();
60 if let Some(cid) = cc_iter.next() {
61 let cid = ObjectId::new(cid.clone());
62 if let Ok(c) = cid.dereference(context, request_counter).await {
66 return Err(anyhow!("No community found in cc").into());
71 /// Fetches the person and community to verify their type, then checks if person is banned from site
73 pub(crate) async fn verify_person_in_community(
74 person_id: &ObjectId<Person>,
75 community_id: &ObjectId<Community>,
76 context: &LemmyContext,
77 request_counter: &mut i32,
78 ) -> Result<(), LemmyError> {
79 let community = community_id.dereference(context, request_counter).await?;
80 let person = person_id.dereference(context, request_counter).await?;
81 check_community_or_site_ban(&person, community.id, context.pool()).await
84 /// Simply check that the url actually refers to a valid group.
85 async fn verify_community(
86 community_id: &ObjectId<Community>,
87 context: &LemmyContext,
88 request_counter: &mut i32,
89 ) -> Result<(), LemmyError> {
90 community_id.dereference(context, request_counter).await?;
94 fn verify_activity(activity: &dyn ActivityFields) -> Result<(), LemmyError> {
95 check_is_apub_id_valid(activity.actor(), false)?;
96 verify_domains_match(activity.id_unchecked(), activity.actor())?;
100 /// Verify that the actor is a community mod. This check is only run if the community is local,
101 /// because in case of remote communities, admins can also perform mod actions. As admin status
102 /// is not federated, we cant verify their actions remotely.
103 pub(crate) async fn verify_mod_action(
104 actor_id: &ObjectId<Person>,
105 community_id: ObjectId<Community>,
106 context: &LemmyContext,
107 ) -> Result<(), LemmyError> {
108 let community = blocking(context.pool(), move |conn| {
109 Community::read_from_apub_id(conn, &community_id.into())
114 let actor_id: DbUrl = actor_id.clone().into();
115 let actor = blocking(context.pool(), move |conn| {
116 Person::read_from_apub_id(conn, &actor_id)
120 // Note: this will also return true for admins in addition to mods, but as we dont know about
121 // remote admins, it doesnt make any difference.
122 let community_id = community.id;
123 let actor_id = actor.id;
124 let is_mod_or_admin = blocking(context.pool(), move |conn| {
125 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
128 if !is_mod_or_admin {
129 return Err(anyhow!("Not a mod").into());
135 /// For Add/Remove community moderator activities, check that the target field actually contains
136 /// /c/community/moderators. Any different values are unsupported.
137 fn verify_add_remove_moderator_target(
139 community: &ObjectId<Community>,
140 ) -> Result<(), LemmyError> {
141 if target != &generate_moderators_url(&community.clone().into())?.into_inner() {
142 return Err(anyhow!("Unkown target url").into());
147 /// Generate a unique ID for an activity, in the format:
148 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
149 fn generate_activity_id<T>(kind: T) -> Result<Url, ParseError>
154 "{}/activities/{}/{}",
155 Settings::get().get_protocol_and_hostname(),
156 kind.to_string().to_lowercase(),