[Posts] Standardize thumbnail attributes (#684)

Thumbnails used to have four different sets of data-attributes depending on how they were displayed.
This commit is contained in:
Cinder 2024-07-26 13:12:54 -07:00
parent ba5b6b0fff
commit 164f5b4851
6 changed files with 88 additions and 110 deletions

View File

@ -9,7 +9,7 @@ module DeferredPosts
def deferred_posts def deferred_posts
Post.includes(:uploader).where(id: deferred_post_ids.to_a).find_each.reduce({}) do |post_hash, p| Post.includes(:uploader).where(id: deferred_post_ids.to_a).find_each.reduce({}) do |post_hash, p|
post_hash[p.id] = p.minimal_attributes post_hash[p.id] = p.thumbnail_attributes
post_hash post_hash
end end
end end

View File

@ -23,28 +23,7 @@ class PostsDecorator < ApplicationDecorator
end end
def data_attributes def data_attributes
post = object { data: object.thumbnail_attributes }
attributes = {
"data-id" => post.id,
"data-has-sound" => post.has_tag?("video_with_sound", "flash_with_sound"),
"data-tags" => post.tag_string,
"data-rating" => post.rating,
"data-flags" => post.status_flags,
"data-uploader-id" => post.uploader_id,
"data-uploader" => post.uploader_name,
"data-file-ext" => post.file_ext,
"data-score" => post.score,
"data-fav-count" => post.fav_count,
"data-is-favorited" => post.favorited_by?(CurrentUser.user.id)
}
if post.visible?
attributes["data-file-url"] = post.file_url
attributes["data-large-file-url"] = post.large_file_url
attributes["data-preview-file-url"] = post.preview_file_url
end
attributes
end end
def cropped_url(options) def cropped_url(options)

View File

@ -238,7 +238,7 @@ Blacklist.postMatch = function (post, entry) {
uploader_id: $post.data("uploader-id"), uploader_id: $post.data("uploader-id"),
user: $post.data("uploader").toString().toLowerCase(), user: $post.data("uploader").toString().toLowerCase(),
flags: $post.data("flags"), flags: $post.data("flags"),
is_fav: $post.data("is-favorited"), is_favorited: $post.data("is-favorited"),
}; };
return Blacklist.postMatchObject(post_data, entry); return Blacklist.postMatchObject(post_data, entry);
}; };
@ -272,7 +272,7 @@ Blacklist.postMatchObject = function (post, entry) {
tags.push(`user:${post.user}`); tags.push(`user:${post.user}`);
tags.push(`height:${post.height}`); tags.push(`height:${post.height}`);
tags.push(`width:${post.width}`); tags.push(`width:${post.width}`);
if (post.is_fav) if (post.is_favorited)
tags.push("fav:me"); tags.push("fav:me");
$.each(post.flags.match(/\S+/g) || [], function (i, v) { $.each(post.flags.match(/\S+/g) || [], function (i, v) {
tags.push(`status:${v}`); tags.push(`status:${v}`);

View File

@ -4,61 +4,74 @@ import LS from "./local_storage";
const Thumbnails = {}; const Thumbnails = {};
Thumbnails.initialize = function () { Thumbnails.initialize = function () {
const clearPlaceholder = function (post) {
if (post.hasClass("thumb-placeholder-link")) {
post.removeClass("thumb-placeholder-link");
} else {
post.empty();
}
};
const postsData = window.___deferred_posts || {}; const postsData = window.___deferred_posts || {};
const posts = $(".post-thumb.placeholder, .thumb-placeholder-link"); const posts = $(".post-thumb.placeholder, .thumb-placeholder-link");
const DAB = LS.get("dab") === "1"; const DAB = LS.get("dab") === "1";
$.each(posts, function (i, post) {
const p = $(post); for (const post of posts) {
const postID = p.data("id"); const $post = $(post);
// Placeholder is valid
const postID = $post.data("id");
if (!postID) { if (!postID) {
clearPlaceholder(p); clearPlaceholder($post);
return; return;
} }
// Data exists for this post
const postData = postsData[postID]; const postData = postsData[postID];
if (!postData) { if (!postData) {
clearPlaceholder(p); clearPlaceholder($post);
return; return;
} }
let blacklist_hit_count = 0;
$.each(Blacklist.entries, function (j, entry) { // Building the element
if (Blacklist.postMatchObject(postData, entry)) { const thumbnail = $("<div>")
.addClass("post-thumbnail blacklistable")
.toggleClass("dtext", $post.hasClass("thumb-placeholder-link"));
for (const key in postData)
thumbnail.attr("data-" + key.replace(/_/g, "-"), postData[key]);
const link = $("<a>")
.attr("href", `/posts/${postData.id}`)
.appendTo(thumbnail);
$("<img>")
.attr({
src: postData["preview_url"] || "/images/deleted-preview.png",
height: postData["preview_url"] ? postData["preview_height"] : 150,
width: postData["preview_url"] ? postData["preview_width"] : 150,
title: `Rating: ${postData.rating}\r\nID: ${postData.id}\r\nStatus: ${postData.flags}\r\nDate: ${postData["created_at"]}\r\n\r\n${postData.tags}`,
alt: postData.tags,
class: "post-thumbnail-img",
})
.appendTo(link);
// Disgusting implementation of the blacklist
if (!DAB) {
let blacklist_hit_count = 0;
for (const entry of Blacklist.entries) {
if (!Blacklist.postMatchObject(postData, entry))
continue;
entry.hits += 1; entry.hits += 1;
blacklist_hit_count += 1; blacklist_hit_count += 1;
} }
});
const newTag = $("<div>"); if (blacklist_hit_count > 0)
const blacklisted = DAB ? false : blacklist_hit_count > 0; thumbnail.addClass("blacklisted");
for (const key in postData) {
newTag.attr("data-" + key.replace(/_/g, "-"), postData[key]);
} }
newTag.attr("class", blacklisted ? "post-thumbnail blacklisted" : "post-thumbnail");
if (p.hasClass("thumb-placeholder-link")) $post.replaceWith(thumbnail);
newTag.addClass("dtext"); }
const img = $("<img>");
img.attr("src", postData.preview_url || "/images/deleted-preview.png"); function clearPlaceholder (post) {
img.attr({ if (post.hasClass("thumb-placeholder-link"))
height: postData.preview_url ? postData.preview_height : 150, post.removeClass("thumb-placeholder-link");
width: postData.preview_url ? postData.preview_width : 150, else post.empty();
title: `Rating: ${postData.rating}\r\nID: ${postData.id}\r\nStatus: ${postData.status}\r\nDate: ${postData.created_at}\r\n\r\n${postData.tags}`, }
alt: postData.tags,
class: "post-thumbnail-img",
});
const link = $("<a>");
link.attr("href", `/posts/${postData.id}`);
link.append(img);
newTag.append(link);
p.replaceWith(newTag);
});
}; };
$(document).ready(function () { $(() => {
Thumbnails.initialize(); Thumbnails.initialize();
$(window).on("e621:add_deferred_posts", (_, posts) => { $(window).on("e621:add_deferred_posts", (_, posts) => {
window.___deferred_posts = window.___deferred_posts || {}; window.___deferred_posts = window.___deferred_posts || {};

View File

@ -1408,31 +1408,39 @@ class Post < ApplicationRecord
list list
end end
def minimal_attributes def thumbnail_attributes
preview_dims = preview_dimensions attributes = {
hash = { id: id,
status: status, flags: status_flags,
flags: status_flags, tags: tag_string,
file_ext: file_ext, rating: rating,
id: id, file_ext: file_ext,
created_at: created_at,
rating: rating, width: image_width,
preview_width: preview_dims[1], height: image_height,
width: image_width, size: file_size,
preview_height: preview_dims[0],
height: image_height, created_at: created_at,
tags: tag_string, uploader: uploader_name,
score: score, uploader_id: uploader_id,
uploader_id: uploader_id,
uploader: uploader_name score: score,
fav_count: fav_count,
is_favorited: favorited_by?(CurrentUser.user.id),
pools: pool_ids,
} }
if visible? if visible?
hash[:md5] = md5 attributes[:md5] = md5
hash[:preview_url] = preview_file_url attributes[:preview_url] = preview_file_url
hash[:cropped_url] = crop_file_url attributes[:large_url] = large_file_url
attributes[:file_url] = file_url
attributes[:preview_width] = preview_dimensions[1]
attributes[:preview_height] = preview_dimensions[0]
end end
hash
attributes
end end
def status def status

View File

@ -105,31 +105,9 @@ class PostPresenter < Presenter
end end
def self.data_attributes(post, include_post: false) def self.data_attributes(post, include_post: false)
attributes = { attributes = post.thumbnail_attributes
"data-id" => post.id, attributes[:post] = post_attribute_attribute(post).to_json if include_post
"data-has-sound" => post.has_tag?("video_with_sound", "flash_with_sound"), { data: attributes }
"data-tags" => post.tag_string,
"data-rating" => post.rating,
"data-width" => post.image_width,
"data-height" => post.image_height,
"data-flags" => post.status_flags,
"data-score" => post.score,
"data-file-ext" => post.file_ext,
"data-uploader-id" => post.uploader_id,
"data-uploader" => post.uploader_name,
"data-is-favorited" => post.favorited_by?(CurrentUser.user.id)
}
if post.visible?
attributes["data-md5"] = post.md5
attributes["data-file-url"] = post.file_url
attributes["data-large-file-url"] = post.large_file_url
attributes["data-preview-file-url"] = post.preview_file_url
end
attributes["data-post"] = post_attribute_attribute(post).to_json if include_post
attributes
end end
def self.post_attribute_attribute(post) def self.post_attribute_attribute(post)