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;
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 pub(crate) async fn extract_community(
54 context: &LemmyContext,
55 request_counter: &mut i32,
56 ) -> Result<ApubCommunity, LemmyError> {
57 let mut cc_iter = cc.iter();
59 if let Some(cid) = cc_iter.next() {
60 let cid = ObjectId::new(cid.clone());
61 if let Ok(c) = cid.dereference(context, request_counter).await {
65 return Err(anyhow!("No community found in cc").into());
70 /// Fetches the person and community to verify their type, then checks if person is banned from site
72 pub(crate) async fn verify_person_in_community(
73 person_id: &ObjectId<ApubPerson>,
74 community_id: &ObjectId<ApubCommunity>,
75 context: &LemmyContext,
76 request_counter: &mut i32,
77 ) -> Result<(), LemmyError> {
78 let community = community_id.dereference(context, request_counter).await?;
79 let person = person_id.dereference(context, request_counter).await?;
80 check_community_or_site_ban(person.deref(), community.id, context.pool()).await
83 /// Simply check that the url actually refers to a valid group.
84 async fn verify_community(
85 community_id: &ObjectId<ApubCommunity>,
86 context: &LemmyContext,
87 request_counter: &mut i32,
88 ) -> Result<(), LemmyError> {
89 community_id.dereference(context, request_counter).await?;
93 fn verify_activity(activity: &dyn ActivityFields, settings: &Settings) -> Result<(), LemmyError> {
94 check_is_apub_id_valid(activity.actor(), false, settings)?;
95 verify_domains_match(activity.id_unchecked(), activity.actor())?;
99 /// Verify that the actor is a community mod. This check is only run if the community is local,
100 /// because in case of remote communities, admins can also perform mod actions. As admin status
101 /// is not federated, we cant verify their actions remotely.
102 pub(crate) async fn verify_mod_action(
103 actor_id: &ObjectId<ApubPerson>,
104 community_id: &ObjectId<ApubCommunity>,
105 context: &LemmyContext,
106 request_counter: &mut i32,
107 ) -> Result<(), LemmyError> {
108 let community = community_id.dereference_local(context).await?;
111 let actor = actor_id.dereference(context, request_counter).await?;
113 // Note: this will also return true for admins in addition to mods, but as we dont know about
114 // remote admins, it doesnt make any difference.
115 let community_id = community.id;
116 let actor_id = actor.id;
117 let is_mod_or_admin = blocking(context.pool(), move |conn| {
118 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
121 if !is_mod_or_admin {
122 return Err(anyhow!("Not a mod").into());
128 /// For Add/Remove community moderator activities, check that the target field actually contains
129 /// /c/community/moderators. Any different values are unsupported.
130 fn verify_add_remove_moderator_target(
132 community: &ObjectId<ApubCommunity>,
133 ) -> Result<(), LemmyError> {
134 if target != &generate_moderators_url(&community.clone().into())?.into_inner() {
135 return Err(anyhow!("Unkown target url").into());
140 pub(crate) fn verify_is_public(to: &[Url]) -> Result<(), LemmyError> {
141 if !to.contains(&public()) {
142 return Err(anyhow!("Object is not public").into());
147 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
148 if community.deleted || community.removed {
149 Err(anyhow!("New post or comment cannot be created in deleted or removed community").into())
155 /// Generate a unique ID for an activity, in the format:
156 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
157 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
162 "{}/activities/{}/{}",
163 protocol_and_hostname,
164 kind.to_string().to_lowercase(),