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]
27 )
28
29 plug(
30 OAuthScopesPlug,
31 %{scopes: ["read:statuses"]} when action in [:messages, :index]
32 )
33
34 plug(OpenApiSpex.Plug.CastAndValidate)
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 messages(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: id} = params) do
55 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id) do
56 messages =
57 from(o in Object,
58 where: fragment("?->>'type' = ?", o.data, "ChatMessage"),
59 where:
60 fragment(
61 """
62 (?->>'actor' = ? and ?->'to' = ?)
63 OR (?->>'actor' = ? and ?->'to' = ?)
64 """,
65 o.data,
66 ^user.ap_id,
67 o.data,
68 ^[chat.recipient],
69 o.data,
70 ^chat.recipient,
71 o.data,
72 ^[user.ap_id]
73 )
74 )
75 |> Pagination.fetch_paginated(params |> stringify_keys())
76
77 conn
78 |> put_view(ChatMessageView)
79 |> render("index.json", for: user, objects: messages, chat: chat)
80 else
81 _ ->
82 conn
83 |> put_status(:not_found)
84 |> json(%{error: "not found"})
85 end
86 end
87
88 def index(%{assigns: %{user: %{id: user_id}}} = conn, params) do
89 chats =
90 from(c in Chat,
91 where: c.user_id == ^user_id,
92 order_by: [desc: c.updated_at]
93 )
94 |> Pagination.fetch_paginated(params |> stringify_keys)
95
96 conn
97 |> put_view(ChatView)
98 |> render("index.json", chats: chats)
99 end
100
101 def create(%{assigns: %{user: user}} = conn, params) do
102 with %User{ap_id: recipient} <- User.get_by_id(params[:id]),
103 {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
104 conn
105 |> put_view(ChatView)
106 |> render("show.json", chat: chat)
107 end
108 end
109 end