summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--users/app/controllers/v1/messages_controller.rb27
-rw-r--r--users/app/models/message.rb10
-rw-r--r--users/app/models/user.rb12
-rw-r--r--users/app/models/user_message.rb25
-rw-r--r--users/config/routes.rb2
-rw-r--r--users/test/functional/v1/messages_controller_test.rb54
6 files changed, 130 insertions, 0 deletions
diff --git a/users/app/controllers/v1/messages_controller.rb b/users/app/controllers/v1/messages_controller.rb
new file mode 100644
index 0000000..d49b161
--- /dev/null
+++ b/users/app/controllers/v1/messages_controller.rb
@@ -0,0 +1,27 @@
+module V1
+ class MessagesController < ApplicationController
+
+ # TODO need to add authentication
+ respond_to :json
+
+ # for now, will not pass unseen, so unseen will always be true
+ def user_messages(unseen = true)
+ user = User.find(params[:user_id])
+ render json: (user ? user.messages : [] )
+ end
+
+ # routes ensure this is only for PUT
+ def mark_read
+ user_message = UserMessage.find_by_user_id_and_message_id([params[:user_id], params[:message_id]])
+ user_message.seen = true if user_message
+
+ # TODO what to return?
+ if user_message and user_message.save
+ render json: true
+ else
+ render json: false
+ end
+ end
+
+ end
+end
diff --git a/users/app/models/message.rb b/users/app/models/message.rb
new file mode 100644
index 0000000..38fa71e
--- /dev/null
+++ b/users/app/models/message.rb
@@ -0,0 +1,10 @@
+class Message < CouchRest::Model::Base
+
+ use_database :messages
+
+ property :text, String
+
+ design do
+ end
+
+end
diff --git a/users/app/models/user.rb b/users/app/models/user.rb
index 720f5a9..fe3a127 100644
--- a/users/app/models/user.rb
+++ b/users/app/models/user.rb
@@ -72,6 +72,18 @@ class User < CouchRest::Model::Base
Ticket.for_user(self).limit(count).all #defaults to having most recent updated first
end
+ def messages(unseen = true)
+
+ user_messages = unseen ? UserMessage.by_user_id_and_seen(:key => [self.id, false]).all : UserMessage.by_user_id(:key => self.id).all
+
+ messages = []
+ user_messages.each do |um|
+ messages << Message.find(um.message.id)
+ end
+ messages
+
+ end
+
# DEPRECATED
#
# Please set the key on the identity directly
diff --git a/users/app/models/user_message.rb b/users/app/models/user_message.rb
new file mode 100644
index 0000000..37aae0b
--- /dev/null
+++ b/users/app/models/user_message.rb
@@ -0,0 +1,25 @@
+class UserMessage < CouchRest::Model::Base
+
+ use_database :user_messages
+ belongs_to :user
+ belongs_to :message
+
+ validates :user_id, presence: true
+ validates :message_id, presence: true
+
+ # should not have multiple rows connecting one user to particular message:
+ validates_uniqueness_of :user_id, :scope => [:message_id]
+
+ property :seen, TrueClass, :default => false
+
+ design do
+ view :by_user_id
+ view :by_message_id
+ view :by_user_id_and_seen
+ view :by_user_id_and_message_id
+ own_path = Pathname.new(File.dirname(__FILE__))
+ load_views(own_path.join('..', 'designs', 'user_message'))
+
+ end
+
+end
diff --git a/users/config/routes.rb b/users/config/routes.rb
index 736b283..9a7c531 100644
--- a/users/config/routes.rb
+++ b/users/config/routes.rb
@@ -6,6 +6,8 @@ Rails.application.routes.draw do
resources :sessions, :only => [:new, :create, :update]
delete "logout" => "sessions#destroy", :as => "logout"
resources :users, :only => [:create, :update, :destroy, :index]
+ get "user_messages/:user_id" => "messages#user_messages"
+ put "mark_read/:user_id/:message_id" => "messages#mark_read"
end
scope "(:locale)", :locale => MATCH_LOCALE do
diff --git a/users/test/functional/v1/messages_controller_test.rb b/users/test/functional/v1/messages_controller_test.rb
new file mode 100644
index 0000000..7bffa8f
--- /dev/null
+++ b/users/test/functional/v1/messages_controller_test.rb
@@ -0,0 +1,54 @@
+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
+ end
+
+ teardown do
+ @user_message.destroy
+ @user.destroy
+ @message.destroy
+ end
+
+ test "get messages for user" do
+ get :user_messages, :user_id => @user.id
+ assert response.body.include? @message.text
+ assert response.body.include? @message.id
+ end
+
+ test "mark message read for user" do
+ assert !@user_message.seen
+ put :mark_read, :user_id => @user.id, :message_id => @message.id
+ @user_message.reload
+ assert @user_message.seen
+ assert_json_response true
+ end
+
+ test "do not get seen messages" do
+ @user_message.seen = true
+ @user_message.save
+ get :user_messages, :user_id => @user.id
+ assert !(response.body.include? @message.text)
+ assert !(response.body.include? @message.id)
+ end
+
+ test "empty messages for non-existing user" do
+ get :user_messages, :user_id => 'some random string'
+ assert_json_response []
+ end
+
+ test "mark read responds even with bad inputs" do
+ put :mark_read, :user_id => 'nonsense', :message_id => 'more nonsense'
+ assert_json_response false
+ end
+
+end