2016-02-12 09:58:39 -05:00
|
|
|
# == Subscribable concern
|
|
|
|
#
|
|
|
|
# Users can subscribe to these models.
|
|
|
|
#
|
|
|
|
# Used by Issue, MergeRequest, Label
|
|
|
|
#
|
|
|
|
|
|
|
|
module Subscribable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2017-06-08 11:16:27 -04:00
|
|
|
has_many :subscriptions, dependent: :destroy, as: :subscribable # rubocop:disable Cop/ActiveRecordDependent
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
def subscribed?(user, project = nil)
|
2017-10-31 12:15:03 -04:00
|
|
|
return false unless user
|
|
|
|
|
2016-11-04 14:19:08 -04:00
|
|
|
if subscription = subscriptions.find_by(user: user, project: project)
|
2016-03-01 11:33:13 -05:00
|
|
|
subscription.subscribed
|
|
|
|
else
|
2016-11-04 14:19:08 -04:00
|
|
|
subscribed_without_subscriptions?(user, project)
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|
2016-03-01 11:33:13 -05:00
|
|
|
end
|
2016-02-12 09:58:39 -05:00
|
|
|
|
2016-03-01 11:33:13 -05:00
|
|
|
# Override this method to define custom logic to consider a subscribable as
|
|
|
|
# subscribed without an explicit subscription record.
|
2016-11-04 14:19:08 -04:00
|
|
|
def subscribed_without_subscriptions?(user, project)
|
2016-03-01 11:33:13 -05:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2016-11-04 14:19:08 -04:00
|
|
|
def subscribers(project)
|
2017-06-21 09:48:12 -04:00
|
|
|
subscriptions_available(project)
|
|
|
|
.where(subscribed: true)
|
|
|
|
.map(&:user)
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
def toggle_subscription(user, project = nil)
|
2016-11-15 16:59:12 -05:00
|
|
|
unsubscribe_from_other_levels(user, project)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
find_or_initialize_subscription(user, project)
|
|
|
|
.update(subscribed: !subscribed?(user, project))
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
def subscribe(user, project = nil)
|
2016-11-15 16:59:12 -05:00
|
|
|
unsubscribe_from_other_levels(user, project)
|
|
|
|
|
|
|
|
find_or_initialize_subscription(user, project)
|
|
|
|
.update(subscribed: true)
|
2016-05-12 16:48:09 -04:00
|
|
|
end
|
|
|
|
|
2016-11-14 17:42:22 -05:00
|
|
|
def unsubscribe(user, project = nil)
|
2016-11-15 16:59:12 -05:00
|
|
|
unsubscribe_from_other_levels(user, project)
|
|
|
|
|
|
|
|
find_or_initialize_subscription(user, project)
|
|
|
|
.update(subscribed: false)
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|
2016-10-31 15:19:14 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-11-15 16:59:12 -05:00
|
|
|
def unsubscribe_from_other_levels(user, project)
|
|
|
|
other_subscriptions = subscriptions.where(user: user)
|
|
|
|
|
|
|
|
other_subscriptions =
|
|
|
|
if project.blank?
|
|
|
|
other_subscriptions.where.not(project: nil)
|
|
|
|
else
|
|
|
|
other_subscriptions.where(project: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
other_subscriptions.update_all(subscribed: false)
|
|
|
|
end
|
|
|
|
|
2016-11-04 14:19:08 -04:00
|
|
|
def find_or_initialize_subscription(user, project)
|
2017-06-21 09:48:12 -04:00
|
|
|
subscriptions
|
|
|
|
.find_or_initialize_by(user_id: user.id, project_id: project.try(:id))
|
2016-11-14 17:42:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def subscriptions_available(project)
|
|
|
|
t = Subscription.arel_table
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
subscriptions
|
|
|
|
.where(t[:project_id].eq(nil).or(t[:project_id].eq(project.try(:id))))
|
2016-10-31 15:19:14 -04:00
|
|
|
end
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|