1ef3477c8874bc01753b26a7bfb32d7195414217
[akkoma] / lib / pleroma / web / pleroma_api / controllers / chat_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4 defmodule Pleroma.Web.PleromaAPI.ChatController do
5 use Pleroma.Web, :controller
6
7 alias Pleroma.Chat
8 alias Pleroma.Object
9 alias Pleroma.Pagination
10 alias Pleroma.Plugs.OAuthScopesPlug
11 alias Pleroma.Repo
12 alias Pleroma.User
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.PleromaAPI.ChatMessageView
15 alias Pleroma.Web.PleromaAPI.ChatView
16
17 import Ecto.Query
18 import Pleroma.Web.ActivityPub.ObjectValidator, only: [stringify_keys: 1]
19
20 # TODO
21 # - Error handling
22
23 plug(
24 OAuthScopesPlug,
25 %{scopes: ["write:statuses"]} when action in [:post_chat_message, :create, :mark_as_read]
26 )
27
28 plug(
29 OAuthScopesPlug,
30 %{scopes: ["read:statuses"]} when action in [:messages, :index]
31 )
32
33 plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError)
34
35 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation
36
37 def post_chat_message(
38 %{body_params: %{content: content} = params, assigns: %{user: %{id: user_id} = user}} =
39 conn,
40 %{
41 id: id
42 }
43 ) do
44 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id),
45 %User{} = recipient <- User.get_cached_by_ap_id(chat.recipient),
46 {:ok, activity} <-
47 CommonAPI.post_chat_message(user, recipient, content, media_id: params[:media_id]),
48 message <- Object.normalize(activity) do
49 conn
50 |> put_view(ChatMessageView)
51 |> render("show.json", for: user, object: message, chat: chat)
52 end
53 end
54
55 def mark_as_read(%{assigns: %{user: %{id: user_id}}} = conn, %{id: id}) do
56 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id),
57 {:ok, chat} <- Chat.mark_as_read(chat) do
58 conn
59 |> put_view(ChatView)
60 |> render("show.json", chat: chat)
61 end
62 end
63
64 def messages(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: id} = params) do
65 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id) do
66 messages =
67 chat
68 |> Chat.messages_for_chat_query()
69 |> Pagination.fetch_paginated(params |> stringify_keys())
70
71 conn
72 |> put_view(ChatMessageView)
73 |> render("index.json", for: user, objects: messages, chat: chat)
74 else
75 _ ->
76 conn
77 |> put_status(:not_found)
78 |> json(%{error: "not found"})
79 end
80 end
81
82 def index(%{assigns: %{user: %{id: user_id}}} = conn, params) do
83 chats =
84 from(c in Chat,
85 where: c.user_id == ^user_id,
86 order_by: [desc: c.updated_at]
87 )
88 |> Pagination.fetch_paginated(params |> stringify_keys)
89
90 conn
91 |> put_view(ChatView)
92 |> render("index.json", chats: chats)
93 end
94
95 def create(%{assigns: %{user: user}} = conn, params) do
96 with %User{ap_id: recipient} <- User.get_by_id(params[:id]),
97 {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
98 conn
99 |> put_view(ChatView)
100 |> render("show.json", chat: chat)
101 end
102 end
103 end