Fix #4930: "Show scores" setting should be preserved

Make "show scores" setting persistent.

The setting is stored in a `post_preview_show_votes` cookie. This means
it's remembered on a per-device basis, but not on a per-account basis.
This is so users without an account can use the setting, and so you can
use different settings on desktop and mobile.

The `view=score` URL param has been replaced by `show_votes=true`. The
`show_votes` URL param overrides the `post_preview_show_votes` cookie.
This commit is contained in:
evazion
2021-12-09 15:40:22 -06:00
parent 3de93f556e
commit 7976d12cd0
6 changed files with 47 additions and 33 deletions

View File

@@ -9,12 +9,13 @@ class PostsController < ApplicationController
format.html { redirect_to(@post) }
end
elsif params[:random].to_s.truthy?
post_set = PostSets::Post.new(params[:tags], params[:page], params[:limit], format: request.format.symbol, view: params[:view])
post_set = PostSets::Post.new(params[:tags], params[:page], params[:limit], format: request.format.symbol)
query = "#{post_set.normalized_query.to_s} random:#{post_set.per_page}".strip
redirect_to posts_path(tags: query, page: params[:page], limit: params[:limit], format: request.format.symbol, view: params[:view], size: params[:size])
redirect_to posts_path(tags: query, page: params[:page], limit: params[:limit], format: request.format.symbol)
else
tag_query = params[:tags] || params.dig(:post, :tags)
@post_set = PostSets::Post.new(tag_query, params[:page], params[:limit], format: request.format.symbol, view: params[:view])
@show_votes = (params[:show_votes].presence || cookies[:post_preview_show_votes].presence || "false").truthy?
@post_set = PostSets::Post.new(tag_query, params[:page], params[:limit], format: request.format.symbol, show_votes: @show_votes)
@preview_size = params[:size].presence || cookies[:post_preview_size].presence || PostPreviewComponent::DEFAULT_SIZE
@posts = authorize @post_set.posts, policy_class: PostPolicy
@post_set.log!
@@ -56,6 +57,7 @@ class PostsController < ApplicationController
def update
@post = authorize Post.find(params[:id])
@post.update(permitted_attributes(@post))
@show_votes = (params[:show_votes].presence || cookies[:post_preview_show_votes].presence || "false").truthy?
@preview_size = params[:size].presence || cookies[:post_preview_size].presence || PostPreviewComponent::DEFAULT_SIZE
respond_with_post_after_update(@post)
end

View File

