Add mentionees as watchers

This commit is contained in:
Jorge Manrubia
2025-04-23 12:38:06 +02:00
parent 8ebb3f2fa9
commit edcc9e3279
7 changed files with 43 additions and 4 deletions
+6
View File
@@ -23,6 +23,8 @@ class Card < ApplicationRecord
end
end
delegate :accessible_to?, to: :collection
def title=(new_title)
self[:title] = new_title.presence || "Untitled"
end
@@ -30,4 +32,8 @@ class Card < ApplicationRecord
def cache_key
[ super, collection.name ].compact.join("/")
end
def was_mentioned(mention)
watch_by(mention.mentionee)
end
end
+4 -2
View File
@@ -13,7 +13,9 @@ module Card::Watchable
end
def watch_by(user)
watches.where(user: user).first_or_create.update!(watching: true)
if accessible_to?(user)
watches.where(user: user).first_or_create.update!(watching: true)
end
end
def unwatch_by(user)
@@ -21,7 +23,7 @@ module Card::Watchable
end
def watchers_and_subscribers(include_only_watching: false)
involvements = include_only_watching ? [ :watching, :everything ] : :everything
involvements = include_only_watching ? [:watching, :everything] : :everything
subscribers = collection.users.where(accesses: { involvement: involvements })
User.where(id: subscribers.pluck(:id) +
+4
View File
@@ -32,6 +32,10 @@ module Collection::Accessible
accesses.find_by(user: user)
end
def accessible_to?(user)
access_for(user).present?
end
private
def grant_access_to_everyone
accesses.grant_to(User.all) if all_access_previously_changed?(to: true)
+2
View File
@@ -13,6 +13,8 @@ class Comment < ApplicationRecord
after_create_commit :watch_card_by_creator, :track_commented_card
after_destroy_commit :cleanup_events
delegate :watch_by, to: :card
def to_partial_path
"cards/#{super}"
end
+7
View File
@@ -5,7 +5,14 @@ class Mention < ApplicationRecord
belongs_to :mentioner, class_name: "User"
belongs_to :mentionee, class_name: "User", inverse_of: :mentions
after_create :add_mentionee_as_watcher
def self_mention?
mentioner == mentionee
end
private
def add_mentionee_as_watcher
container.watch_by(mentionee)
end
end
+16 -2
View File
@@ -1,5 +1,6 @@
class Notifier::Event < Notifier
delegate :card, :creator, to: :source
delegate :watchers_and_subscribers, to: :card
private
def resource
@@ -15,9 +16,22 @@ class Notifier::Event < Notifier
when "assigned"
source.assignees.excluding(card.collection.access_only_users)
when "published"
card.watchers_and_subscribers(include_only_watching: true).without(creator)
watchers_and_subscribers(include_only_watching: true).without(creator, *mentionees)
when "commented"
watchers_and_subscribers.without(creator, *mentionees)
else
card.watchers_and_subscribers.without(creator)
watchers_and_subscribers.without(creator)
end
end
def mentionees
case source.action
when "published"
source.card.mentionees
when "commented"
source.comment.mentionees
else
[]
end
end
end
+4
View File
@@ -9,6 +9,10 @@ module User::Accessor
after_create_commit :grant_access_to_collections
end
def can_access_card?(card)
card.collection.accessible_to?(self)
end
private
def grant_access_to_collections
Access.insert_all Collection.all_access.pluck(:id).collect { |collection_id| { collection_id: collection_id, user_id: id } }