[Posts] Add verified artist search (#663)

This commit is contained in:
Donovan Daniels 2024-09-07 15:34:25 -05:00 committed by GitHub
parent 84dcafc8c8
commit f315504660
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 80 additions and 26 deletions

View File

@ -72,6 +72,7 @@ module PostIndex
deleted: { type: "boolean" },
has_children: { type: "boolean" },
has_pending_replacements: { type: "boolean" },
artverified: { type: "boolean" },
},
},
}
@ -153,20 +154,24 @@ module PostIndex
LEFT OUTER JOIN post_replacements2 pr ON p.id = pr.post_id AND pr.status = 'pending'
WHERE p.id IN (#{post_ids})
SQL
verified_artists_sql = <<-SQL.squish
SELECT name, linked_user_id FROM artists WHERE linked_user_id IS NOT NULL
SQL
# Run queries
conn = ApplicationRecord.connection
deletions = conn.execute(deletion_sql)
deleter_ids = deletions.values.map {|p,did,dr| [p,did]}.to_h
del_reasons = deletions.values.map {|p,did,dr| [p,dr]}.to_h
comment_counts = conn.execute(comments_sql).values.to_h
pool_ids = conn.execute(pools_sql).values.map(&array_parse).to_h
set_ids = conn.execute(sets_sql).values.map(&array_parse).to_h
fave_ids = conn.execute(faves_sql).values.map(&array_parse).to_h
commenter_ids = conn.execute(commenter_sql).values.map(&array_parse).to_h
noter_ids = conn.execute(noter_sql).values.map(&array_parse).to_h
child_ids = conn.execute(child_sql).values.map(&array_parse).to_h
notes = Hash.new { |h,k| h[k] = [] }
deletions = conn.execute(deletion_sql)
deleter_ids = deletions.values.map {|p,did,dr| [p,did]}.to_h
del_reasons = deletions.values.map {|p,did,dr| [p,dr]}.to_h
comment_counts = conn.execute(comments_sql).values.to_h
pool_ids = conn.execute(pools_sql).values.map(&array_parse).to_h
set_ids = conn.execute(sets_sql).values.map(&array_parse).to_h
fave_ids = conn.execute(faves_sql).values.map(&array_parse).to_h
commenter_ids = conn.execute(commenter_sql).values.map(&array_parse).to_h
noter_ids = conn.execute(noter_sql).values.map(&array_parse).to_h
child_ids = conn.execute(child_sql).values.map(&array_parse).to_h
verified_artists = conn.execute(verified_artists_sql).values.to_h
notes = Hash.new { |h,k| h[k] = [] }
conn.execute(note_sql).values.each { |p,b| notes[p] << b }
pending_replacements = conn.execute(pending_replacements_sql).values.to_h
@ -183,19 +188,20 @@ module PostIndex
empty = []
batch.map! do |p|
index_options = {
comment_count: comment_counts[p.id] || 0,
pools: pool_ids[p.id] || empty,
sets: set_ids[p.id] || empty,
faves: fave_ids[p.id] || empty,
upvotes: upvote_ids[p.id] || empty,
downvotes: downvote_ids[p.id] || empty,
children: child_ids[p.id] || empty,
commenters: commenter_ids[p.id] || empty,
noters: noter_ids[p.id] || empty,
notes: notes[p.id] || empty,
deleter: deleter_ids[p.id] || empty,
del_reason: del_reasons[p.id] || empty,
has_pending_replacements: pending_replacements[p.id]
comment_count: comment_counts[p.id] || 0,
pools: pool_ids[p.id] || empty,
sets: set_ids[p.id] || empty,
faves: fave_ids[p.id] || empty,
upvotes: upvote_ids[p.id] || empty,
downvotes: downvote_ids[p.id] || empty,
children: child_ids[p.id] || empty,
commenters: commenter_ids[p.id] || empty,
noters: noter_ids[p.id] || empty,
notes: notes[p.id] || empty,
deleter: deleter_ids[p.id] || empty,
del_reason: del_reasons[p.id] || empty,
has_pending_replacements: pending_replacements[p.id],
artverified: p.tag_array.any? { |tag| verified_artists.key?(tag) && verified_artists[tag] == p.uploader_id },
}
{
@ -274,7 +280,8 @@ module PostIndex
pending: is_pending,
deleted: is_deleted,
has_children: has_children,
has_pending_replacements: options.key?(:has_pending_replacements) ? options[:has_pending_replacements] : replacements.pending.any?
has_pending_replacements: options.key?(:has_pending_replacements) ? options[:has_pending_replacements] : replacements.pending.any?,
artverified: options.key?(:artverified) ? options[:artverified] : uploader_linked_artists.any?,
}
end
end

View File

@ -169,6 +169,10 @@ class ElasticPostQueryBuilder < ElasticQueryBuilder
must.push({term: {has_pending_replacements: q[:pending_replacements]}})
end
if q.include?(:artverified)
must.push({ term: { artverified: q[:artverified] } })
end
add_tag_string_search_relation(q[:tags])
case q[:order]

View File

@ -8,7 +8,7 @@ class TagQuery
].freeze
BOOLEAN_METATAGS = %w[
hassource hasdescription isparent ischild inpool pending_replacements
hassource hasdescription isparent ischild inpool pending_replacements artverified
].freeze
NEGATABLE_METATAGS = %w[

View File

@ -22,6 +22,7 @@ class Artist < ApplicationRecord
after_save :update_wiki
after_save :propagate_locked, if: :should_propagate_locked
after_save :clear_url_string_changed
after_save :update_posts_index, if: :saved_change_to_linked_user_id?
has_many :members, class_name: "Artist", foreign_key: "group_name", primary_key: "name"
has_many :urls, dependent: :destroy, class_name: "ArtistUrl", autosave: true
@ -563,4 +564,8 @@ class Artist < ApplicationRecord
def log_destroy
ModAction.log(:artist_delete, { artist_id: id, artist_name: name })
end
def update_posts_index
Post.tag_match_system(name).each(&:update_index)
end
end

View File

@ -0,0 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "environment"))
client = Post.document_store.client
client.indices.put_mapping(index: Post.document_store.index_name, body: { properties: { artverified: { type: "boolean" } } })

View File

@ -0,0 +1,13 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "environment"))
artists = Artist.select(:name, :linked_user_id).where.not(linked_user_id: nil).to_h { |artist| [artist.name, artist.linked_user_id] }
client = Post.document_store.client
Post.find_each do |post|
puts post.id
next unless post.tag_array.any? { |tag| artists.key?(tag) && artists[tag] == post.uploader_id }
client.update(index: Post.document_store.index_name, id: post.id, body: { doc: { artverified: true } })
end

View File

@ -1978,6 +1978,24 @@ class PostTest < ActiveSupport::TestCase
end
end
should "return posts for verified artists" do
assert_tag_match([], "artverified:true")
assert_tag_match([], "artverified:false")
artist = create(:artist, linked_user: @user)
post = create(:post, tag_string: artist.name, uploader: @user)
assert_tag_match([], "artverified:false")
assert_tag_match([post], "artverified:true")
end
should "return posts for verified artists after update" do
assert_tag_match([], "artverified:true")
assert_tag_match([], "artverified:false")
post = create(:post, tag_string: "artist:test", uploader: @user)
with_inline_jobs { create(:artist, name: "test", linked_user: @user) }
assert_tag_match([], "artverified:false")
assert_tag_match([post], "artverified:true")
end
should "return posts for replacements" do
assert_tag_match([], "pending_replacements:true")
assert_tag_match([], "pending_replacements:false")