From bd867c51c4d9e3d4c6b4c55d326eb9b13b89288b Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 10 Feb 2014 14:23:38 +0100 Subject: minor: move some logic from message controller into model --- users/app/controllers/v1/messages_controller.rb | 5 +---- users/app/models/message.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'users/app') diff --git a/users/app/controllers/v1/messages_controller.rb b/users/app/controllers/v1/messages_controller.rb index 1b994ca..55292ff 100644 --- a/users/app/controllers/v1/messages_controller.rb +++ b/users/app/controllers/v1/messages_controller.rb @@ -13,10 +13,7 @@ module V1 def update message = Message.find(params[:id]) if (message and current_user) - message.user_ids_to_show.delete(current_user.id) - # is it necessary to keep track of what users have already seen it? - message.user_ids_have_shown << current_user.id if !message.user_ids_have_shown.include?(current_user.id) - # TODO: is it quicker to call uniq! after adding rather than check if it is already included? + message.mark_as_read_by(current_user) message.save render json: true else diff --git a/users/app/models/message.rb b/users/app/models/message.rb index d9ccee8..7c05e06 100644 --- a/users/app/models/message.rb +++ b/users/app/models/message.rb @@ -13,4 +13,18 @@ class Message < CouchRest::Model::Base load_views(own_path.join('..', 'designs', 'message')) end + def mark_as_read_by(user) + user_ids_to_show.delete(user.id) + # is it necessary to keep track of what users have already seen it? + user_ids_have_shown << user.id unless read_by?(user) + # TODO: is it quicker to call uniq! after adding rather than check if it is already included? + end + + def read_by?(user) + user_ids_have_shown.include?(user.id) + end + + def unread_by?(user) + user_ids_to_shown.include?(user.id) + end end -- cgit v1.2.3 From 2f00e2ec3c076e4910008904bc01e09df097944e Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 10 Feb 2014 14:24:08 +0100 Subject: minor: rename var that holds a single user id to userId --- users/app/designs/message/by_user_ids_to_show.js | 6 +++--- users/app/designs/message/by_user_ids_to_show_and_created_at.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'users/app') diff --git a/users/app/designs/message/by_user_ids_to_show.js b/users/app/designs/message/by_user_ids_to_show.js index 95ccd0d..e33566b 100644 --- a/users/app/designs/message/by_user_ids_to_show.js +++ b/users/app/designs/message/by_user_ids_to_show.js @@ -1,7 +1,7 @@ function (doc) { if (doc.type === 'Message' && doc.user_ids_to_show && Array.isArray(doc.user_ids_to_show)) { - doc.user_ids_to_show.forEach(function (userIdsToShow) { - emit(userIdsToShow, 1); + doc.user_ids_to_show.forEach(function (userId) { + emit(userId, 1); }); } -} \ No newline at end of file +} diff --git a/users/app/designs/message/by_user_ids_to_show_and_created_at.js b/users/app/designs/message/by_user_ids_to_show_and_created_at.js index 18969b8..54e4604 100644 --- a/users/app/designs/message/by_user_ids_to_show_and_created_at.js +++ b/users/app/designs/message/by_user_ids_to_show_and_created_at.js @@ -2,8 +2,8 @@ // call with something like Message.by_user_ids_to_show_and_created_at.startkey([user_id, start_date]).endkey([user_id,end_date]) function (doc) { if (doc.type === 'Message' && doc.user_ids_to_show && Array.isArray(doc.user_ids_to_show)) { - doc.user_ids_to_show.forEach(function (userIdsToShow) { - emit([userIdsToShow, doc.created_at], 1); + doc.user_ids_to_show.forEach(function (userId) { + emit([userId, doc.created_at], 1); }); } -} \ No newline at end of file +} -- cgit v1.2.3 From e3577906891f9ab0edd987544e3432f354bc1721 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 10 Feb 2014 19:41:02 +0100 Subject: fix unread_by? to check user_ids_to_show, remove TODO uniq! is probably not any faster than just checking include? --- users/app/models/message.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'users/app') diff --git a/users/app/models/message.rb b/users/app/models/message.rb index 7c05e06..424f094 100644 --- a/users/app/models/message.rb +++ b/users/app/models/message.rb @@ -17,7 +17,6 @@ class Message < CouchRest::Model::Base user_ids_to_show.delete(user.id) # is it necessary to keep track of what users have already seen it? user_ids_have_shown << user.id unless read_by?(user) - # TODO: is it quicker to call uniq! after adding rather than check if it is already included? end def read_by?(user) @@ -25,6 +24,6 @@ class Message < CouchRest::Model::Base end def unread_by?(user) - user_ids_to_shown.include?(user.id) + user_ids_to_show.include?(user.id) end end -- cgit v1.2.3