]> Untitled Git - lemmy.git/blob - crates/api_common/src/lib.rs
Move code to generate apub urls into lemmy_api_common
[lemmy.git] / crates / api_common / src / lib.rs
1 pub mod comment;
2 pub mod community;
3 pub mod person;
4 pub mod post;
5 pub mod private_message;
6 #[cfg(feature = "full")]
7 pub mod request;
8 pub mod sensitive;
9 pub mod site;
10 #[cfg(feature = "full")]
11 pub mod utils;
12 pub mod websocket;
13
14 #[macro_use]
15 extern crate strum_macros;
16 pub extern crate lemmy_db_schema;
17 pub extern crate lemmy_db_views;
18 pub extern crate lemmy_db_views_actor;
19 pub extern crate lemmy_db_views_moderator;
20
21 use crate::websocket::chat_server::ChatServer;
22 use actix::Addr;
23 use anyhow::Context;
24 use lemmy_db_schema::{newtypes::DbUrl, source::secret::Secret, utils::DbPool};
25 use lemmy_utils::{
26   error::LemmyError,
27   location_info,
28   rate_limit::RateLimitCell,
29   settings::{structs::Settings, SETTINGS},
30 };
31 use reqwest_middleware::ClientWithMiddleware;
32 use url::{ParseError, Url};
33
34 pub struct LemmyContext {
35   pool: DbPool,
36   chat_server: Addr<ChatServer>,
37   client: ClientWithMiddleware,
38   settings: Settings,
39   secret: Secret,
40   rate_limit_cell: RateLimitCell,
41 }
42
43 impl LemmyContext {
44   pub fn create(
45     pool: DbPool,
46     chat_server: Addr<ChatServer>,
47     client: ClientWithMiddleware,
48     settings: Settings,
49     secret: Secret,
50     settings_updated_channel: RateLimitCell,
51   ) -> LemmyContext {
52     LemmyContext {
53       pool,
54       chat_server,
55       client,
56       settings,
57       secret,
58       rate_limit_cell: settings_updated_channel,
59     }
60   }
61   pub fn pool(&self) -> &DbPool {
62     &self.pool
63   }
64   pub fn chat_server(&self) -> &Addr<ChatServer> {
65     &self.chat_server
66   }
67   pub fn client(&self) -> &ClientWithMiddleware {
68     &self.client
69   }
70   pub fn settings(&self) -> &'static Settings {
71     &SETTINGS
72   }
73   pub fn secret(&self) -> &Secret {
74     &self.secret
75   }
76   pub fn settings_updated_channel(&self) -> &RateLimitCell {
77     &self.rate_limit_cell
78   }
79 }
80
81 impl Clone for LemmyContext {
82   fn clone(&self) -> Self {
83     LemmyContext {
84       pool: self.pool.clone(),
85       chat_server: self.chat_server.clone(),
86       client: self.client.clone(),
87       settings: self.settings.clone(),
88       secret: self.secret.clone(),
89       rate_limit_cell: self.rate_limit_cell.clone(),
90     }
91   }
92 }
93
94 pub enum EndpointType {
95   Community,
96   Person,
97   Post,
98   Comment,
99   PrivateMessage,
100 }
101
102 /// Generates an apub endpoint for a given domain, IE xyz.tld
103 pub fn generate_local_apub_endpoint(
104   endpoint_type: EndpointType,
105   name: &str,
106   domain: &str,
107 ) -> Result<DbUrl, ParseError> {
108   let point = match endpoint_type {
109     EndpointType::Community => "c",
110     EndpointType::Person => "u",
111     EndpointType::Post => "post",
112     EndpointType::Comment => "comment",
113     EndpointType::PrivateMessage => "private_message",
114   };
115
116   Ok(Url::parse(&format!("{}/{}/{}", domain, point, name))?.into())
117 }
118
119 pub fn generate_followers_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
120   Ok(Url::parse(&format!("{}/followers", actor_id))?.into())
121 }
122
123 pub fn generate_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
124   Ok(Url::parse(&format!("{}/inbox", actor_id))?.into())
125 }
126
127 pub fn generate_site_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
128   let mut actor_id: Url = actor_id.clone().into();
129   actor_id.set_path("site_inbox");
130   Ok(actor_id.into())
131 }
132
133 pub fn generate_shared_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, LemmyError> {
134   let actor_id: Url = actor_id.clone().into();
135   let url = format!(
136     "{}://{}{}/inbox",
137     &actor_id.scheme(),
138     &actor_id.host_str().context(location_info!())?,
139     if let Some(port) = actor_id.port() {
140       format!(":{}", port)
141     } else {
142       String::new()
143     },
144   );
145   Ok(Url::parse(&url)?.into())
146 }
147
148 pub fn generate_outbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
149   Ok(Url::parse(&format!("{}/outbox", actor_id))?.into())
150 }
151
152 pub fn generate_moderators_url(community_id: &DbUrl) -> Result<DbUrl, LemmyError> {
153   Ok(Url::parse(&format!("{}/moderators", community_id))?.into())
154 }