2017-02-11 05:20:42 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2017-02-11 05:20:42 +00:00
|
|
|
class FollowService
|
2017-09-13 14:11:04 +00:00
|
|
|
class << self
|
2017-02-12 17:53:04 +00:00
|
|
|
def follow(entity, user, reason)
|
2017-09-03 13:07:29 +00:00
|
|
|
return unless user
|
2017-02-11 05:20:42 +00:00
|
|
|
|
2017-03-08 20:02:22 +00:00
|
|
|
return if (reason == 'created' || reason == 'contributed') && !should_auto_follow(entity, user, reason)
|
2017-03-08 18:50:39 +00:00
|
|
|
|
2017-02-12 17:53:04 +00:00
|
|
|
follow = Follow.where(followed: entity, user: user).first_or_create
|
2017-09-13 14:11:04 +00:00
|
|
|
unless follow.update(muted: false)
|
|
|
|
raise follow.errors.full_messages.join("\n")
|
|
|
|
end
|
2017-02-12 17:53:04 +00:00
|
|
|
if FollowReason::REASONS.include?(reason) && !follow.follow_reason.read_attribute(reason)
|
|
|
|
follow.follow_reason.update_attribute(reason, true)
|
|
|
|
end
|
2017-02-11 05:20:42 +00:00
|
|
|
end
|
2017-02-16 13:42:35 +00:00
|
|
|
|
2017-02-12 17:53:04 +00:00
|
|
|
def unfollow(entity, user)
|
2017-09-03 19:11:52 +00:00
|
|
|
follow = Follow.where(followed: entity, user: user).first
|
2018-01-21 22:21:00 +00:00
|
|
|
return unless follow
|
|
|
|
|
|
|
|
unless follow.update(muted: true)
|
|
|
|
raise follow.errors.full_messages.join("\n")
|
2017-09-13 14:11:04 +00:00
|
|
|
end
|
2017-02-12 17:53:04 +00:00
|
|
|
end
|
2017-02-16 13:42:35 +00:00
|
|
|
|
2017-02-12 17:53:04 +00:00
|
|
|
def remove_reason(entity, user, reason)
|
|
|
|
return unless FollowReason::REASONS.include?(reason)
|
|
|
|
follow = Follow.where(followed: entity, user: user).first
|
2018-01-21 22:21:00 +00:00
|
|
|
return unless follow
|
|
|
|
|
|
|
|
follow.follow_reason.update_attribute(reason, false)
|
|
|
|
follow.destroy unless follow.follow_reason.has_reason
|
2017-02-11 05:20:42 +00:00
|
|
|
end
|
2017-02-16 13:42:35 +00:00
|
|
|
|
2017-02-12 17:53:04 +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)
|
2017-09-03 19:11:52 +00:00
|
|
|
follow = Follow.where(followed: entity, user: user).first
|
|
|
|
return false if follow && follow.muted
|
2017-03-08 18:50:39 +00:00
|
|
|
if entity.class == Topic
|
2018-01-21 22:21:00 +00:00
|
|
|
return user.settings.follow_topic_on_created == '1' if reason == 'created'
|
|
|
|
return user.settings.follow_topic_on_contributed == '1' if reason == 'contributed'
|
2017-03-08 18:50:39 +00:00
|
|
|
elsif entity.class == Map
|
2018-01-21 22:21:00 +00:00
|
|
|
return user.settings.follow_map_on_created == '1' if reason == 'created'
|
|
|
|
return user.settings.follow_map_on_contributed == '1' if reason == 'contributed'
|
2017-03-08 18:50:39 +00:00
|
|
|
end
|
|
|
|
end
|
2017-02-11 05:20:42 +00:00
|
|
|
end
|
|
|
|
end
|