Files
fizzy/app/models/bubble/watchable.rb
T
Kevin McConnell 0820badcc3 Add involvement types to accesses
This provides a way to set the level of involvement that a user has with
a collection, and from which we determine the level of notifications to
send. Users can be access-only, watching, or being notified about
everything.

If you're access-only, you won't get an notifications. If you're
watching, you'll only get notifications for the items you're watching
(which includes the items you've been assigned, have commented on, etc).
If you're set to everything you'll get notifications about all activity
in that collection.

This change replaces our previous concept of subscriptions. Where
previously you'd subscribe to a collection to get notifications in it,
now you'll simply set the notification level on your access.
2025-04-03 16:35:13 +01:00

32 lines
916 B
Ruby

module Bubble::Watchable
extend ActiveSupport::Concern
included do
has_many :watches, dependent: :destroy
has_many :watchers, -> { merge(Watch.watching) }, through: :watches, source: :user
after_create :set_watching_for_creator
end
def watched_by?(user)
watchers_and_subscribers(include_only_watching: true).include?(user)
end
def set_watching(user, watching)
watches.where(user: user).first_or_create.update!(watching: watching)
end
def watchers_and_subscribers(include_only_watching: false)
involvements = include_only_watching ? [ :watching, :everything ] : :everything
subscribers = bucket.users.where(accesses: { involvement: involvements })
User.where(id: subscribers.pluck(:id) +
watches.watching.pluck(:user_id) - watches.not_watching.pluck(:user_id))
end
private
def set_watching_for_creator
set_watching(creator, true)
end
end