metamaps--metamaps/app/services/follow_service.rb

50 lines
1.5 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
class FollowService
class << self
def follow(entity, user, reason)
2017-03-08 19:13:47 +00:00
return unless user && is_tester(user)
return if (reason == 'created' || reason == 'contributed') && !should_auto_follow(entity, user, reason)
2017-03-08 18:50:39 +00:00
follow = Follow.where(followed: entity, user: user).first_or_create
if FollowReason::REASONS.include?(reason) && !follow.follow_reason.read_attribute(reason)
follow.follow_reason.update_attribute(reason, true)
end
end
2017-02-16 13:42:35 +00:00
def unfollow(entity, user)
Follow.where(followed: entity, user: user).destroy_all
end
2017-02-16 13:42:35 +00:00
def remove_reason(entity, user, reason)
return unless FollowReason::REASONS.include?(reason)
follow = Follow.where(followed: entity, user: user).first
if follow
follow.follow_reason.update_attribute(reason, false)
if !follow.follow_reason.has_reason
follow.destroy
end
end
end
2017-02-16 13:42:35 +00:00
protected
2017-02-16 13:42:35 +00:00
2017-03-08 18:50:39 +00:00
def should_auto_follow(entity, user, reason)
if entity.class == Topic
if reason == 'created'
return user.settings.follow_topic_on_created == '1'
elsif reason == 'contributed'
return user.settings.follow_topic_on_contributed == '1'
end
elsif entity.class == Map
if reason == 'created'
return user.settings.follow_map_on_created == '1'
elsif reason == 'contributed'
return user.settings.follow_map_on_contributed == '1'
2017-03-08 18:50:39 +00:00
end
end
end
end
end