diff --git a/app/presenters/comment_presenter.rb b/app/presenters/comment_presenter.rb index 161c77718..17af87688 100644 --- a/app/presenters/comment_presenter.rb +++ b/app/presenters/comment_presenter.rb @@ -1,27 +1,28 @@ # frozen_string_literal: true class CommentPresenter < BasePresenter - def initialize(comment) - @comment = comment - end - def as_json(opts={}) { - id: @comment.id, - guid: @comment.guid, - text: @comment.message.plain_text_for_json, - author: @comment.author.as_api_response(:backbone), - created_at: @comment.created_at, - mentioned_people: @comment.mentioned_people.as_api_response(:backbone) + id: id, + guid: guid, + text: message.plain_text_for_json, + author: author.as_api_response(:backbone), + created_at: created_at, + mentioned_people: mentioned_people.as_api_response(:backbone) } end def as_api_response { - guid: @comment.guid, - body: @comment.message.plain_text_for_json, - author: PersonPresenter.new(@comment.author).as_api_json, - created_at: @comment.created_at + guid: guid, + body: message.plain_text_for_json, + author: PersonPresenter.new(author).as_api_json, + created_at: created_at, + mentioned_people: build_mentioned_people_json } end + + def build_mentioned_people_json + mentioned_people.map {|m| PersonPresenter.new(m).as_api_json } + end end diff --git a/lib/schemas/api_v1.json b/lib/schemas/api_v1.json index 84511bd4a..c4b7dbd9f 100644 --- a/lib/schemas/api_v1.json +++ b/lib/schemas/api_v1.json @@ -4,7 +4,8 @@ "oneOf": [ {"$ref": "https://diaspora.software/api/v1/schema.json#/definitions/aspects"}, {"$ref": "https://diaspora.software/api/v1/schema.json#/definitions/aspect"}, - {"$ref": "https://diaspora.software/api/v1/schema.json#/definitions/comments_or_messages"}, + {"$ref": "https://diaspora.software/api/v1/schema.json#/definitions/comments"}, + {"$ref": "https://diaspora.software/api/v1/schema.json#/definitions/messages"}, {"$ref": "https://diaspora.software/api/v1/schema.json#/definitions/users"}, {"$ref": "https://diaspora.software/api/v1/schema.json#/definitions/conversations"}, {"$ref": "https://diaspora.software/api/v1/schema.json#/definitions/conversation"}, @@ -87,7 +88,26 @@ "additionalProperties": false }, - "comments_or_messages": { + "comments": { + "type": "array", + "items": { + "type": "object", + "properties": { + "guid": { "$ref": "https://diaspora.software/api/v1/schema.json#/definitions/guid" }, + "created_at": { "$ref": "https://diaspora.software/api/v1/schema.json#/definitions/timestamp" }, + "author": { "$ref": "https://diaspora.software/api/v1/schema.json#/definitions/short_profile" }, + "body": { "type": "string" }, + "mentioned_people": { + "type": "array", + "items": { "$ref": "https://diaspora.software/api/v1/schema.json#/definitions/short_profile" } + } + }, + "required": ["guid", "created_at", "author", "body"], + "additionalProperties": false + } + }, + + "messages": { "type": "array", "items": { "type": "object", diff --git a/spec/integration/api/comments_controller_spec.rb b/spec/integration/api/comments_controller_spec.rb index 2a69ecbb2..d3d7a7f1f 100644 --- a/spec/integration/api/comments_controller_spec.rb +++ b/spec/integration/api/comments_controller_spec.rb @@ -72,6 +72,19 @@ describe Api::V1::CommentsController do comment = response_body(response) confirm_comment_format(comment, auth.user, comment_text) end + + it "creates with mentions" do + comment_text = "hello @{#{alice.diaspora_handle}} from Bob!" + post( + api_v1_post_comments_path(post_id: @status.guid), + params: {body: comment_text, access_token: access_token} + ) + expect(response.status).to eq(201) + comment = response_body(response) + confirm_comment_format(comment, auth.user, comment_text) + expect(comment["mentioned_people"].size).to eq(1) + expect(comment["mentioned_people"][0]).to include("diaspora_id" => alice.diaspora_handle) + end end context "wrong post id" do @@ -142,7 +155,7 @@ describe Api::V1::CommentsController do confirm_comment_format(comments[0], auth.user, @comment_text1) confirm_comment_format(comments[1], auth.user, @comment_text2) - expect(comments.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/comments_or_messages") + expect(comments.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/comments") end end diff --git a/spec/integration/api/messages_controller_spec.rb b/spec/integration/api/messages_controller_spec.rb index b345c9e70..bc7528a07 100644 --- a/spec/integration/api/messages_controller_spec.rb +++ b/spec/integration/api/messages_controller_spec.rb @@ -128,7 +128,7 @@ describe Api::V1::MessagesController do messages = response_body_data(response) expect(messages.length).to eq(1) - expect(messages.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/comments_or_messages") + expect(messages.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/messages") confirm_message_format(messages[0], "first message", auth.user) conversation = get_conversation(@conversation_guid)