Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[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 Pleroma.Web.ActivityPub.ObjectValidator, only: [stringify_keys: 1]
18
19 import Ecto.Query
20
21 # TODO
22 # - Error handling
23
24 plug(
25 OAuthScopesPlug,
26 %{scopes: ["write:statuses"]} when action in [:post_chat_message, :create, :mark_as_read]
27 )
28
29 plug(
30 OAuthScopesPlug,
31 %{scopes: ["read:statuses"]} when action in [:messages, :index]
32 )
33
34 plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError)
35
36 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation
37
38 def post_chat_message(
39 %{body_params: %{content: content}, assigns: %{user: %{id: user_id} = user}} = 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} <- CommonAPI.post_chat_message(user, recipient, content),
47 message <- Object.normalize(activity) do
48 conn
49 |> put_view(ChatMessageView)
50 |> render("show.json", for: user, object: message, chat: chat)
51 end
52 end
53
54 def mark_as_read(%{assigns: %{user: %{id: user_id}}} = conn, %{id: id}) do
55 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id),
56 {:ok, chat} <- Chat.mark_as_read(chat) do
57 conn
58 |> put_view(ChatView)
59 |> render("show.json", chat: chat)
60 end
61 end
62
63 def messages(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: id} = params) do
64 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id) do
65 messages =
66 from(o in Object,
67 where: fragment("?->>'type' = ?", o.data, "ChatMessage"),
68 where:
69 fragment(
70 """
71 (?->>'actor' = ? and ?->'to' = ?)
72 OR (?->>'actor' = ? and ?->'to' = ?)
73 """,
74 o.data,
75 ^user.ap_id,
76 o.data,
77 ^[chat.recipient],
78 o.data,
79 ^chat.recipient,
80 o.data,
81 ^[user.ap_id]
82 )
83 )
84 |> Pagination.fetch_paginated(params |> stringify_keys())
85
86 conn
87 |> put_view(ChatMessageView)
88 |> render("index.json", for: user, objects: messages, chat: chat)
89 else
90 _ ->
91 conn
92 |> put_status(:not_found)
93 |> json(%{error: "not found"})
94 end
95 end
96
97 def index(%{assigns: %{user: %{id: user_id}}} = conn, params) do
98 chats =
99 from(c in Chat,
100 where: c.user_id == ^user_id,
101 order_by: [desc: c.updated_at]
102 )
103 |> Pagination.fetch_paginated(params |> stringify_keys)
104
105 conn
106 |> put_view(ChatView)
107 |> render("index.json", chats: chats)
108 end
109
110 def create(%{assigns: %{user: user}} = conn, params) do
111 with %User{ap_id: recipient} <- User.get_by_id(params[:id]),
112 {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
113 conn
114 |> put_view(ChatView)
115 |> render("show.json", chat: chat)
116 end
117 end
118 end