add e621 posts controller, index posts filter view

This commit is contained in:
Dylan Knutson
2024-12-25 23:37:38 +00:00
parent 6b4e11e907
commit 3c41cd5b7d
12 changed files with 146 additions and 15 deletions

View File

@@ -0,0 +1,5 @@
class Domain::E621::PostsController < ApplicationController
def show
@post = Domain::E621::Post.find_by!(e621_id: params[:e621_id])
end
end

View File

@@ -1,10 +1,12 @@
class IndexedPostsController < ApplicationController
def index
@posts =
IndexedPost
.includes(:postable)
.order(created_at: :desc)
.page(params[:page])
.per(50)
@posts = IndexedPost.includes(postable: :file)
active_sources = (params[:sources] || SourceHelper.all_source_names)
unless SourceHelper.has_all_sources?(active_sources)
postable_types = SourceHelper.source_names_to_class_names(active_sources)
@posts =
@posts.where(postable_type: postable_types) if postable_types.any?
end
@posts = @posts.order(created_at: :desc).page(params[:page]).per(50)
end
end

View File

@@ -0,0 +1,21 @@
module SourceHelper
def self.source_name_to_class_name
{
"furaffinity" => "Domain::Fa::Post",
"e621" => "Domain::E621::Post",
"inkbunny" => "Domain::Inkbunny::Post",
}
end
def self.all_source_names
source_name_to_class_name.keys
end
def self.source_names_to_class_names(list)
list.map { |source| source_name_to_class_name[source] }.compact
end
def self.has_all_sources?(list)
list.sort == all_source_names.sort
end
end

View File

@@ -9,7 +9,7 @@ module HasIndexedPost
validate: false,
autosave: true
after_initialize :ensure_indexed_post!
before_create :ensure_indexed_post!
def ensure_indexed_post!
self.indexed_post ||=

View File

@@ -1,7 +1,9 @@
class IndexedPost < ReduxApplicationRecord
belongs_to :postable, polymorphic: true, inverse_of: :indexed_post
validates_presence_of :postable_id, :postable_type
belongs_to :postable, polymorphic: true, inverse_of: :indexed_post
has_one :file, through: :postable
def show_path
case postable_type
when "Domain::Fa::Post"
@@ -27,10 +29,10 @@ class IndexedPost < ReduxApplicationRecord
def file_sha256
case postable_type
when "Domain::Fa::Post"
postable&.file&.response_sha256
when "Domain::E621::Post"
when "Domain::Fa::Post", "Domain::E621::Post"
postable&.file&.response_sha256
when "Domain::Inkbunny::Post"
postable&.files&.first&.blob_entry_sha256
else
raise("Unsupported postable type: #{postable_type}")
end

View File

@@ -0,0 +1,69 @@
<div
id="<%= dom_id post %>"
class="mx-auto mt-4 flex w-full max-w-2xl flex-col gap-4 pb-4"
>
<section class="rounded-md border border-slate-300 bg-slate-50 p-4">
<span class="text-md italic">E621 Post #<%= @post.e621_id %></span>
<div>
Rating: <%= @post.rating.upcase %> | Score: <%= @post.score %> | Created:
<%= @post.created_at.strftime("%Y-%m-%d") %>
</div>
<% if @post.artists_array.any? %>
<div>Artists: <%= @post.artists_array.join(", ") %></div>
<% end %>
</section>
<section>
<% if @post.file %>
<%= render partial: "log_entries/content_container",
locals: {
log_entry: @post.file,
} %>
<% else %>
<div class="p-4 text-center italic text-slate-400">No file available</div>
<% end %>
</section>
<section class="sky-section">
<div class="section-header">Post Description</div>
<% if @post.description.present? %>
<div class="bg-slate-800 p-4 text-slate-200">
<%= simple_format(@post.description) %>
</div>
<% else %>
<div class="bg-slate-50 p-4 text-center italic text-slate-400">
No description
</div>
<% end %>
</section>
<section class="sky-section">
<div class="section-header">Tags</div>
<div class="bg-slate-100 p-4">
<% if @post.tags_array.any? %>
<div class="flex flex-wrap gap-2">
<% @post.tags_array.each do |tag| %>
<span class="rounded bg-slate-200 px-2 py-1 text-sm"
><%= tag %></span
>
<% end %>
</div>
<% else %>
<div class="text-center italic text-slate-400">No tags</div>
<% end %>
</div>
</section>
<% if @post.sources_array.any? %>
<section class="sky-section">
<div class="section-header">Sources</div>
<div class="bg-slate-100 p-4">
<% @post.sources_array.each do |source| %>
<div>
<%= link_to source, source, class: "text-blue-600 hover:underline", target: "_blank" %>
</div>
<% end %>
</div>
</section>
<% end %>
</div>

