2 use lemmy_api_common::blocking;
4 check_community_or_site_ban,
5 check_is_apub_id_valid,
6 fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
7 generate_moderators_url,
9 use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields};
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::LemmyError;
17 use lemmy_websocket::LemmyContext;
25 pub mod private_message;
29 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
30 /// doesn't have a site ban.
31 async fn verify_person(
33 context: &LemmyContext,
34 request_counter: &mut i32,
35 ) -> Result<(), LemmyError> {
36 let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
38 return Err(anyhow!("Person {} is banned", person_id).into());
43 /// Fetches the person and community to verify their type, then checks if person is banned from site
45 async fn verify_person_in_community(
48 context: &LemmyContext,
49 request_counter: &mut i32,
50 ) -> Result<Community, LemmyError> {
51 let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
52 let mut cc_iter = cc.iter();
53 let community: Community = loop {
54 if let Some(cid) = cc_iter.next() {
55 if let Ok(c) = get_or_fetch_and_upsert_community(cid, context, request_counter).await {
59 return Err(anyhow!("No community found in cc").into());
62 check_community_or_site_ban(&person, community.id, context.pool()).await?;
66 /// Simply check that the url actually refers to a valid group.
67 async fn verify_community(
69 context: &LemmyContext,
70 request_counter: &mut i32,
71 ) -> Result<(), LemmyError> {
72 get_or_fetch_and_upsert_community(community_id, context, request_counter).await?;
76 fn verify_activity(common: &ActivityCommonFields) -> Result<(), LemmyError> {
77 check_is_apub_id_valid(&common.actor, false)?;
78 verify_domains_match(common.id_unchecked(), &common.actor)?;
82 async fn verify_mod_action(
85 context: &LemmyContext,
86 ) -> Result<(), LemmyError> {
87 let community = blocking(context.pool(), move |conn| {
88 Community::read_from_apub_id(conn, &activity_cc.into())
93 let actor_id: DbUrl = actor_id.clone().into();
94 let actor = blocking(context.pool(), move |conn| {
95 Person::read_from_apub_id(conn, &actor_id)
99 // Note: this will also return true for admins in addition to mods, but as we dont know about
100 // remote admins, it doesnt make any difference.
101 let community_id = community.id;
102 let actor_id = actor.id;
103 let is_mod_or_admin = blocking(context.pool(), move |conn| {
104 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
107 if !is_mod_or_admin {
108 return Err(anyhow!("Not a mod").into());
114 /// For Add/Remove community moderator activities, check that the target field actually contains
115 /// /c/community/moderators. Any different values are unsupported.
116 fn verify_add_remove_moderator_target(target: &Url, community: Url) -> Result<(), LemmyError> {
117 if target != &generate_moderators_url(&community.into())?.into_inner() {
118 return Err(anyhow!("Unkown target url").into());