diff options
author | Dessalines <happydooby@gmail.com> | 2019-04-19 21:06:25 -0700 |
---|---|---|
committer | Dessalines <happydooby@gmail.com> | 2019-04-19 21:06:25 -0700 |
commit | b25b1b6a0de25c536d58ae4ff5dbedc50b73d0d0 (patch) | |
tree | 22a2ea818d6bfcdb390e398c4c9bd5e05595587c /server/src | |
parent | 25b75de07887d4c0cc4929d968ef690236b06d24 (diff) |
Saving replies, the actual fixes will be in the merge to dev.
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/actions/comment.rs | 110 | ||||
-rw-r--r-- | server/src/actions/comment_view.rs | 47 | ||||
-rw-r--r-- | server/src/actions/community.rs | 8 | ||||
-rw-r--r-- | server/src/actions/community_view.rs | 6 | ||||
-rw-r--r-- | server/src/actions/moderator.rs | 6 | ||||
-rw-r--r-- | server/src/actions/post.rs | 163 | ||||
-rw-r--r-- | server/src/actions/post_view.rs | 49 | ||||
-rw-r--r-- | server/src/lib.rs | 10 | ||||
-rw-r--r-- | server/src/schema.rs | 45 | ||||
-rw-r--r-- | server/src/websocket_server/server.rs | 293 |
10 files changed, 576 insertions, 161 deletions
diff --git a/server/src/actions/comment.rs b/server/src/actions/comment.rs index f6eee5f1..c3aa0107 100644 --- a/server/src/actions/comment.rs +++ b/server/src/actions/comment.rs @@ -1,9 +1,9 @@ extern crate diesel; -use schema::{comment, comment_like}; +use schema::{comment, comment_like, comment_saved}; use diesel::*; use diesel::result::Error; use serde::{Deserialize, Serialize}; -use {Crud, Likeable}; +use {Crud, Likeable, Saveable}; use actions::post::Post; // WITH RECURSIVE MyTree AS ( @@ -22,7 +22,8 @@ pub struct Comment { pub post_id: i32, pub parent_id: Option<i32>, pub content: String, - pub removed: Option<bool>, + pub removed: bool, + pub read: bool, pub published: chrono::NaiveDateTime, pub updated: Option<chrono::NaiveDateTime> } @@ -38,27 +39,6 @@ pub struct CommentForm { pub updated: Option<chrono::NaiveDateTime> } -#[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)] -#[belongs_to(Comment)] -#[table_name = "comment_like"] -pub struct CommentLike { - pub id: i32, - pub user_id: i32, - pub comment_id: i32, - pub post_id: i32, - pub score: i16, - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset, Clone)] -#[table_name="comment_like"] -pub struct CommentLikeForm { - pub user_id: i32, - pub comment_id: i32, - pub post_id: i32, - pub score: i16 -} - impl Crud<CommentForm> for Comment { fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> { use schema::comment::dsl::*; @@ -87,6 +67,27 @@ impl Crud<CommentForm> for Comment { } } +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)] +#[belongs_to(Comment)] +#[table_name = "comment_like"] +pub struct CommentLike { + pub id: i32, + pub user_id: i32, + pub comment_id: i32, + pub post_id: i32, + pub score: i16, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name="comment_like"] +pub struct CommentLikeForm { + pub user_id: i32, + pub comment_id: i32, + pub post_id: i32, + pub score: i16 +} + impl Likeable <CommentLikeForm> for CommentLike { fn read(conn: &PgConnection, comment_id_from: i32) -> Result<Vec<Self>, Error> { use schema::comment_like::dsl::*; @@ -119,6 +120,39 @@ impl CommentLike { } } +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Comment)] +#[table_name = "comment_saved"] +pub struct CommentSaved { + pub id: i32, + pub comment_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name="comment_saved"] +pub struct CommentSavedForm { + pub comment_id: i32, + pub user_id: i32, +} + +impl Saveable <CommentSavedForm> for CommentSaved { + fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> { + use schema::comment_saved::dsl::*; + insert_into(comment_saved) + .values(comment_saved_form) + .get_result::<Self>(conn) + } + fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> { + use schema::comment_saved::dsl::*; + diesel::delete(comment_saved + .filter(comment_id.eq(comment_saved_form.comment_id)) + .filter(user_id.eq(comment_saved_form.user_id))) + .execute(conn) + } +} + #[cfg(test)] mod tests { use establish_connection; @@ -150,7 +184,7 @@ mod tests { description: None, category_id: 1, creator_id: inserted_user.id, - removed: None, + removed: false, updated: None }; @@ -162,8 +196,8 @@ mod tests { url: None, body: None, community_id: inserted_community.id, - removed: None, - locked: None, + removed: false, + locked: false, updated: None }; @@ -185,7 +219,8 @@ mod tests { content: "A test comment".into(), creator_id: inserted_user.id, post_id: inserted_post.id, - removed: Some(false), + removed: false, + read: false, parent_id: None, published: inserted_comment.published, updated: None @@ -202,6 +237,7 @@ mod tests { let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap(); + // Comment Like let comment_like_form = CommentLikeForm { comment_id: inserted_comment.id, post_id: inserted_post.id, @@ -220,9 +256,25 @@ mod tests { score: 1 }; + // Comment Saved + let comment_saved_form = CommentSavedForm { + comment_id: inserted_comment.id, + user_id: inserted_user.id, + }; + + let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap(); + + let expected_comment_saved = CommentSaved { + id: inserted_comment_saved.id, + comment_id: inserted_comment.id, + user_id: inserted_user.id, + published: inserted_comment_saved.published, + }; + let read_comment = Comment::read(&conn, inserted_comment.id).unwrap(); let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap(); let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap(); + let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap(); let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap(); Comment::delete(&conn, inserted_child_comment.id).unwrap(); Post::delete(&conn, inserted_post.id).unwrap(); @@ -233,8 +285,10 @@ mod tests { assert_eq!(expected_comment, inserted_comment); assert_eq!(expected_comment, updated_comment); assert_eq!(expected_comment_like, inserted_comment_like); + assert_eq!(expected_comment_saved, inserted_comment_saved); assert_eq!(expected_comment.id, inserted_child_comment.parent_id.unwrap()); assert_eq!(1, like_removed); + assert_eq!(1, saved_removed); assert_eq!(1, num_deleted); } diff --git a/server/src/actions/comment_view.rs b/server/src/actions/comment_view.rs index 0848ee1c..36043716 100644 --- a/server/src/actions/comment_view.rs +++ b/server/src/actions/comment_view.rs @@ -13,18 +13,20 @@ table! { post_id -> Int4, parent_id -> Nullable<Int4>, content -> Text, - removed -> Nullable<Bool>, + removed -> Bool, + read -> Bool, published -> Timestamp, updated -> Nullable<Timestamp>, community_id -> Int4, - banned -> Nullable<Bool>, + banned -> Bool, + banned_from_community -> Bool, creator_name -> Varchar, score -> BigInt, upvotes -> BigInt, downvotes -> BigInt, user_id -> Nullable<Int4>, my_vote -> Nullable<Int4>, - am_mod -> Nullable<Bool>, + saved -> Nullable<Bool>, } } @@ -36,18 +38,20 @@ pub struct CommentView { pub post_id: i32, pub parent_id: Option<i32>, pub content: String, - pub removed: Option<bool>, + pub removed: bool, + pub read: bool, pub published: chrono::NaiveDateTime, pub updated: Option<chrono::NaiveDateTime>, pub community_id: i32, - pub banned: Option<bool>, + pub banned: bool, + pub banned_from_community: bool, pub creator_name: String, pub score: i64, pub upvotes: i64, pub downvotes: i64, pub user_id: Option<i32>, pub my_vote: Option<i32>, - pub am_mod: Option<bool>, + pub saved: Option<bool>, } impl CommentView { @@ -57,6 +61,7 @@ impl CommentView { for_post_id: Option<i32>, for_creator_id: Option<i32>, my_user_id: Option<i32>, + saved_only: bool, page: Option<i64>, limit: Option<i64>, ) -> Result<Vec<Self>, Error> { @@ -81,6 +86,10 @@ impl CommentView { if let Some(for_post_id) = for_post_id { query = query.filter(post_id.eq(for_post_id)); }; + + if saved_only { + query = query.filter(saved.eq(true)); + } query = match sort { // SortType::Hot => query.order_by(hot_rank.desc()), @@ -159,7 +168,7 @@ mod tests { description: None, category_id: 1, creator_id: inserted_user.id, - removed: None, + removed: false, updated: None }; @@ -171,8 +180,8 @@ mod tests { url: None, body: None, community_id: inserted_community.id, - removed: None, - locked: None, + removed: false, + locked: false, updated: None }; @@ -205,8 +214,10 @@ mod tests { post_id: inserted_post.id, community_id: inserted_community.id, parent_id: None, - removed: Some(false), - banned: None, + removed: false, + read: false, + banned: false, + banned_from_community: false, published: inserted_comment.published, updated: None, creator_name: inserted_user.name.to_owned(), @@ -215,7 +226,7 @@ mod tests { upvotes: 1, user_id: None, my_vote: None, - am_mod: None, + saved: None, }; let expected_comment_view_with_user = CommentView { @@ -225,8 +236,10 @@ mod tests { post_id: inserted_post.id, community_id: inserted_community.id, parent_id: None, - removed: Some(false), - banned: None, + removed: false, + read: false, + banned: false, + banned_from_community: false, published: inserted_comment.published, updated: None, creator_name: inserted_user.name.to_owned(), @@ -235,11 +248,11 @@ mod tests { upvotes: 1, user_id: Some(inserted_user.id), my_vote: Some(1), - am_mod: None, + saved: None, }; - let read_comment_views_no_user = CommentView::list(&conn, &SortType::New, Some(inserted_post.id), None, None, None, None).unwrap(); - let read_comment_views_with_user = CommentView::list(&conn, &SortType::New, Some(inserted_post.id), None, Some(inserted_user.id), None, None).unwrap(); + let read_comment_views_no_user = CommentView::list(&conn, &SortType::New, Some(inserted_post.id), None, None, false, None, None).unwrap(); + let read_comment_views_with_user = CommentView::list(&conn, &SortType::New, Some(inserted_post.id), None, Some(inserted_user.id), false, None, None).unwrap(); let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap(); let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap(); Post::delete(&conn, inserted_post.id).unwrap(); diff --git a/server/src/actions/community.rs b/server/src/actions/community.rs index ac331934..594518ba 100644 --- a/server/src/actions/community.rs +++ b/server/src/actions/community.rs @@ -14,7 +14,7 @@ pub struct Community { pub description: Option<String>, pub category_id: i32, pub creator_id: i32, - pub removed: Option<bool>, + pub removed: bool, pub published: chrono::NaiveDateTime, pub updated: Option<chrono::NaiveDateTime> } @@ -27,7 +27,7 @@ pub struct CommunityForm { pub description: Option<String>, pub category_id: i32, pub creator_id: i32, - pub removed: Option<bool>, + pub removed: bool, pub updated: Option<chrono::NaiveDateTime> } @@ -236,7 +236,7 @@ mod tests { title: "nada".to_owned(), description: None, category_id: 1, - removed: None, + removed: false, updated: None, }; @@ -249,7 +249,7 @@ mod tests { title: "nada".to_owned(), description: None, category_id: 1, - removed: Some(false), + removed: false, published: inserted_community.published, updated: None }; diff --git a/server/src/actions/community_view.rs b/server/src/actions/community_view.rs index 4db97491..8966ee15 100644 --- a/server/src/actions/community_view.rs +++ b/server/src/actions/community_view.rs @@ -12,7 +12,7 @@ table! { description -> Nullable<Text>, category_id -> Int4, creator_id -> Int4, - removed -> Nullable<Bool>, + removed -> Bool, published -> Timestamp, updated -> Nullable<Timestamp>, creator_name -> Varchar, @@ -22,7 +22,6 @@ table! { number_of_comments -> BigInt, user_id -> Nullable<Int4>, subscribed -> Nullable<Bool>, - am_mod -> Nullable<Bool>, } } @@ -83,7 +82,7 @@ pub struct CommunityView { pub description: Option<String>, pub category_id: i32, pub creator_id: i32, - pub removed: Option<bool>, + pub removed: bool, pub published: chrono::NaiveDateTime, pub updated: Option<chrono::NaiveDateTime>, pub creator_name: String, @@ -93,7 +92,6 @@ pub struct CommunityView { pub number_of_comments: i64, pub user_id: Option<i32>, pub subscribed: Option<bool>, - pub am_mod: Option<bool>, } impl CommunityView { diff --git a/server/src/actions/moderator.rs b/server/src/actions/moderator.rs index a97b2120..e0d885ce 100644 --- a/server/src/actions/moderator.rs +++ b/server/src/actions/moderator.rs @@ -441,7 +441,7 @@ mod tests { description: None, category_id: 1, creator_id: inserted_user.id, - removed: None, + removed: false, updated: None }; @@ -453,8 +453,8 @@ mod tests { body: None, creator_id: inserted_user.id, community_id: inserted_community.id, - removed: None, - locked: None, + removed: false, + locked: false, updated: None }; diff --git a/server/src/actions/post.rs b/server/src/actions/post.rs index 468b3a9b..0fd0e5c5 100644 --- a/server/src/actions/post.rs +++ b/server/src/actions/post.rs @@ -1,9 +1,9 @@ extern crate diesel; -use schema::{post, post_like}; +use schema::{post, post_like, post_saved, post_read}; use diesel::*; use diesel::result::Error; use serde::{Deserialize, Serialize}; -use {Crud, Likeable}; +use {Crud, Likeable, Saveable, Readable}; #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)] #[table_name="post"] @@ -14,8 +14,8 @@ pub struct Post { pub body: Option<String>, pub creator_id: i32, pub community_id: i32, - pub removed: Option<bool>, - pub locked: Option<bool>, + pub removed: bool, + pub locked: bool, pub published: chrono::NaiveDateTime, pub updated: Option<chrono::NaiveDateTime> } @@ -28,30 +28,11 @@ pub struct PostForm { pub body: Option<String>, pub creator_id: i32, pub community_id: i32, - pub removed: Option<bool>, - pub locked: Option<bool>, + pub removed: bool, + pub locked: bool, pub updated: Option<chrono::NaiveDateTime> } -#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] -#[belongs_to(Post)] -#[table_name = "post_like"] -pub struct PostLike { - pub id: i32, - pub post_id: i32, - pub user_id: i32, - pub score: i16, - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset, Clone)] -#[table_name="post_like"] -pub struct PostLikeForm { - pub post_id: i32, - pub user_id: i32, - pub score: i16 -} - impl Crud<PostForm> for Post { fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> { use schema::post::dsl::*; @@ -80,6 +61,25 @@ impl Crud<PostForm> for Post { } } +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Post)] +#[table_name = "post_like"] +pub struct PostLike { + pub id: i32, + pub post_id: i32, + pub user_id: i32, + pub score: i16, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name="post_like"] +pub struct PostLikeForm { + pub post_id: i32, + pub user_id: i32, + pub score: i16 +} + impl Likeable <PostLikeForm> for PostLike { fn read(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> { use schema::post_like::dsl::*; @@ -102,6 +102,72 @@ impl Likeable <PostLikeForm> for PostLike { } } +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Post)] +#[table_name = "post_saved"] +pub struct PostSaved { + pub id: i32, + pub post_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name="post_saved"] +pub struct PostSavedForm { + pub post_id: i32, + pub user_id: i32, +} + +impl Saveable <PostSavedForm> for PostSaved { + fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> { + use schema::post_saved::dsl::*; + insert_into(post_saved) + .values(post_saved_form) + .get_result::<Self>(conn) + } + fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> { + use schema::post_saved::dsl::*; + diesel::delete(post_saved + .filter(post_id.eq(post_saved_form.post_id)) + .filter(user_id.eq(post_saved_form.user_id))) + .execute(conn) + } +} + +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Post)] +#[table_name = "post_read"] +pub struct PostRead { + pub id: i32, + pub post_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name="post_read"] +pub struct PostReadForm { + pub post_id: i32, + pub user_id: i32, +} + +impl Readable <PostReadForm> for PostRead { + fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> { + use schema::post_read::dsl::*; + insert_into(post_read) + .values(post_read_form) + .get_result::<Self>(conn) + } + fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> { + use schema::post_read::dsl::*; + diesel::delete(post_read + .filter(post_id.eq(post_read_form.post_id)) + .filter(user_id.eq(post_read_form.user_id))) + .execute(conn) + } +} + #[cfg(test)] mod tests { use establish_connection; @@ -132,7 +198,7 @@ mod tests { description: None, category_id: 1, creator_id: inserted_user.id, - removed: None, + removed: false, updated: None }; @@ -144,8 +210,8 @@ mod tests { body: None, creator_id: inserted_user.id, community_id: inserted_community.id, - removed: None, - locked: None, + removed: false, + locked: false, updated: None }; @@ -159,11 +225,12 @@ mod tests { creator_id: inserted_user.id, community_id: inserted_community.id, published: inserted_post.published, - removed: Some(false), - locked: Some(false), + removed: false, + locked: false, updated: None }; + // Post Like let post_like_form = PostLikeForm { post_id: inserted_post.id, user_id: inserted_user.id, @@ -179,10 +246,42 @@ mod tests { published: inserted_post_like.published, score: 1 }; + + // Post Save + let post_saved_form = PostSavedForm { + post_id: inserted_post.id, + user_id: inserted_user.id, + }; + + let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap(); + + let expected_post_saved = PostSaved { + id: inserted_post_saved.id, + post_id: inserted_post.id, + user_id: inserted_user.id, + published: inserted_post_saved.published, + }; + + // Post Read + let post_read_form = PostReadForm { + post_id: inserted_post.id, + user_id: inserted_user.id, + }; + + let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap(); + + let expected_post_read = PostRead { + id: inserted_post_read.id, + post_id: inserted_post.id, + user_id: inserted_user.id, + published: inserted_post_read.published, + }; let read_post = Post::read(&conn, inserted_post.id).unwrap(); let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap(); let like_removed = PostLike::remove(&conn, &post_like_form).unwrap(); + let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap(); + let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap(); let num_deleted = Post::delete(&conn, inserted_post.id).unwrap(); Community::delete(&conn, inserted_community.id).unwrap(); User_::delete(&conn, inserted_user.id).unwrap(); @@ -191,7 +290,11 @@ mod tests { assert_eq!(expected_post, inserted_post); assert_eq!(expected_post, updated_post); assert_eq!(expected_post_like, inserted_post_like); + assert_eq!(expected_post_saved, inserted_post_saved); + assert_eq!(expected_post_read, inserted_post_read); assert_eq!(1, like_removed); + assert_eq!(1, saved_removed); + assert_eq!(1, read_removed); assert_eq!(1, num_deleted); } diff --git a/server/src/actions/post_view.rs b/server/src/actions/post_view.rs index 7ab490aa..78fcef63 100644 --- a/server/src/actions/post_view.rs +++ b/server/src/actions/post_view.rs @@ -19,8 +19,8 @@ table! { body -> Nullable<Text>, creator_id -> Int4, community_id -> Int4, - removed -> Nullable<Bool>, - locked -> Nullable<Bool>, + removed -> Bool, + locked -> Bool, published -> Timestamp, updated -> Nullable<Timestamp>, creator_name -> Varchar, @@ -33,7 +33,8 @@ table! { user_id -> Nullable<Int4>, my_vote -> Nullable<Int4>, subscribed -> Nullable<Bool>, - am_mod -> Nullable<Bool>, + read -> Nullable<Bool>, + saved -> Nullable<Bool>, } } @@ -47,8 +48,8 @@ pub struct PostView { pub body: Option<String>, pub creator_id: i32, pub community_id: i32, - pub removed: Option<bool>, - pub locked: Option<bool>, + pub removed: bool, + pub locked: bool, pub published: chrono::NaiveDateTime, pub updated: Option<chrono::NaiveDateTime>, pub creator_name: String, @@ -61,7 +62,8 @@ pub struct PostView { pub user_id: Option<i32>, pub my_vote: Option<i32>, pub subscribed: Option<bool>, - pub am_mod: Option<bool>, + pub read: Option<bool>, + pub saved: Option<bool>, } impl PostView { @@ -71,6 +73,8 @@ impl PostView { for_community_id: Option<i32>, for_creator_id: Option<i32>, my_user_id: Option<i32>, + saved_only: bool, + unread_only: bool, page: Option<i64>, limit: Option<i64>, ) -> Result<Vec<Self>, Error> { @@ -88,6 +92,15 @@ impl PostView { query = query.filter(creator_id.eq(for_creator_id)); }; + // TODO these are wrong, bc they'll only show saved for your logged in user, not theirs + if saved_only { + query = query.filter(saved.eq(true)); + }; + + if unread_only { + query = query.filter(read.eq(false)); + }; + match type_ { PostListingType::Subscribed => { query = query.filter(subscribed.eq(true)); @@ -187,7 +200,7 @@ mod tests { description: None, creator_id: inserted_user.id, category_id: 1, - removed: None, + removed: false, updated: None }; @@ -199,8 +212,8 @@ mod tests { body: None, creator_id: inserted_user.id, community_id: inserted_community.id, - removed: None, - locked: None, + removed: false, + locked: false, updated: None }; @@ -239,8 +252,8 @@ mod tests { creator_id: inserted_user.id, creator_name: user_name.to_owned(), community_id: inserted_community.id, - removed: Some(false), - locked: Some(false), + removed: false, + locked: false, community_name: community_name.to_owned(), number_of_comments: 0, score: 1, @@ -250,7 +263,8 @@ mod tests { published: inserted_post.published, updated: None, subscribed: None, - am_mod: None, + read: None, + saved: None, }; let expected_post_listing_with_user = PostView { @@ -260,8 +274,8 @@ mod tests { name: post_name.to_owned(), url: None, body: None, - removed: Some(false), - locked: Some(false), + removed: false, + locked: false, creator_id: inserted_user.id, creator_name: user_name.to_owned(), community_id: inserted_community.id, @@ -274,12 +288,13 @@ mod tests { published: inserted_post.published, updated: None, subscribed: None, - am_mod: None, + read: None, + saved: None, }; - let read_post_listings_with_user = PostView::list(&conn, PostListingType::Community, &SortType::New, Some(inserted_community.id), None, Some(inserted_user.id), None, None).unwrap(); - let read_post_listings_no_user = PostView::list(&conn, PostListingType::Community, &SortType::New, Some(inserted_community.id), None, None, None, None).unwrap(); + let read_post_listings_with_user = PostView::list(&conn, PostListingType::Community, &SortType::New, Some(inserted_community.id), None, Some(inserted_user.id), false, false, None, None).unwrap(); + let read_post_listings_no_user = PostView::list(&conn, PostListingType::Community, &SortType::New, Some(inserted_community.id), None, None, false, false, None, None).unwrap(); let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap(); let read_post_listing_with_user = PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap(); diff --git a/server/src/lib.rs b/server/src/lib.rs index 3390dbdc..31c1af7c 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -55,6 +55,16 @@ pub trait Bannable<T> { fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized; } +pub trait Saveable<T> { + fn save(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized; + fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized; +} + +pub trait Readable<T> { + fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized; + fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized; +} + pub fn establish_connection() -> PgConnection { let db_url = Settings::get().db_url; PgConnection::establish(&db_url) diff --git a/server/src/schema.rs b/server/src/schema.rs index f431610a..65c2ae55 100644 --- a/server/src/schema.rs +++ b/server/src/schema.rs @@ -12,7 +12,8 @@ table! { post_id -> Int4, parent_id -> Nullable<Int4>, content -> Text, - removed -> Nullable<Bool>, + removed -> Bool, + read -> Bool, published -> Timestamp, updated -> Nullable<Timestamp>, } @@ -30,6 +31,15 @@ table! { } table! { + comment_saved (id) { + id -> Int4, + comment_id -> Int4, + user_id -> Int4, + published -> Timestamp, + } +} + +table! { community (id) { id -> Int4, name -> Varchar, @@ -37,7 +47,7 @@ table! { description -> Nullable<Text>, category_id -> Int4, creator_id -> Int4, - removed -> Nullable<Bool>, + removed -> Bool, published -> Timestamp, updated -> Nullable<Timestamp>, } @@ -168,8 +178,8 @@ table! { body -> Nullable<Text>, creator_id -> Int4, community_id -> Int4, - removed -> Nullable<Bool>, - locked -> Nullable<Bool>, + removed -> Bool, + locked -> Bool, published -> Timestamp, updated -> Nullable<Timestamp>, } @@ -186,6 +196,24 @@ table! { } table! { + post_read (id) { + id -> Int4, + post_id -> Int4, + user_id -> Int4, + published -> Timestamp, + } +} + +table! { + post_saved (id) { + id -> Int4, + post_id -> Int4, + user_id -> Int4, + published -> Timestamp, + } +} + +table! { site (id) { id -> Int4, name -> Varchar, @@ -225,6 +253,8 @@ joinable!(comment -> user_ (creator_id)); joinable!(comment_like -> comment (comment_id)); joinable!(comment_like -> post (post_id)); joinable!(comment_like -> user_ (user_id)); +joinable!(comment_saved -> comment (comment_id)); +joinable!(comment_saved -> user_ (user_id)); joinable!(community -> category (category_id)); joinable!(community -> user_ (creator_id)); joinable!(community_follower -> community (community_id)); @@ -247,6 +277,10 @@ joinable!(post -> community (community_id)); joinable!(post -> user_ (creator_id)); joinable!(post_like -> post (post_id)); joinable!(post_like -> user_ (user_id)); +joinable!(post_read -> post (post_id)); +joinable!(post_read -> user_ (user_id)); +joinable!(post_saved -> post (post_id)); +joinable!(post_saved -> user_ (user_id)); joinable!(site -> user_ (creator_id)); joinable!(user_ban -> user_ (user_id)); @@ -254,6 +288,7 @@ allow_tables_to_appear_in_same_query!( category, comment, comment_like, + comment_saved, community, community_follower, community_moderator, @@ -268,6 +303,8 @@ allow_tables_to_appear_in_same_query!( mod_remove_post, post, post_like, + post_read, + post_saved, site, user_, user_ban, diff --git a/server/src/websocket_server/server.rs b/server/src/websocket_server/server.rs index a4c5b620..d1f72109 100644 --- a/server/src/websocket_server/server.rs +++ b/server/src/websocket_server/server.rs @@ -11,7 +11,7 @@ use bcrypt::{verify}; use std::str::FromStr; use diesel::PgConnection; -use {Crud, Joinable, Likeable, Followable, Bannable, establish_connection, naive_now, naive_from_unix, SortType, has_slurs, remove_slurs}; +use {Crud, Joinable, Likeable, Followable, Bannable, Saveable, establish_connection, naive_now, naive_from_unix, SortType, has_slurs, remove_slurs}; use a |