Files
danbooru/app/helpers/posts_helper.rb
evazion 231e4872ed Re-enable post search counts and view counts.
Re-enable post view counts, post search counts, and missed search
counts. These were disabled in 89adf88d5 because of a bug caused by the
upgrade to rack-2.0.8 in a58dd83ad.

The bug was that rack-2.0.8 changed `session.id` to return a value of a
new wrapper type that doesn't respond to `to_s`. Previously it just
returned a string. Now we have to call `session[:session_id]` or
`session.id.public_id` to get a plain string. This was an undocumented
breaking change in rack-2.0.8 to fix CVE-2019-16782.
2019-12-25 23:53:50 -06:00

123 lines
3.8 KiB
Ruby

module PostsHelper
def post_search_counts_enabled?
Danbooru.config.enable_post_search_counts && Danbooru.config.reportbooru_server.present? && Danbooru.config.reportbooru_key.present?
end
def discover_mode?
params[:tags] =~ /order:rank/ || params[:action] =~ /searches|viewed/
end
def next_page_url
current_page = (params[:page] || 1).to_i
url_for(nav_params_for(current_page + 1)).html_safe
end
def prev_page_url
current_page = (params[:page] || 1).to_i
if current_page >= 2
url_for(nav_params_for(current_page - 1)).html_safe
else
nil
end
end
def missed_post_search_count_js
return unless post_search_counts_enabled?
return unless params[:ms] == "1" && @post_set.post_count == 0 && @post_set.is_single_tag?
sig = generate_reportbooru_signature(params[:tags])
render "posts/partials/index/missed_search_count", sig: sig
end
def post_search_count_js
return unless post_search_counts_enabled?
return unless params[:action] == "index" && params[:page].nil? && params[:tags].present?
tags = Tag.scan_query(params[:tags]).sort.join(" ")
sig = generate_reportbooru_signature("ps-#{tags}")
render "posts/partials/index/search_count", sig: sig
end
def post_view_count_js
return unless post_search_counts_enabled?
msg = generate_reportbooru_signature(params[:id])
render "posts/partials/show/view_count", msg: msg
end
def generate_reportbooru_signature(value)
verifier = ActiveSupport::MessageVerifier.new(Danbooru.config.reportbooru_key, serializer: JSON, digest: "SHA256")
verifier.generate("#{value},#{session[:session_id]}")
end
def post_source_tag(post)
# Only allow http:// and https:// links. Disallow javascript: links.
if post.source =~ %r!\Ahttps?://!i
external_link_to(post.normalized_source, strip: :subdomain) + " ".html_safe + link_to("»", post.source, rel: "external noreferrer nofollow")
else
post.source
end
end
def post_favlist(post)
post.favorited_users.reverse_each.map {|user| link_to_user(user)}.join(", ").html_safe
end
def has_parent_message(post, parent_post_set)
html = ""
html << "This post belongs to a "
html << link_to("parent", posts_path(:tags => "parent:#{post.parent_id}"))
html << " (deleted)" if parent_post_set.parent.first.is_deleted?
sibling_count = parent_post_set.children.count - 1
if sibling_count > 0
html << " and has "
text = (sibling_count == 1) ? "a sibling" : "#{sibling_count} siblings"
html << link_to(text, posts_path(:tags => "parent:#{post.parent_id}"))
end
html << " (#{link_to_wiki "learn more", "help:post_relationships"}) "
html << link_to("&laquo; hide".html_safe, "#", :id => "has-parent-relationship-preview-link")
html.html_safe
end
def has_children_message(post, children_post_set)
html = ""
html << "This post has "
text = (children_post_set.children.count == 1) ? "a child" : "#{children_post_set.children.count} children"
html << link_to(text, posts_path(:tags => "parent:#{post.id}"))
html << " (#{link_to_wiki "learn more", "help:post_relationships"}) "
html << link_to("&laquo; hide".html_safe, "#", :id => "has-children-relationship-preview-link")
html.html_safe
end
def pool_link(pool)
render("posts/partials/show/pool_link", post: @post, pool: pool)
end
def is_pool_selected?(pool)
return false if params.key?(:q)
return false if params.key?(:favgroup_id)
return false if !params.key?(:pool_id)
return params[:pool_id].to_i == pool.id
end
def show_tag_change_notice?
Tag.scan_query(params[:tags]).size == 1 && TagChangeNoticeService.get_forum_topic_id(params[:tags])
end
private
def nav_params_for(page)
query_params = params.except(:controller, :action, :id).merge(page: page).permit!
{ params: query_params }
end
end