[15 Min Fix]: Extract a post's followers out of service and onto Article model (#13229)
* Move authors followers out of Notifications::NotifiableAction::Send and into Article model * Rename authors_followers to followers * Update followers method in service, oops * Update app/services/notifications/notifiable_action/send.rb Co-authored-by: Michael Kohl <citizen428@dev.to> Co-authored-by: Michael Kohl <citizen428@dev.to>
This commit is contained in:
parent
48c5828404
commit
97678456d6
4 changed files with 27 additions and 15 deletions
|
|
@ -435,6 +435,19 @@ class Article < ApplicationRecord
|
|||
doc.to_html
|
||||
end
|
||||
|
||||
def followers
|
||||
# This will return an array, but the items will NOT be ActiveRecord objects.
|
||||
# The followers may also occasionally be nil because orphaned follows can possibly exist in the database.
|
||||
followers = user.followers_scoped.where(subscription_status: "all_articles").map(&:follower)
|
||||
|
||||
if organization_id
|
||||
org_followers = organization.followers_scoped.where(subscription_status: "all_articles")
|
||||
followers += org_followers.map(&:follower)
|
||||
end
|
||||
|
||||
followers.uniq.compact
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def search_score
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ module Notifications
|
|||
end
|
||||
|
||||
def call
|
||||
return unless notifiable.is_a?(Article)
|
||||
|
||||
json_data = {
|
||||
user: user_data(notifiable.user),
|
||||
article: article_data(notifiable)
|
||||
|
|
@ -23,9 +25,7 @@ module Notifications
|
|||
json_data[:organization] = organization_data(notifiable.organization) if notifiable.organization_id
|
||||
|
||||
notifications_attributes = []
|
||||
# followers is an array and not an activerecord object
|
||||
# followers can occasionally be nil because orphaned follows can possibly exist in the db (for now)
|
||||
followers.sort_by(&:updated_at).reverse[0..10_000].each do |follower|
|
||||
notifiable.followers.sort_by(&:updated_at).last(10_000).reverse_each do |follower|
|
||||
now = Time.current
|
||||
notifications_attributes.push(
|
||||
user_id: follower.id,
|
||||
|
|
@ -53,17 +53,6 @@ module Notifications
|
|||
|
||||
attr_reader :notifiable, :action
|
||||
|
||||
def followers
|
||||
followers = notifiable.user.followers_scoped.where(subscription_status: "all_articles").map(&:follower)
|
||||
|
||||
if notifiable.organization_id
|
||||
org_followers = notifiable.organization.followers_scoped.where(subscription_status: "all_articles")
|
||||
followers += org_followers.map(&:follower)
|
||||
end
|
||||
|
||||
followers.uniq.compact
|
||||
end
|
||||
|
||||
def choose_upsert_index(action)
|
||||
return :index_notifications_on_user_notifiable_and_action_not_null if action.present?
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ module Notifications
|
|||
|
||||
sidekiq_options queue: :low_priority, retry: 10
|
||||
def perform(notifiable_id, notifiable_type, action)
|
||||
# checking type, but leaving space for notifyable types
|
||||
# checking type, but leaving space for notifiable types
|
||||
return unless notifiable_type == "Article"
|
||||
|
||||
notifiable = notifiable_type.constantize.find_by(id: notifiable_id)
|
||||
|
|
|
|||
|
|
@ -1118,6 +1118,16 @@ RSpec.describe Article, type: :model do
|
|||
end
|
||||
end
|
||||
|
||||
describe "#followers" do
|
||||
it "returns an array of users who follow the article's author" do
|
||||
following_user = create(:user)
|
||||
following_user.follow(user)
|
||||
|
||||
expect(article.followers.length).to eq(1)
|
||||
expect(article.followers.first.username).to eq(following_user.username)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#update_score" do
|
||||
it "stably sets the correct blackbox values" do
|
||||
create(:reaction, reactable: article, points: 1)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue