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
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, :show]
41 plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError)
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, 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: %{id: user_id} = user}} = conn, _params) do
144 |> User.blocked_users_ap_ids()
145 |> Enum.concat(User.muted_users_ap_ids(user))
149 |> Chat.for_user_query()
150 |> where([c], c.recipient not in ^exclude_users)
153 render(conn, "index.json", chats: chats)
156 def create(%{assigns: %{user: user}} = conn, %{id: id}) do
157 with %User{ap_id: recipient} <- User.get_cached_by_id(id),
158 {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
159 render(conn, "show.json", chat: chat)
163 def show(%{assigns: %{user: user}} = conn, %{id: id}) do
164 with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do
165 render(conn, "show.json", chat: chat)
169 defp idempotency_key(conn) do
170 case get_req_header(conn, "idempotency-key") do