From 2350362183ebd48849749c63841109ccfea1edf4 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 6 Jul 2022 01:58:42 -0500 Subject: [PATCH] uploads: add ability to search your uploads by AI tags. Add ability to search your unposted uploads using AI tags. Like with media assets, only basic tags are supported (no metatags) and complex multi-tag searches will probably be slow. The default AI tag confidence threshold is 50%. There's a hidden search[min_score] URL param that lets you change this. --- app/models/upload.rb | 21 ++++++++++++++++++++- app/views/uploads/index.html.erb | 14 ++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/models/upload.rb b/app/models/upload.rb index 447326d8d..09f2f7bb3 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -87,16 +87,35 @@ class Upload < ApplicationRecord end end + def self.ai_tags_match(tag_string, score_range: (50..)) + upload_media_assets = AITagQuery.search(tag_string, relation: UploadMediaAsset.all, foreign_key: :media_asset_id, score_range: score_range) + where(upload_media_assets.where("upload_media_assets.upload_id = uploads.id").arel.exists) + end + def self.search(params) q = search_attributes(params, :id, :created_at, :updated_at, :source, :referer_url, :status, :media_asset_count, :uploader, :upload_media_assets, :media_assets, :posts) + if params[:ai_tags_match].present? + min_score = params.fetch(:min_score, 50).to_i + q = q.ai_tags_match(params[:ai_tags_match], score_range: (min_score..)) + end + if params[:is_posted].to_s.truthy? q = q.where.not(id: Upload.where.missing(:posts)) elsif params[:is_posted].to_s.falsy? q = q.where(id: Upload.where.missing(:posts)) end - q.apply_default_order(params) + case params[:order] + when "id", "id_desc" + q = q.order(id: :desc) + when "id_asc" + q = q.order(id: :asc) + else + q = q.apply_default_order(params) + end + + q end def async_process_upload! diff --git a/app/views/uploads/index.html.erb b/app/views/uploads/index.html.erb index 1d32549e3..35b3a9a68 100644 --- a/app/views/uploads/index.html.erb +++ b/app/views/uploads/index.html.erb @@ -2,10 +2,20 @@

My Uploads

+ <%= search_form_for(current_page_path) do |f| %> + <%= f.input :ai_tags_match, label: "Tags", input_html: { value: params.dig(:search, :ai_tags_match), data: { autocomplete: "tag-query" } } %> + <%= f.input :status, collection: %w[pending completed error], include_blank: true, selected: params.dig(:search, :status) %> + <%= f.input :is_posted, as: :hidden, input_html: { value: params.dig(:search, :is_posted) } %> + <%= f.input :min_score, as: :hidden, input_html: { value: params.dig(:search, :min_score) } %> + + <%= f.input :order, collection: [%w[Newest id], %w[Oldest id_asc]], include_blank: true, selected: params[:search][:order] %> + <%= f.submit "Search" %> + <% end %> +
<%= link_to "All", current_page_path(search: search_params.to_h.without("is_posted")), class: ["inline-block p-1 pb-2", (search_params[:is_posted].nil? ? "border-current border-b-2 -mb-px" : "inactive-link")] %> - <%= link_to "Posted", current_page_path(search: { is_posted: true }), class: ["inline-block p-1 pb-2", (search_params[:is_posted].to_s.truthy? ? "border-current border-b-2 -mb-px" : "inactive-link")] %> - <%= link_to "Unposted", current_page_path(search: { is_posted: false }), class: ["inline-block p-1 pb-2", (search_params[:is_posted].to_s.falsy? ? "border-current border-b-2 -mb-px" : "inactive-link")] %> + <%= link_to "Posted", current_page_path(search: search_params.merge(is_posted: true)), class: ["inline-block p-1 pb-2", (search_params[:is_posted].to_s.truthy? ? "border-current border-b-2 -mb-px" : "inactive-link")] %> + <%= link_to "Unposted", current_page_path(search: search_params.merge(is_posted: false)), class: ["inline-block p-1 pb-2", (search_params[:is_posted].to_s.falsy? ? "border-current border-b-2 -mb-px" : "inactive-link")] %>