debian-mirror-gitlab/app/models/concerns/subscribable.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
3.5 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
# == Subscribable concern
#
# Users can subscribe to these models.
#
# Used by Issue, MergeRequest, Label
#
module Subscribable
extend ActiveSupport::Concern
included do
2017-09-10 17:25:29 +05:30
has_many :subscriptions, dependent: :destroy, as: :subscribable # rubocop:disable Cop/ActiveRecordDependent
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
def subscribed?(user, project = nil)
2018-03-17 18:26:18 +05:30
return false unless user
2021-04-29 21:17:54 +05:30
if (subscription = lazy_subscription(user, project)&.itself)
2016-06-02 11:05:42 +05:30
subscription.subscribed
else
2017-08-17 22:00:37 +05:30
subscribed_without_subscriptions?(user, project)
2016-06-02 11:05:42 +05:30
end
end
2021-04-29 21:17:54 +05:30
def lazy_subscription(user, project = nil)
return unless user
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-06-02 11:05:42 +05:30
# Override this method to define custom logic to consider a subscribable as
# subscribed without an explicit subscription record.
2017-08-17 22:00:37 +05:30
def subscribed_without_subscriptions?(user, project)
2016-06-02 11:05:42 +05:30
false
end
2017-08-17 22:00:37 +05:30
def subscribers(project)
2018-12-05 23:21:45 +05:30
relation = subscriptions_available(project)
.where(subscribed: true)
.select(:user_id)
User.where(id: relation)
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
def toggle_subscription(user, project = nil)
unsubscribe_from_other_levels(user, project)
2021-04-29 21:17:54 +05:30
new_value = !subscribed?(user, project)
2017-09-10 17:25:29 +05:30
find_or_initialize_subscription(user, project)
2021-04-29 21:17:54 +05:30
.update(subscribed: new_value)
2017-08-17 22:00:37 +05:30
end
def subscribe(user, project = nil)
unsubscribe_from_other_levels(user, project)
find_or_initialize_subscription(user, project)
.update(subscribed: true)
end
def unsubscribe(user, project = nil)
unsubscribe_from_other_levels(user, project)
find_or_initialize_subscription(user, project)
.update(subscribed: false)
2016-06-02 11:05:42 +05:30
end
2019-12-26 22:10:19 +05:30
def set_subscription(user, desired_state, project = nil)
if desired_state
subscribe(user, project)
else
unsubscribe(user, project)
end
end
2017-08-17 22:00:37 +05:30
private
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
def find_or_initialize_subscription(user, project)
2021-04-29 21:17:54 +05:30
BatchLoader::Executor.clear_current
2017-09-10 17:25:29 +05:30
subscriptions
.find_or_initialize_by(user_id: user.id, project_id: project.try(:id))
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
def subscriptions_available(project)
t = Subscription.arel_table
2017-09-10 17:25:29 +05:30
subscriptions
.where(t[:project_id].eq(nil).or(t[:project_id].eq(project.try(:id))))
2016-06-02 11:05:42 +05:30
end
2023-05-27 22:25:52 +05:30
def subscribable_type
# handle project and group labels as well as issuable subscriptions
if self.class.ancestors.include?(Label)
'Label'
elsif self.class.ancestors.include?(Issue)
'Issue'
else
self.class.name
end
end
2016-06-02 11:05:42 +05:30
end