diff --git a/app/controllers/cards_controller.rb b/app/controllers/cards_controller.rb index e52926955..7f1377e17 100644 --- a/app/controllers/cards_controller.rb +++ b/app/controllers/cards_controller.rb @@ -1,9 +1,9 @@ require "ostruct" class CardsController < ApplicationController - include CollectionScoped, Collections::ColumnsScoped + include Collections::ColumnsScoped - skip_before_action :set_collection, only: :index + before_action :set_collection, only: %i[ create ] before_action :set_card, only: %i[ show edit update destroy ] enable_collection_filtering only: :index @@ -47,8 +47,12 @@ class CardsController < ApplicationController end private + def set_collection + @collection = Current.user.collections.find(params[:collection_id]) + end + def set_card - @card = @collection.cards.find params[:id] + @card = Current.user.accessible_cards.find params[:id] end def card_params diff --git a/app/helpers/cards_helper.rb b/app/helpers/cards_helper.rb index 9d332794b..cdf5c5d7b 100644 --- a/app/helpers/cards_helper.rb +++ b/app/helpers/cards_helper.rb @@ -49,7 +49,7 @@ module CardsHelper end def button_to_delete_card(card) - button_to collection_card_path(card.collection, card), + button_to card_path(card), method: :delete, class: "btn txt-negative borderless txt-small", data: { turbo_frame: "_top", turbo_confirm: "Are you sure you want to permanently delete this card?" } do concat(icon_tag("trash")) concat(tag.span("Delete this card")) diff --git a/app/jobs/card/remove_inaccessible_notifications_job.rb b/app/jobs/card/remove_inaccessible_notifications_job.rb new file mode 100644 index 000000000..db7dec4e3 --- /dev/null +++ b/app/jobs/card/remove_inaccessible_notifications_job.rb @@ -0,0 +1,5 @@ +class Card::RemoveInaccessibleNotificationsJob < ApplicationJob + def perform(card) + card.remove_inaccessible_notifications + end +end diff --git a/app/models/ai/list_cards_tool.rb b/app/models/ai/list_cards_tool.rb index ec105ba73..a32456ddf 100644 --- a/app/models/ai/list_cards_tool.rb +++ b/app/models/ai/list_cards_tool.rb @@ -102,7 +102,7 @@ class Ai::ListCardsTool < Ai::Tool creator: card.creator.as_json(only: [ :id, :name ]), assignees: card.assignees.as_json(only: [ :id, :name ]), description: card.description.to_plain_text.truncate(1000), - url: collection_card_url(card.collection, card) + url: card_url(card) } end end diff --git a/app/models/ai/list_comments_tool.rb b/app/models/ai/list_comments_tool.rb index 8bc7a875a..20fa21417 100644 --- a/app/models/ai/list_comments_tool.rb +++ b/app/models/ai/list_comments_tool.rb @@ -89,7 +89,7 @@ class Ai::ListCommentsTool < Ai::Tool reacter: reaction.reacter.as_json(only: [ :id, :name ]) } end, - url: collection_card_url(comment.card.collection_id, comment.card, anchor: "comment_#{comment.id}") + url: card_url(comment.card, anchor: "comment_#{comment.id}") } end end diff --git a/app/models/card.rb b/app/models/card.rb index d8dc388d2..00214fa5d 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -39,6 +39,8 @@ class Card < ApplicationRecord end end + delegate :accessible_to?, to: :collection + def cache_key [ super, collection.name ].compact.join("/") end @@ -61,15 +63,18 @@ class Card < ApplicationRecord def handle_collection_change transaction do - old_collection = Collection.find_by(id: collection_id_before_last_save) - if old_collection.present? - track_event "collection_changed", particulars: { - old_collection: old_collection.name, - new_collection: collection.name - } - end + track_collection_change_event grant_access_to_assignees unless collection.all_access? end + + remove_inaccessible_notifications_later + end + + def track_collection_change_event + old_collection = Collection.find_by(id: collection_id_before_last_save) + if old_collection.present? + track_event "collection_changed", particulars: { old_collection: old_collection.name, new_collection: collection.name } + end end def grant_access_to_assignees diff --git a/app/models/card/promptable.rb b/app/models/card/promptable.rb index b9a00597e..c0374759b 100644 --- a/app/models/card/promptable.rb +++ b/app/models/card/promptable.rb @@ -27,7 +27,7 @@ module Card::Promptable * Collection id: #{collection_id} * Collection name: #{collection.name} * Number of comments: #{comments.count} - * Path: #{collection_card_path(collection, self, script_name: Account.sole.slug)} + * Path: #{card_path(self, script_name: Account.sole.slug)} END OF CARD #{id} PROMPT diff --git a/app/models/card/readable.rb b/app/models/card/readable.rb index 84db015ed..06c811715 100644 --- a/app/models/card/readable.rb +++ b/app/models/card/readable.rb @@ -7,17 +7,25 @@ module Card::Readable end end + def remove_inaccessible_notifications + User.find_each do |user| + notifications_for(user).destroy_all unless accessible_to?(user) + end + end + private - def notifications_for(user) - user.notifications.unread.where(source: notification_sources) + def remove_inaccessible_notifications_later + Card::RemoveInaccessibleNotificationsJob.perform_later(self) end - def notification_sources - event_notification_sources + mention_notification_sources + def notifications_for(user) + scope = user.notifications.unread + scope.where(source: event_notification_sources) + .or(scope.where(source: mention_notification_sources)) end def event_notification_sources - events + comment_creation_events + events.or(comment_creation_events) end def comment_creation_events @@ -25,7 +33,7 @@ module Card::Readable end def mention_notification_sources - mentions + comment_mentions + mentions.or(comment_mentions) end def comment_mentions diff --git a/app/models/comment/promptable.rb b/app/models/comment/promptable.rb index eb11fde80..93c931432 100644 --- a/app/models/comment/promptable.rb +++ b/app/models/comment/promptable.rb @@ -20,7 +20,7 @@ module Comment::Promptable * Card title: #{card.title} * Created by: #{creator.name}} * Created at: #{created_at}} - * Path: #{collection_card_path(card.collection, card, anchor: ActionView::RecordIdentifier.dom_id(self), script_name: Account.sole.slug)} + * Path: #{card_path(card, anchor: ActionView::RecordIdentifier.dom_id(self), script_name: Account.sole.slug)} END OF COMMENT #{id} PROMPT end diff --git a/app/models/notification.rb b/app/models/notification.rb index 3f5a00aac..0fa2350ee 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -10,6 +10,7 @@ class Notification < ApplicationRecord scope :ordered, -> { order(read_at: :desc, created_at: :desc) } after_create_commit :broadcast_unread + after_destroy_commit :broadcast_read after_create :bundle delegate :notifiable_target, to: :source diff --git a/app/models/notification_pusher.rb b/app/models/notification_pusher.rb index 6fd81e0a5..b5926e2be 100644 --- a/app/models/notification_pusher.rb +++ b/app/models/notification_pusher.rb @@ -113,6 +113,6 @@ class NotificationPusher end def card_path(card) - "#{account_prefix}#{collection_card_path(card.collection, card)}" + "#{account_prefix}#{Rails.application.routes.url_helpers.card_path(card)}" end end diff --git a/app/views/cards/container/_image.html.erb b/app/views/cards/container/_image.html.erb index b38f05c07..de2341aa1 100644 --- a/app/views/cards/container/_image.html.erb +++ b/app/views/cards/container/_image.html.erb @@ -4,7 +4,7 @@ Remove background image <% end %> <% elsif !card.closed? %> - <%= form_with model: card, url: collection_card_path(card.collection, card), data: { controller: "form" } do |form| %> + <%= form_with model: card, url: card_path(card), data: { controller: "form" } do |form| %>