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} = params, assigns: %{user: %{id: user_id} = user}} =
40 conn,
41 %{
42 id: id
43 }
44 ) do
45 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id),
46 %User{} = recipient <- User.get_cached_by_ap_id(chat.recipient),
47 {:ok, activity} <-
48 CommonAPI.post_chat_message(user, recipient, content, media_id: params[:media_id]),
49 message <- Object.normalize(activity) do
50 conn
51 |> put_view(ChatMessageView)
52 |> render("show.json", for: user, object: message, chat: chat)
53 end
54 end
55
56 def mark_as_read(%{assigns: %{user: %{id: user_id}}} = conn, %{id: id}) do
57 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id),
58 {:ok, chat} <- Chat.mark_as_read(chat) do
59 conn
60 |> put_view(ChatView)
61 |> render("show.json", chat: chat)
62 end
63 end
64
65 def messages(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: id} = params) do
66 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id) do
67 messages =
68 from(o in Object,
69 where: fragment("?->>'type' = ?", o.data, "ChatMessage"),
70 where:
71 fragment(
72 """
73 (?->>'actor' = ? and ?->'to' = ?)
74 OR (?->>'actor' = ? and ?->'to' = ?)
75 """,
76 o.data,
77 ^user.ap_id,
78 o.data,
79 ^[chat.recipient],
80 o.data,
81 ^chat.recipient,
82 o.data,
83 ^[user.ap_id]
84 )
85 )
86 |> Pagination.fetch_paginated(params |> stringify_keys())
87
88 conn
89 |> put_view(ChatMessageView)
90 |> render("index.json", for: user, objects: messages, chat: chat)
91 else
92 _ ->
93 conn
94 |> put_status(:not_found)
95 |> json(%{error: "not found"})
96 end
97 end
98
99 def index(%{assigns: %{user: %{id: user_id}}} = conn, params) do
100 chats =
101 from(c in Chat,
102 where: c.user_id == ^user_id,
103 order_by: [desc: c.updated_at]
104 )
105 |> Pagination.fetch_paginated(params |> stringify_keys)
106
107 conn
108 |> put_view(ChatView)
109 |> render("index.json", chats: chats)
110 end
111
112 def create(%{assigns: %{user: user}} = conn, params) do
113 with %User{ap_id: recipient} <- User.get_by_id(params[:id]),
114 {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
115 conn
116 |> put_view(ChatView)
117 |> render("show.json", chat: chat)
118 end
119 end
120 end