summaryrefslogtreecommitdiff
path: root/users
diff options
context:
space:
mode:
Diffstat (limited to 'users')
-rw-r--r--users/app/controllers/v1/messages_controller.rb12
-rw-r--r--users/app/models/user.rb13
-rw-r--r--users/app/models/user_message.rb3
-rw-r--r--users/test/functional/v1/messages_controller_test.rb33
4 files changed, 49 insertions, 12 deletions
diff --git a/users/app/controllers/v1/messages_controller.rb b/users/app/controllers/v1/messages_controller.rb
index d49b161..18f9f46 100644
--- a/users/app/controllers/v1/messages_controller.rb
+++ b/users/app/controllers/v1/messages_controller.rb
@@ -12,6 +12,7 @@ module V1
# routes ensure this is only for PUT
def mark_read
+=begin
user_message = UserMessage.find_by_user_id_and_message_id([params[:user_id], params[:message_id]])
user_message.seen = true if user_message
@@ -22,6 +23,17 @@ module V1
render json: false
end
end
+=end
+ if (user = User.find(params[:user_id])) && Message.find(params[:message_id])
+ user.message_ids_seen << params[:message_id] if !user.message_ids_seen.include?(params[:message_id]) #is it quicker to instead run uniq after adding?
+ user.message_ids_to_see.delete(params[:message_id])
+ user.save
+ render json: true
+ return
+ else
+ render json: false
+ end
+ end
end
end
diff --git a/users/app/models/user.rb b/users/app/models/user.rb
index fe3a127..fc63ae9 100644
--- a/users/app/models/user.rb
+++ b/users/app/models/user.rb
@@ -13,6 +13,9 @@ class User < CouchRest::Model::Base
property :desired_service_level_code, Integer, :accessible => true
property :effective_service_level_code, Integer, :accessible => true
+ property :message_ids_to_see, [String]
+ property :message_ids_seen, [String]
+
before_save :update_effective_service_level
validates :login, :password_salt, :password_verifier,
@@ -74,6 +77,7 @@ class User < CouchRest::Model::Base
def messages(unseen = true)
+=begin
user_messages = unseen ? UserMessage.by_user_id_and_seen(:key => [self.id, false]).all : UserMessage.by_user_id(:key => self.id).all
messages = []
@@ -81,6 +85,15 @@ class User < CouchRest::Model::Base
messages << Message.find(um.message.id)
end
messages
+=end
+
+ message_ids = unseen ? self.message_ids_to_see : self.message_ids_to_see + self.message_ids_seen # TODO check unique?
+
+ messages = []
+ message_ids.each do |message_id|
+ messages << Message.find(message_id)
+ end
+ messages
end
diff --git a/users/app/models/user_message.rb b/users/app/models/user_message.rb
index 37aae0b..51ca46c 100644
--- a/users/app/models/user_message.rb
+++ b/users/app/models/user_message.rb
@@ -1,4 +1,5 @@
-class UserMessage < CouchRest::Model::Base
+# TODO WOULD NOT USE anymore, in proposed alternate, so delete file.
+class XXXXUserMessage < CouchRest::Model::Base
use_database :user_messages
belongs_to :user
diff --git a/users/test/functional/v1/messages_controller_test.rb b/users/test/functional/v1/messages_controller_test.rb
index 7bffa8f..d7ada82 100644
--- a/users/test/functional/v1/messages_controller_test.rb
+++ b/users/test/functional/v1/messages_controller_test.rb
@@ -1,20 +1,23 @@
require 'test_helper'
class V1::MessagesControllerTest < ActionController::TestCase
-
+
#TODO ensure authentication for all tests here
setup do
- @user = FactoryGirl.build(:user)
- @user.save
@message = Message.new(:text => 'a test message')
@message.save
- @user_message = UserMessage.new(:message_id => @message.id, :user_id => @user.id)
- @user_message.save
+ @user = FactoryGirl.build(:user)
+ @user.message_ids_to_see << @message.id
+ @user.save
+
+ # @user_message = UserMessage.new(:message_id => @message.id, :user_id => @user.id)
+ # @user_message.save
+
end
teardown do
- @user_message.destroy
+ # @user_message.destroy
@user.destroy
@message.destroy
end
@@ -26,16 +29,24 @@ class V1::MessagesControllerTest < ActionController::TestCase
end
test "mark message read for user" do
- assert !@user_message.seen
+ #assert !@user_message.seen
+ assert @user.message_ids_to_see.include?(@message.id)
+ assert !@user.message_ids_seen.include?(@message.id)
+
put :mark_read, :user_id => @user.id, :message_id => @message.id
- @user_message.reload
- assert @user_message.seen
+ #@user_message.reload
+ #assert @user_message.seen
+ @user.reload
+ assert !@user.message_ids_to_see.include?(@message.id)
+ assert @user.message_ids_seen.include?(@message.id)
assert_json_response true
end
test "do not get seen messages" do
- @user_message.seen = true
- @user_message.save
+ # @user_message.seen = true
+ # @user_message.save
+ put :mark_read, :user_id => @user.id, :message_id => @message.id
+ @user.reload
get :user_messages, :user_id => @user.id
assert !(response.body.include? @message.text)
assert !(response.body.include? @message.id)