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