aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2020-04-21 18:23:00 +0200
committerlain <lain@soykaf.club>2020-04-21 18:23:00 +0200
commit66c2eb670b273d808f0a9c1ae087df064718ca3d (patch)
tree94750ae05b09e609a26a8f0904395ba9f6de0643 /lib
parent97ad0c45977261df3068ca4f0c3febce3173c058 (diff)
downloadpleroma-66c2eb670b273d808f0a9c1ae087df064718ca3d.tar.gz
ChatController: Validate parameters.
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/web/api_spec/operations/chat_operation.ex4
-rw-r--r--lib/pleroma/web/pleroma_api/controllers/chat_controller.ex22
2 files changed, 18 insertions, 8 deletions
diff --git a/lib/pleroma/web/api_spec/operations/chat_operation.ex b/lib/pleroma/web/api_spec/operations/chat_operation.ex
index 5bd41ec4f..dc99bd773 100644
--- a/lib/pleroma/web/api_spec/operations/chat_operation.ex
+++ b/lib/pleroma/web/api_spec/operations/chat_operation.ex
@@ -21,6 +21,7 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
%Operation{
tags: ["chat"],
summary: "Create a chat",
+ operationId: "ChatController.create",
parameters: [
Operation.parameter(
:ap_id,
@@ -47,6 +48,7 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
%Operation{
tags: ["chat"],
summary: "Get a list of chats that you participated in",
+ operationId: "ChatController.index",
parameters: [
Operation.parameter(:limit, :query, :integer, "How many results to return", example: 20),
Operation.parameter(:min_id, :query, :string, "Return only chats after this id"),
@@ -67,6 +69,7 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
%Operation{
tags: ["chat"],
summary: "Get the most recent messages of the chat",
+ operationId: "ChatController.messages",
parameters: [
Operation.parameter(:id, :path, :string, "The ID of the Chat"),
Operation.parameter(:limit, :query, :integer, "How many results to return", example: 20),
@@ -89,6 +92,7 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
%Operation{
tags: ["chat"],
summary: "Post a message to the chat",
+ operationId: "ChatController.post_chat_message",
parameters: [
Operation.parameter(:id, :path, :string, "The ID of the Chat")
],
diff --git a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
index 9d8b9b3cf..771ad6217 100644
--- a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
+++ b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
@@ -14,6 +14,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
alias Pleroma.Web.PleromaAPI.ChatMessageView
alias Pleroma.Web.PleromaAPI.ChatView
+ import Pleroma.Web.ActivityPub.ObjectValidator, only: [stringify_keys: 1]
+
import Ecto.Query
# TODO
@@ -29,12 +31,16 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
%{scopes: ["read:statuses"]} when action in [:messages, :index]
)
+ plug(OpenApiSpex.Plug.CastAndValidate)
+
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation
- def post_chat_message(%{assigns: %{user: %{id: user_id} = user}} = conn, %{
- "id" => id,
- "content" => content
- }) do
+ def post_chat_message(
+ %{body_params: %{content: content}, assigns: %{user: %{id: user_id} = user}} = conn,
+ %{
+ id: id
+ }
+ ) do
with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id),
%User{} = recipient <- User.get_cached_by_ap_id(chat.recipient),
{:ok, activity} <- CommonAPI.post_chat_message(user, recipient, content),
@@ -45,7 +51,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
end
end
- def messages(%{assigns: %{user: %{id: user_id} = user}} = conn, %{"id" => id} = params) do
+ def messages(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: id} = params) do
with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id) do
messages =
from(o in Object,
@@ -66,7 +72,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
^[user.ap_id]
)
)
- |> Pagination.fetch_paginated(params)
+ |> Pagination.fetch_paginated(params |> stringify_keys())
conn
|> put_view(ChatMessageView)
@@ -85,7 +91,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
where: c.user_id == ^user_id,
order_by: [desc: c.updated_at]
)
- |> Pagination.fetch_paginated(params)
+ |> Pagination.fetch_paginated(params |> stringify_keys)
conn
|> put_view(ChatView)
@@ -93,7 +99,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
end
def create(%{assigns: %{user: user}} = conn, params) do
- recipient = params["ap_id"] |> URI.decode_www_form()
+ recipient = params[:ap_id]
with {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
conn