[Search] Add pending replacements field

This commit is contained in:
Earlopain 2021-10-05 21:14:00 +02:00
parent 22a4fe038d
commit 147ec019e8
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
7 changed files with 59 additions and 2 deletions

View File

@ -63,6 +63,7 @@ module PostIndex
indexes :pending, type: 'boolean'
indexes :deleted, type: 'boolean'
indexes :has_children, type: 'boolean'
indexes :has_pending_replacements, type: 'boolean'
end
end
@ -138,6 +139,11 @@ module PostIndex
GROUP BY post_id) pfi
INNER JOIN post_flags pf ON pf.id = pfi.mid;
SQL
pending_replacements_sql = <<-SQL
SELECT DISTINCT p.id, CASE WHEN pr.post_id IS NULL THEN false ELSE true END FROM posts p
LEFT OUTER JOIN post_replacements2 pr ON p.id = pr.post_id AND pr.status = 'pending'
WHERE p.id IN (#{post_ids})
SQL
# Run queries
conn = ApplicationRecord.connection
@ -153,6 +159,7 @@ module PostIndex
child_ids = conn.execute(child_sql).values.map(&array_parse).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
# Special handling for votes to do it with one query
vote_ids = conn.execute(votes_sql).values.map do |pid, uids, scores|
@ -178,7 +185,8 @@ module PostIndex
noters: noter_ids[p.id] || empty,
notes: notes[p.id] || empty,
deleter: deleter_ids[p.id] || empty,
del_reason: del_reasons[p.id] || empty
del_reason: del_reasons[p.id] || empty,
has_pending_replacements: pending_replacements[p.id]
}
{
@ -258,6 +266,7 @@ 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?
}
end
end

View File

@ -443,6 +443,10 @@ class ElasticPostQueryBuilder
(q[:inpool] ? must : must_not).push({exists: {field: :pools}})
end
if q.include?(:pending_replacements)
must.push({term: {has_pending_replacements: q[:pending_replacements]}})
end
add_tag_string_search_relation(q[:tags], must)
if q[:upvote].present?

View File

@ -13,7 +13,9 @@ class PostReplacement < ApplicationRecord
validate :no_pending_duplicates, on: :create
validate :write_storage_file, on: :create
after_create -> { post.update_index }
before_destroy :remove_files
after_destroy -> { post.update_index }
def replacement_url_parsed
return nil unless replacement_url =~ %r!\Ahttps?://!i
@ -177,6 +179,7 @@ class PostReplacement < ApplicationRecord
processor = UploadService::Replacer.new(post: post, replacement: self)
processor.process!
end
post.update_index
end
def promote!
@ -186,11 +189,13 @@ class PostReplacement < ApplicationRecord
update_attribute(:status, 'promoted')
new_post
end
post.update_index
end
def reject!
ModAction.log(:post_replacement_reject, {post_id: post.id, replacement_id: self.id})
update_attribute(:status, 'rejected')
post.update_index
end
end

View File

@ -10,7 +10,7 @@ class Tag < ApplicationRecord
BOOLEAN_METATAGS = %w[
hassource hasdescription ratinglocked notelocked statuslocked
tagslocked hideanon hidegoogle isparent ischild inpool
tagslocked hideanon hidegoogle isparent ischild inpool pending_replacements
]
METATAGS = %w[

View File

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

View File

@ -0,0 +1,8 @@
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment'))
Post.find_each do |post|
puts post.id.to_s
post.__elasticsearch__.update_document_attributes has_pending_replacements: post.replacements.pending.any?
end

View File

@ -2118,6 +2118,31 @@ class PostTest < ActiveSupport::TestCase
relation = Post.tag_match("-aaa id:>0")
end
end
should "return posts for replacements" do
assert_tag_match([], "pending_replacements:true")
assert_tag_match([], "pending_replacements:false")
post = FactoryBot.create(:post)
replacement = FactoryBot.create(:png_replacement, creator: @user, creator_ip_addr: '127.0.0.1', post: post)
assert_tag_match([post], "pending_replacements:true")
end
should "return no posts when the replacement is not pending anymore" do
post1 = FactoryBot.create(:post)
post2 = FactoryBot.create(:post)
post3 = FactoryBot.create(:post)
post4 = FactoryBot.create(:post)
replacement1 = FactoryBot.create(:png_replacement, creator: @user, creator_ip_addr: '127.0.0.1', post: post1)
replacement1.reject!
replacement2 = FactoryBot.create(:png_replacement, creator: @user, creator_ip_addr: '127.0.0.1', post: post2)
replacement2.approve!
replacement3 = FactoryBot.create(:png_replacement, creator: @user, creator_ip_addr: '127.0.0.1', post: post3)
replacement3.promote!
replacement4 = FactoryBot.create(:png_replacement, creator: @user, creator_ip_addr: '127.0.0.1', post: post4)
replacement4.destroy!
assert_tag_match([], "pending_replacements:true")
assert_tag_match([post1, post2, post3, post4], "pending_replacements:false")
end
end
context "Voting:" do