]> Untitled Git - lemmy.git/commitdiff
Merge branch 'main' into move_views_to_diesel
authorDessalines <tyhou13@gmx.com>
Wed, 16 Dec 2020 19:01:16 +0000 (14:01 -0500)
committerDessalines <tyhou13@gmx.com>
Wed, 16 Dec 2020 19:01:16 +0000 (14:01 -0500)
1  2 
lemmy_db/src/source/comment.rs
lemmy_db/src/source/community.rs
lemmy_db/src/source/post.rs
lemmy_db/src/source/user_mention.rs
lemmy_structs/src/lib.rs

index 239762599a010e4156c773660aab68ffb4dc860e,3e7d06be289afb3e7af56b43d377caffec50dfbf..380bdb3d157db6b1a259cc81dcf8416549c3cfa3
@@@ -1,14 -1,13 +1,14 @@@
  use super::post::Post;
  use crate::{
    naive_now,
 -  schema::{comment, comment_like, comment_saved},
 +  schema::{comment, comment_alias_1, comment_like, comment_saved},
    ApubObject,
    Crud,
    Likeable,
    Saveable,
  };
  use diesel::{dsl::*, result::Error, *};
 +use serde::Serialize;
  use url::{ParseError, Url};
  
  // WITH RECURSIVE MyTree AS (
@@@ -18,7 -17,7 +18,7 @@@
  // )
  // SELECT * FROM MyTree;
  
 -#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug)]
 +#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
  #[belongs_to(Post)]
  #[table_name = "comment"]
  pub struct Comment {
    pub local: bool,
  }
  
 +#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
 +#[belongs_to(Post)]
 +#[table_name = "comment_alias_1"]
 +pub struct CommentAlias1 {
 +  pub id: i32,
 +  pub creator_id: i32,
 +  pub post_id: i32,
 +  pub parent_id: Option<i32>,
 +  pub content: String,
 +  pub removed: bool,
 +  pub read: bool, // Whether the recipient has read the comment or not
 +  pub published: chrono::NaiveDateTime,
 +  pub updated: Option<chrono::NaiveDateTime>,
 +  pub deleted: bool,
 +  pub ap_id: String,
 +  pub local: bool,
 +}
 +
  #[derive(Insertable, AsChangeset, Clone)]
  #[table_name = "comment"]
  pub struct CommentForm {
@@@ -209,7 -190,7 +209,7 @@@ pub struct CommentLike 
    pub id: i32,
    pub user_id: i32,
    pub comment_id: i32,
 -  pub post_id: i32,
 +  pub post_id: i32, // TODO this is redundant
    pub score: i16,
    pub published: chrono::NaiveDateTime,
  }
  pub struct CommentLikeForm {
    pub user_id: i32,
    pub comment_id: i32,
 -  pub post_id: i32,
 +  pub post_id: i32, // TODO this is redundant
    pub score: i16,
  }
  
@@@ -228,6 -209,9 +228,9 @@@ impl Likeable<CommentLikeForm> for Comm
      use crate::schema::comment_like::dsl::*;
      insert_into(comment_like)
        .values(comment_like_form)
