]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/post.rs
Tag posts and comments with language (fixes #440) (#2269)
[lemmy.git] / crates / db_schema / src / impls / post.rs
index 2b71c663f4bb500c5e4a662e30b3b812521f4b15..07c652e3148265b32d117500803f3c29cf60351b 100644 (file)
@@ -1,5 +1,4 @@
 use crate::{
-  naive_now,
   newtypes::{CommunityId, DbUrl, PersonId, PostId},
   source::post::{
     Post,
@@ -12,11 +11,9 @@ use crate::{
     PostSavedForm,
   },
   traits::{Crud, DeleteableOrRemoveable, Likeable, Readable, Saveable},
+  utils::{naive_now, FETCH_LIMIT_MAX},
 };
-use chrono::NaiveDateTime;
-use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
-use lemmy_apub_lib::traits::ApubObject;
-use lemmy_utils::LemmyError;
+use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, *};
 use url::Url;
 
 impl Crud for Post {
@@ -53,9 +50,11 @@ impl Post {
     use crate::schema::post::dsl::*;
     post
       .filter(community_id.eq(the_community_id))
+      .filter(deleted.eq(false))
+      .filter(removed.eq(false))
       .then_order_by(published.desc())
       .then_order_by(stickied.desc())
-      .limit(20)
+      .limit(FETCH_LIMIT_MAX)
       .load::<Self>(conn)
   }
 
@@ -164,6 +163,82 @@ impl Post {
       .set(post_form)
       .get_result::<Self>(conn)
   }
+  pub fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
+    use crate::schema::post::dsl::*;
+    let object_id: DbUrl = object_id.into();
+    Ok(
+      post
+        .filter(ap_id.eq(object_id))
+        .first::<Post>(conn)
+        .ok()
+        .map(Into::into),
+    )
+  }
+
+  pub fn fetch_pictrs_posts_for_creator(
+    conn: &PgConnection,
+    for_creator_id: PersonId,
+  ) -> Result<Vec<Self>, Error> {
+    use crate::schema::post::dsl::*;
+    let pictrs_search = "%pictrs/image%";
+
+    post
+      .filter(creator_id.eq(for_creator_id))
+      .filter(url.like(pictrs_search))
+      .load::<Self>(conn)
+  }
+
+  /// Sets the url and thumbnails fields to None
+  pub fn remove_pictrs_post_images_and_thumbnails_for_creator(
+    conn: &PgConnection,
+    for_creator_id: PersonId,
+  ) -> Result<Vec<Self>, Error> {
+    use crate::schema::post::dsl::*;
+    let pictrs_search = "%pictrs/image%";
+
+    diesel::update(
+      post
+        .filter(creator_id.eq(for_creator_id))
+        .filter(url.like(pictrs_search)),
+    )
+    .set((
+      url.eq::<Option<String>>(None),
+      thumbnail_url.eq::<Option<String>>(None),
+    ))
+    .get_results::<Self>(conn)
+  }
+
+  pub fn fetch_pictrs_posts_for_community(
+    conn: &PgConnection,
+    for_community_id: CommunityId,
+  ) -> Result<Vec<Self>, Error> {
+    use crate::schema::post::dsl::*;
+    let pictrs_search = "%pictrs/image%";
+    post
+      .filter(community_id.eq(for_community_id))
+      .filter(url.like(pictrs_search))
+      .load::<Self>(conn)
+  }
+
+  /// Sets the url and thumbnails fields to None
+  pub fn remove_pictrs_post_images_and_thumbnails_for_community(
+    conn: &PgConnection,
+    for_community_id: CommunityId,
+  ) -> Result<Vec<Self>, Error> {
+    use crate::schema::post::dsl::*;
+    let pictrs_search = "%pictrs/image%";
+
+    diesel::update(
+      post
+        .filter(community_id.eq(for_community_id))
+        .filter(url.like(pictrs_search)),
+    )
+    .set((
+      url.eq::<Option<String>>(None),
+      thumbnail_url.eq::<Option<String>>(None),
+    ))
+    .get_results::<Self>(conn)
+  }
 }
 
 impl Likeable for PostLike {
@@ -241,45 +316,23 @@ impl DeleteableOrRemoveable for Post {
     self.body = None;
     self.embed_title = None;
     self.embed_description = None;
-    self.embed_html = None;
+    self.embed_video_url = None;
     self.thumbnail_url = None;
 
     self
   }
 }
 
-impl ApubObject for Post {
-  type DataType = PgConnection;
-
-  fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
-    None
-  }
-
-  fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError> {
-    use crate::schema::post::dsl::*;
-    let object_id: DbUrl = object_id.into();
-    Ok(post.filter(ap_id.eq(object_id)).first::<Self>(conn).ok())
-  }
-
-  fn delete(self, conn: &PgConnection) -> Result<(), LemmyError> {
-    use crate::schema::post::dsl::*;
-    diesel::update(post.find(self.id))
-      .set((deleted.eq(true), updated.eq(naive_now())))
-      .get_result::<Self>(conn)?;
-    Ok(())
-  }
-}
-
 #[cfg(test)]
 mod tests {
   use crate::{
-    establish_unpooled_connection,
     source::{
       community::{Community, CommunityForm},
       person::*,
       post::*,
     },
     traits::{Crud, Likeable, Readable, Saveable},
+    utils::establish_unpooled_connection,
   };
   use serial_test::serial;
 
@@ -290,6 +343,7 @@ mod tests {
 
     let new_person = PersonForm {
       name: "jim".into(),
+      public_key: Some("pubkey".to_string()),
       ..PersonForm::default()
     };
 
@@ -298,6 +352,7 @@ mod tests {
     let new_community = CommunityForm {
       name: "test community_3".to_string(),
       title: "nada".to_owned(),
+      public_key: Some("pubkey".to_string()),
       ..CommunityForm::default()
     };
 
@@ -328,10 +383,11 @@ mod tests {
       updated: None,
       embed_title: None,
       embed_description: None,
-      embed_html: None,
+      embed_video_url: None,
       thumbnail_url: None,
       ap_id: inserted_post.ap_id.to_owned(),
       local: true,
+      language_id: Default::default(),
     };
 
     // Post Like