Skip to content

Commit 15f02f0

Browse files
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>
1 parent 45c56df commit 15f02f0

19 files changed

Lines changed: 211 additions & 5 deletions

File tree

crates/api/src/local_user/save_settings.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use lemmy_db_schema::{
1414
source::{
1515
actor_language::LocalUserLanguage,
1616
local_user::{LocalUser, LocalUserUpdateForm},
17+
local_user_vote_display_mode::{LocalUserVoteDisplayMode, LocalUserVoteDisplayModeUpdateForm},
1718
person::{Person, PersonUpdateForm},
1819
},
1920
traits::Crud,
@@ -136,5 +137,15 @@ pub async fn save_user_settings(
136137
.await
137138
.ok();
138139

140+
// Update the vote display modes
141+
let vote_display_modes_form = LocalUserVoteDisplayModeUpdateForm {
142+
score: data.show_scores,
143+
upvotes: data.show_upvotes,
144+
downvotes: data.show_downvotes,
145+
upvote_percentage: data.show_upvote_percentage,
146+
};
147+
LocalUserVoteDisplayMode::update(&mut context.pool(), local_user_id, &vote_display_modes_form)
148+
.await?;
149+
139150
Ok(Json(SuccessResponse::default()))
140151
}

crates/api_common/src/person.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ pub struct SaveUserSettings {
8686
pub show_nsfw: Option<bool>,
8787
pub blur_nsfw: Option<bool>,
8888
pub auto_expand: Option<bool>,
89-
/// Show post and comment scores.
90-
pub show_scores: Option<bool>,
9189
/// Your user's theme.
9290
pub theme: Option<String>,
9391
pub default_sort_type: Option<SortType>,
@@ -122,13 +120,19 @@ pub struct SaveUserSettings {
122120
pub open_links_in_new_tab: Option<bool>,
123121
/// Enable infinite scroll
124122
pub infinite_scroll_enabled: Option<bool>,
123+
/// A post-view mode that changes how multiple post listings look.
125124
pub post_listing_mode: Option<PostListingMode>,
126125
/// Whether to allow keyboard navigation (for browsing and interacting with posts and comments).
127126
pub enable_keyboard_navigation: Option<bool>,
128127
/// Whether user avatars or inline images in the UI that are gifs should be allowed to play or should be paused
129128
pub enable_animated_images: Option<bool>,
130129
/// Whether to auto-collapse bot comments.
131130
pub collapse_bot_comments: Option<bool>,
131+
/// Some vote display mode settings
132+
pub show_scores: Option<bool>,
133+
pub show_upvotes: Option<bool>,
134+
pub show_downvotes: Option<bool>,
135+
pub show_upvote_percentage: Option<bool>,
132136
}
133137

134138
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq, Hash)]

