From 9583efbcacbb957a26baa4cf27d07bc3e3d89651 Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 4 Jan 2024 01:08:50 +0100 Subject: [PATCH] Fix update issues with reader and media See the discussion on #574 for the reasons behind this. --- .../commontator/comments_controller.rb | 5 +++-- app/models/user.rb | 20 ++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/controllers/commontator/comments_controller.rb b/app/controllers/commontator/comments_controller.rb index 31058b3da..68a9a70e0 100644 --- a/app/controllers/commontator/comments_controller.rb +++ b/app/controllers/commontator/comments_controller.rb @@ -209,8 +209,9 @@ def update_unread_status # make sure that the thread associated to this comment is marked as read # by the comment creator (unless some other user posted a comment in it # that has not yet been read) - @reader = Reader.find_or_create_by(user: current_user, - thread: @commontator_thread) + @reader = Reader.find_by(user: current_user, thread: @commontator_thread) + return unless @reader + if unseen_comments? @update_icon = true return diff --git a/app/models/user.rb b/app/models/user.rb index 7745f2260..a9b424b9e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -550,14 +550,20 @@ def subscribed_commentable_media_with_comments end def media_latest_comments - media = subscribed_commentable_media_with_comments - .map do |m| - { medium: m, - thread: m.commontator_thread, - latest_comment: m.commontator_thread - .comments.max_by(&:created_at) } + relevant_media = [] + + subscribed_commentable_media_with_comments.each do |m| + latest_comment = m.commontator_thread.comments + .reject { |c| c.creator == self } + .max_by(&:created_at) + next unless latest_comment + + relevant_media << { medium: m, + thread: m.commontator_thread, + latest_comment: latest_comment } end - media.sort_by { |x| x[:latest_comment].created_at }.reverse + + relevant_media.sort_by { |x| x[:latest_comment].created_at }.reverse end # lecture that are in the active term