+       .on_conflict((comment_id, user_id))
+       .do_update()
+       .set(comment_like_form)
        .get_result::<Self>(conn)
    }
    fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> {
@@@ -263,6 -247,9 +266,9 @@@ impl Saveable<CommentSavedForm> for Com
      use crate::schema::comment_saved::dsl::*;
      insert_into(comment_saved)
        .values(comment_saved_form)
+       .on_conflict((comment_id, user_id))
+       .do_update()
+       .set(comment_saved_form)
        .get_result::<Self>(conn)
    }
    fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
  #[cfg(test)]
  mod tests {
    use crate::{
 -    comment::*,
 -    community::*,
 -    post::*,
 +    source::{comment::*, community::*, post::*, user::*},
      tests::establish_unpooled_connection,
 -    user::*,
      Crud,
      ListingType,
      SortType,
index ea7a028e1f863e2831edea344b1cdb0d11eee31b,35d54c3f182b9edfe967b59c28840d2a16625b4b..84db0c7c418c70f5056a69a867b0ae8a23123d99
@@@ -8,9 -8,8 +8,9 @@@ use crate::
    Joinable,
  };
  use diesel::{dsl::*, result::Error, *};
 +use serde::Serialize;
  
 -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug)]
 +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
  #[table_name = "community"]
  pub struct Community {
    pub id: i32,
    pub banner: Option<String>,
  }
  
 +/// A safe representation of community, without the sensitive info
 +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
 +#[table_name = "community"]
 +pub struct CommunitySafe {
 +  pub id: i32,
 +  pub name: String,
 +  pub title: String,
 +  pub description: Option<String>,
 +  pub category_id: i32,
 +  pub creator_id: i32,
 +  pub removed: bool,
 +  pub published: chrono::NaiveDateTime,
 +  pub updated: Option<chrono::NaiveDateTime>,
 +  pub deleted: bool,
 +  pub nsfw: bool,
 +  pub actor_id: String,
 +  pub local: bool,
 +  pub icon: Option<String>,
 +  pub banner: Option<String>,
 +}
 +
 +mod safe_type {
 +  use crate::{schema::community::columns::*, source::community::Community, ToSafe};
 +  type Columns = (
 +    id,
 +    name,
 +    title,
 +    description,
 +    category_id,
 +    creator_id,
 +    removed,
 +    published,
 +    updated,
 +    deleted,
 +    nsfw,
 +    actor_id,
 +    local,
 +    icon,
 +    banner,
 +  );
 +
 +  impl ToSafe for Community {
 +    type SafeColumns = Columns;
 +    fn safe_columns_tuple() -> Self::SafeColumns {
 +      (
 +        id,
 +        name,
 +        title,
 +        description,
 +        category_id,
 +        creator_id,
 +        removed,
 +        published,
 +        updated,
 +        deleted,
 +        nsfw,
 +        actor_id,
 +        local,
 +        icon,
 +        banner,
 +      )
 +    }
 +  }
 +}
 +
  #[derive(Insertable, AsChangeset, Debug)]
  #[table_name = "community"]
  pub struct CommunityForm {
@@@ -223,14 -157,14 +223,14 @@@ impl Community 
    }
  
    fn community_mods_and_admins(conn: &PgConnection, community_id: i32) -> Result<Vec<i32>, Error> {
 -    use crate::{community_view::CommunityModeratorView, user_view::UserView};
 +    use crate::views::{community_moderator_view::CommunityModeratorView, user_view::UserViewSafe};
      let mut mods_and_admins: Vec<i32> = Vec::new();
      mods_and_admins.append(
        &mut CommunityModeratorView::for_community(conn, community_id)
 -        .map(|v| v.into_iter().map(|m| m.user_id).collect())?,
 +        .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?,
      );
      mods_and_admins
 -      .append(&mut UserView::admins(conn).map(|v| v.into_iter().map(|a| a.id).collect())?);
 +      .append(&mut UserViewSafe::admins(conn).map(|v| v.into_iter().map(|a| a.user.id).collect())?);
      Ok(mods_and_admins)
    }
  
@@@ -375,6 -309,9 +375,9 @@@ impl Followable<CommunityFollowerForm> 
      use crate::schema::community_follower::dsl::*;
      insert_into(community_follower)
        .values(community_follower_form)