View File

@@ -0,0 +1,2 @@
<p style="color: green"><%= notice %></p>
<%= render @post %>

View File

@@ -6,7 +6,7 @@
</div>
<div class="flex items-center justify-center p-4">
<% if post.file_blob_entry.present? %>
<% if post.file_sha256.present? %>
<%= link_to post.show_path do %>
<img
class="max-h-[300px] max-w-[300px] rounded-md border border-slate-300 object-contain shadow-md"

View File

@@ -2,6 +2,33 @@
<h1 class="text-2xl">All Posts <%= page_str(params) %></h1>
</div>
<div class="mx-auto mb-4 mt-4 flex justify-center gap-4">
<% active_sources = (params[:sources] || SourceHelper.all_source_names).uniq %>
<% SourceHelper.all_source_names.each do |source| %>
<% is_active = active_sources.include?(source) %>
<% link_sources =
(
if is_active
active_sources - [source]
else
active_sources + [source]
end
) %>
<% posts_path_url =
if SourceHelper.has_all_sources?(link_sources)
indexed_posts_path
else
indexed_posts_path(sources: link_sources)
end %>
<%= link_to(
"#{source.titleize} #{is_active ? "(On)" : "(Off)"}",
posts_path_url,
class:
"px-4 py-2 rounded #{is_active ? "bg-blue-500 text-white" : "bg-gray-300"}",
) %>
<% end %>
</div>
<%= render partial: "shared/pagination_controls", locals: { collection: @posts } %>
<div class="mx-auto flex flex-wrap justify-center">

View File

@@ -1,4 +1,7 @@
if Rails.env.development? || Rails.env.staging?
# Rack::MiniProfiler.config.pre_authorize_cb = lambda { |env| true }
Rack::MiniProfiler.config.authorization_mode = :allow_all
Rack::MiniProfiler.config.enable_advanced_debugging_tools = true
Rack::MiniProfiler.config.position = "top-right"
Rack::MiniProfiler.config.disable_caching = false
Rack::MiniProfiler.config.skip_paths = [%r{/blobs/.+/contents.jpg$}]

View File

@@ -62,7 +62,7 @@ Rails.application.routes.draw do
end
end
resources :posts, only: [:index], controller: "indexed_posts"
resources :indexed_posts, only: [:index], path: "posts"
resources :blobs, only: [:show], slug: :sha256

View File

@@ -4,7 +4,7 @@
"@4az/prettier-plugin-html-erb@^0.0.6":
version "0.0.6"
resolved "https://registry.yarnpkg.com/@4az/prettier-plugin-html-erb/-/prettier-plugin-html-erb-0.0.6.tgz#1040cd6e3bc488c6668b03120eb119caebc49fc7"
resolved "https://registry.npmjs.org/@4az/prettier-plugin-html-erb/-/prettier-plugin-html-erb-0.0.6.tgz"
integrity sha512-PB2jcgU6mIlp8tTLzDD756FcT1+lKKCCwACh/XX+hU5nEEaNsdX6eHZkznlqyw1LiFpyYGc381M44gywOpPy4Q==
"@alloc/quick-lru@^5.2.0":
@@ -3993,7 +3993,7 @@ postcss@^8.4.19, postcss@^8.4.21, postcss@^8.4.47, postcss@^8.4.49:
prettier-plugin-tailwindcss@^0.6.9:
version "0.6.9"
resolved "https://registry.yarnpkg.com/prettier-plugin-tailwindcss/-/prettier-plugin-tailwindcss-0.6.9.tgz#db84c32918eae9b44e5a5f0aa4d1249cc39fa739"
resolved "https://registry.npmjs.org/prettier-plugin-tailwindcss/-/prettier-plugin-tailwindcss-0.6.9.tgz"
integrity sha512-r0i3uhaZAXYP0At5xGfJH876W3HHGHDp+LCRUJrs57PBeQ6mYHMwr25KH8NPX44F2yGTvdnH7OqCshlQx183Eg==
prettier@^3.4.2: