]> Untitled Git - lemmy.git/blobdiff - crates/db_views_actor/src/community_person_ban_view.rs
Adding temporary bans. Fixes #1423 (#1999)
[lemmy.git] / crates / db_views_actor / src / community_person_ban_view.rs
index c63d15a0fe9028873e69deb077a1bfbbb3133430..6c67bd82a12b711fdbbe3f514adf1effd284ed72 100644 (file)
@@ -1,15 +1,16 @@
-use diesel::{result::Error, *};
-use lemmy_db_queries::ToSafe;
+use diesel::{dsl::*, result::Error, *};
 use lemmy_db_schema::{
+  newtypes::{CommunityId, PersonId},
   schema::{community, community_person_ban, person},
   source::{
     community::{Community, CommunitySafe},
     person::{Person, PersonSafe},
   },
+  traits::ToSafe,
 };
-use serde::Serialize;
+use serde::{Deserialize, Serialize};
 
-#[derive(Debug, Serialize, Clone)]
+#[derive(Debug, Serialize, Deserialize, Clone)]
 pub struct CommunityPersonBanView {
   pub community: CommunitySafe,
   pub person: PersonSafe,
@@ -18,8 +19,8 @@ pub struct CommunityPersonBanView {
 impl CommunityPersonBanView {
   pub fn get(
     conn: &PgConnection,
-    from_person_id: i32,
-    from_community_id: i32,
+    from_person_id: PersonId,
+    from_community_id: CommunityId,
   ) -> Result<Self, Error> {
     let (community, person) = community_person_ban::table
       .inner_join(community::table)
@@ -30,6 +31,11 @@ impl CommunityPersonBanView {
       ))
       .filter(community_person_ban::community_id.eq(from_community_id))
       .filter(community_person_ban::person_id.eq(from_person_id))
+      .filter(
+        community_person_ban::expires
+          .is_null()
+          .or(community_person_ban::expires.gt(now)),
+      )
       .order_by(community_person_ban::published)
       .first::<(CommunitySafe, PersonSafe)>(conn)?;