diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb index 0ca020e4..f8f01724 100644 --- a/app/controllers/activities_controller.rb +++ b/app/controllers/activities_controller.rb @@ -10,7 +10,6 @@ def index .includes(subject: [ :topic, { message: :sender } ]) .order(created_at: :desc) .limit(100) - mark_shown_as_read!(@activities) @unread_count = base_scope.unread.count end @@ -19,8 +18,26 @@ def mark_all_read redirect_to activities_path, notice: "Marked all as read" end + def read + activity = base_scope.find(params[:id]) + related_activities = base_scope.where(subject: activity.subject) + related_activities.mark_all_as_read! + redirect_to path_for_subject(activity.subject) + end + private + def path_for_subject(subject) + case subject + when Note + topic_path(subject.topic, anchor: subject.message_id ? helpers.message_dom_id(subject.message) : "thread-notes") + when Message + topic_path(subject.topic, anchor: helpers.message_dom_id(subject)) + else + activities_path + end + end + def base_scope current_user.activities.visible end @@ -36,10 +53,4 @@ def apply_query(scope) Activity.from("(#{note_query.to_sql} UNION #{message_query.to_sql}) AS activities") end - - def mark_shown_as_read!(activities) - unread_ids = activities.select { |a| a.read_at.nil? }.map(&:id) - return if unread_ids.empty? - Activity.where(id: unread_ids).update_all(read_at: Time.current) - end end diff --git a/app/models/activity.rb b/app/models/activity.rb index b01dee3b..52e7377f 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -8,4 +8,8 @@ class Activity < ApplicationRecord def mark_read! update!(read_at: Time.current) end + + def self.mark_all_as_read! + self.where(read_at: nil).update_all(read_at: Time.current) + end end diff --git a/app/views/activities/index.html.slim b/app/views/activities/index.html.slim index 42769fd9..6edb2c06 100644 --- a/app/views/activities/index.html.slim +++ b/app/views/activities/index.html.slim @@ -24,7 +24,7 @@ - if subject.is_a?(Note) .activity-body .activity-title - = link_to(subject.topic.title, topic_path(subject.topic, anchor: subject.message_id ? message_dom_id(subject.message) : "thread-notes")) + = link_to subject.topic.title, read_activity_path(activity) .activity-snippet = truncate(subject.body, length: 160) - if subject.note_tags.any? .activity-tags @@ -36,7 +36,7 @@ - elsif subject.is_a?(Message) .activity-body .activity-title - = link_to(subject.topic.title, topic_path(subject.topic, anchor: message_dom_id(subject))) + = link_to subject.topic.title, read_activity_path(activity) .activity-snippet strong> = subject.subject = truncate(subject.body, length: 140) diff --git a/config/routes.rb b/config/routes.rb index c7bb766f..d76bdf96 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -100,6 +100,7 @@ end resources :activities, only: [ :index ] do post :mark_all_read, on: :collection + get :read, on: :member end resources :notes, only: [ :create, :update, :destroy ] resources :note_mentions, only: [ :destroy ]