diff options
Diffstat (limited to 'users/app')
-rw-r--r-- | users/app/controllers/v1/messages_controller.rb | 5 | ||||
-rw-r--r-- | users/app/designs/message/by_user_ids_to_show.js | 6 | ||||
-rw-r--r-- | users/app/designs/message/by_user_ids_to_show_and_created_at.js | 6 | ||||
-rw-r--r-- | users/app/models/message.rb | 13 |
4 files changed, 20 insertions, 10 deletions
diff --git a/users/app/controllers/v1/messages_controller.rb b/users/app/controllers/v1/messages_controller.rb index 90986e2..f71d0f1 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/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 +} diff --git a/users/app/models/message.rb b/users/app/models/message.rb index d9ccee8..424f094 100644 --- a/users/app/models/message.rb +++ b/users/app/models/message.rb @@ -13,4 +13,17 @@ 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) + end + + def read_by?(user) + user_ids_have_shown.include?(user.id) + end + + def unread_by?(user) + user_ids_to_show.include?(user.id) + end end |