Add post status below post on index pages

This commit is contained in:
Kira 2019-08-03 18:49:41 -07:00
parent 813a700b26
commit 1861441100
10 changed files with 92 additions and 25 deletions

View File

@ -146,6 +146,22 @@ module PostsHelper
Tag.scan_query(params[:tags]).size == 1 && TagChangeNoticeService.get_forum_topic_id(params[:tags]) Tag.scan_query(params[:tags]).size == 1 && TagChangeNoticeService.get_forum_topic_id(params[:tags])
end end
def post_stats_section(post)
status_flags = []
status_flags << 'P' if post.parent_id
status_flags << 'C' if post.has_children?
status_flags << 'U' if post.is_pending?
status_flags << 'F' if post.is_flagged?
post_score_icon = "#{"&uarr;" if post.score > 0}#{"&darr;" if post.score < 0}#{"&varr;" if post.score == 0}"
score = tag.span("#{post_score_icon}#{post.score}".html_safe, class: "post-score-score " + score_class(post.score))
favs = tag.span("&hearts;#{post.fav_count}".html_safe, class: 'post-score-faves')
comments = tag.span "C#{post.comment_count}", class: 'post-score-comments'
rating = tag.span(post.rating.upcase, class: "post-score-rating")
status = tag.span(status_flags.join(''), class: 'post-score-extras')
tag.div score + favs + comments + rating + status, class: 'post-score', id: "post-score-#{post.id}"
end
private private
def nav_params_for(page) def nav_params_for(page)
@ -158,24 +174,24 @@ module PostsHelper
vote_score = voted ? vote.score : 0 vote_score = voted ? vote.score : 0
post_score = post.score post_score = post.score
def score_class(score)
return 'score-neutral' if score == 0
score > 0 ? 'score-positive' : 'score-negative'
end
def confirm_score_class(score, want) def confirm_score_class(score, want)
return 'score-neutral' if score != want || score == 0 return 'score-neutral' if score != want || score == 0
score_class(score) score_class(score)
end end
up_tag = tag.span(tag.a('&#x25B2;'.html_safe, class: 'post-vote-up-link', 'data-id': post.id), up_tag = tag.span(tag.a('&#x25B2;'.html_safe, class: 'post-vote-up-link', 'data-id': post.id),
class: confirm_score_class(vote_score, 1), class: confirm_score_class(vote_score, 1),
id: "post-vote-up-#{post.id}") id: "post-vote-up-#{post.id}")
down_tag = tag.span(tag.a('&#x25BC;'.html_safe, class: 'post-vote-down-link', 'data-id': post.id), down_tag = tag.span(tag.a('&#x25BC;'.html_safe, class: 'post-vote-down-link', 'data-id': post.id),
class: confirm_score_class(vote_score, -1), class: confirm_score_class(vote_score, -1),
id: "post-vote-down-#{post.id}") id: "post-vote-down-#{post.id}")
vote_block = tag.span(" (vote ".html_safe + up_tag + "/" + down_tag + ")") vote_block = tag.span(" (vote ".html_safe + up_tag + "/" + down_tag + ")")
score_tag = tag.span(post.score, class: "post-score #{score_class(post_score)}", id: "post-score-#{post.id}", title: "#{post.up_score} up/#{post.down_score} down") score_tag = tag.span(post.score, class: "post-score #{score_class(post_score)}", id: "post-score-#{post.id}", title: "#{post.up_score} up/#{post.down_score} down")
score_tag + (CurrentUser.is_voter? ? vote_block : '') score_tag + (CurrentUser.is_voter? ? vote_block : '')
end end
def score_class(score)
return 'score-neutral' if score == 0
score > 0 ? 'score-positive' : 'score-negative'
end
end end

View File

@ -127,7 +127,7 @@ module PostIndex
scores = scores[1..-2].split(",").map(&:to_i) scores = scores[1..-2].split(",").map(&:to_i)
[pid.to_i, uids.zip(scores)] [pid.to_i, uids.zip(scores)]
end end
upvote_ids = vote_ids.map { |pid, user| [pid, user.reject { |uid, s| s <= 0 }.map {|uid, _| uid}] }.to_h upvote_ids = vote_ids.map { |pid, user| [pid, user.reject { |uid, s| s <= 0 }.map {|uid, _| uid}] }.to_h
downvote_ids = vote_ids.map { |pid, user| [pid, user.reject { |uid, s| s >= 0 }.map {|uid, _| uid}] }.to_h downvote_ids = vote_ids.map { |pid, user| [pid, user.reject { |uid, s| s >= 0 }.map {|uid, _| uid}] }.to_h
@ -181,7 +181,7 @@ module PostIndex
tag_count_meta: tag_count_meta, tag_count_meta: tag_count_meta,
tag_count_species: tag_count_species, tag_count_species: tag_count_species,
tag_count_invalid: tag_count_invalid, tag_count_invalid: tag_count_invalid,
comment_count: options[:comment_count] || Comment.where(post_id: id).count, comment_count: options[:comment_count] || comment_count,
file_size: file_size, file_size: file_size,
parent: parent_id, parent: parent_id,

