Skip to content

Commit

Permalink
AO3-5292 - Ensure people search is updated when works are posted, and…
Browse files Browse the repository at this point in the history
… cover more types of work editing. (#3210)

* AO3-5292 Update people search when editing works.

* AO3-5292 Minor fixes for Hound.
  • Loading branch information
tickinginstant authored and elzj committed Dec 19, 2017
1 parent f067c93 commit c39b474
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/models/bookmark.rb
Expand Up @@ -111,7 +111,7 @@ def invalidate_bookmark_count
def update_pseud_index
return unless $rollout.active?(:start_new_indexing)
return unless destroyed? || saved_change_to_id? || saved_change_to_private?
AsyncIndexer.index(PseudIndexer, [pseud_id], :background)
IndexQueue.enqueue_id(Pseud, pseud_id, :background)
end

def visible?(current_user=User.current_user)
Expand Down
7 changes: 7 additions & 0 deletions app/models/creatorship.rb
Expand Up @@ -3,6 +3,8 @@ class Creatorship < ApplicationRecord
belongs_to :creation, polymorphic: true, touch: true

before_destroy :expire_caches
after_create :update_pseud_index
after_destroy :update_pseud_index

validate :unique_index

Expand Down Expand Up @@ -37,4 +39,9 @@ def expire_caches
end
end

def update_pseud_index
return unless creation_type == 'Work'
return unless creation.respond_to?(:reindex_changed_pseud)
creation.reindex_changed_pseud(pseud_id)
end
end
7 changes: 7 additions & 0 deletions app/models/filter_tagging.rb
Expand Up @@ -10,6 +10,13 @@ class FilterTagging < ApplicationRecord
validates_presence_of :filter, :filterable

before_destroy :expire_caches
after_create :update_pseud_index
after_destroy :update_pseud_index

def update_pseud_index
return unless filter.is_a?(Fandom) && filterable.is_a?(Work)
IndexQueue.enqueue_ids(Pseud, filterable.pseud_ids, :background)
end

def self.find(*args)
raise "id is not guaranteed to be unique. please install composite_primary_keys gem and set the primary key to id,filter_id"
Expand Down
11 changes: 10 additions & 1 deletion app/models/indexing/index_queue.rb
Expand Up @@ -22,7 +22,12 @@ def self.enqueue(object, label)

def self.enqueue_id(klass, id, label)
key = get_key(klass, label)
queue = self.new(key).add_id(id)
new(key).add_id(id)
end

def self.enqueue_ids(klass, ids, label)
key = get_key(klass, label)
new(key).add_ids(ids)
end

####################
Expand All @@ -40,6 +45,10 @@ def add_id(id)
REDIS.sadd(name, id)
end

def add_ids(ids)
REDIS.sadd(name, ids) unless ids.blank?
end

def run
return unless exists?
rename
Expand Down
2 changes: 1 addition & 1 deletion app/models/indexing/scheduled_reindex_job.rb
Expand Up @@ -5,7 +5,7 @@ def self.perform(reindex_type)
when 'main'
%w(Pseud Tag Work Bookmark Series ExternalWork)
when 'background'
%w(Work Bookmark)
%w(Work Bookmark Pseud)
when 'stats'
%w(StatCounter)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/tag.rb
Expand Up @@ -701,7 +701,7 @@ def reindex_pseuds
joins("JOIN filter_taggings ON filter_taggings.filterable_id = creatorships.creation_id").
where("filter_taggings.filter_id = ? AND filter_taggings.filterable_type = 'Work' AND creatorships.creation_type = 'Work'", id).
find_in_batches do |batch|
AsyncIndexer.index(PseudIndexer, batch.map(&:pseud_id).uniq, :background)
IndexQueue.enqueue_ids(Pseud, batch.map(&:pseud_id), :background)
end
end

Expand Down
13 changes: 10 additions & 3 deletions app/models/work.rb
Expand Up @@ -20,7 +20,9 @@ class Work < ApplicationRecord
# creatorships can't have dependent => destroy because we email the
# user in a before_destroy callback
has_many :creatorships, as: :creation
has_many :pseuds, through: :creatorships, after_remove: :expire_pseud
has_many :pseuds,
through: :creatorships,
after_remove: [:expire_pseud, :reindex_changed_pseud]
has_many :users, -> { distinct }, through: :pseuds

has_many :external_creatorships, as: :creation, dependent: :destroy, inverse_of: :creation
Expand Down Expand Up @@ -255,15 +257,20 @@ def expire_caches
Work.expire_work_tag_groups_id(self.id)
end

def reindex_changed_pseud(pseud)
pseud = pseud.id if pseud.respond_to?(:id)
IndexQueue.enqueue_id(Pseud, pseud, :background)
end

def update_pseud_index
return unless $rollout.active?(:start_new_indexing)
return unless should_reindex_pseuds?
AsyncIndexer.index(PseudIndexer, [pseuds.pluck(:id)], :background)
IndexQueue.enqueue_ids(Pseud, pseud_ids, :background)
end

def should_reindex_pseuds?
pertinent_attributes = %w(id posted restricted in_anon_collection
in_unrevealed_collection hidden_by_admin authors_to_sort_on)
in_unrevealed_collection hidden_by_admin)
destroyed? || (saved_changes.keys & pertinent_attributes).present?
end

