2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
|
|
2021-04-02 20:09:02 -04:00
|
|
|
if (subscription = lazy_subscription(user, project)&.itself)
|
|
|
|
subscription.subscribed
|
|
|
|
else
|
|
|
|
subscribed_without_subscriptions?(user, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def lazy_subscription(user, project = nil)
|
|
|
|
return unless user
|
|
|
|
|
|
|
|
# handle project and group labels as well as issuable subscriptions
|
|
|
|
subscribable_type = self.class.ancestors.include?(Label) ? 'Label' : self.class.name
|
|
|
|
BatchLoader.for(id: id, subscribable_type: subscribable_type, project_id: project&.id).batch do |items, loader|
|
|
|
|
values = items.each_with_object({ ids: Set.new, subscribable_types: Set.new, project_ids: Set.new }) do |item, result|
|
|
|
|
result[:ids] << item[:id]
|
|
|
|
result[:subscribable_types] << item[:subscribable_type]
|
|
|
|
result[:project_ids] << item[:project_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
subscriptions = Subscription.where(subscribable_id: values[:ids], subscribable_type: values[:subscribable_types], project_id: values[:project_ids], user: user)
|
|
|
|
|
|
|
|
subscriptions.each do |subscription|
|
|
|
|
loader.call({
|
|
|
|
id: subscription.subscribable_id,
|
|
|
|
subscribable_type: subscription.subscribable_type,
|
|
|
|
project_id: subscription.project_id
|
|
|
|
}, subscription)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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)
|
2018-10-02 08:39:46 -04:00
|
|
|
relation = subscriptions_available(project)
|
|
|
|
.where(subscribed: true)
|
|
|
|
.select(:user_id)
|
|
|
|
|
|
|
|
User.where(id: relation)
|
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)
|
|
|
|
|
2021-04-13 11:11:24 -04:00
|
|
|
new_value = !subscribed?(user, project)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
find_or_initialize_subscription(user, project)
|
2021-04-13 11:11:24 -04:00
|
|
|
.update(subscribed: new_value)
|
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
|
|
|
|
2019-10-31 14:06:53 -04:00
|
|
|
def set_subscription(user, desired_state, project = nil)
|
|
|
|
if desired_state
|
|
|
|
subscribe(user, project)
|
|
|
|
else
|
|
|
|
unsubscribe(user, project)
|
|
|
|
end
|
|
|
|
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)
|
2021-04-13 11:11:24 -04:00
|
|
|
BatchLoader::Executor.clear_current
|
|
|
|
|
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
|