1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
7 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
11 alias Pleroma.Chat.MessageReference
13 alias Pleroma.Pagination
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
18 alias Pleroma.Web.Plugs.OAuthScopesPlug
22 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
26 %{scopes: ["write:chats"]}
31 :mark_message_as_read,
38 %{scopes: ["read:chats"]} when action in [:messages, :index, :index2, :show]
41 plug(Pleroma.Web.ApiSpec.CastAndValidate)
43 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation
45 def delete_message(%{assigns: %{user: %{id: user_id} = user}} = conn, %{
46 message_id: message_id,
49 with %MessageReference{} = cm_ref <-
50 MessageReference.get_by_id(message_id),
51 ^chat_id <- to_string(cm_ref.chat_id),
52 %Chat{user_id: ^user_id} <- Chat.get_by_id(chat_id),
53 {:ok, _} <- remove_or_delete(cm_ref, user) do
55 |> put_view(MessageReferenceView)
56 |> render("show.json", chat_message_reference: cm_ref)
59 {:error, :could_not_delete}
63 defp remove_or_delete(
64 %{object: %{data: %{"actor" => actor, "id" => id}}},
65 %{ap_id: actor} = user
67 with %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
68 CommonAPI.delete(activity.id, user)
72 defp remove_or_delete(cm_ref, _), do: MessageReference.delete(cm_ref)
74 def post_chat_message(
75 %{body_params: params, assigns: %{user: user}} = conn,
78 with {:ok, chat} <- Chat.get_by_user_and_id(user, id),
79 %User{} = recipient <- User.get_cached_by_ap_id(chat.recipient),
81 CommonAPI.post_chat_message(user, recipient, params[:content],
82 media_id: params[:media_id],
83 idempotency_key: idempotency_key(conn)
85 message <- Object.normalize(activity, fetch: false),
86 cm_ref <- MessageReference.for_chat_and_object(chat, message) do
88 |> put_view(MessageReferenceView)
89 |> render("show.json", chat_message_reference: cm_ref)
93 |> put_status(:unprocessable_entity)
94 |> json(%{error: message})
98 |> put_status(:bad_request)
99 |> json(%{error: message})
103 def mark_message_as_read(
104 %{assigns: %{user: %{id: user_id}}} = conn,
105 %{id: chat_id, message_id: message_id}
107 with %MessageReference{} = cm_ref <- MessageReference.get_by_id(message_id),
108 ^chat_id <- to_string(cm_ref.chat_id),
109 %Chat{user_id: ^user_id} <- Chat.get_by_id(chat_id),
110 {:ok, cm_ref} <- MessageReference.mark_as_read(cm_ref) do
112 |> put_view(MessageReferenceView)
113 |> render("show.json", chat_message_reference: cm_ref)
118 %{body_params: %{last_read_id: last_read_id}, assigns: %{user: user}} = conn,
121 with {:ok, chat} <- Chat.get_by_user_and_id(user, id),
122 {_n, _} <- MessageReference.set_all_seen_for_chat(chat, last_read_id) do
123 render(conn, "show.json", chat: chat)
127 def messages(%{assigns: %{user: user}} = conn, %{id: id} = params) do
128 with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do
131 |> MessageReference.for_chat_query()
132 |> Pagination.fetch_paginated(params)
135 |> add_link_headers(chat_message_refs)
136 |> put_view(MessageReferenceView)
137 |> render("index.json", chat_message_references: chat_message_refs)
141 def index(%{assigns: %{user: user}} = conn, params) do
143 index_query(user, params)
146 render(conn, "index.json", chats: chats)
149 def index2(%{assigns: %{user: user}} = conn, params) do
151 index_query(user, params)
152 |> Pagination.fetch_paginated(params)
155 |> add_link_headers(chats)
156 |> render("index.json", chats: chats)
159 defp index_query(%{id: user_id} = user, params) do
161 User.cached_blocked_users_ap_ids(user) ++
162 if params[:with_muted], do: [], else: User.cached_muted_users_ap_ids(user)
165 |> Chat.for_user_query()
166 |> where([c], c.recipient not in ^exclude_users)
169 def create(%{assigns: %{user: user}} = conn, %{id: id}) do
170 with %User{ap_id: recipient} <- User.get_cached_by_id(id),
171 {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
172 render(conn, "show.json", chat: chat)
176 def show(%{assigns: %{user: user}} = conn, %{id: id}) do
177 with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do
178 render(conn, "show.json", chat: chat)
182 defp idempotency_key(conn) do
183 case get_req_header(conn, "idempotency-key") do