Expand Down
102 changes: 96 additions & 6 deletions features/search/people_search.feature
Expand Up @@ -2,12 +2,10 @@ Feature: Search pseuds
As a user
I want to use search to find other users

Background:
Given I have loaded the fixtures
And I am logged in as "testuser"
And testuser can use the new search

Scenario: Search by name
Given I have loaded the fixtures
And I am logged in as "testuser"
And testuser can use the new search
When I go to the search people page
And I fill in "Name" with "testuser"
And I press "Search People"
Expand All @@ -18,8 +16,100 @@ Feature: Search pseuds
And I should not see "sad user"

Scenario: Search by fandom
Given I have loaded the fixtures
And I am logged in as "testuser"
And testuser can use the new search
When I go to the search people page
And I fill in "Fandom" with "Ghost Soup"
And I press "Search People"
Then I should see "testuser2"
And I should not see "testy"
And I should not see "testy"

Scenario: Search by fandom updates when a work is posted.
Given a canonical fandom "Ghost Soup"
And I am logged in as "testuser"
And testuser can use the new search
When I post a work "Drabble Collection" with fandom "Ghost Soup"
And all indexing jobs have been run
And I go to the search people page
And I fill in "Fandom" with "Ghost Soup"
And I press "Search People"
Then I should see "testuser" within "ol.pseud.group"

Scenario: Search by fandom updates when a fandom is added to a work.
Given a canonical fandom "Ghost Soup"
And I am logged in as "testuser"
And I post the work "Drabble Collection" with fandom "MCU"
And all indexing jobs have been run
And testuser can use the new search
When I edit the work "Drabble Collection"
And I fill in "Fandom" with "MCU, Ghost Soup"
And I press "Post Without Preview"
And all indexing jobs have been run
And I go to the search people page
And I fill in "Fandom" with "Ghost Soup"
And I press "Search People"
Then I should see "testuser" within "ol.pseud.group"

Scenario: Search by fandom updates when a fandom is removed from a work.
Given a canonical fandom "Ghost Soup"
And I am logged in as "testuser"
And I post the work "Drabble Collection" with fandom "MCU, Ghost Soup"
And all indexing jobs have been run
And testuser can use the new search
When I edit the work "Drabble Collection"
And I fill in "Fandom" with "MCU"
And I press "Post Without Preview"
And all indexing jobs have been run
And I go to the search people page
And I fill in "Fandom" with "Ghost Soup"
And I press "Search People"
Then I should not see "testuser" within "ol.pseud.group"

Scenario: Search by fandom updates when an author is added to a work.
Given a canonical fandom "Ghost Soup"
And I am logged in as "testuser"
And I post the work "Drabble Collection" with fandom "Ghost Soup"
And all indexing jobs have been run
And testuser can use the new search
When I edit the work "Drabble Collection"
And I add the co-author "alice"
And I press "Post Without Preview"
And all indexing jobs have been run
And I go to the search people page
And I fill in "Fandom" with "Ghost Soup"
And I press "Search People"
Then I should see "testuser" within "ol.pseud.group"
And I should see "alice" within "ol.pseud.group"

Scenario: Search by fandom updates when an author is removed from a work.
Given a canonical fandom "Ghost Soup"
And I am logged in as "testuser"
And I set up a draft "Drabble Collection" with fandom "Ghost Soup"
And I add the co-author "alice"
And I press "Post Without Preview"
And all indexing jobs have been run
And testuser can use the new search
When I edit the work "Drabble Collection"
And I follow "Remove Me As Author"
And all indexing jobs have been run
And I go to the search people page
And I fill in "Fandom" with "Ghost Soup"
And I press "Search People"
Then I should see "alice" within "ol.pseud.group"
But I should not see "testuser" within "ol.pseud.group"

Scenario: Search by fandom updates when a work is orphaned.
Given a canonical fandom "Ghost Soup"
And I have an orphan account
And I am logged in as "testuser"
And I post the work "Drabble Collection" with fandom "Ghost Soup"
And all indexing jobs have been run
And testuser can use the new search
When I orphan the work "Drabble Collection"
And all indexing jobs have been run
And I go to the search people page
And I fill in "Fandom" with "Ghost Soup"
And I press "Search People"
Then I should see "orphan_account" within "ol.pseud.group"
But I should not see "testuser" within "ol.pseud.group"

0 comments on commit c39b474

Please sign in to comment.