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