2 check_is_apub_id_valid,
4 generate_moderators_url,
6 objects::{community::ApubCommunity, person::ApubPerson},
8 use activitystreams_kinds::public;
9 use lemmy_api_common::blocking;
11 activity_queue::send_activity,
14 verify::verify_domains_match,
16 use lemmy_db_schema::source::community::Community;
17 use lemmy_db_views_actor::{
18 community_person_ban_view::CommunityPersonBanView,
19 community_view::CommunityView,
21 use lemmy_utils::{settings::structs::Settings, LemmyError};
22 use lemmy_websocket::LemmyContext;
25 use url::{ParseError, Url};
34 pub mod private_message;
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 #[tracing::instrument(skip_all)]
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
46 .dereference(context, context.client(), request_counter)
49 let error = LemmyError::from(anyhow::anyhow!("Person {} is banned", person_id));
50 return Err(error.with_message("banned"));
55 /// Fetches the person and community to verify their type, then checks if person is banned from site
57 #[tracing::instrument(skip_all)]
58 pub(crate) async fn verify_person_in_community(
59 person_id: &ObjectId<ApubPerson>,
60 community: &ApubCommunity,
61 context: &LemmyContext,
62 request_counter: &mut i32,
63 ) -> Result<(), LemmyError> {
64 let person = person_id
65 .dereference(context, context.client(), request_counter)
68 return Err(LemmyError::from_message("Person is banned from site"));
70 let person_id = person.id;
71 let community_id = community.id;
73 move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
74 if blocking(context.pool(), is_banned).await? {
75 return Err(LemmyError::from_message("Person is banned from community"));
81 fn verify_activity(id: &Url, actor: &Url, settings: &Settings) -> Result<(), LemmyError> {
82 check_is_apub_id_valid(actor, false, settings)?;
83 verify_domains_match(id, actor)?;
87 /// Verify that the actor is a community mod. This check is only run if the community is local,
88 /// because in case of remote communities, admins can also perform mod actions. As admin status
89 /// is not federated, we cant verify their actions remotely.
90 #[tracing::instrument(skip_all)]
91 pub(crate) async fn verify_mod_action(
92 actor_id: &ObjectId<ApubPerson>,
93 community: &ApubCommunity,
94 context: &LemmyContext,
95 request_counter: &mut i32,
96 ) -> Result<(), LemmyError> {
99 .dereference(context, context.client(), request_counter)
102 // Note: this will also return true for admins in addition to mods, but as we dont know about
103 // remote admins, it doesnt make any difference.
104 let community_id = community.id;
105 let actor_id = actor.id;
106 let is_mod_or_admin = blocking(context.pool(), move |conn| {
107 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
110 if !is_mod_or_admin {
111 return Err(LemmyError::from_message("Not a mod"));
117 /// For Add/Remove community moderator activities, check that the target field actually contains
118 /// /c/community/moderators. Any different values are unsupported.
119 fn verify_add_remove_moderator_target(
121 community: &ApubCommunity,
122 ) -> Result<(), LemmyError> {
123 if target != &generate_moderators_url(&community.actor_id)?.into() {
124 return Err(LemmyError::from_message("Unkown target url"));
129 pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
130 if ![to, cc].iter().any(|set| set.contains(&public())) {
131 return Err(LemmyError::from_message("Object is not public"));
136 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
137 if community.deleted || community.removed {
138 Err(LemmyError::from_message(
139 "New post or comment cannot be created in deleted or removed community",
146 /// Generate a unique ID for an activity, in the format:
147 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
148 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
153 "{}/activities/{}/{}",
154 protocol_and_hostname,
155 kind.to_string().to_lowercase(),
161 #[tracing::instrument(skip_all)]
162 async fn send_lemmy_activity<T: Serialize>(
163 context: &LemmyContext,
166 actor: &dyn ActorType,
169 ) -> Result<(), LemmyError> {
170 if !context.settings().federation.enabled || inboxes.is_empty() {
173 let activity = WithContext::new(activity);
175 info!("Sending activity {}", activity_id.to_string());
177 // Don't send anything to ourselves
178 // TODO: this should be a debug assert
179 let hostname = context.settings().get_hostname_without_port()?;
180 let inboxes: Vec<&Url> = inboxes
182 .filter(|i| i.domain().expect("valid inbox url") != hostname)
185 let serialised_activity = serde_json::to_string(&activity)?;
187 let object_value = serde_json::to_value(&activity)?;
188 insert_activity(activity_id, object_value, true, sensitive, context.pool()).await?;
196 context.activity_queue(),