Move single used schemas to Marker operation schema
This commit is contained in:
parent
8096565653
commit
babcae7130
|
@ -6,8 +6,6 @@ defmodule Pleroma.Web.ApiSpec.MarkerOperation do
|
||||||
alias OpenApiSpex.Operation
|
alias OpenApiSpex.Operation
|
||||||
alias OpenApiSpex.Schema
|
alias OpenApiSpex.Schema
|
||||||
alias Pleroma.Web.ApiSpec.Helpers
|
alias Pleroma.Web.ApiSpec.Helpers
|
||||||
alias Pleroma.Web.ApiSpec.Schemas.MarkersResponse
|
|
||||||
alias Pleroma.Web.ApiSpec.Schemas.MarkersUpsertRequest
|
|
||||||
|
|
||||||
def open_api_operation(action) do
|
def open_api_operation(action) do
|
||||||
operation = String.to_existing_atom("#{action}_operation")
|
operation = String.to_existing_atom("#{action}_operation")
|
||||||
|
@ -16,7 +14,7 @@ def open_api_operation(action) do
|
||||||
|
|
||||||
def index_operation do
|
def index_operation do
|
||||||
%Operation{
|
%Operation{
|
||||||
tags: ["markers"],
|
tags: ["Markers"],
|
||||||
summary: "Get saved timeline position",
|
summary: "Get saved timeline position",
|
||||||
security: [%{"oAuth" => ["read:statuses"]}],
|
security: [%{"oAuth" => ["read:statuses"]}],
|
||||||
operationId: "MarkerController.index",
|
operationId: "MarkerController.index",
|
||||||
|
@ -32,21 +30,111 @@ def index_operation do
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
responses: %{
|
responses: %{
|
||||||
200 => Operation.response("Marker", "application/json", MarkersResponse)
|
200 => Operation.response("Marker", "application/json", response()),
|
||||||
|
403 => Operation.response("Error", "application/json", api_error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def upsert_operation do
|
def upsert_operation do
|
||||||
%Operation{
|
%Operation{
|
||||||
tags: ["markers"],
|
tags: ["Markers"],
|
||||||
summary: "Save position in timeline",
|
summary: "Save position in timeline",
|
||||||
operationId: "MarkerController.upsert",
|
operationId: "MarkerController.upsert",
|
||||||
requestBody: Helpers.request_body("Parameters", MarkersUpsertRequest, required: true),
|
requestBody: Helpers.request_body("Parameters", upsert_request(), required: true),
|
||||||
security: [%{"oAuth" => ["follow", "write:blocks"]}],
|
security: [%{"oAuth" => ["follow", "write:blocks"]}],
|
||||||
responses: %{
|
responses: %{
|
||||||
200 => Operation.response("Marker", "application/json", MarkersResponse)
|
200 => Operation.response("Marker", "application/json", response()),
|
||||||
|
403 => Operation.response("Error", "application/json", api_error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp marker do
|
||||||
|
%Schema{
|
||||||
|
title: "Marker",
|
||||||
|
description: "Schema for a marker",
|
||||||
|
type: :object,
|
||||||
|
properties: %{
|
||||||
|
last_read_id: %Schema{type: :string},
|
||||||
|
version: %Schema{type: :integer},
|
||||||
|
updated_at: %Schema{type: :string},
|
||||||
|
pleroma: %Schema{
|
||||||
|
type: :object,
|
||||||
|
properties: %{
|
||||||
|
unread_count: %Schema{type: :integer}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
example: %{
|
||||||
|
"last_read_id" => "35098814",
|
||||||
|
"version" => 361,
|
||||||
|
"updated_at" => "2019-11-26T22:37:25.239Z",
|
||||||
|
"pleroma" => %{"unread_count" => 5}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp response do
|
||||||
|
%Schema{
|
||||||
|
title: "MarkersResponse",
|
||||||
|
description: "Response schema for markers",
|
||||||
|
type: :object,
|
||||||
|
properties: %{
|
||||||
|
notifications: %Schema{allOf: [marker()], nullable: true},
|
||||||
|
home: %Schema{allOf: [marker()], nullable: true}
|
||||||
|
},
|
||||||
|
items: %Schema{type: :string},
|
||||||
|
example: %{
|
||||||
|
"notifications" => %{
|
||||||
|
"last_read_id" => "35098814",
|
||||||
|
"version" => 361,
|
||||||
|
"updated_at" => "2019-11-26T22:37:25.239Z",
|
||||||
|
"pleroma" => %{"unread_count" => 0}
|
||||||
|
},
|
||||||
|
"home" => %{
|
||||||
|
"last_read_id" => "103206604258487607",
|
||||||
|
"version" => 468,
|
||||||
|
"updated_at" => "2019-11-26T22:37:25.235Z",
|
||||||
|
"pleroma" => %{"unread_count" => 10}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp upsert_request do
|
||||||
|
%Schema{
|
||||||
|
title: "MarkersUpsertRequest",
|
||||||
|
description: "Request schema for marker upsert",
|
||||||
|
type: :object,
|
||||||
|
properties: %{
|
||||||
|
notifications: %Schema{
|
||||||
|
type: :object,
|
||||||
|
properties: %{
|
||||||
|
last_read_id: %Schema{type: :string}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
home: %Schema{
|
||||||
|
type: :object,
|
||||||
|
properties: %{
|
||||||
|
last_read_id: %Schema{type: :string}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
example: %{
|
||||||
|
"home" => %{
|
||||||
|
"last_read_id" => "103194548672408537",
|
||||||
|
"version" => 462,
|
||||||
|
"updated_at" => "2019-11-24T19:39:39.337Z"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp api_error do
|
||||||
|
%Schema{
|
||||||
|
type: :object,
|
||||||
|
properties: %{error: %Schema{type: :string}}
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
# Pleroma: A lightweight social networking server
|
|
||||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
|
|
||||||
defmodule Pleroma.Web.ApiSpec.Schemas.Marker do
|
|
||||||
require OpenApiSpex
|
|
||||||
alias OpenApiSpex.Schema
|
|
||||||
|
|
||||||
OpenApiSpex.schema(%{
|
|
||||||
title: "Marker",
|
|
||||||
description: "Schema for a marker",
|
|
||||||
type: :object,
|
|
||||||
properties: %{
|
|
||||||
last_read_id: %Schema{type: :string},
|
|
||||||
version: %Schema{type: :integer},
|
|
||||||
updated_at: %Schema{type: :string},
|
|
||||||
pleroma: %Schema{
|
|
||||||
type: :object,
|
|
||||||
properties: %{
|
|
||||||
unread_count: %Schema{type: :integer}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
example: %{
|
|
||||||
"last_read_id" => "35098814",
|
|
||||||
"version" => 361,
|
|
||||||
"updated_at" => "2019-11-26T22:37:25.239Z",
|
|
||||||
"pleroma" => %{"unread_count" => 5}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
end
|
|
|
@ -1,35 +0,0 @@
|
||||||
# Pleroma: A lightweight social networking server
|
|
||||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
|
|
||||||
defmodule Pleroma.Web.ApiSpec.Schemas.MarkersResponse do
|
|
||||||
require OpenApiSpex
|
|
||||||
alias OpenApiSpex.Schema
|
|
||||||
|
|
||||||
alias Pleroma.Web.ApiSpec.Schemas.Marker
|
|
||||||
|
|
||||||
OpenApiSpex.schema(%{
|
|
||||||
title: "MarkersResponse",
|
|
||||||
description: "Response schema for markers",
|
|
||||||
type: :object,
|
|
||||||
properties: %{
|
|
||||||
notifications: %Schema{allOf: [Marker], nullable: true},
|
|
||||||
home: %Schema{allOf: [Marker], nullable: true}
|
|
||||||
},
|
|
||||||
items: %Schema{type: :string},
|
|
||||||
example: %{
|
|
||||||
"notifications" => %{
|
|
||||||
"last_read_id" => "35098814",
|
|
||||||
"version" => 361,
|
|
||||||
"updated_at" => "2019-11-26T22:37:25.239Z",
|
|
||||||
"pleroma" => %{"unread_count" => 0}
|
|
||||||
},
|
|
||||||
"home" => %{
|
|
||||||
"last_read_id" => "103206604258487607",
|
|
||||||
"version" => 468,
|
|
||||||
"updated_at" => "2019-11-26T22:37:25.235Z",
|
|
||||||
"pleroma" => %{"unread_count" => 10}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
end
|
|
|
@ -1,35 +0,0 @@
|
||||||
# Pleroma: A lightweight social networking server
|
|
||||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
|
|
||||||
defmodule Pleroma.Web.ApiSpec.Schemas.MarkersUpsertRequest do
|
|
||||||
require OpenApiSpex
|
|
||||||
alias OpenApiSpex.Schema
|
|
||||||
|
|
||||||
OpenApiSpex.schema(%{
|
|
||||||
title: "MarkersUpsertRequest",
|
|
||||||
description: "Request schema for marker upsert",
|
|
||||||
type: :object,
|
|
||||||
properties: %{
|
|
||||||
notifications: %Schema{
|
|
||||||
type: :object,
|
|
||||||
properties: %{
|
|
||||||
last_read_id: %Schema{type: :string}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
home: %Schema{
|
|
||||||
type: :object,
|
|
||||||
properties: %{
|
|
||||||
last_read_id: %Schema{type: :string}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
example: %{
|
|
||||||
"home" => %{
|
|
||||||
"last_read_id" => "103194548672408537",
|
|
||||||
"version" => 462,
|
|
||||||
"updated_at" => "2019-11-24T19:39:39.337Z"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
end
|
|
|
@ -6,6 +6,8 @@ defmodule Pleroma.Web.MastodonAPI.MarkerController do
|
||||||
use Pleroma.Web, :controller
|
use Pleroma.Web, :controller
|
||||||
alias Pleroma.Plugs.OAuthScopesPlug
|
alias Pleroma.Plugs.OAuthScopesPlug
|
||||||
|
|
||||||
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
||||||
|
|
||||||
plug(
|
plug(
|
||||||
OAuthScopesPlug,
|
OAuthScopesPlug,
|
||||||
%{scopes: ["read:statuses"]}
|
%{scopes: ["read:statuses"]}
|
||||||
|
@ -15,7 +17,6 @@ defmodule Pleroma.Web.MastodonAPI.MarkerController do
|
||||||
plug(OAuthScopesPlug, %{scopes: ["write:statuses"]} when action == :upsert)
|
plug(OAuthScopesPlug, %{scopes: ["write:statuses"]} when action == :upsert)
|
||||||
|
|
||||||
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
||||||
plug(OpenApiSpex.Plug.CastAndValidate)
|
|
||||||
|
|
||||||
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.MarkerOperation
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.MarkerOperation
|
||||||
|
|
||||||
|
@ -27,10 +28,7 @@ def index(%{assigns: %{user: user}} = conn, params) do
|
||||||
|
|
||||||
# POST /api/v1/markers
|
# POST /api/v1/markers
|
||||||
def upsert(%{assigns: %{user: user}, body_params: params} = conn, _) do
|
def upsert(%{assigns: %{user: user}, body_params: params} = conn, _) do
|
||||||
params =
|
params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
|
||||||
params
|
|
||||||
|> Map.from_struct()
|
|
||||||
|> Map.new(fn {key, value} -> {to_string(key), value} end)
|
|
||||||
|
|
||||||
with {:ok, result} <- Pleroma.Marker.upsert(user, params),
|
with {:ok, result} <- Pleroma.Marker.upsert(user, params),
|
||||||
markers <- Map.values(result) do
|
markers <- Map.values(result) do
|
||||||
|
|
|
@ -6,12 +6,13 @@ defmodule Pleroma.Web.MastodonAPI.MarkerView do
|
||||||
use Pleroma.Web, :view
|
use Pleroma.Web, :view
|
||||||
|
|
||||||
def render("markers.json", %{markers: markers}) do
|
def render("markers.json", %{markers: markers}) do
|
||||||
Enum.reduce(markers, %{}, fn m, acc ->
|
Map.new(markers, fn m ->
|
||||||
Map.put_new(acc, m.timeline, %{
|
{m.timeline,
|
||||||
|
%{
|
||||||
last_read_id: m.last_read_id,
|
last_read_id: m.last_read_id,
|
||||||
version: m.lock_version,
|
version: m.lock_version,
|
||||||
updated_at: NaiveDateTime.to_iso8601(m.updated_at)
|
updated_at: NaiveDateTime.to_iso8601(m.updated_at)
|
||||||
})
|
}}
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,10 +4,8 @@
|
||||||
|
|
||||||
defmodule Pleroma.Web.MastodonAPI.MarkerControllerTest do
|
defmodule Pleroma.Web.MastodonAPI.MarkerControllerTest do
|
||||||
use Pleroma.Web.ConnCase
|
use Pleroma.Web.ConnCase
|
||||||
alias Pleroma.Web.ApiSpec
|
|
||||||
|
|
||||||
import Pleroma.Factory
|
import Pleroma.Factory
|
||||||
import OpenApiSpex.TestAssertions
|
|
||||||
|
|
||||||
describe "GET /api/v1/markers" do
|
describe "GET /api/v1/markers" do
|
||||||
test "gets markers with correct scopes", %{conn: conn} do
|
test "gets markers with correct scopes", %{conn: conn} do
|
||||||
|
@ -25,7 +23,7 @@ test "gets markers with correct scopes", %{conn: conn} do
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> assign(:token, token)
|
|> assign(:token, token)
|
||||||
|> get("/api/v1/markers?timeline[]=notifications")
|
|> get("/api/v1/markers?timeline[]=notifications")
|
||||||
|> json_response(200)
|
|> json_response_and_validate_schema(200)
|
||||||
|
|
||||||
assert response == %{
|
assert response == %{
|
||||||
"notifications" => %{
|
"notifications" => %{
|
||||||
|
@ -34,8 +32,6 @@ test "gets markers with correct scopes", %{conn: conn} do
|
||||||
"version" => 0
|
"version" => 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_schema(response, "MarkersResponse", ApiSpec.spec())
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "gets markers with missed scopes", %{conn: conn} do
|
test "gets markers with missed scopes", %{conn: conn} do
|
||||||
|
@ -49,7 +45,7 @@ test "gets markers with missed scopes", %{conn: conn} do
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> assign(:token, token)
|
|> assign(:token, token)
|
||||||
|> get("/api/v1/markers", %{timeline: ["notifications"]})
|
|> get("/api/v1/markers", %{timeline: ["notifications"]})
|
||||||
|> json_response(403)
|
|> json_response_and_validate_schema(403)
|
||||||
|
|
||||||
assert response == %{"error" => "Insufficient permissions: read:statuses."}
|
assert response == %{"error" => "Insufficient permissions: read:statuses."}
|
||||||
end
|
end
|
||||||
|
@ -69,7 +65,7 @@ test "creates a marker with correct scopes", %{conn: conn} do
|
||||||
home: %{last_read_id: "777"},
|
home: %{last_read_id: "777"},
|
||||||
notifications: %{"last_read_id" => "69420"}
|
notifications: %{"last_read_id" => "69420"}
|
||||||
})
|
})
|
||||||
|> json_response(200)
|
|> json_response_and_validate_schema(200)
|
||||||
|
|
||||||
assert %{
|
assert %{
|
||||||
"notifications" => %{
|
"notifications" => %{
|
||||||
|
@ -78,8 +74,6 @@ test "creates a marker with correct scopes", %{conn: conn} do
|
||||||
"version" => 0
|
"version" => 0
|
||||||
}
|
}
|
||||||
} = response
|
} = response
|
||||||
|
|
||||||
assert_schema(response, "MarkersResponse", ApiSpec.spec())
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "updates exist marker", %{conn: conn} do
|
test "updates exist marker", %{conn: conn} do
|
||||||
|
@ -101,7 +95,7 @@ test "updates exist marker", %{conn: conn} do
|
||||||
home: %{last_read_id: "777"},
|
home: %{last_read_id: "777"},
|
||||||
notifications: %{"last_read_id" => "69888"}
|
notifications: %{"last_read_id" => "69888"}
|
||||||
})
|
})
|
||||||
|> json_response(200)
|
|> json_response_and_validate_schema(200)
|
||||||
|
|
||||||
assert response == %{
|
assert response == %{
|
||||||
"notifications" => %{
|
"notifications" => %{
|
||||||
|
@ -110,8 +104,6 @@ test "updates exist marker", %{conn: conn} do
|
||||||
"version" => 0
|
"version" => 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_schema(response, "MarkersResponse", ApiSpec.spec())
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "creates a marker with missed scopes", %{conn: conn} do
|
test "creates a marker with missed scopes", %{conn: conn} do
|
||||||
|
@ -122,11 +114,12 @@ test "creates a marker with missed scopes", %{conn: conn} do
|
||||||
conn
|
conn
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> assign(:token, token)
|
|> assign(:token, token)
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|> post("/api/v1/markers", %{
|
|> post("/api/v1/markers", %{
|
||||||
home: %{last_read_id: "777"},
|
home: %{last_read_id: "777"},
|
||||||
notifications: %{"last_read_id" => "69420"}
|
notifications: %{"last_read_id" => "69420"}
|
||||||
})
|
})
|
||||||
|> json_response(403)
|
|> json_response_and_validate_schema(403)
|
||||||
|
|
||||||
assert response == %{"error" => "Insufficient permissions: write:statuses."}
|
assert response == %{"error" => "Insufficient permissions: write:statuses."}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue