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};
30 pub mod create_or_update;
35 /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
36 /// doesn't have a site ban.
37 #[tracing::instrument(skip_all)]
38 async fn verify_person(
39 person_id: &ObjectId<ApubPerson>,
40 context: &LemmyContext,
41 request_counter: &mut i32,
42 ) -> Result<(), LemmyError> {
43 let person = person_id
44 .dereference(context, context.client(), request_counter)
47 let error = LemmyError::from(anyhow::anyhow!("Person {} is banned", person_id));
48 return Err(error.with_message("banned"));
53 /// Fetches the person and community to verify their type, then checks if person is banned from site
55 #[tracing::instrument(skip_all)]
56 pub(crate) async fn verify_person_in_community(
57 person_id: &ObjectId<ApubPerson>,
58 community: &ApubCommunity,
59 context: &LemmyContext,
60 request_counter: &mut i32,
61 ) -> Result<(), LemmyError> {
62 let person = person_id
63 .dereference(context, context.client(), request_counter)
66 return Err(LemmyError::from_message("Person is banned from site"));
68 let person_id = person.id;
69 let community_id = community.id;
71 move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
72 if blocking(context.pool(), is_banned).await? {
73 return Err(LemmyError::from_message("Person is banned from community"));
79 fn verify_activity(id: &Url, actor: &Url, settings: &Settings) -> Result<(), LemmyError> {
80 check_is_apub_id_valid(actor, false, settings)?;
81 verify_domains_match(id, actor)?;
85 /// Verify that the actor is a community mod. This check is only run if the community is local,
86 /// because in case of remote communities, admins can also perform mod actions. As admin status
87 /// is not federated, we cant verify their actions remotely.
88 #[tracing::instrument(skip_all)]
89 pub(crate) async fn verify_mod_action(
90 actor_id: &ObjectId<ApubPerson>,
91 community: &ApubCommunity,
92 context: &LemmyContext,
93 request_counter: &mut i32,
94 ) -> Result<(), LemmyError> {
97 .dereference(context, context.client(), request_counter)
100 // Note: this will also return true for admins in addition to mods, but as we dont know about
101 // remote admins, it doesnt make any difference.
102 let community_id = community.id;
103 let actor_id = actor.id;
104 let is_mod_or_admin = blocking(context.pool(), move |conn| {
105 CommunityView::is_mod_or_admin(conn, actor_id, community_id)
108 if !is_mod_or_admin {
109 return Err(LemmyError::from_message("Not a mod"));
115 /// For Add/Remove community moderator activities, check that the target field actually contains
116 /// /c/community/moderators. Any different values are unsupported.
117 fn verify_add_remove_moderator_target(
119 community: &ApubCommunity,
120 ) -> Result<(), LemmyError> {
121 if target != &generate_moderators_url(&community.actor_id)?.into() {
122 return Err(LemmyError::from_message("Unkown target url"));
127 pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
128 if ![to, cc].iter().any(|set| set.contains(&public())) {
129 return Err(LemmyError::from_message("Object is not public"));
134 pub(crate) fn check_community_deleted_or_removed(community: &Community) -> Result<(), LemmyError> {
135 if community.deleted || community.removed {
136 Err(LemmyError::from_message(
137 "New post or comment cannot be created in deleted or removed community",
144 /// Generate a unique ID for an activity, in the format:
145 /// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`
146 fn generate_activity_id<T>(kind: T, protocol_and_hostname: &str) -> Result<Url, ParseError>
151 "{}/activities/{}/{}",
152 protocol_and_hostname,
153 kind.to_string().to_lowercase(),
159 #[tracing::instrument(skip_all)]
160 async fn send_lemmy_activity<T: Serialize>(
161 context: &LemmyContext,
164 actor: &dyn ActorType,
167 ) -> Result<(), LemmyError> {
168 if !context.settings().federation.enabled || inboxes.is_empty() {
171 let activity = WithContext::new(activity);
173 info!("Sending activity {}", activity_id.to_string());
175 // Don't send anything to ourselves
176 // TODO: this should be a debug assert
177 let hostname = context.settings().get_hostname_without_port()?;
178 let inboxes: Vec<&Url> = inboxes
180 .filter(|i| i.domain().expect("valid inbox url") != hostname)
183 let serialised_activity = serde_json::to_string(&activity)?;
185 let object_value = serde_json::to_value(&activity)?;
186 insert_activity(activity_id, object_value, true, sensitive, context.pool()).await?;
194 context.activity_queue(),