Make conversation API data format ok with docs

* Base the API requesting on GUID instead of ID
* Include read field
* Do not include messages in conversation results
This commit is contained in:
Frank Rousseau 2017-05-26 23:17:52 +02:00
parent d03b830b07
commit 454be1b468
6 changed files with 61 additions and 44 deletions

View file

@ -30,7 +30,7 @@ module Api
def create
conversation = conversation_service.build(
params[:subject],
params[:text],
params[:body],
params[:recipients]
)
conversation.save!
@ -54,7 +54,7 @@ module Api
end
def conversation_as_json(conversation)
ConversationPresenter.new(conversation).as_json
ConversationPresenter.new(conversation).as_api_json
end
end
end

View file

@ -1,17 +1,17 @@
class ConversationPresenter < BasePresenter
def initialize(conversation)
@conversation = conversation
end
def as_json
def as_api_json
read =
@presentable.conversation_visibilities.length > 0 and
@presentable.conversation_visibilities[0].unread == 0
{
id: @conversation.id,
guid: @conversation.guid,
created_at: @conversation.created_at,
subject: @conversation.subject,
messages: @conversation.messages.map {|x| x.as_json["message"] },
author: @conversation.author,
participants: @conversation.participants
guid: @presentable.guid,
subject: @presentable.subject,
created_at: @presentable.created_at,
read: read,
participants: @presentable.participants.map {
|x| PersonPresenter.new(x).as_api_json
}
}
end
end

View file

@ -10,6 +10,15 @@ class PersonPresenter < BasePresenter
}
end
def as_api_json
{
guid: guid,
diaspora_id: diaspora_handle,
name: name,
avatar: AvatarPresenter.new(@presentable).medium,
}
end
def full_hash
base_hash_with_contact.merge(
relationship: relationship,

View file

@ -4,7 +4,12 @@ class ConversationService
end
def all_for_user
Conversation.where(author_id: @user.person.id).all
Conversation.where(author_id: @user.person.id)
.joins(:conversation_visibilities)
.where(conversation_visibilities: {
person_id: @user.person_id
})
.all
end
def build(subject, text, recipients)
@ -22,24 +27,26 @@ class ConversationService
@user.build_conversation(opts)
end
def find!(conversation_id)
def find!(conversation_guid)
conversation = Conversation.find_by!({guid: conversation_guid})
@user.conversations
.joins(:conversation_visibilities)
.where(conversation_visibilities: {
person_id: @user.person_id,
conversation_id: conversation_id
conversation_id: conversation.id
}).first!
end
def destroy!(conversation_id)
conversation = find!(conversation_id)
def destroy!(conversation_guid)
conversation = find!(conversation_guid)
conversation.destroy!
end
def get_visibility(conversation_id)
def get_visibility(conversation_guid)
conversation = find!(conversation_guid)
ConversationVisibility.where(
person_id: @user.person.id,
conversation_id: conversation_id
conversation_id: conversation.id
).first!
end
end

View file

@ -12,11 +12,7 @@ describe Api::V0::ConversationsController 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
}
@ -26,12 +22,13 @@ describe Api::V0::ConversationsController do
context "with valid data" do
it "creates the conversation" do
post api_v0_conversations_path, @conversation
response_body = JSON.parse(response.body)["conversation"]
conversation = JSON.parse(response.body)["conversation"]
expect(response.status).to eq 201
expect(response_body["messages"][0]["id"]).to_not be_nil
expect(response_body["id"]).to_not be_nil
expect(response_body["participants"].length).to eq 2
expect(conversation["guid"]).to_not be_nil
expect(conversation["subject"]).to eq @conversation[:subject]
expect(conversation["created_at"]).to_not be_nil
expect(conversation["participants"].length).to eq 2
end
end
@ -63,14 +60,18 @@ describe Api::V0::ConversationsController do
end
it "returns the corresponding conversation" do
conversation_id = JSON.parse(response.body)["conversation"]["id"]
conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
get(
api_v0_conversation_path(conversation_id),
api_v0_conversation_path(conversation_guid),
access_token: access_token
)
expect(response.status).to eq 200
result_id = JSON.parse(response.body)["conversation"]["id"]
expect(result_id).to eq conversation_id
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
end
@ -86,20 +87,20 @@ describe Api::V0::ConversationsController do
end
describe "#delete" do
context "valid conversation ID" do
context "valid conversation GUID" do
before do
post api_v0_conversations_path, @conversation
end
it "deletes the conversation" do
conversation_id = JSON.parse(response.body)["conversation"]["id"]
conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
delete(
api_v0_conversation_path(conversation_id),
api_v0_conversation_path(conversation_guid),
access_token: access_token
)
expect(response.status).to eq 204
get(
api_v0_conversation_path(conversation_id),
api_v0_conversation_path(conversation_guid),
access_token: access_token
)
expect(response.status).to eq 404

View file

@ -9,13 +9,13 @@ describe ConversationService do
describe "#find!" do
it "returns the conversation, if it is the user's conversation" do
expect(alice_conversation_service.find!(conversation.id)).to eq(
expect(alice_conversation_service.find!(conversation.guid)).to eq(
conversation
)
end
it "returns the conversation, if the user is recipient" do
expect(bob_conversation_service.find!(conversation.id)).to eq(
expect(bob_conversation_service.find!(conversation.guid)).to eq(
conversation
)
end
@ -28,7 +28,7 @@ describe ConversationService do
it "raises RecordNotFound if the user is not recipient" do
expect {
eve_conversation_service.find!(conversation.id)
eve_conversation_service.find!(conversation.guid)
}.to raise_error ActiveRecord::RecordNotFound
end
end
@ -61,7 +61,7 @@ describe ConversationService do
describe "#get_visibility" do
it "returns visibility for current user" do
visibility = alice_conversation_service.get_visibility(conversation.id)
visibility = alice_conversation_service.get_visibility(conversation.guid)
expect(visibility).to_not be_nil
end
@ -74,9 +74,9 @@ describe ConversationService do
describe "#destroy!" do
it "deletes the conversation, when it is the user conversation" do
alice_conversation_service.destroy!(conversation.id)
alice_conversation_service.destroy!(conversation.guid)
expect {
alice_conversation_service.find!(conversation.id)
alice_conversation_service.find!(conversation.guid)
}.to raise_error ActiveRecord::RecordNotFound
end
@ -88,7 +88,7 @@ describe ConversationService do
it "raises RecordNotFound if the user is not part of the conversation" do
expect {
eve_conversation_service.destroy!(conversation.id)
eve_conversation_service.destroy!(conversation.guid)
}.to raise_error ActiveRecord::RecordNotFound
end
end