From 3f2be132bdf058c3ced3afe6a0881870afc3e064 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Wed, 21 Jan 2015 22:40:31 -0500 Subject: [PATCH] VRFS-2539 protect against error during notification load if the related session is missing / clean up the notifications of nonexistent sessions --- ruby/lib/jam_ruby/models/music_session.rb | 1 + ruby/lib/jam_ruby/models/notification.rb | 9 ++++++-- .../assets/javascripts/notificationPanel.js | 21 ++++++++++++------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/ruby/lib/jam_ruby/models/music_session.rb b/ruby/lib/jam_ruby/models/music_session.rb index f59e0ca21..a29d0b2f9 100644 --- a/ruby/lib/jam_ruby/models/music_session.rb +++ b/ruby/lib/jam_ruby/models/music_session.rb @@ -427,6 +427,7 @@ module JamRuby # returns one user history per user, with instruments all crammed together, and with total duration def unique_user_histories + # only get the active users if the session is in progress user_filter = "music_sessions_user_history.session_removed_at is null" if self.session_removed_at.nil? MusicSessionUserHistory diff --git a/ruby/lib/jam_ruby/models/notification.rb b/ruby/lib/jam_ruby/models/notification.rb index 5fa628f3a..e0ec6590b 100644 --- a/ruby/lib/jam_ruby/models/notification.rb +++ b/ruby/lib/jam_ruby/models/notification.rb @@ -42,7 +42,7 @@ module JamRuby # used for persisted notifications def formatted_msg # target_user, band, session, recording, invitation, join_request = nil - source_user, band = nil + source_user, band, session = nil unless self.source_user_id.nil? source_user = User.find(self.source_user_id) @@ -53,7 +53,12 @@ module JamRuby end unless self.session_id.nil? - session = MusicSession.find(self.session_id) + session = MusicSession.find_by_id(self.session_id) + + # remove all notifications related to this session if it's not found + if session.nil? + Notification.delete_all "(session_id = '#{session_id}')" + end end self.class.format_msg(self.description, {:user => source_user, :band => band, :session => session}) diff --git a/web/app/assets/javascripts/notificationPanel.js b/web/app/assets/javascripts/notificationPanel.js index ca393b18d..fdf0ac3ee 100644 --- a/web/app/assets/javascripts/notificationPanel.js +++ b/web/app/assets/javascripts/notificationPanel.js @@ -207,19 +207,24 @@ isLoading = true; // retrieve pending notifications for this user rest.getNotifications(buildParams()) - .done(function(response) { - updateNotificationList(response); - isLoading = false; - }) - .fail(function() { - isLoading = false; - app.ajaxError(); - }) + .done(function(response) { + updateNotificationList(response); + isLoading = false; + }) + .fail(function() { + isLoading = false; + app.ajaxError(); + }) } function updateNotificationList(response) { $.each(response, function(index, val) { + // this means the session no longer exists + if (response.fan_access == null && response.musician_access == null) { + return; + } + if(val.description == context.JK.MessageType.TEXT_MESSAGE) { val.formatted_msg = textMessageDialog.formatTextMessage(val.message.substring(0, 200), val.source_user_id, val.source_user.name, val.message.length > 200).html(); }