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:
parent
2be3e9eaf3
commit
5040363f7a
6 changed files with 61 additions and 44 deletions
|
|
@ -30,7 +30,7 @@ module Api
|
||||||
def create
|
def create
|
||||||
conversation = conversation_service.build(
|
conversation = conversation_service.build(
|
||||||
params[:subject],
|
params[:subject],
|
||||||
params[:text],
|
params[:body],
|
||||||
params[:recipients]
|
params[:recipients]
|
||||||
)
|
)
|
||||||
conversation.save!
|
conversation.save!
|
||||||
|
|
@ -54,7 +54,7 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
def conversation_as_json(conversation)
|
def conversation_as_json(conversation)
|
||||||
ConversationPresenter.new(conversation).as_json
|
ConversationPresenter.new(conversation).as_api_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
class ConversationPresenter < BasePresenter
|
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: @presentable.guid,
|
||||||
guid: @conversation.guid,
|
subject: @presentable.subject,
|
||||||
created_at: @conversation.created_at,
|
created_at: @presentable.created_at,
|
||||||
subject: @conversation.subject,
|
read: read,
|
||||||
messages: @conversation.messages.map {|x| x.as_json["message"] },
|
participants: @presentable.participants.map {
|
||||||
author: @conversation.author,
|
|x| PersonPresenter.new(x).as_api_json
|
||||||
participants: @conversation.participants
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,15 @@ class PersonPresenter < BasePresenter
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def as_api_json
|
||||||
|
{
|
||||||
|
guid: guid,
|
||||||
|
diaspora_id: diaspora_handle,
|
||||||
|
name: name,
|
||||||
|
avatar: AvatarPresenter.new(@presentable).medium,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def full_hash
|
def full_hash
|
||||||
base_hash_with_contact.merge(
|
base_hash_with_contact.merge(
|
||||||
relationship: relationship,
|
relationship: relationship,
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,12 @@ class ConversationService
|
||||||
end
|
end
|
||||||
|
|
||||||
def all_for_user
|
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
|
end
|
||||||
|
|
||||||
def build(subject, text, recipients)
|
def build(subject, text, recipients)
|
||||||
|
|
@ -22,24 +27,26 @@ class ConversationService
|
||||||
@user.build_conversation(opts)
|
@user.build_conversation(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def find!(conversation_id)
|
def find!(conversation_guid)
|
||||||
|
conversation = Conversation.find_by!({guid: conversation_guid})
|
||||||
@user.conversations
|
@user.conversations
|
||||||
.joins(:conversation_visibilities)
|
.joins(:conversation_visibilities)
|
||||||
.where(conversation_visibilities: {
|
.where(conversation_visibilities: {
|
||||||
person_id: @user.person_id,
|
person_id: @user.person_id,
|
||||||
conversation_id: conversation_id
|
conversation_id: conversation.id
|
||||||
}).first!
|
}).first!
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy!(conversation_id)
|
def destroy!(conversation_guid)
|
||||||
conversation = find!(conversation_id)
|
conversation = find!(conversation_guid)
|
||||||
conversation.destroy!
|
conversation.destroy!
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_visibility(conversation_id)
|
def get_visibility(conversation_guid)
|
||||||
|
conversation = find!(conversation_guid)
|
||||||
ConversationVisibility.where(
|
ConversationVisibility.where(
|
||||||
person_id: @user.person.id,
|
person_id: @user.person.id,
|
||||||
conversation_id: conversation_id
|
conversation_id: conversation.id
|
||||||
).first!
|
).first!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,7 @@ describe Api::V0::ConversationsController 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
|
||||||
}
|
}
|
||||||
|
|
@ -26,12 +22,13 @@ describe Api::V0::ConversationsController do
|
||||||
context "with valid data" do
|
context "with valid data" do
|
||||||
it "creates the conversation" do
|
it "creates the conversation" do
|
||||||
post api_v0_conversations_path, @conversation
|
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.status).to eq 201
|
||||||
expect(response_body["messages"][0]["id"]).to_not be_nil
|
expect(conversation["guid"]).to_not be_nil
|
||||||
expect(response_body["id"]).to_not be_nil
|
expect(conversation["subject"]).to eq @conversation[:subject]
|
||||||
expect(response_body["participants"].length).to eq 2
|
expect(conversation["created_at"]).to_not be_nil
|
||||||
|
expect(conversation["participants"].length).to eq 2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -63,14 +60,18 @@ describe Api::V0::ConversationsController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the corresponding conversation" do
|
it "returns the corresponding conversation" do
|
||||||
conversation_id = JSON.parse(response.body)["conversation"]["id"]
|
conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
|
||||||
get(
|
get(
|
||||||
api_v0_conversation_path(conversation_id),
|
api_v0_conversation_path(conversation_guid),
|
||||||
access_token: access_token
|
access_token: access_token
|
||||||
)
|
)
|
||||||
expect(response.status).to eq 200
|
expect(response.status).to eq 200
|
||||||
result_id = JSON.parse(response.body)["conversation"]["id"]
|
conversation = JSON.parse(response.body)["conversation"]
|
||||||
expect(result_id).to eq conversation_id
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -86,20 +87,20 @@ describe Api::V0::ConversationsController do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#delete" do
|
describe "#delete" do
|
||||||
context "valid conversation ID" do
|
context "valid conversation GUID" do
|
||||||
before do
|
before do
|
||||||
post api_v0_conversations_path, @conversation
|
post api_v0_conversations_path, @conversation
|
||||||
end
|
end
|
||||||
|
|
||||||
it "deletes the conversation" do
|
it "deletes the conversation" do
|
||||||
conversation_id = JSON.parse(response.body)["conversation"]["id"]
|
conversation_guid = JSON.parse(response.body)["conversation"]["guid"]
|
||||||
delete(
|
delete(
|
||||||
api_v0_conversation_path(conversation_id),
|
api_v0_conversation_path(conversation_guid),
|
||||||
access_token: access_token
|
access_token: access_token
|
||||||
)
|
)
|
||||||
expect(response.status).to eq 204
|
expect(response.status).to eq 204
|
||||||
get(
|
get(
|
||||||
api_v0_conversation_path(conversation_id),
|
api_v0_conversation_path(conversation_guid),
|
||||||
access_token: access_token
|
access_token: access_token
|
||||||
)
|
)
|
||||||
expect(response.status).to eq 404
|
expect(response.status).to eq 404
|
||||||
|
|
|
||||||
|
|
@ -9,13 +9,13 @@ describe ConversationService do
|
||||||
|
|
||||||
describe "#find!" do
|
describe "#find!" do
|
||||||
it "returns the conversation, if it is the user's conversation" 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
|
conversation
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the conversation, if the user is recipient" do
|
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
|
conversation
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
@ -28,7 +28,7 @@ describe ConversationService do
|
||||||
|
|
||||||
it "raises RecordNotFound if the user is not recipient" do
|
it "raises RecordNotFound if the user is not recipient" do
|
||||||
expect {
|
expect {
|
||||||
eve_conversation_service.find!(conversation.id)
|
eve_conversation_service.find!(conversation.guid)
|
||||||
}.to raise_error ActiveRecord::RecordNotFound
|
}.to raise_error ActiveRecord::RecordNotFound
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -61,7 +61,7 @@ describe ConversationService do
|
||||||
|
|
||||||
describe "#get_visibility" do
|
describe "#get_visibility" do
|
||||||
it "returns visibility for current user" 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
|
expect(visibility).to_not be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -74,9 +74,9 @@ describe ConversationService do
|
||||||
|
|
||||||
describe "#destroy!" do
|
describe "#destroy!" do
|
||||||
it "deletes the conversation, when it is the user conversation" 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 {
|
expect {
|
||||||
alice_conversation_service.find!(conversation.id)
|
alice_conversation_service.find!(conversation.guid)
|
||||||
}.to raise_error ActiveRecord::RecordNotFound
|
}.to raise_error ActiveRecord::RecordNotFound
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -88,7 +88,7 @@ describe ConversationService do
|
||||||
|
|
||||||
it "raises RecordNotFound if the user is not part of the conversation" do
|
it "raises RecordNotFound if the user is not part of the conversation" do
|
||||||
expect {
|
expect {
|
||||||
eve_conversation_service.destroy!(conversation.id)
|
eve_conversation_service.destroy!(conversation.guid)
|
||||||
}.to raise_error ActiveRecord::RecordNotFound
|
}.to raise_error ActiveRecord::RecordNotFound
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue