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, ApiError, LemmyError};
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 res = actix_web::web::block(move || {
36 let conn = pool.get()?;
38 Ok(res) as Result<T, LemmyError>
45 pub async fn is_mod_or_admin(
48 community_id: CommunityId,
49 ) -> Result<(), LemmyError> {
50 let is_mod_or_admin = blocking(pool, move |conn| {
51 CommunityView::is_mod_or_admin(conn, person_id, community_id)
55 return Err(ApiError::err_plain("not_a_mod_or_admin").into());
60 pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> {
61 if !local_user_view.person.admin {
62 return Err(ApiError::err_plain("not_an_admin").into());
67 pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> {
68 blocking(pool, move |conn| Post::read(conn, post_id))
70 .map_err(|_| ApiError::err_plain("couldnt_find_post").into())
73 pub async fn mark_post_as_read(
77 ) -> Result<PostRead, LemmyError> {
78 let post_read_form = PostReadForm { post_id, person_id };
80 blocking(pool, move |conn| {
81 PostRead::mark_as_read(conn, &post_read_form)
84 .map_err(|e| ApiError::err("couldnt_mark_post_as_read", e).into())
87 pub async fn mark_post_as_unread(
91 ) -> Result<usize, LemmyError> {
92 let post_read_form = PostReadForm { post_id, person_id };
94 blocking(pool, move |conn| {
95 PostRead::mark_as_unread(conn, &post_read_form)
98 .map_err(|e| ApiError::err("couldnt_mark_post_as_read", e).into())
101 pub async fn get_local_user_view_from_jwt(
105 ) -> Result<LocalUserView, LemmyError> {
106 let claims = Claims::decode(jwt, &secret.jwt_secret)
107 .map_err(|e| ApiError::err("not_logged_in", e))?
109 let local_user_id = LocalUserId(claims.sub);
110 let local_user_view =
111 blocking(pool, move |conn| LocalUserView::read(conn, local_user_id)).await??;
112 // Check for a site ban
113 if local_user_view.person.banned {
114 return Err(ApiError::err_plain("site_ban").into());
117 // Check for user deletion
118 if local_user_view.person.deleted {
119 return Err(ApiError::err_plain("deleted").into());
122 check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
127 /// Checks if user's token was issued before user's password reset.
128 pub fn check_validator_time(
129 validator_time: &chrono::NaiveDateTime,
131 ) -> Result<(), LemmyError> {
132 let user_validation_time = validator_time.timestamp();
133 if user_validation_time > claims.iat {
134 Err(ApiError::err_plain("not_logged_in").into())
140 pub async fn get_local_user_view_from_jwt_opt(
141 jwt: &Option<String>,
144 ) -> Result<Option<LocalUserView>, LemmyError> {
146 Some(jwt) => Ok(Some(get_local_user_view_from_jwt(jwt, pool, secret).await?)),
151 pub async fn get_local_user_settings_view_from_jwt(
155 ) -> Result<LocalUserSettingsView, LemmyError> {
156 let claims = Claims::decode(jwt, &secret.jwt_secret)
157 .map_err(|e| ApiError::err("not_logged_in", e))?
159 let local_user_id = LocalUserId(claims.sub);
160 let local_user_view = blocking(pool, move |conn| {
161 LocalUserSettingsView::read(conn, local_user_id)
164 // Check for a site ban
165 if local_user_view.person.banned {
166 return Err(ApiError::err_plain("site_ban").into());
169 check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
174 pub async fn get_local_user_settings_view_from_jwt_opt(
175 jwt: &Option<String>,
178 ) -> Result<Option<LocalUserSettingsView>, LemmyError> {
180 Some(jwt) => Ok(Some(
181 get_local_user_settings_view_from_jwt(jwt, pool, secret).await?,
187 pub async fn check_community_ban(
189 community_id: CommunityId,
191 ) -> Result<(), LemmyError> {
193 move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
194 if blocking(pool, is_banned).await? {
195 Err(ApiError::err_plain("community_ban").into())
201 pub async fn check_community_deleted_or_removed(
202 community_id: CommunityId,
204 ) -> Result<(), LemmyError> {
205 let community = blocking(pool, move |conn| Community::read(conn, community_id))
207 .map_err(|e| ApiError::err("couldnt_find_community", e))?;
208 if community.deleted || community.removed {
209 Err(ApiError::err_plain("deleted").into())
215 pub fn check_post_deleted_or_removed(post: &Post) -> Result<(), LemmyError> {
216 if post.deleted || post.removed {
217 Err(ApiError::err_plain("deleted").into())
223 pub async fn check_person_block(
225 potential_blocker_id: PersonId,
227 ) -> Result<(), LemmyError> {
228 let is_blocked = move |conn: &'_ _| PersonBlock::read(conn, potential_blocker_id, my_id).is_ok();
229 if blocking(pool, is_blocked).await? {
230 Err(ApiError::err_plain("person_block").into())
236 pub async fn check_downvotes_enabled(score: i16, pool: &DbPool) -> Result<(), LemmyError> {
238 let site = blocking(pool, Site::read_simple).await??;
239 if !site.enable_downvotes {
240 return Err(ApiError::err_plain("downvotes_disabled").into());
246 pub async fn build_federated_instances(
248 federation_config: &FederationConfig,
250 ) -> Result<Option<FederatedInstances>, LemmyError> {
251 let federation = federation_config.to_owned();
252 if federation.enabled {
253 let distinct_communities = blocking(pool, move |conn| {
254 Community::distinct_federated_communities(conn)
258 let allowed = federation.allowed_instances;
259 let blocked = federation.blocked_instances;
261 let mut linked = distinct_communities
263 .map(|actor_id| Ok(Url::parse(actor_id)?.host_str().unwrap_or("").to_string()))
264 .collect::<Result<Vec<String>, LemmyError>>()?;
266 if let Some(allowed) = allowed.as_ref() {
267 linked.extend_from_slice(allowed);
270 if let Some(blocked) = blocked.as_ref() {
271 linked.retain(|a| !blocked.contains(a) && !a.eq(hostname));
274 // Sort and remove dupes
275 linked.sort_unstable();
278 Ok(Some(FederatedInstances {
288 /// Checks the password length
289 pub fn password_length_check(pass: &str) -> Result<(), LemmyError> {
290 if !(10..=60).contains(&pass.len()) {
291 Err(ApiError::err_plain("invalid_password").into())
297 /// Checks the site description length
298 pub fn site_description_length_check(description: &str) -> Result<(), LemmyError> {
299 if description.len() > 150 {
300 Err(ApiError::err_plain("site_description_length_overflow").into())
306 /// Checks for a honeypot. If this field is filled, fail the rest of the function
307 pub fn honeypot_check(honeypot: &Option<String>) -> Result<(), LemmyError> {
308 if honeypot.is_some() {
309 Err(ApiError::err_plain("honeypot_fail").into())