ChatController: Basic message posting.
This commit is contained in:
parent
e8fd0dd689
commit
2cc6841424
|
@ -7,9 +7,35 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
|
|||
alias Pleroma.Chat
|
||||
alias Pleroma.Object
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.CommonAPI
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
# TODO
|
||||
# - Oauth stuff
|
||||
# - Views / Representers
|
||||
# - Error handling
|
||||
|
||||
def post_chat_message(%{assigns: %{user: %{id: user_id} = user}} = conn, %{
|
||||
"id" => id,
|
||||
"content" => content
|
||||
}) 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),
|
||||
message <- Object.normalize(activity) do
|
||||
represented_message = %{
|
||||
actor: message.data["actor"],
|
||||
id: message.id,
|
||||
content: message.data["content"]
|
||||
}
|
||||
|
||||
conn
|
||||
|> json(represented_message)
|
||||
end
|
||||
end
|
||||
|
||||
def messages(%{assigns: %{user: %{id: user_id} = user}} = conn, %{"id" => id}) do
|
||||
with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id) do
|
||||
messages =
|
||||
|
|
|
@ -290,6 +290,7 @@ defmodule Pleroma.Web.Router do
|
|||
post("/chats/by-ap-id/:ap_id", ChatController, :create)
|
||||
get("/chats", ChatController, :index)
|
||||
get("/chats/:id/messages", ChatController, :messages)
|
||||
post("/chats/:id/messages", ChatController, :post_chat_message)
|
||||
end
|
||||
|
||||
scope [] do
|
||||
|
|
|
@ -9,6 +9,23 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
|
|||
|
||||
import Pleroma.Factory
|
||||
|
||||
describe "POST /api/v1/pleroma/chats/:id/messages" do
|
||||
test "it posts a message to the chat", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
|
||||
{:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
|
||||
|
||||
result =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
|
||||
|> json_response(200)
|
||||
|
||||
assert result["content"] == "Hallo!!"
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /api/v1/pleroma/chats/:id/messages" do
|
||||
# TODO
|
||||
# - Test that statuses don't show
|
||||
|
|
Loading…
Reference in a new issue