+       .on_conflict((community_id, user_id))
+       .do_update()
+       .set(community_follower_form)
        .get_result::<Self>(conn)
    }
    fn follow_accepted(conn: &PgConnection, community_id_: i32, user_id_: i32) -> Result<Self, Error>
  
  #[cfg(test)]
  mod tests {
 -  use crate::{community::*, tests::establish_unpooled_connection, user::*, ListingType, SortType};
 +  use crate::{
 +    source::{community::*, user::*},
 +    tests::establish_unpooled_connection,
 +    ListingType,
 +    SortType,
 +  };
  
    #[test]
    fn test_crud() {
index b584798e87a98cc884abdb0cbe15bca2b166e44a,b42c23c72145aa47dd9eff1615198198e2b30488..098ce8835316db5a72a6c005773e3ce4a3581634
@@@ -8,10 -8,9 +8,10 @@@ use crate::
    Saveable,
  };
  use diesel::{dsl::*, result::Error, *};
 +use serde::Serialize;
  use url::{ParseError, Url};
  
 -#[derive(Queryable, Identifiable, PartialEq, Debug)]
 +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
  #[table_name = "post"]
  pub struct Post {
    pub id: i32,
@@@ -241,6 -240,9 +241,9 @@@ impl Likeable<PostLikeForm> for PostLik
      use crate::schema::post_like::dsl::*;
      insert_into(post_like)
        .values(post_like_form)
+       .on_conflict((post_id, user_id))
+       .do_update()
+       .set(post_like_form)
        .get_result::<Self>(conn)
    }
    fn remove(conn: &PgConnection, user_id: i32, post_id: i32) -> Result<usize, Error> {
@@@ -276,6 -278,9 +279,9 @@@ impl Saveable<PostSavedForm> for PostSa
      use crate::schema::post_saved::dsl::*;
      insert_into(post_saved)
        .values(post_saved_form)
+       .on_conflict((post_id, user_id))
+       .do_update()
+       .set(post_saved_form)
        .get_result::<Self>(conn)
    }
    fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
@@@ -332,8 -337,10 +338,8 @@@ impl Readable<PostReadForm> for PostRea
  #[cfg(test)]
  mod tests {
    use crate::{
 -    community::*,
 -    post::*,
 +    source::{community::*, post::*, user::*},
      tests::establish_unpooled_connection,
 -    user::*,
      ListingType,
      SortType,
    };
index ed5c2c797bc8c0ac5a2ccdfb0d3f608b7fb0fda6,b382a24e029f21f980a7ec546d0b20e5fbe52b1c..bf53cb4200504854fbdaecce9515158fe1eec54a
@@@ -1,9 -1,8 +1,9 @@@
  use super::comment::Comment;
  use crate::{schema::user_mention, Crud};
  use diesel::{dsl::*, result::Error, *};
 +use serde::Serialize;
  
 -#[derive(Queryable, Associations, Identifiable, PartialEq, Debug)]
 +#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
  #[belongs_to(Comment)]
  #[table_name = "user_mention"]
  pub struct UserMention {
@@@ -30,8 -29,13 +30,13 @@@ impl Crud<UserMentionForm> for UserMent
  
    fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
      use crate::schema::user_mention::dsl::*;
+     // since the return here isnt utilized, we dont need to do an update
+     // but get_result doesnt return the existing row here
      insert_into(user_mention)
        .values(user_mention_form)
+       .on_conflict((recipient_id, comment_id))
+       .do_update()
+       .set(user_mention_form)
        .get_result::<Self>(conn)
    }
  
@@@ -74,8 -78,12 +79,8 @@@ impl UserMention 
  #[cfg(test)]
  mod tests {
    use crate::{
 -    comment::*,
 -    community::*,
 -    post::*,
 +    source::{comment::*, community::*, post::*, user::*, user_mention::*},
      tests::establish_unpooled_connection,
 -    user::*,
 -    user_mention::*,
      ListingType,
      SortType,
    };
diff --combined lemmy_structs/src/lib.rs
index 3a2e28d94ce0b175a08f5f60741e5e94b5796eed,a0dbdab651e2a675fda724ea8aa9dd0174313ed8..dc06a40cd1c05415353bf5b08b203998df6c8829
@@@ -7,12 -7,10 +7,12 @@@ pub mod websocket
  
  use diesel::PgConnection;
  use lemmy_db::{
 -  comment::Comment,
 -  post::Post,
 -  user::User_,
 -  user_mention::{UserMention, UserMentionForm},
 +  source::{
 +    comment::Comment,
 +    post::Post,
 +    user::User_,
 +    user_mention::{UserMention, UserMentionForm},
 +  },
    Crud,
    DbPool,
  };
@@@ -100,10 -98,7 +100,7 @@@ fn do_send_local_notifs
  
        // Allow this to fail softly, since comment edits might re-update or replace it
        // Let the uniqueness handle this fail
-       match UserMention::create(&conn, &user_mention_form) {
-         Ok(_mention) => (),
-         Err(_e) => error!("{}", &_e),
-       };
+       let _ = UserMention::create(&conn, &user_mention_form);
  
        // Send an email to those users that have notifications on
        if do_send_email && mention_user.send_notifications_to_email {