From d95bdd61a4c88902bc4bc413247d87f368dd91cb Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 12 Aug 2020 13:47:32 +0200 Subject: [PATCH] Add post_read back in, do some cleanup --- server/lemmy_db/src/activity.rs | 4 -- server/lemmy_db/src/category.rs | 4 -- server/lemmy_db/src/comment.rs | 4 -- server/lemmy_db/src/lib.rs | 10 +-- server/lemmy_db/src/moderator.rs | 36 ---------- server/lemmy_db/src/password_reset_request.rs | 3 - server/lemmy_db/src/post.rs | 69 ++++++++++++++++--- server/lemmy_db/src/private_message.rs | 4 -- server/lemmy_db/src/schema.rs | 12 ++++ server/lemmy_db/src/site.rs | 4 -- server/lemmy_db/src/user_mention.rs | 4 -- server/lemmy_db/src/user_view.rs | 4 -- 12 files changed, 76 insertions(+), 82 deletions(-) diff --git a/server/lemmy_db/src/activity.rs b/server/lemmy_db/src/activity.rs index 905c88be9..a83233426 100644 --- a/server/lemmy_db/src/activity.rs +++ b/server/lemmy_db/src/activity.rs @@ -34,10 +34,6 @@ impl Crud for Activity { activity.find(activity_id).first::(conn) } - fn delete(_conn: &PgConnection, _activity_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result { use crate::schema::activity::dsl::*; insert_into(activity) diff --git a/server/lemmy_db/src/category.rs b/server/lemmy_db/src/category.rs index f8eaedb97..ff4e757bf 100644 --- a/server/lemmy_db/src/category.rs +++ b/server/lemmy_db/src/category.rs @@ -23,10 +23,6 @@ impl Crud for Category { category.find(category_id).first::(conn) } - fn delete(_conn: &PgConnection, _category_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, new_category: &CategoryForm) -> Result { insert_into(category) .values(new_category) diff --git a/server/lemmy_db/src/comment.rs b/server/lemmy_db/src/comment.rs index db678070a..6802ad62c 100644 --- a/server/lemmy_db/src/comment.rs +++ b/server/lemmy_db/src/comment.rs @@ -172,10 +172,6 @@ pub struct CommentLikeForm { } impl Likeable for CommentLike { - fn read(_conn: &PgConnection, _comment_id_from: i32) -> Result, Error> { - unimplemented!() - } - fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result { use crate::schema::comment_like::dsl::*; insert_into(comment_like) diff --git a/server/lemmy_db/src/lib.rs b/server/lemmy_db/src/lib.rs index edfc26468..3c22d1776 100644 --- a/server/lemmy_db/src/lib.rs +++ b/server/lemmy_db/src/lib.rs @@ -50,9 +50,12 @@ pub trait Crud { fn update(conn: &PgConnection, id: i32, form: &T) -> Result where Self: Sized; - fn delete(conn: &PgConnection, id: i32) -> Result + fn delete(_conn: &PgConnection, _id: i32) -> Result where - Self: Sized; + Self: Sized, + { + unimplemented!() + } } pub trait Followable { @@ -74,9 +77,6 @@ pub trait Joinable { } pub trait Likeable { - fn read(conn: &PgConnection, id: i32) -> Result, Error> - where - Self: Sized; fn like(conn: &PgConnection, form: &T) -> Result where Self: Sized; diff --git a/server/lemmy_db/src/moderator.rs b/server/lemmy_db/src/moderator.rs index da5e9cc3c..70c253fe3 100644 --- a/server/lemmy_db/src/moderator.rs +++ b/server/lemmy_db/src/moderator.rs @@ -41,10 +41,6 @@ impl Crud for ModRemovePost { mod_remove_post.find(from_id).first::(conn) } - fn delete(_conn: &PgConnection, _from_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result { use crate::schema::mod_remove_post::dsl::*; insert_into(mod_remove_post) @@ -84,10 +80,6 @@ impl Crud for ModLockPost { mod_lock_post.find(from_id).first::(conn) } - fn delete(_conn: &PgConnection, _from_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result { use crate::schema::mod_lock_post::dsl::*; insert_into(mod_lock_post) @@ -127,10 +119,6 @@ impl Crud for ModStickyPost { mod_sticky_post.find(from_id).first::(conn) } - fn delete(_conn: &PgConnection, _from_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result { use crate::schema::mod_sticky_post::dsl::*; insert_into(mod_sticky_post) @@ -172,10 +160,6 @@ impl Crud for ModRemoveComment { mod_remove_comment.find(from_id).first::(conn) } - fn delete(_conn: &PgConnection, _from_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result { use crate::schema::mod_remove_comment::dsl::*; insert_into(mod_remove_comment) @@ -219,10 +203,6 @@ impl Crud for ModRemoveCommunity { mod_remove_community.find(from_id).first::(conn) } - fn delete(_conn: &PgConnection, _from_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result { use crate::schema::mod_remove_community::dsl::*; insert_into(mod_remove_community) @@ -272,10 +252,6 @@ impl Crud for ModBanFromCommunity { mod_ban_from_community.find(from_id).first::(conn) } - fn delete(_conn: &PgConnection, _from_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result { use crate::schema::mod_ban_from_community::dsl::*; insert_into(mod_ban_from_community) @@ -323,10 +299,6 @@ impl Crud for ModBan { mod_ban.find(from_id).first::(conn) } - fn delete(_conn: &PgConnection, _from_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, form: &ModBanForm) -> Result { use crate::schema::mod_ban::dsl::*; insert_into(mod_ban).values(form).get_result::(conn) @@ -366,10 +338,6 @@ impl Crud for ModAddCommunity { mod_add_community.find(from_id).first::(conn) } - fn delete(_conn: &PgConnection, _from_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result { use crate::schema::mod_add_community::dsl::*; insert_into(mod_add_community) @@ -409,10 +377,6 @@ impl Crud for ModAdd { mod_add.find(from_id).first::(conn) } - fn delete(_conn: &PgConnection, _from_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, form: &ModAddForm) -> Result { use crate::schema::mod_add::dsl::*; insert_into(mod_add).values(form).get_result::(conn) diff --git a/server/lemmy_db/src/password_reset_request.rs b/server/lemmy_db/src/password_reset_request.rs index 8c54e3295..06615187e 100644 --- a/server/lemmy_db/src/password_reset_request.rs +++ b/server/lemmy_db/src/password_reset_request.rs @@ -28,9 +28,6 @@ impl Crud for PasswordResetRequest { .find(password_reset_request_id) .first::(conn) } - fn delete(_conn: &PgConnection, _password_reset_request_id: i32) -> Result { - unimplemented!() - } fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result { insert_into(password_reset_request) .values(form) diff --git a/server/lemmy_db/src/post.rs b/server/lemmy_db/src/post.rs index 44cdb4e9a..33b78cd6e 100644 --- a/server/lemmy_db/src/post.rs +++ b/server/lemmy_db/src/post.rs @@ -1,10 +1,4 @@ -use crate::{ - naive_now, - schema::{post, post_like, post_saved}, - Crud, - Likeable, - Saveable, -}; +use crate::{naive_now, schema::{post, post_like, post_read, post_saved}, Crud, Likeable, Saveable, Readable}; use diesel::{dsl::*, result::Error, *}; use serde::{Deserialize, Serialize}; use url::{ParseError, Url}; @@ -200,9 +194,6 @@ pub struct PostLikeForm { } impl Likeable for PostLike { - fn read(_conn: &PgConnection, _post_id_from: i32) -> Result, Error> { - unimplemented!() - } fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result { use crate::schema::post_like::dsl::*; insert_into(post_like) @@ -255,6 +246,46 @@ impl Saveable for PostSaved { } } +#[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 for PostRead { + fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result { + use crate::schema::post_read::dsl::*; + insert_into(post_read) + .values(post_read_form) + .get_result::(conn) + } + + fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result { + use crate::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 crate::{ @@ -397,10 +428,26 @@ mod tests { 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(); @@ -410,8 +457,10 @@ mod tests { 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/lemmy_db/src/private_message.rs b/server/lemmy_db/src/private_message.rs index af911015f..16c6f9bf8 100644 --- a/server/lemmy_db/src/private_message.rs +++ b/server/lemmy_db/src/private_message.rs @@ -37,10 +37,6 @@ impl Crud for PrivateMessage { private_message.find(private_message_id).first::(conn) } - fn delete(_conn: &PgConnection, _private_message_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result { use crate::schema::private_message::dsl::*; insert_into(private_message) diff --git a/server/lemmy_db/src/schema.rs b/server/lemmy_db/src/schema.rs index 431dd70ad..c446edd9f 100644 --- a/server/lemmy_db/src/schema.rs +++ b/server/lemmy_db/src/schema.rs @@ -359,6 +359,15 @@ table! { } } +table! { + post_read (id) { + id -> Int4, + post_id -> Int4, + user_id -> Int4, + published -> Timestamp, + } +} + table! { post_saved (id) { id -> Int4, @@ -504,6 +513,8 @@ 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)); @@ -536,6 +547,7 @@ allow_tables_to_appear_in_same_query!( post, post_aggregates_fast, post_like, + post_read, post_saved, private_message, site, diff --git a/server/lemmy_db/src/site.rs b/server/lemmy_db/src/site.rs index 9250370bf..36b3e833d 100644 --- a/server/lemmy_db/src/site.rs +++ b/server/lemmy_db/src/site.rs @@ -39,10 +39,6 @@ impl Crud for Site { site.first::(conn) } - fn delete(_conn: &PgConnection, _site_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, new_site: &SiteForm) -> Result { use crate::schema::site::dsl::*; insert_into(site).values(new_site).get_result::(conn) diff --git a/server/lemmy_db/src/user_mention.rs b/server/lemmy_db/src/user_mention.rs index 1831cedcc..a8ec88d21 100644 --- a/server/lemmy_db/src/user_mention.rs +++ b/server/lemmy_db/src/user_mention.rs @@ -28,10 +28,6 @@ impl Crud for UserMention { user_mention.find(user_mention_id).first::(conn) } - fn delete(_conn: &PgConnection, _user_mention_id: i32) -> Result { - unimplemented!() - } - fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result { use crate::schema::user_mention::dsl::*; insert_into(user_mention) diff --git a/server/lemmy_db/src/user_view.rs b/server/lemmy_db/src/user_view.rs index caa2babaf..08f4c79cf 100644 --- a/server/lemmy_db/src/user_view.rs +++ b/server/lemmy_db/src/user_view.rs @@ -159,10 +159,6 @@ impl<'a> UserQueryBuilder<'a> { } impl UserView { - pub fn read(_conn: &PgConnection, _from_user_id: i32) -> Result { - unimplemented!() - } - pub fn admins(conn: &PgConnection) -> Result, Error> { use super::user_view::user_fast::dsl::*; use diesel::sql_types::{Nullable, Text};