8 use crate::site::FederatedInstances;
10 newtypes::{CommunityId, LocalUserId, PersonId, PostId},
13 person_block::PersonBlock,
14 post::{Post, PostRead, PostReadForm},
18 traits::{Crud, Readable},
21 use lemmy_db_views::local_user_view::{LocalUserSettingsView, LocalUserView};
22 use lemmy_db_views_actor::{
23 community_person_ban_view::CommunityPersonBanView,
24 community_view::CommunityView,
26 use lemmy_utils::{claims::Claims, settings::structs::FederationConfig, LemmyError, Sensitive};
29 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
31 F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
34 let pool = pool.clone();
35 let blocking_span = tracing::info_span!("blocking operation");
36 let res = actix_web::web::block(move || {
37 let entered = blocking_span.enter();
38 let conn = pool.get()?;
41 Ok(res) as Result<T, LemmyError>
48 pub async fn is_mod_or_admin(
51 community_id: CommunityId,
52 ) -> Result<(), LemmyError> {
53 let is_mod_or_admin = blocking(pool, move |conn| {
54 CommunityView::is_mod_or_admin(conn, person_id, community_id)
58 return Err(LemmyError::from_message("not_a_mod_or_admin"));
63 pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> {
64 if !local_user_view.person.admin {
65 return Err(LemmyError::from_message("not_an_admin"));
70 pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> {
71 blocking(pool, move |conn| Post::read(conn, post_id))
73 .map_err(LemmyError::from)
74 .map_err(|e| e.with_message("couldnt_find_post"))
77 pub async fn mark_post_as_read(
81 ) -> Result<PostRead, LemmyError> {
82 let post_read_form = PostReadForm { post_id, person_id };
84 blocking(pool, move |conn| {
85 PostRead::mark_as_read(conn, &post_read_form)
88 .map_err(LemmyError::from)
89 .map_err(|e| e.with_message("couldnt_mark_post_as_read"))
92 pub async fn mark_post_as_unread(
96 ) -> Result<usize, LemmyError> {
97 let post_read_form = PostReadForm { post_id, person_id };
99 blocking(pool, move |conn| {
100 PostRead::mark_as_unread(conn, &post_read_form)
103 .map_err(LemmyError::from)
104 .map_err(|e| e.with_message("couldnt_mark_post_as_read"))
107 pub async fn get_local_user_view_from_jwt(
111 ) -> Result<LocalUserView, LemmyError> {
112 let claims = Claims::decode(jwt, &secret.jwt_secret)
113 .map_err(LemmyError::from)
114 .map_err(|e| e.with_message("not_logged_in"))?
116 let local_user_id = LocalUserId(claims.sub);
117 let local_user_view =
118 blocking(pool, move |conn| LocalUserView::read(conn, local_user_id)).await??;
119 // Check for a site ban
120 if local_user_view.person.banned {
121 return Err(LemmyError::from_message("site_ban"));
124 // Check for user deletion
125 if local_user_view.person.deleted {
126 return Err(LemmyError::from_message("deleted"));
129 check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
134 /// Checks if user's token was issued before user's password reset.
135 pub fn check_validator_time(
136 validator_time: &chrono::NaiveDateTime,
138 ) -> Result<(), LemmyError> {
139 let user_validation_time = validator_time.timestamp();
140 if user_validation_time > claims.iat {
141 Err(LemmyError::from_message("not_logged_in"))
147 pub async fn get_local_user_view_from_jwt_opt(
148 jwt: Option<&Sensitive<String>>,
151 ) -> Result<Option<LocalUserView>, LemmyError> {
153 Some(jwt) => Ok(Some(get_local_user_view_from_jwt(jwt, pool, secret).await?)),
158 pub async fn get_local_user_settings_view_from_jwt(
159 jwt: &Sensitive<String>,
162 ) -> Result<LocalUserSettingsView, LemmyError> {
163 let claims = Claims::decode(jwt.as_ref(), &secret.jwt_secret)
164 .map_err(LemmyError::from)
165 .map_err(|e| e.with_message("not_logged_in"))?
167 let local_user_id = LocalUserId(claims.sub);
168 let local_user_view = blocking(pool, move |conn| {
169 LocalUserSettingsView::read(conn, local_user_id)
172 // Check for a site ban
173 if local_user_view.person.banned {
174 return Err(LemmyError::from_message("site_ban"));
177 check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
182 pub async fn get_local_user_settings_view_from_jwt_opt(
183 jwt: Option<&Sensitive<String>>,
186 ) -> Result<Option<LocalUserSettingsView>, LemmyError> {
188 Some(jwt) => Ok(Some(
189 get_local_user_settings_view_from_jwt(jwt, pool, secret).await?,
195 pub async fn check_community_ban(
197 community_id: CommunityId,
199 ) -> Result<(), LemmyError> {
201 move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
202 if blocking(pool, is_banned).await? {
203 Err(LemmyError::from_message("community_ban"))
209 pub async fn check_community_deleted_or_removed(
210 community_id: CommunityId,
212 ) -> Result<(), LemmyError> {
213 let community = blocking(pool, move |conn| Community::read(conn, community_id))
215 .map_err(LemmyError::from)
216 .map_err(|e| e.with_message("couldnt_find_community"))?;
217 if community.deleted || community.removed {
218 Err(LemmyError::from_message("deleted"))
224 pub fn check_post_deleted_or_removed(post: &Post) -> Result<(), LemmyError> {
225 if post.deleted || post.removed {
226 Err(LemmyError::from_message("deleted"))
232 pub async fn check_person_block(
234 potential_blocker_id: PersonId,
236 ) -> Result<(), LemmyError> {
237 let is_blocked = move |conn: &'_ _| PersonBlock::read(conn, potential_blocker_id, my_id).is_ok();
238 if blocking(pool, is_blocked).await? {
239 Err(LemmyError::from_message("person_block"))
245 pub async fn check_downvotes_enabled(score: i16, pool: &DbPool) -> Result<(), LemmyError> {
247 let site = blocking(pool, Site::read_simple).await??;
248 if !site.enable_downvotes {
249 return Err(LemmyError::from_message("downvotes_disabled"));
255 pub async fn build_federated_instances(
257 federation_config: &FederationConfig,
259 ) -> Result<Option<FederatedInstances>, LemmyError> {
260 let federation = federation_config.to_owned();
261 if federation.enabled {
262 let distinct_communities = blocking(pool, move |conn| {
263 Community::distinct_federated_communities(conn)
267 let allowed = federation.allowed_instances;
268 let blocked = federation.blocked_instances;
270 let mut linked = distinct_communities
272 .map(|actor_id| Ok(Url::parse(actor_id)?.host_str().unwrap_or("").to_string()))
273 .collect::<Result<Vec<String>, LemmyError>>()?;
275 if let Some(allowed) = allowed.as_ref() {
276 linked.extend_from_slice(allowed);
279 if let Some(blocked) = blocked.as_ref() {
280 linked.retain(|a| !blocked.contains(a) && !a.eq(hostname));
283 // Sort and remove dupes
284 linked.sort_unstable();
287 Ok(Some(FederatedInstances {
297 /// Checks the password length
298 pub fn password_length_check(pass: &str) -> Result<(), LemmyError> {
299 if !(10..=60).contains(&pass.len()) {
300 Err(LemmyError::from_message("invalid_password"))
306 /// Checks the site description length
307 pub fn site_description_length_check(description: &str) -> Result<(), LemmyError> {
308 if description.len() > 150 {
309 Err(LemmyError::from_message("site_description_length_overflow"))
315 /// Checks for a honeypot. If this field is filled, fail the rest of the function
316 pub fn honeypot_check(honeypot: &Option<String>) -> Result<(), LemmyError> {
317 if honeypot.is_some() {
318 Err(LemmyError::from_message("honeypot_fail"))