crates/api_crud/src/user/create.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use lemmy_db_schema::{
2121
source::{
2222
captcha_answer::{CaptchaAnswer, CheckCaptchaAnswer},
2323
local_user::{LocalUser, LocalUserInsertForm},
24+
local_user_vote_display_mode::LocalUserVoteDisplayMode,
2425
person::{Person, PersonInsertForm},
2526
registration_application::{RegistrationApplication, RegistrationApplicationInsertForm},
2627
},
@@ -183,6 +184,7 @@ pub async fn register(
183184
if local_site.require_email_verification {
184185
let local_user_view = LocalUserView {
185186
local_user: inserted_local_user,
187+
local_user_vote_display_mode: LocalUserVoteDisplayMode::default(),
186188
person: inserted_person,
187189
counts: PersonAggregates::default(),
188190
};

crates/apub/src/api/user_settings_backup.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use lemmy_db_schema::{
1717
instance::Instance,
1818
instance_block::{InstanceBlock, InstanceBlockForm},
1919
local_user::{LocalUser, LocalUserUpdateForm},
20+
local_user_vote_display_mode::{LocalUserVoteDisplayMode, LocalUserVoteDisplayModeUpdateForm},
2021
person::{Person, PersonUpdateForm},
2122
person_block::{PersonBlock, PersonBlockForm},
2223
post::{PostSaved, PostSavedForm},
@@ -50,6 +51,7 @@ pub struct UserSettingsBackup {
5051
// TODO: might be worth making a separate struct for settings backup, to avoid breakage in case
5152
// fields are renamed, and to avoid storing unnecessary fields like person_id or email
5253
pub settings: Option<LocalUser>,
54+
pub vote_display_mode_settings: Option<LocalUserVoteDisplayMode>,
5355
#[serde(default)]
5456
pub followed_communities: Vec<ObjectId<ApubCommunity>>,
5557
#[serde(default)]
@@ -80,6 +82,7 @@ pub async fn export_settings(
8082
matrix_id: local_user_view.person.matrix_user_id,
8183
bot_account: local_user_view.person.bot_account.into(),
8284
settings: Some(local_user_view.local_user),
85+
vote_display_mode_settings: Some(local_user_view.local_user_vote_display_mode),
8386
followed_communities: vec_into(lists.followed_communities),
8487
blocked_communities: vec_into(lists.blocked_communities),
8588
blocked_instances: lists.blocked_instances,
@@ -132,6 +135,27 @@ pub async fn import_settings(
132135
)
133136
.await?;
134137

138+
// Update the vote display mode settings
139+
let vote_display_mode_form = LocalUserVoteDisplayModeUpdateForm {
140+
score: data.vote_display_mode_settings.as_ref().map(|s| s.score),
141+
upvotes: data.vote_display_mode_settings.as_ref().map(|s| s.upvotes),
142+
downvotes: data
143+
.vote_display_mode_settings
144+
.as_ref()
145+
.map(|s| s.downvotes),
146+
upvote_percentage: data
147+
.vote_display_mode_settings
148+
.as_ref()
149+
.map(|s| s.upvote_percentage),
150+
};
151+
152+
LocalUserVoteDisplayMode::update(
153+
&mut context.pool(),
154+
local_user_view.local_user.id,
155+
&vote_display_mode_form,
156+
)
157+
.await?;
158+
135159
let url_count = data.followed_communities.len()
136160
+ data.blocked_communities.len()
137161
+ data.blocked_users.len()

crates/db_schema/src/impls/local_user.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
source::{
55
actor_language::{LocalUserLanguage, SiteLanguage},
66
local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
7+
local_user_vote_display_mode::{LocalUserVoteDisplayMode, LocalUserVoteDisplayModeInsertForm},
78
},
89
traits::Crud,
910
utils::{
@@ -211,6 +212,12 @@ impl Crud for LocalUser {
211212
LocalUserLanguage::update(pool, vec![], local_user_.id).await?;
212213
}
213214

215+
// Create their vote_display_modes
216+
let vote_display_mode_form = LocalUserVoteDisplayModeInsertForm::builder()
217+
.local_user_id(local_user_.id)
218+
.build();
219+
LocalUserVoteDisplayMode::create(pool, &vote_display_mode_form).await?;
220+
214221
Ok(local_user_)
215222
}
216223
async fn update(
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use crate::{
2+
newtypes::LocalUserId,
3+
schema::local_user_vote_display_mode,
4+
source::local_user_vote_display_mode::{
5+
LocalUserVoteDisplayMode,
6+
LocalUserVoteDisplayModeInsertForm,
7+
LocalUserVoteDisplayModeUpdateForm,
8+
},
9+
utils::{get_conn, DbPool},
10+
};
11+
use diesel::{dsl::insert_into, result::Error, QueryDsl};
12+
use diesel_async::RunQueryDsl;
13+
14+
impl LocalUserVoteDisplayMode {
15+
pub async fn read(pool: &mut DbPool<'_>) -> Result<Self, Error> {
16+
let conn = &mut get_conn(pool).await?;
17+
local_user_vote_display_mode::table
18+
.first::<Self>(conn)
19+
.await
20+
}
21+
22+
pub async fn create(
23+
pool: &mut DbPool<'_>,
24+
form: &LocalUserVoteDisplayModeInsertForm,
25+
) -> Result<Self, Error> {
26+
let conn = &mut get_conn(pool).await?;
27+
insert_into(local_user_vote_display_mode::table)
28+
.values(form)
29+
.get_result::<Self>(conn)
30+
.await
31+
}
32+
pub async fn update(
33+
pool: &mut DbPool<'_>,
34+
local_user_id: LocalUserId,
35+
form: &LocalUserVoteDisplayModeUpdateForm,
36+
) -> Result<(), Error> {
37+
// avoid error "There are no changes to save. This query cannot be built"
38+
if form.is_empty() {
39+
return Ok(());
40+
}
41+
let conn = &mut get_conn(pool).await?;
42+
diesel::update(local_user_vote_display_mode::table.find(local_user_id))
43+
.set(form)
44+
.get_result::<Self>(conn)
45+
.await?;
46+
Ok(())
47+
}
48+
}
49+
50+
impl LocalUserVoteDisplayModeUpdateForm {
51+
fn is_empty(&self) -> bool {
52+
self.score.is_none()
53+
&& self.upvotes.is_none()
54+
&& self.downvotes.is_none()
55+
&& self.upvote_percentage.is_none()
56+
}
57+
}

crates/db_schema/src/impls/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod language;
1818
pub mod local_site;
1919
pub mod local_site_rate_limit;
2020
pub mod local_user;
21+
pub mod local_user_vote_display_mode;
2122
pub mod login_token;
2223
pub mod moderator;
2324
pub mod password_reset_request;

crates/db_schema/src/schema.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,16 @@ diesel::table! {
454454
}
455455
}
456456

457+
diesel::table! {
458+
local_user_vote_display_mode (local_user_id) {
459+
local_user_id -> Int4,
460+
score -> Bool,
461+
upvotes -> Bool,
462+
downvotes -> Bool,
463+
upvote_percentage -> Bool,
464+
}
465+
}
466+
457467
diesel::table! {
458468
login_token (token) {
459469
token -> Text,
@@ -961,6 +971,7 @@ diesel::joinable!(local_site_rate_limit -> local_site (local_site_id));
961971
diesel::joinable!(local_user -> person (person_id));
962972
diesel::joinable!(local_user_language -> language (language_id));
963973
diesel::joinable!(local_user_language -> local_user (local_user_id));
974+
diesel::joinable!(local_user_vote_display_mode -> local_user (local_user_id));
964975
diesel::joinable!(login_token -> local_user (user_id));
965976
diesel::joinable!(mod_add_community -> community (community_id));
966977
diesel::joinable!(mod_ban_from_community -> community (community_id));
@@ -1043,6 +1054,7 @@ diesel::allow_tables_to_appear_in_same_query!(
10431054
local_site_rate_limit,
10441055
local_user,
10451056
local_user_language,
1057+
local_user_vote_display_mode,
10461058
login_token,
10471059
mod_add,
10481060
mod_add_community,

crates/db_schema/src/source/local_user.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct LocalUser {
3636
pub show_avatars: bool,
3737
pub send_notifications_to_email: bool,
3838
/// Whether to show comment / post scores.
39+
// TODO now that there is a vote_display_mode, this can be gotten rid of in future releases.
3940
pub show_scores: bool,
4041
/// Whether to show bot accounts.
4142
pub show_bot_accounts: bool,
@@ -55,6 +56,7 @@ pub struct LocalUser {
5556
pub infinite_scroll_enabled: bool,
5657
/// Whether the person is an admin.
5758
pub admin: bool,
59+
/// A post-view mode that changes how multiple post listings look.
5860
pub post_listing_mode: PostListingMode,
5961
pub totp_2fa_enabled: bool,
6062
/// Whether to allow keyboard navigation (for browsing and interacting with posts and comments).
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use crate::newtypes::LocalUserId;
2+
#[cfg(feature = "full")]
3+
use crate::schema::local_user_vote_display_mode;
4+
use serde::{Deserialize, Serialize};
5+
use serde_with::skip_serializing_none;
6+
#[cfg(feature = "full")]
7+
use ts_rs::TS;
8+
use typed_builder::TypedBuilder;
9+
10+
#[skip_serializing_none]
11+
#[derive(PartialEq, Eq, Debug, Clone, Default, Serialize, Deserialize)]
12+
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
13+
#[cfg_attr(feature = "full", diesel(table_name = local_user_vote_display_mode))]
14+
#[cfg_attr(feature = "full", diesel(primary_key(local_user_id)))]
15+
#[cfg_attr(
16+
feature = "full",
17+
diesel(belongs_to(crate::source::local_site::LocalUser))
18+
)]
19+
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
20+
#[cfg_attr(feature = "full", ts(export))]
21+
/// The vote display settings for your user.
22+
pub struct LocalUserVoteDisplayMode {
23+
pub local_user_id: LocalUserId,
24+
pub score: bool,
25+
pub upvotes: bool,
26+
pub downvotes: bool,
27+
pub upvote_percentage: bool,
28+
}
29+
30+
#[derive(Clone, TypedBuilder)]
31+
#[builder(field_defaults(default))]
32+
#[cfg_attr(feature = "full", derive(Insertable))]
33+
#[cfg_attr(feature = "full", diesel(table_name = local_user_vote_display_mode))]
34+
pub struct LocalUserVoteDisplayModeInsertForm {
35+
#[builder(!default)]
36+
pub local_user_id: LocalUserId,
37+
pub score: Option<bool>,
38+
pub upvotes: Option<bool>,
39+
pub downvotes: Option<bool>,
40+
pub upvote_percentage: Option<bool>,
41+
}
42+
43+
#[derive(Clone, Default)]
44+
#[cfg_attr(feature = "full", derive(AsChangeset))]
45+
#[cfg_attr(feature = "full", diesel(table_name = local_user_vote_display_mode))]
46+
pub struct LocalUserVoteDisplayModeUpdateForm {
47+
pub score: Option<bool>,
48+
pub upvotes: Option<bool>,
49+
pub downvotes: Option<bool>,
50+
pub upvote_percentage: Option<bool>,
51+
}

0 commit comments

Comments
 (0)