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, replace_params: false)
43 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation
47 assigns: %{user: %{id: user_id} = user},
51 message_id: message_id,
59 with %MessageReference{} = cm_ref <-
60 MessageReference.get_by_id(message_id),
61 ^chat_id <- to_string(cm_ref.chat_id),
62 %Chat{user_id: ^user_id} <- Chat.get_by_id(chat_id),
63 {:ok, _} <- remove_or_delete(cm_ref, user) do
65 |> put_view(MessageReferenceView)
66 |> render("show.json", chat_message_reference: cm_ref)
69 {:error, :could_not_delete}
73 defp remove_or_delete(
74 %{object: %{data: %{"actor" => actor, "id" => id}}},
75 %{ap_id: actor} = user
77 with %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
78 CommonAPI.delete(activity.id, user)
82 defp remove_or_delete(cm_ref, _), do: MessageReference.delete(cm_ref)
84 def post_chat_message(
86 private: %{open_api_spex: %{body_params: params, params: %{id: id}}},
87 assigns: %{user: user}
91 with {:ok, chat} <- Chat.get_by_user_and_id(user, id),
92 {_, %User{} = recipient} <- {:user, User.get_cached_by_ap_id(chat.recipient)},
94 CommonAPI.post_chat_message(user, recipient, params[:content],
95 media_id: params[:media_id],
96 idempotency_key: idempotency_key(conn)
98 message <- Object.normalize(activity, fetch: false),
99 cm_ref <- MessageReference.for_chat_and_object(chat, message) do
101 |> put_view(MessageReferenceView)
102 |> render("show.json", chat_message_reference: cm_ref)
104 {:reject, message} ->
106 |> put_status(:unprocessable_entity)
107 |> json(%{error: message})
111 |> put_status(:bad_request)
112 |> json(%{error: message})
116 |> put_status(:bad_request)
117 |> json(%{error: "Recipient does not exist"})
121 def mark_message_as_read(
123 assigns: %{user: %{id: user_id}},
124 private: %{open_api_spex: %{params: %{id: chat_id, message_id: message_id}}}
128 with %MessageReference{} = cm_ref <- MessageReference.get_by_id(message_id),
129 ^chat_id <- to_string(cm_ref.chat_id),
130 %Chat{user_id: ^user_id} <- Chat.get_by_id(chat_id),
131 {:ok, cm_ref} <- MessageReference.mark_as_read(cm_ref) do
133 |> put_view(MessageReferenceView)
134 |> render("show.json", chat_message_reference: cm_ref)
140 assigns: %{user: user},
143 body_params: %{last_read_id: last_read_id},
150 with {:ok, chat} <- Chat.get_by_user_and_id(user, id),
151 {_n, _} <- MessageReference.set_all_seen_for_chat(chat, last_read_id) do
152 render(conn, "show.json", chat: chat)
158 assigns: %{user: user},
159 private: %{open_api_spex: %{params: %{id: id} = params}}
163 with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do
166 |> MessageReference.for_chat_query()
167 |> Pagination.fetch_paginated(params)
170 |> add_link_headers(chat_message_refs)
171 |> put_view(MessageReferenceView)
172 |> render("index.json", chat_message_references: chat_message_refs)
176 def index(%{assigns: %{user: user}, private: %{open_api_spex: %{params: params}}} = conn, _) do
178 index_query(user, params)
181 render(conn, "index.json", chats: chats)
184 def index2(%{assigns: %{user: user}, private: %{open_api_spex: %{params: params}}} = conn, _) do
186 index_query(user, params)
187 |> Pagination.fetch_paginated(params)
190 |> add_link_headers(chats)
191 |> render("index.json", chats: chats)
194 defp index_query(%{id: user_id} = user, params) do
196 User.cached_blocked_users_ap_ids(user) ++
197 if params[:with_muted], do: [], else: User.cached_muted_users_ap_ids(user)
200 |> Chat.for_user_query()
201 |> where([c], c.recipient not in ^exclude_users)
204 def create(%{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
205 with %User{ap_id: recipient} <- User.get_cached_by_id(id),
206 {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
207 render(conn, "show.json", chat: chat)
211 def show(%{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
212 with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do
213 render(conn, "show.json", chat: chat)
217 defp idempotency_key(conn) do
218 case get_req_header(conn, "idempotency-key") do