View File

@ -35,12 +35,16 @@ $red_color: #e45f5f;
$green_color: #3e9e49; $green_color: #3e9e49;
$dark_grey_color: hsl(0, 0%, 50%); $dark_grey_color: hsl(0, 0%, 50%);
$explicit_rating_color: $red_color;
$questionable_rating_color: hsl(50, 100%, 70%);
$safe_rating_color: $green_color;
@mixin animated-icon { @mixin animated-icon {
content: ""; content: "";
position: absolute; position: absolute;
width: 20px; width: 20px;
height: 20px; height: 20px;
color: white; color: white;
background-color: rgba(0,0,0,0.5); background-color: rgba(0,0,0,0.5);
margin: 2px; margin: 2px;
text-align: center; text-align: center;
@ -51,7 +55,7 @@ $dark_grey_color: hsl(0, 0%, 50%);
position: absolute; position: absolute;
width: 20px; width: 20px;
height: 20px; height: 20px;
color: white; color: white;
background-color: rgba(0,0,0,0.5); background-color: rgba(0,0,0,0.5);
margin: 2px; margin: 2px;
text-align: center; text-align: center;

View File

@ -41,12 +41,31 @@ article.post-preview {
} }
.desc { .desc {
font-size: 80%; background-color: $grey_color;
margin-bottom: 0; font-size: 80%;
margin-bottom: 0;
}
.post-score>span {
margin-left: 0.5em;
}
&.post-rating-explicit .post-score-rating {
color: $explicit_rating_color;
}
&.post-rating-safe .post-score-rating {
color: $safe_rating_color;
}
&.post-rating-questionable .post-score-rating {
color: $questionable_rating_color;
} }
img { img {
margin: auto; margin: auto;
max-height: 150px;
max-width: 150px;
} }
&[data-tags~=animated]:before, &[data-file-ext=swf]:before, &[data-file-ext=webm]:before, &[data-file-ext=mp4]:before, &[data-file-ext=zip]:before { &[data-tags~=animated]:before, &[data-file-ext=swf]:before, &[data-file-ext=webm]:before, &[data-file-ext=mp4]:before, &[data-file-ext=zip]:before {

View File

@ -170,10 +170,16 @@
} }
img { img {
max-width: none;
max-height: none;
width: 33.3vw; width: 33.3vw;
border: none !important; border: none !important;
} }
.desc {
display: none;
}
&[data-tags~=animated]:before, &[data-file-ext=swf]:before, &[data-file-ext=webm]:before, &[data-file-ext=mp4]:before, &[data-file-ext=zip]:before { &[data-tags~=animated]:before, &[data-file-ext=swf]:before, &[data-file-ext=webm]:before, &[data-file-ext=mp4]:before, &[data-file-ext=zip]:before {
@include animated-icon; @include animated-icon;
} }

View File

@ -5,7 +5,7 @@ class Comment < ApplicationRecord
validate :validate_post_exists, :on => :create validate :validate_post_exists, :on => :create
validate :validate_creator_is_not_limited, :on => :create validate :validate_creator_is_not_limited, :on => :create
validates_presence_of :body, :message => "has no content" validates_presence_of :body, :message => "has no content"
belongs_to :post belongs_to :post, counter_cache: :comment_count
belongs_to_creator belongs_to_creator
belongs_to_updater belongs_to_updater
user_status_counter :comment_count user_status_counter :comment_count

View File

@ -20,6 +20,8 @@ class PostPresenter < Presenter
return "" return ""
end end
options[:stats] |= !options[:avatar] && !options[:inline]
locals = {} locals = {}
locals[:article_attrs] = { locals[:article_attrs] = {
@ -40,7 +42,7 @@ class PostPresenter < Presenter
locals[:link_params]["post_set_id"] = options[:post_set_id] locals[:link_params]["post_set_id"] = options[:post_set_id]
end end
locals[:tooltip] = "#{post.tag_string} rating:#{post.rating} score:#{post.score}" locals[:tooltip] = "Rating: #{post.rating}\nID: #{post.id}\nStatus: #{post.status}\nScore: #{post.score}\n\n#{post.tag_string}"
locals[:cropped_url] = if Danbooru.config.enable_image_cropping && options[:show_cropped] && post.has_cropped? && !CurrentUser.user.disable_cropped_thumbnails? locals[:cropped_url] = if Danbooru.config.enable_image_cropping && options[:show_cropped] && post.has_cropped? && !CurrentUser.user.disable_cropped_thumbnails?
post.crop_file_url post.crop_file_url
@ -80,13 +82,18 @@ class PostPresenter < Presenter
locals[:size] = nil locals[:size] = nil
end end
if options[:stats]
locals[:post] = post
locals[:stats] = true
end
ApplicationController.render(partial: "posts/partials/index/preview", locals: locals) ApplicationController.render(partial: "posts/partials/index/preview", locals: locals)
end end
def self.preview_class(post, highlight_score: nil, pool: nil, size: nil, similarity: nil, **options) def self.preview_class(post, highlight_score: nil, pool: nil, size: nil, similarity: nil, **options)
klass = ["post-preview"] klass = ["post-preview", "captioned"]
# klass << " large-cropped" if post.has_cropped? && options[:show_cropped] # Always captioned with new post stats section.
klass << "captioned" if pool || size || similarity # klass << "captioned" if pool || size || similarity
klass << "post-status-pending" if post.is_pending? klass << "post-status-pending" if post.is_pending?
klass << "post-status-flagged" if post.is_flagged? klass << "post-status-flagged" if post.is_flagged?
klass << "post-status-deleted" if post.is_deleted? klass << "post-status-deleted" if post.is_deleted?
@ -94,6 +101,9 @@ class PostPresenter < Presenter
klass << "post-status-has-children" if post.has_visible_children? klass << "post-status-has-children" if post.has_visible_children?
klass << "post-pos-score" if highlight_score && post.score >= 3 klass << "post-pos-score" if highlight_score && post.score >= 3
klass << "post-neg-score" if highlight_score && post.score <= -3 klass << "post-neg-score" if highlight_score && post.score <= -3
klass << "post-rating-safe" if post.rating == 's'
klass << "post-rating-questionable" if post.rating == 'q'
klass << "post-rating-explicit" if post.rating == 'e'
klass klass
end end

View File

@ -1,11 +1,16 @@
<%= content_tag(:article, article_attrs) do -%> <%= content_tag(:article, article_attrs) do -%>
<%= link_to polymorphic_path(link_target, link_params) do -%> <%= link_to polymorphic_path(link_target, link_params) do -%>
<%= content_tag(:picture) do -%> <%= content_tag(:picture) do -%>
<%= tag.source media: "(max-width: 660px)", srcset: cropped_url -%> <%= tag.source media: "(max-width: 800px)", srcset: cropped_url -%>
<%= tag.source media: "(min-width: 660px)", srcset: preview_url -%> <%= tag.source media: "(min-width: 800px)", srcset: preview_url -%>
<%= tag.img class: "has-cropped-#{has_cropped}", src: preview_url, title: tooltip, alt: alt_text -%> <%= tag.img class: "has-cropped-#{has_cropped}", src: preview_url, title: tooltip, alt: alt_text -%>
<% end -%> <% end -%>
<% end -%> <% end -%>
<% if stats -%>
<div class="desc">
<%= post_stats_section(post) %>
</div>
<% end -%>
<% if pool -%> <% if pool -%>
<p class="desc"> <p class="desc">
<%= link_to pool.pretty_name.truncate(80), pool %> <%= link_to pool.pretty_name.truncate(80), pool %>

View File

@ -0,0 +1,5 @@
class AddPostCommentCounter < ActiveRecord::Migration[5.2]
def change
add_column :posts, :comment_count, :integer, null: false, default: 0
end
end

View File

@ -1966,7 +1966,8 @@ CREATE TABLE public.posts (
locked_tags text, locked_tags text,
tag_count_species integer DEFAULT 0 NOT NULL, tag_count_species integer DEFAULT 0 NOT NULL,
tag_count_invalid integer DEFAULT 0 NOT NULL, tag_count_invalid integer DEFAULT 0 NOT NULL,
description text DEFAULT ''::text NOT NULL description text DEFAULT ''::text NOT NULL,
comment_count integer DEFAULT 0 NOT NULL
); );
@ -5184,6 +5185,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20190714122705'), ('20190714122705'),
('20190717205018'), ('20190717205018'),
('20190718201354'), ('20190718201354'),
('20190801210547'); ('20190801210547'),
('20190804010156');