Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a vote_display_mode local_user setting. (#4450)
* Add a vote_display_mode local_user setting. - Fixes #4449 * Changing HideDownvotes to Score. * Adding ScoreAndDownvote display mode. * Adding upvote and downvote mode. * Extracting vote_display_mode to another table. * Fixing fmt. * Remove published and updated columns. --------- Co-authored-by: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com>
- Loading branch information
1 parent
45c56df
commit 15f02f0
Showing
19 changed files
with
211 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
crates/db_schema/src/impls/local_user_vote_display_mode.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::{ | ||
newtypes::LocalUserId, | ||
schema::local_user_vote_display_mode, | ||
source::local_user_vote_display_mode::{ | ||
LocalUserVoteDisplayMode, | ||
LocalUserVoteDisplayModeInsertForm, | ||
LocalUserVoteDisplayModeUpdateForm, | ||
}, | ||
utils::{get_conn, DbPool}, | ||
}; | ||
use diesel::{dsl::insert_into, result::Error, QueryDsl}; | ||
use diesel_async::RunQueryDsl; | ||
|
||
impl LocalUserVoteDisplayMode { | ||
pub async fn read(pool: &mut DbPool<'_>) -> Result<Self, Error> { | ||
let conn = &mut get_conn(pool).await?; | ||
local_user_vote_display_mode::table | ||
.first::<Self>(conn) | ||
.await | ||
} | ||
|
||
pub async fn create( | ||
pool: &mut DbPool<'_>, | ||
form: &LocalUserVoteDisplayModeInsertForm, | ||
) -> Result<Self, Error> { | ||
let conn = &mut get_conn(pool).await?; | ||
insert_into(local_user_vote_display_mode::table) | ||
.values(form) | ||
.get_result::<Self>(conn) | ||
.await | ||
} | ||
pub async fn update( | ||
pool: &mut DbPool<'_>, | ||
local_user_id: LocalUserId, | ||
form: &LocalUserVoteDisplayModeUpdateForm, | ||
) -> Result<(), Error> { | ||
// avoid error "There are no changes to save. This query cannot be built" | ||
if form.is_empty() { | ||
return Ok(()); | ||
} | ||
let conn = &mut get_conn(pool).await?; | ||
diesel::update(local_user_vote_display_mode::table.find(local_user_id)) | ||
.set(form) | ||
.get_result::<Self>(conn) | ||
.await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl LocalUserVoteDisplayModeUpdateForm { | ||
fn is_empty(&self) -> bool { | ||
self.score.is_none() | ||
&& self.upvotes.is_none() | ||
&& self.downvotes.is_none() | ||
&& self.upvote_percentage.is_none() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
crates/db_schema/src/source/local_user_vote_display_mode.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::newtypes::LocalUserId; | ||
#[cfg(feature = "full")] | ||
use crate::schema::local_user_vote_display_mode; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_with::skip_serializing_none; | ||
#[cfg(feature = "full")] | ||
use ts_rs::TS; | ||
use typed_builder::TypedBuilder; | ||
|
||
#[skip_serializing_none] | ||
#[derive(PartialEq, Eq, Debug, Clone, Default, Serialize, Deserialize)] | ||
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))] | ||
#[cfg_attr(feature = "full", diesel(table_name = local_user_vote_display_mode))] | ||
#[cfg_attr(feature = "full", diesel(primary_key(local_user_id)))] | ||
#[cfg_attr( | ||
feature = "full", | ||
diesel(belongs_to(crate::source::local_site::LocalUser)) | ||
)] | ||
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] | ||
#[cfg_attr(feature = "full", ts(export))] | ||
/// The vote display settings for your user. | ||
pub struct LocalUserVoteDisplayMode { | ||
pub local_user_id: LocalUserId, | ||
pub score: bool, | ||
pub upvotes: bool, | ||
pub downvotes: bool, | ||
pub upvote_percentage: bool, | ||
} | ||
|
||
#[derive(Clone, TypedBuilder)] | ||
#[builder(field_defaults(default))] | ||
#[cfg_attr(feature = "full", derive(Insertable))] | ||
#[cfg_attr(feature = "full", diesel(table_name = local_user_vote_display_mode))] | ||
pub struct LocalUserVoteDisplayModeInsertForm { | ||
#[builder(!default)] | ||
pub local_user_id: LocalUserId, | ||
pub score: Option<bool>, | ||
pub upvotes: Option<bool>, | ||
pub downvotes: Option<bool>, | ||
pub upvote_percentage: Option<bool>, | ||
} | ||
|
||
#[derive(Clone, Default)] | ||
#[cfg_attr(feature = "full", derive(AsChangeset))] | ||
#[cfg_attr(feature = "full", diesel(table_name = local_user_vote_display_mode))] | ||
pub struct LocalUserVoteDisplayModeUpdateForm { | ||
pub score: Option<bool>, | ||
pub upvotes: Option<bool>, | ||
pub downvotes: Option<bool>, | ||
pub upvote_percentage: Option<bool>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.