@@ -26,6 +26,7 @@ Post.initialize_all = function() {
this.initialize_excerpt();
this.initialize_gestures();
this.initialize_post_preview_size_menu();
this.initialize_post_preview_options_menu();
}
if ($("#c-posts").length && $("#a-show").length) {
@@ -255,6 +256,20 @@ Post.initialize_post_preview_size_menu = function() {
});
}
Post.initialize_post_preview_options_menu = function() {
$(document).on("click.danbooru", "a.post-preview-show-votes", (e) => {
Cookie.put("post_preview_show_votes", "true");
location.reload();
e.preventDefault();
});
$(document).on("click.danbooru", "a.post-preview-hide-votes", (e) => {
Cookie.put("post_preview_show_votes", "false");
location.reload();
e.preventDefault();
});
}
Post.view_original = function(e = null) {
if (Utility.test_max_width(660)) {
// Do the default behavior (navigate to image)
@@ -424,10 +439,10 @@ Post.update = async function(post_id, mode, params) {
Post.show_pending_update_notice()
let urlParams = new URLSearchParams(window.location.search);
let view = urlParams.get("view");
let show_votes = urlParams.get("show_votes");
let size = urlParams.get("size");
await $.ajax({ type: "PUT", url: `/posts/${post_id}.js`, data: { mode, view, size, ...params }});
await $.ajax({ type: "PUT", url: `/posts/${post_id}.js`, data: { mode, show_votes, size, ...params }});
Post.pending_update_count -= 1;
Post.show_pending_update_notice();

View File

@@ -7,17 +7,18 @@ module PostSets
MAX_PER_PAGE = 200
MAX_SIDEBAR_TAGS = 25
attr_reader :page, :format, :tag_string, :query, :normalized_query, :view
attr_reader :page, :format, :tag_string, :query, :normalized_query, :show_votes
delegate :post_count, to: :normalized_query
alias_method :show_votes?, :show_votes
def initialize(tags, page = 1, per_page = nil, user: CurrentUser.user, format: "html", view: "simple")
def initialize(tags, page = 1, per_page = nil, user: CurrentUser.user, format: "html", show_votes: false)
@query = PostQueryBuilder.new(tags, user, tag_limit: user.tag_query_limit, safe_mode: CurrentUser.safe_mode?, hide_deleted_posts: user.hide_deleted_posts?)
@normalized_query = query.normalized_query
@tag_string = tags
@page = page
@per_page = per_page
@format = format.to_s
@view = view.presence || "simple"
@show_votes = show_votes
end
def humanized_tag_string
@@ -126,10 +127,6 @@ module PostSets
end
end
def show_votes?
view == "score"
end
def includes
if show_votes?
[:media_asset, :vote_by_current_user]

View File

@@ -21,7 +21,7 @@
<section id="tag-box">
<h2>Tags</h2>
<%= render_search_tag_list(@post_set.related_tags, current_query: params[:tags], show_extra_links: policy(Post).show_extra_links?, search_params: { view: params[:view], size: params[:size] }) %>
<%= render_search_tag_list(@post_set.related_tags, current_query: params[:tags], show_extra_links: policy(Post).show_extra_links?, search_params: { show_votes: params[:show_votes], size: params[:size] }) %>
</section>
<section id="options-box">
@@ -75,69 +75,69 @@
<a class="mobile-only" href="#search-box">Search &raquo;</a>
</li>
<%= render PopupMenuComponent.new(classes: "hidden post-preview-size-menu") do |menu| %>
<%= render PopupMenuComponent.new(classes: "post-preview-size-menu") do |menu| %>
<% menu.button do %>
<span class="text-sm">Size <%= caret_down_icon %></span>
<% end %>
<% menu.item do %>
<% if @preview_size == "150" %>
<%= link_to "Small", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "150"), class: "font-bold", rel: "nofollow" %>
<%= link_to "Small", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "150"), class: "font-bold", rel: "nofollow" %>
<% else %>
<%= link_to "Small", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "150"), rel: "nofollow" %>
<%= link_to "Small", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "150"), rel: "nofollow" %>
<% end %>
<% end %>
<% menu.item do %>
<% if @preview_size == "180" %>
<%= link_to "Medium", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "180"), class: "font-bold", rel: "nofollow" %>
<%= link_to "Medium", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "180"), class: "font-bold", rel: "nofollow" %>
<% else %>
<%= link_to "Medium", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "180"), rel: "nofollow" %>
<%= link_to "Medium", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "180"), rel: "nofollow" %>
<% end %>
<% end %>
<% menu.item do %>
<% if @preview_size == "225" %>
<%= link_to "Large", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "225"), class: "font-bold", rel: "nofollow" %>
<%= link_to "Large", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "225"), class: "font-bold", rel: "nofollow" %>
<% else %>
<%= link_to "Large", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "225"), rel: "nofollow" %>
<%= link_to "Large", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "225"), rel: "nofollow" %>
<% end %>
<% end %>
<%# Mobile only %>
<% menu.item do %>
<% if @preview_size == "360" %>
<%= link_to "Huge", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "360"), class: "mobile-only font-bold", rel: "nofollow" %>
<%= link_to "Huge", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "360"), class: "mobile-only font-bold", rel: "nofollow" %>
<% else %>
<%= link_to "Huge", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "360"), class: "mobile-only", rel: "nofollow" %>
<%= link_to "Huge", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "360"), class: "mobile-only", rel: "nofollow" %>
<% end %>
<% end %>
<%# Desktop only %>
<% menu.item do %>
<% if @preview_size == "270" %>
<%= link_to "Huge", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "270"), class: "desktop-only font-bold", rel: "nofollow" %>
<%= link_to "Huge", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "270"), class: "desktop-only font-bold", rel: "nofollow" %>
<% else %>
<%= link_to "Huge", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "270"), class: "desktop-only", rel: "nofollow" %>
<%= link_to "Huge", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "270"), class: "desktop-only", rel: "nofollow" %>
<% end %>
<% end %>
<%# Desktop only %>
<% menu.item do %>
<% if @preview_size == "360" %>
<%= link_to "Gigantic", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "360"), class: "desktop-only font-bold", rel: "nofollow" %>
<%= link_to "Gigantic", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "360"), class: "desktop-only font-bold", rel: "nofollow" %>
<% else %>
<%= link_to "Gigantic", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: params[:view], size: "360"), class: "desktop-only", rel: "nofollow" %>
<%= link_to "Gigantic", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: params[:show_votes], size: "360"), class: "desktop-only", rel: "nofollow" %>
<% end %>
<% end %>
<% end %>
<%= render PopupMenuComponent.new do |menu| %>
<%= render PopupMenuComponent.new(classes: "post-preview-options-menu") do |menu| %>
<% menu.item do %>
<% if params[:view] == "score" %>
<%= link_to "Hide scores", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: nil, size: params[:size]), rel: "nofollow" %>
<% if @show_votes %>
<%= link_to "Hide scores", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: nil, size: params[:size]), class: "post-preview-hide-votes", rel: "nofollow" %>
<% else %>
<%= link_to "Show scores", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], view: "score", size: params[:size]), rel: "nofollow" %>
<%= link_to "Show scores", posts_path(tags: params[:tags], page: params[:page], limit: params[:limit], show_votes: true, size: params[:size]), class: "post-preview-show-votes", rel: "nofollow" %>
<% end %>
<% end %>
<% end %>

View File

@@ -6,8 +6,8 @@
<% if params[:random] %>
<%= hidden_field_tag :random, params[:random] %>
<% end %>
<% if params[:view] %>
<%= hidden_field_tag :view, params[:view] %>
<% if params[:show_votes] %>
<%= hidden_field_tag :show_votes, params[:show_votes] %>
<% end %>
<% if params[:size] %>
<%= hidden_field_tag :size, params[:size] %>

View File

@@ -1,6 +1,6 @@
<% if @post.valid? %>
var $post = $("#post_<%= @post.id %>");
var $new_post = $("<%= j post_preview(@post, fit: :compact, show_deleted: true, show_votes: params[:view] == "score", size: @preview_size) %>");
var $new_post = $("<%= j post_preview(@post, fit: :compact, show_deleted: true, show_votes: @show_votes, size: @preview_size) %>");
Danbooru.Blacklist.apply_post($new_post.get(0));
$("#post_<%= @post.id %>").replaceWith($new_post);
<% if params[:mode] == "quick-edit" %>