diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2016-11-18 20:31:42 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2016-11-18 20:31:42 +0800 |
commit | 578ced29f24f9c0db3159d366a974927fbe3946b (patch) | |
tree | 21c33c421b2b435e31eb2923a66563f80bb9eb40 /app/models/concerns | |
parent | 6d1c5761cd520f3cb7fa4dbb1a1937c29f468884 (diff) | |
parent | 688ff26df3f288e5cd50096a01014188a5e4011b (diff) | |
download | gitlab-ce-578ced29f24f9c0db3159d366a974927fbe3946b.tar.gz |
Merge remote-tracking branch 'upstream/master' into fix-cancelling-pipelines
* upstream/master: (162 commits)
Grapify the repository API
Fix wrong link URL
Add note pointing to limitations section in environments.md
Clarify the limitation for special chars is for Review Apps
Change order of limitations list in environments.md
Add an example of invalid characters to branches for review apps
Grammar fix in environments.md: s/base/basis
Add changelog entry for #24276 / !7500
Allow registering users where the username contains dots (.).
bump rouge to 2.0.7
Fix indentation
Update copy.md with issue guideline updates and merge request guidelines
Refactor github import to reduce number of API calls
Fix missing URL from environments docs
Check all namespaces on validation of new username.
Allow sorting groups in API
Use the public CE repo URL instead of the one used in the runner
Fix typos
Fix wrong changelog item
Add missing item for 8.13.6
...
Diffstat (limited to 'app/models/concerns')
-rw-r--r-- | app/models/concerns/issuable.rb | 2 | ||||
-rw-r--r-- | app/models/concerns/subscribable.rb | 64 |
2 files changed, 49 insertions, 17 deletions
diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index 664bb594aa9..ec9e7e5ae2b 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -215,7 +215,7 @@ module Issuable end end - def subscribed_without_subscriptions?(user) + def subscribed_without_subscriptions?(user, project) participants(user).include?(user) end diff --git a/app/models/concerns/subscribable.rb b/app/models/concerns/subscribable.rb index 083257f1005..83daa9b1a64 100644 --- a/app/models/concerns/subscribable.rb +++ b/app/models/concerns/subscribable.rb @@ -12,39 +12,71 @@ module Subscribable has_many :subscriptions, dependent: :destroy, as: :subscribable end - def subscribed?(user) - if subscription = subscriptions.find_by_user_id(user.id) + def subscribed?(user, project = nil) + if subscription = subscriptions.find_by(user: user, project: project) subscription.subscribed else - subscribed_without_subscriptions?(user) + subscribed_without_subscriptions?(user, project) end end # Override this method to define custom logic to consider a subscribable as # subscribed without an explicit subscription record. - def subscribed_without_subscriptions?(user) + def subscribed_without_subscriptions?(user, project) false end - def subscribers - subscriptions.where(subscribed: true).map(&:user) + def subscribers(project) + subscriptions_available(project). + where(subscribed: true). + map(&:user) end - def toggle_subscription(user) - subscriptions. - find_or_initialize_by(user_id: user.id). - update(subscribed: !subscribed?(user)) + def toggle_subscription(user, project = nil) + unsubscribe_from_other_levels(user, project) + + find_or_initialize_subscription(user, project). + update(subscribed: !subscribed?(user, project)) + 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) end - def subscribe(user) + 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) subscriptions. - find_or_initialize_by(user_id: user.id). - update(subscribed: true) + find_or_initialize_by(user_id: user.id, project_id: project.try(:id)) end - def unsubscribe(user) + def subscriptions_available(project) + t = Subscription.arel_table + subscriptions. - find_or_initialize_by(user_id: user.id). - update(subscribed: false) + where(t[:project_id].eq(nil).or(t[:project_id].eq(project.try(:id)))) end end |