diff --git a/app/controllers/api/v0/base_controller.rb b/app/controllers/api/v0/base_controller.rb index 0ff5b2219..1c20f2956 100644 --- a/app/controllers/api/v0/base_controller.rb +++ b/app/controllers/api/v0/base_controller.rb @@ -8,19 +8,26 @@ module Api protected rescue_from Exception do |e| + pp e logger.error e.message render json: {error: e.message}, status: 500 end rescue_from ActiveRecord::RecordNotFound do + pp e + logger.error e.message render json: {error: I18n.t("api.error.not_found")}, status: 404 end rescue_from ActiveRecord::RecordInvalid do |e| + pp e + logger.error e.message render json: {error: e.to_s}, status: 400 end rescue_from ActionController::ParameterMissing do |e| + pp e + logger.error e.message render json: { error: I18n.t("api.error.wrong_parameters"), message: e.message diff --git a/app/controllers/api/v0/conversations_controller.rb b/app/controllers/api/v0/conversations_controller.rb index 8e394396e..ff0addc17 100644 --- a/app/controllers/api/v0/conversations_controller.rb +++ b/app/controllers/api/v0/conversations_controller.rb @@ -16,7 +16,14 @@ module Api end def index - conversations = conversation_service.all_for_user + filter = {} + if params[:only_after] then + filter["only_after"] = params[:only_after] + end + if params[:unread] then + filter["unread"] = params[:unread] + end + conversations = conversation_service.all_for_user(filter) render json: conversations.map {|x| conversation_as_json(x) } end diff --git a/app/controllers/api/v0/messages_controller.rb b/app/controllers/api/v0/messages_controller.rb index 2ce422fcb..2a2f2f9b7 100644 --- a/app/controllers/api/v0/messages_controller.rb +++ b/app/controllers/api/v0/messages_controller.rb @@ -26,7 +26,10 @@ module Api def index conversation = conversation_service.find!(params[:conversation_id]) - render json: conversation.messages.map {|x| message_json(x)}, status: 201 + conversation.set_read(user) + render json: conversation.messages.map { + |x| message_json(x) + }, status: 201 end def conversation_service diff --git a/app/services/conversation_service.rb b/app/services/conversation_service.rb index 7c8d17160..182dd8500 100644 --- a/app/services/conversation_service.rb +++ b/app/services/conversation_service.rb @@ -3,12 +3,21 @@ class ConversationService @user = user end - def all_for_user - Conversation.where(author_id: @user.person.id) + def all_for_user(filter={}) + conversation_filter = {} + if !filter[:only_after].nil? then + conversation_filter = \ + 'conversations.created_at >= ?', filter[:only_after] + end + + visibility_filter = {person_id: @user.person_id} + if filter[:unread] == true then + visibility_filter["unread"] = 0 + end + + Conversation.where(conversation_filter) .joins(:conversation_visibilities) - .where(conversation_visibilities: { - person_id: @user.person_id - }) + .where(conversation_visibilities: visibility_filter) .all end diff --git a/spec/integration/api/conversations_controller_spec.rb b/spec/integration/api/conversations_controller_spec.rb index 5024111c5..4b9b5eb98 100644 --- a/spec/integration/api/conversations_controller_spec.rb +++ b/spec/integration/api/conversations_controller_spec.rb @@ -24,6 +24,7 @@ describe Api::V0::ConversationsController do context "with valid data" do it "creates the conversation" do post api_v0_conversations_path, @conversation + @conversation_guid = JSON.parse(response.body)["conversation"]["guid"] conversation = JSON.parse(response.body)["conversation"] expect(response.status).to eq 201 @@ -31,6 +32,7 @@ describe Api::V0::ConversationsController do expect(conversation["subject"]).to eq @conversation[:subject] expect(conversation["created_at"]).to_not be_nil expect(conversation["participants"].length).to eq 2 + conversation_service.find!(@conversation_guid) end end @@ -46,13 +48,37 @@ describe Api::V0::ConversationsController do before do post api_v0_conversations_path, @conversation post api_v0_conversations_path, @conversation + sleep(1) + post api_v0_conversations_path, @conversation + conversation_guid = JSON.parse(response.body)["conversation"]["guid"] + conversation = conversation_service.find!(conversation_guid) + conversation.conversation_visibilities[0].unread = 1 + conversation.conversation_visibilities[0].save! + conversation.conversation_visibilities[1].unread = 1 + conversation.conversation_visibilities[1].save! + @date = conversation.created_at end it "returns all the user conversations" do get api_v0_conversations_path, access_token: access_token expect(response.status).to eq 200 + expect(JSON.parse(response.body).length).to eq 3 + end + + it "returns all the user unread conversations" do + get api_v0_conversations_path, unread: true, access_token: access_token + expect(response.status).to eq 200 expect(JSON.parse(response.body).length).to eq 2 end + + it "returns all the user conversations after a given date" do + get( + api_v0_conversations_path, + only_after: @date, access_token: access_token + ) + expect(response.status).to eq 200 + expect(JSON.parse(response.body).length).to eq 1 + end end describe "#show" do @@ -71,7 +97,6 @@ describe Api::V0::ConversationsController do conversation = JSON.parse(response.body)["conversation"] expect(conversation["guid"]).to eq conversation_guid expect(conversation["subject"]).to eq @conversation[:subject] - expect(conversation["created_at"]).to_not be_nil expect(conversation["participants"].length).to eq 2 expect(conversation["read"]).to eq true end @@ -161,4 +186,9 @@ describe Api::V0::ConversationsController do end end end + + def conversation_service + ConversationService.new(alice) + end + end diff --git a/spec/services/conversation_service.rb b/spec/services/conversation_service.rb index 857e8c08c..4633813e4 100644 --- a/spec/services/conversation_service.rb +++ b/spec/services/conversation_service.rb @@ -7,9 +7,51 @@ describe ConversationService do conversation = alice.build_conversation(opts) conversation.save + describe "#all_for_user" do + before do + opts = { + subject: "conversation subject 2", + message: {text: "conversation text 2"}, + participant_ids: [bob.person.id] + } + @conversation = alice.build_conversation(opts) + @conversation.save! + sleep(1) + @date = @conversation.created_at + opts = { + subject: "conversation subject 3", + message: {text: "conversation text 3"}, + participant_ids: [bob.person.id] + } + @conversation = alice.build_conversation(opts) + @conversation.save! + end + + it "returns all conversations" do + expect(alice_conversation_service.all_for_user().length).to eq(3) + expect(bob_conversation_service.all_for_user().length).to eq(3) + end + + it "returns all unread conversations" do + @conversation.conversation_visibilities[0].unread = true + @conversation.conversation_visibilities[0].save! + conversations = bob_conversation_service.all_for_user( + filter={unread: true} + ) + expect(conversations.length).to eq(2) + end + + it "returns conversation after a given date" do + conversations = bob_conversation_service.all_for_user( + filter={only_after: @date} + ) + expect(conversations.length).to eq(2) + end + end + describe "#find!" do it "returns the conversation, if it is the user's conversation" do - expect(alice_conversation_service.find!(conversation.guid)).to eq( + expect(bob_conversation_service.find!(conversation.guid)).to eq( conversation ) end