Make messages API conformed with docs
This commit is contained in:
parent
454be1b468
commit
bbbe3aea7f
4 changed files with 83 additions and 20 deletions
|
|
@ -5,22 +5,37 @@ module Api
|
||||||
require_access_token %w(read write)
|
require_access_token %w(read write)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
before_action only: %i(index) do
|
||||||
|
require_access_token %w(read)
|
||||||
|
end
|
||||||
|
|
||||||
rescue_from ActiveRecord::RecordNotFound do
|
rescue_from ActiveRecord::RecordNotFound do
|
||||||
render json: {error: I18n.t("conversations.not_found")}, status: 404
|
render json: {error: I18n.t("conversations.not_found")}, status: 404
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
conversation = conversation_service.find!(params[:conversation_id])
|
conversation = conversation_service.find!(params[:conversation_id])
|
||||||
opts = params.require(:message).permit(:text)
|
opts = params.require(:body)
|
||||||
message = current_user.build_message(conversation, opts)
|
message = current_user.build_message(conversation, {
|
||||||
|
:text => opts[:body]
|
||||||
|
})
|
||||||
message.save!
|
message.save!
|
||||||
Diaspora::Federation::Dispatcher.defer_dispatch(current_user, message)
|
Diaspora::Federation::Dispatcher.defer_dispatch(current_user, message)
|
||||||
render json: message, status: 201
|
render json: message_json(message), status: 201
|
||||||
|
end
|
||||||
|
|
||||||
|
def index
|
||||||
|
conversation = conversation_service.find!(params[:conversation_id])
|
||||||
|
render json: conversation.messages.map {|x| message_json(x)}, status: 201
|
||||||
end
|
end
|
||||||
|
|
||||||
def conversation_service
|
def conversation_service
|
||||||
ConversationService.new(current_user)
|
ConversationService.new(current_user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def message_json(message)
|
||||||
|
MessagePresenter.new(message).as_api_json
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
11
app/presenters/message_presenter.rb
Normal file
11
app/presenters/message_presenter.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
class MessagePresenter < BasePresenter
|
||||||
|
|
||||||
|
def as_api_json
|
||||||
|
{
|
||||||
|
guid: @presentable.guid,
|
||||||
|
created_at: @presentable.created_at,
|
||||||
|
body: @presentable.text,
|
||||||
|
author: PersonPresenter.new(@presentable.author).as_api_json
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -229,7 +229,7 @@ Rails.application.routes.draw do
|
||||||
end
|
end
|
||||||
resources :conversations, only: %i(show index create destroy) do
|
resources :conversations, only: %i(show index create destroy) do
|
||||||
delete "visibility" => "conversation_visibilities#destroy"
|
delete "visibility" => "conversation_visibilities#destroy"
|
||||||
resources :messages, only: %i(create)
|
resources :messages, only: %i(index create)
|
||||||
end
|
end
|
||||||
get "activity" => "streams#activity", :as => "activity_stream"
|
get "activity" => "streams#activity", :as => "activity_stream"
|
||||||
get "stream" => "streams#multi", :as => "stream"
|
get "stream" => "streams#multi", :as => "stream"
|
||||||
|
|
|
||||||
|
|
@ -13,53 +13,90 @@ describe Api::V0::MessagesController do
|
||||||
@conversation = {
|
@conversation = {
|
||||||
author_id: auth.user.id,
|
author_id: auth.user.id,
|
||||||
subject: "new conversation",
|
subject: "new conversation",
|
||||||
text: "first message",
|
body: "first message",
|
||||||
messages: [{
|
|
||||||
author: auth.user,
|
|
||||||
text: "first message"
|
|
||||||
}],
|
|
||||||
recipients: [alice.person.id],
|
recipients: [alice.person.id],
|
||||||
access_token: access_token
|
access_token: access_token
|
||||||
}
|
}
|
||||||
|
|
||||||
@message = {
|
@message = {
|
||||||
text: "reply to first message"
|
body: "reply to first message"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#create " do
|
describe "#create " do
|
||||||
before do
|
before do
|
||||||
post api_v0_conversations_path, @conversation
|
post api_v0_conversations_path, @conversation
|
||||||
@conversation_id = JSON.parse(response.body)["conversation"]["id"]
|
@conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with valid data" do
|
context "with valid data" do
|
||||||
it "creates the message in the conversation scope" do
|
it "creates the message in the conversation scope" do
|
||||||
post(
|
post(
|
||||||
api_v0_conversation_messages_path(@conversation_id),
|
api_v0_conversation_messages_path(@conversation_guid),
|
||||||
message: @message, access_token: access_token
|
body: @message, access_token: access_token
|
||||||
)
|
)
|
||||||
expect(response.status).to eq 201
|
expect(response.status).to eq 201
|
||||||
expect(JSON.parse(response.body)["message"]["id"]).to_not be_nil
|
|
||||||
|
message = JSON.parse(response.body)
|
||||||
|
expect(message["guid"]).to_not be_nil
|
||||||
|
expect(message["author"]).to_not be_nil
|
||||||
|
expect(message["created_at"]).to_not be_nil
|
||||||
|
expect(message["body"]).to_not be_nil
|
||||||
|
|
||||||
get(
|
get(
|
||||||
api_v0_conversation_path(@conversation_id),
|
api_v0_conversation_messages_path(@conversation_guid),
|
||||||
access_token: access_token
|
access_token: access_token
|
||||||
)
|
)
|
||||||
response_body = JSON.parse(response.body)["conversation"]
|
messages = JSON.parse(response.body)
|
||||||
expect(response_body["messages"].length).to eq 2
|
expect(messages.length).to eq 2
|
||||||
text = response_body["messages"][1]["text"]
|
text = messages[1]["body"]
|
||||||
expect(text).to eq "reply to first message"
|
expect(text).to eq @message[:body]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "without valid data" do
|
context "without valid data" do
|
||||||
it "returns a wrong parameter error (400)" do
|
it "returns a wrong parameter error (400)" do
|
||||||
post(
|
post(
|
||||||
api_v0_conversation_messages_path(@conversation_id),
|
api_v0_conversation_messages_path(@conversation_guid),
|
||||||
access_token: access_token
|
access_token: access_token
|
||||||
)
|
)
|
||||||
expect(response.status).to eq 400
|
expect(response.status).to eq 400
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "with wrong conversation id" do
|
||||||
|
it "returns a a not found error (404)" do
|
||||||
|
post(
|
||||||
|
api_v0_conversation_messages_path(42),
|
||||||
|
access_token: access_token
|
||||||
|
)
|
||||||
|
expect(response.status).to eq 404
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#index " do
|
||||||
|
before do
|
||||||
|
post api_v0_conversations_path, @conversation
|
||||||
|
@conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
|
||||||
|
end
|
||||||
|
|
||||||
|
context "retrieving messages" do
|
||||||
|
it "returns all messages related to conversation" do
|
||||||
|
get(
|
||||||
|
api_v0_conversation_messages_path(@conversation_guid),
|
||||||
|
access_token: access_token
|
||||||
|
)
|
||||||
|
messages = JSON.parse(response.body)
|
||||||
|
expect(messages.length).to eq 1
|
||||||
|
|
||||||
|
message = messages[0]
|
||||||
|
expect(message["guid"]).to_not be_nil
|
||||||
|
expect(message["author"]).to_not be_nil
|
||||||
|
expect(message["created_at"]).to_not be_nil
|
||||||
|
expect(message["body"]).to_not be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue