2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-03-17 19:20:44 -04:00
|
|
|
class PostPresenter < Presenter
|
2022-07-11 14:17:24 -04:00
|
|
|
attr_reader :pool
|
2022-09-17 07:18:33 -04:00
|
|
|
delegate :post_show_sidebar_tag_list_html, :split_tag_list_text, :inline_tag_list_html, to: :tag_set_presenter
|
2013-07-23 16:55:48 -04:00
|
|
|
|
2011-11-06 16:59:21 -05:00
|
|
|
def self.preview(post, options = {})
|
2015-06-20 12:50:57 -04:00
|
|
|
if post.nil?
|
2024-02-25 05:38:55 -05:00
|
|
|
return ""
|
2015-06-20 12:50:57 -04:00
|
|
|
end
|
|
|
|
|
2022-09-29 10:10:04 -04:00
|
|
|
if !options[:show_deleted] && post.is_deleted? && options[:tags] !~ /(?:status:(?:all|any|deleted))|(?:deletedby:)|(?:delreason:)/i
|
2013-03-22 12:38:53 -04:00
|
|
|
return ""
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2019-04-17 22:44:12 -04:00
|
|
|
if post.loginblocked? || post.safeblocked?
|
2013-02-17 15:50:42 -05:00
|
|
|
return ""
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2020-02-05 23:05:04 -05:00
|
|
|
options[:stats] |= !options[:avatar] && !options[:inline]
|
|
|
|
|
2018-10-29 20:46:01 -04:00
|
|
|
locals = {}
|
|
|
|
|
|
|
|
locals[:article_attrs] = {
|
2024-11-18 11:38:22 -05:00
|
|
|
"id" => "post_#{post.id}",
|
|
|
|
"class" => preview_class(post, **options).join(" "),
|
2018-10-29 20:46:01 -04:00
|
|
|
}.merge(data_attributes(post))
|
|
|
|
|
2018-11-06 15:33:04 -05:00
|
|
|
locals[:link_target] = options[:link_target] || post
|
|
|
|
|
|
|
|
locals[:link_params] = {}
|
2019-08-13 14:54:14 -04:00
|
|
|
if options[:tags].present?
|
2018-10-29 20:46:01 -04:00
|
|
|
locals[:link_params]["q"] = options[:tags]
|
|
|
|
end
|
2018-11-06 15:33:04 -05:00
|
|
|
if options[:pool_id]
|
|
|
|
locals[:link_params]["pool_id"] = options[:pool_id]
|
2018-10-29 20:46:01 -04:00
|
|
|
end
|
2019-03-20 08:14:08 -04:00
|
|
|
if options[:post_set_id]
|
|
|
|
locals[:link_params]["post_set_id"] = options[:post_set_id]
|
2018-06-22 18:56:15 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2019-11-30 19:05:05 -05:00
|
|
|
locals[:tooltip] = "Rating: #{post.rating}\nID: #{post.id}\nDate: #{post.created_at}\nStatus: #{post.status}\nScore: #{post.score}\n\n#{post.tag_string}"
|
2018-10-29 20:46:01 -04:00
|
|
|
|
2021-11-14 16:16:36 -05:00
|
|
|
locals[:cropped_url] = if Danbooru.config.enable_image_cropping? && options[:show_cropped] && post.has_cropped? && !CurrentUser.user.disable_cropped_thumbnails?
|
2020-09-26 01:28:26 -04:00
|
|
|
post.crop_file_url
|
|
|
|
else
|
|
|
|
post.preview_file_url
|
|
|
|
end
|
2017-09-12 16:03:59 -04:00
|
|
|
|
2019-02-15 06:14:35 -05:00
|
|
|
locals[:cropped_url] = Danbooru.config.deleted_preview_url if post.deleteblocked?
|
|
|
|
locals[:preview_url] = if post.deleteblocked?
|
|
|
|
Danbooru.config.deleted_preview_url
|
|
|
|
else
|
|
|
|
post.preview_file_url
|
|
|
|
end
|
2018-10-29 20:46:01 -04:00
|
|
|
|
2024-11-18 11:38:22 -05:00
|
|
|
locals[:alt_text] = "post ##{post.id}"
|
2018-10-29 20:46:01 -04:00
|
|
|
|
|
|
|
locals[:has_cropped] = post.has_cropped?
|
2014-08-25 19:41:27 -04:00
|
|
|
|
|
|
|
if options[:pool]
|
2018-10-29 20:46:01 -04:00
|
|
|
locals[:pool] = options[:pool]
|
|
|
|
else
|
|
|
|
locals[:pool] = nil
|
2015-02-20 23:04:23 -05:00
|
|
|
end
|
|
|
|
|
2018-11-02 19:38:28 -04:00
|
|
|
locals[:width] = post.image_width
|
|
|
|
locals[:height] = post.image_height
|
|
|
|
|
2017-11-17 17:28:16 -05:00
|
|
|
if options[:similarity]
|
2018-10-29 20:46:01 -04:00
|
|
|
locals[:similarity] = options[:similarity].round
|
|
|
|
else
|
|
|
|
locals[:similarity] = nil
|
2017-11-17 17:28:16 -05:00
|
|
|
end
|
|
|
|
|
2015-02-20 23:04:23 -05:00
|
|
|
if options[:size]
|
2018-10-29 20:46:01 -04:00
|
|
|
locals[:size] = post.file_size
|
2023-02-13 09:19:57 -05:00
|
|
|
locals[:file_ext] = post.file_ext
|
2018-10-29 20:46:01 -04:00
|
|
|
else
|
|
|
|
locals[:size] = nil
|
2014-08-25 19:41:27 -04:00
|
|
|
end
|
|
|
|
|
2019-08-03 21:49:41 -04:00
|
|
|
if options[:stats]
|
|
|
|
locals[:post] = post
|
|
|
|
locals[:stats] = true
|
2020-02-05 23:05:04 -05:00
|
|
|
else
|
|
|
|
locals[:stats] = false
|
2019-08-03 21:49:41 -04:00
|
|
|
end
|
|
|
|
|
2018-10-29 20:46:01 -04:00
|
|
|
ApplicationController.render(partial: "posts/partials/index/preview", locals: locals)
|
2012-02-17 15:15:12 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2022-09-13 10:14:00 -04:00
|
|
|
def self.preview_class(post, pool: nil, size: nil, similarity: nil, **options)
|
2024-11-18 11:38:22 -05:00
|
|
|
klass = ["thumbnail"]
|
|
|
|
klass << "pending" if post.is_pending?
|
|
|
|
klass << "flagged" if post.is_flagged?
|
|
|
|
klass << "deleted" if post.is_deleted?
|
|
|
|
klass << "has-parent" if post.parent_id
|
|
|
|
klass << "has-children" if post.has_visible_children?
|
|
|
|
klass << "rating-safe" if post.rating == "s"
|
|
|
|
klass << "rating-questionable" if post.rating == "q"
|
|
|
|
klass << "rating-explicit" if post.rating == "e"
|
2024-08-04 18:14:46 -04:00
|
|
|
klass << "blacklistable" unless options[:no_blacklist]
|
2013-04-28 08:36:55 -04:00
|
|
|
klass
|
|
|
|
end
|
|
|
|
|
2020-09-26 01:28:26 -04:00
|
|
|
def self.data_attributes(post, include_post: false)
|
2024-07-26 16:12:54 -04:00
|
|
|
attributes = post.thumbnail_attributes
|
|
|
|
attributes[:post] = post_attribute_attribute(post).to_json if include_post
|
|
|
|
{ data: attributes }
|
2013-07-11 16:52:42 -04:00
|
|
|
end
|
|
|
|
|
2020-09-26 01:28:26 -04:00
|
|
|
def self.post_attribute_attribute(post)
|
|
|
|
alternate_samples = {}
|
|
|
|
Danbooru.config.video_rescales.each do |k,v|
|
|
|
|
next unless post.has_sample_size?(k)
|
|
|
|
dims = post.scaled_sample_dimensions(v)
|
|
|
|
alternate_samples[k] = {
|
|
|
|
type: 'video',
|
|
|
|
height: dims[1],
|
|
|
|
width: dims[0],
|
|
|
|
urls: post.visible? ? [post.scaled_url_ext(k, 'webm'), post.scaled_url_ext(k, 'mp4')] : [nil, nil]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
if post.has_sample_size?('original')
|
|
|
|
alternate_samples['original'] = {
|
|
|
|
type: 'video',
|
|
|
|
height: post.image_height,
|
|
|
|
width: post.image_width,
|
|
|
|
urls: post.visible? ? [nil, post.file_url_ext('mp4')] : [nil, nil]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
Danbooru.config.image_rescales.each do |k,v|
|
|
|
|
next unless post.has_sample_size?(k)
|
|
|
|
dims = post.scaled_sample_dimensions(v)
|
|
|
|
alternate_samples[k] = {
|
|
|
|
type: 'image',
|
|
|
|
height: dims[1],
|
|
|
|
width: dims[0],
|
|
|
|
url: post.visible? ? post.scaled_url_ext(k, 'jpg') : nil
|
|
|
|
}
|
|
|
|
end
|
|
|
|
{
|
|
|
|
id: post.id,
|
|
|
|
created_at: post.created_at,
|
|
|
|
updated_at: post.updated_at,
|
|
|
|
fav_count: post.fav_count,
|
2023-05-10 12:39:38 -04:00
|
|
|
comment_count: post.visible_comment_count(CurrentUser),
|
2020-09-26 01:28:26 -04:00
|
|
|
change_seq: post.change_seq,
|
|
|
|
uploader_id: post.uploader_id,
|
2024-10-30 19:52:53 -04:00
|
|
|
title: post.title,
|
2020-09-26 01:28:26 -04:00
|
|
|
description: post.description,
|
2024-11-10 14:58:15 -05:00
|
|
|
transcript: post.transcript,
|
2020-09-26 01:28:26 -04:00
|
|
|
flags: {
|
|
|
|
pending: post.is_pending,
|
|
|
|
flagged: post.is_flagged,
|
2023-05-10 13:16:48 -04:00
|
|
|
note_locked: post.is_note_locked,
|
|
|
|
status_locked: post.is_status_locked,
|
|
|
|
rating_locked: post.is_rating_locked,
|
2020-09-26 01:28:26 -04:00
|
|
|
deleted: post.is_deleted,
|
|
|
|
has_notes: post.has_notes?
|
|
|
|
},
|
|
|
|
score: {
|
|
|
|
up: post.up_score,
|
|
|
|
down: post.down_score,
|
|
|
|
total: post.score
|
|
|
|
},
|
|
|
|
relationships: {
|
|
|
|
parent_id: post.parent_id,
|
|
|
|
has_children: post.has_children,
|
|
|
|
has_active_children: post.has_active_children,
|
|
|
|
children: []
|
|
|
|
},
|
|
|
|
pools: post.pool_ids,
|
|
|
|
file: {
|
|
|
|
width: post.image_width,
|
|
|
|
height: post.image_height,
|
|
|
|
ext: post.file_ext,
|
|
|
|
size: post.file_size,
|
|
|
|
md5: post.md5,
|
|
|
|
url: post.visible? ? post.file_url : nil
|
|
|
|
},
|
|
|
|
sample: {
|
|
|
|
has: post.has_large?,
|
|
|
|
height: post.large_image_height,
|
|
|
|
width: post.large_image_width,
|
|
|
|
url: post.visible? ? post.large_file_url : nil,
|
|
|
|
alternates: alternate_samples
|
|
|
|
},
|
|
|
|
sources: post.source&.split('\n'),
|
|
|
|
tags: post.tag_string.split(' '),
|
|
|
|
locked_tags: post.locked_tags&.split(' ') || [],
|
|
|
|
is_favorited: post.is_favorited?
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2020-01-01 20:02:27 -05:00
|
|
|
def image_attributes
|
|
|
|
attributes = {
|
|
|
|
:id => "image",
|
|
|
|
class: @post.display_class_for(CurrentUser.user),
|
|
|
|
:alt => humanized_essential_tag_string,
|
|
|
|
"itemprop" => "contentUrl"
|
|
|
|
}
|
|
|
|
|
|
|
|
if @post.bg_color
|
|
|
|
attributes['style'] = "background-color: ##{@post.bg_color};"
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
2010-03-26 16:19:12 -04:00
|
|
|
def initialize(post)
|
2010-03-17 19:20:44 -04:00
|
|
|
@post = post
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2017-02-24 21:59:21 -05:00
|
|
|
def tag_set_presenter
|
|
|
|
@tag_set_presenter ||= TagSetPresenter.new(@post.tag_array)
|
|
|
|
end
|
|
|
|
|
2010-11-06 03:08:27 -04:00
|
|
|
def preview_html
|
|
|
|
PostPresenter.preview(@post)
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2012-10-30 17:58:52 -04:00
|
|
|
def humanized_tag_string
|
2012-11-02 18:22:08 -04:00
|
|
|
@post.tag_string.split(/ /).slice(0, 25).join(", ").tr("_", " ")
|
2012-10-30 17:58:52 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2012-10-30 17:58:52 -04:00
|
|
|
def humanized_essential_tag_string
|
2018-09-30 21:39:12 -04:00
|
|
|
@humanized_essential_tag_string ||= tag_set_presenter.humanized_essential_tag_string(default: "##{@post.id}")
|
2012-10-30 17:58:52 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2018-03-18 19:11:44 -04:00
|
|
|
def filename_for_download
|
|
|
|
"#{humanized_essential_tag_string} - #{@post.md5}.#{@post.file_ext}"
|
|
|
|
end
|
|
|
|
|
2013-03-07 18:31:20 -05:00
|
|
|
def has_nav_links?(template)
|
2023-08-31 09:46:56 -04:00
|
|
|
has_sequential_navigation?(template.params) || @post.has_active_pools? || @post.post_sets.owned.any?
|
2017-07-24 20:30:30 -04:00
|
|
|
end
|
|
|
|
|
2017-08-04 20:28:36 -04:00
|
|
|
def has_sequential_navigation?(params)
|
2023-08-31 02:57:55 -04:00
|
|
|
return false if TagQuery.has_metatag?(params[:q], "order")
|
2019-03-20 08:14:08 -04:00
|
|
|
return false if params[:pool_id].present? || params[:post_set_id].present?
|
2019-06-01 11:19:22 -04:00
|
|
|
true
|
2013-03-07 18:31:20 -05:00
|
|
|
end
|
2020-01-02 12:16:40 -05:00
|
|
|
|
2022-03-16 12:08:40 -04:00
|
|
|
def default_image_size(user)
|
|
|
|
return "original" if @post.force_original_size?
|
2020-01-02 12:16:40 -05:00
|
|
|
return "fit" if user.default_image_size == "large" && !@post.allow_sample_resize?
|
|
|
|
user.default_image_size
|
|
|
|
end
|
2010-03-17 19:20:44 -04:00
|
|
|
end
|