Merge pull request #1132 from metamaps/feature/mod.follow

add active scope to follows model
This commit is contained in:
Connor Turland 2017-09-13 22:45:50 -04:00 committed by GitHub
commit f323796030
6 changed files with 27 additions and 9 deletions

View file

@ -10,7 +10,11 @@ class Follow < ApplicationRecord
validates :user, uniqueness: { scope: :followed, message: 'This entity is already followed by this user' }
after_create :add_subsetting
scope :active, -> {
where(muted: false)
}
private
def add_subsetting

View file

@ -58,8 +58,8 @@ class User < ApplicationRecord
admin: admin }
if (_options[:follows])
json['follows'] = {
topics: following.where(muted: false, followed_type: 'Topic').to_a.map(&:followed_id),
maps: following.where(muted: false, followed_type: 'Map').to_a.map(&:followed_id)
topics: following.active.where(followed_type: 'Topic').to_a.map(&:followed_id),
maps: following.active.where(followed_type: 'Map').to_a.map(&:followed_id)
}
end
if (_options[:follow_settings])

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
class FollowService
class << self
class << self
def follow(entity, user, reason)
return unless user
@ -8,7 +8,9 @@ class FollowService
return if (reason == 'created' || reason == 'contributed') && !should_auto_follow(entity, user, reason)
follow = Follow.where(followed: entity, user: user).first_or_create
follow.update_attribute('muted', false)
unless follow.update(muted: false)
raise follow.errors.full_messages.join("\n")
end
if FollowReason::REASONS.include?(reason) && !follow.follow_reason.read_attribute(reason)
follow.follow_reason.update_attribute(reason, true)
end
@ -16,7 +18,11 @@ class FollowService
def unfollow(entity, user)
follow = Follow.where(followed: entity, user: user).first
follow.update_attribute('muted', true)
if follow
unless follow.update(muted: true)
raise follow.errors.full_messages.join("\n")
end
end
end
def remove_reason(entity, user, reason)

View file

@ -53,8 +53,7 @@ class NotificationService
end
def self.notify_followers(entity, event_type, event, reason_filter = nil, exclude_follows = nil)
follows = entity.follows.where.not(user_id: event.user.id)
follows = follows.where(muted: false)
follows = entity.follows.active.where.not(user_id: event.user.id)
if !exclude_follows.nil?
follows = follows.where.not(id: exclude_follows)

View file

@ -0,0 +1,9 @@
Delayed::Worker.class_eval do
def handle_failed_job_with_notification(job, error)
handle_failed_job_without_notification(job, error)
ExceptionNotifier.notify_exception(error)
end
alias_method_chain :handle_failed_job, :notification
end

View file

@ -5,7 +5,7 @@ namespace :metamaps do
end
def summarize_map_activity
Follow.where(followed_type: 'Map').find_each do |follow|
Follow.active.where(followed_type: 'Map').find_each do |follow|
map = follow.followed
user = follow.user
# add logging and rescue-ing