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)
|
||||
end
|
||||
|
||||
before_action only: %i(index) do
|
||||
require_access_token %w(read)
|
||||
end
|
||||
|
||||
rescue_from ActiveRecord::RecordNotFound do
|
||||
render json: {error: I18n.t("conversations.not_found")}, status: 404
|
||||
end
|
||||
|
||||
def create
|
||||
conversation = conversation_service.find!(params[:conversation_id])
|
||||
opts = params.require(:message).permit(:text)
|
||||
message = current_user.build_message(conversation, opts)
|
||||
opts = params.require(:body)
|
||||
message = current_user.build_message(conversation, {
|
||||
:text => opts[:body]
|
||||
})
|
||||
message.save!
|
||||
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
|
||||
|
||||
def conversation_service
|
||||
ConversationService.new(current_user)
|
||||
end
|
||||
|
||||
def message_json(message)
|
||||
MessagePresenter.new(message).as_api_json
|
||||
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
|
||||
resources :conversations, only: %i(show index create destroy) do
|
||||
delete "visibility" => "conversation_visibilities#destroy"
|
||||
resources :messages, only: %i(create)
|
||||
resources :messages, only: %i(index create)
|
||||
end
|
||||
get "activity" => "streams#activity", :as => "activity_stream"
|
||||
get "stream" => "streams#multi", :as => "stream"
|
||||
|
|
|
|||
|
|
@ -13,53 +13,90 @@ describe Api::V0::MessagesController do
|
|||
@conversation = {
|
||||
author_id: auth.user.id,
|
||||
subject: "new conversation",
|
||||
text: "first message",
|
||||
messages: [{
|
||||
author: auth.user,
|
||||
text: "first message"
|
||||
}],
|
||||
body: "first message",
|
||||
recipients: [alice.person.id],
|
||||
access_token: access_token
|
||||
}
|
||||
|
||||
@message = {
|
||||
text: "reply to first message"
|
||||
body: "reply to first message"
|
||||
}
|
||||
end
|
||||
|
||||
describe "#create " do
|
||||
before do
|
||||
post api_v0_conversations_path, @conversation
|
||||
@conversation_id = JSON.parse(response.body)["conversation"]["id"]
|
||||
@conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
|
||||
end
|
||||
|
||||
context "with valid data" do
|
||||
it "creates the message in the conversation scope" do
|
||||
post(
|
||||
api_v0_conversation_messages_path(@conversation_id),
|
||||
message: @message, access_token: access_token
|
||||
api_v0_conversation_messages_path(@conversation_guid),
|
||||
body: @message, access_token: access_token
|
||||
)
|
||||
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(
|
||||
api_v0_conversation_path(@conversation_id),
|
||||
api_v0_conversation_messages_path(@conversation_guid),
|
||||
access_token: access_token
|
||||
)
|
||||
response_body = JSON.parse(response.body)["conversation"]
|
||||
expect(response_body["messages"].length).to eq 2
|
||||
text = response_body["messages"][1]["text"]
|
||||
expect(text).to eq "reply to first message"
|
||||
messages = JSON.parse(response.body)
|
||||
expect(messages.length).to eq 2
|
||||
text = messages[1]["body"]
|
||||
expect(text).to eq @message[:body]
|
||||
end
|
||||
end
|
||||
|
||||
context "without valid data" do
|
||||
it "returns a wrong parameter error (400)" do
|
||||
post(
|
||||
api_v0_conversation_messages_path(@conversation_id),
|
||||
api_v0_conversation_messages_path(@conversation_guid),
|
||||
access_token: access_token
|
||||
)
|
||||
expect(response.status).to eq 400
|
||||
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
|
||||
|
||||
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