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.PleromaAPI.ChatView
19 alias Pleroma.Web.Plugs.OAuthScopesPlug
23 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
27 %{scopes: ["write:chats"]}
32 :mark_message_as_read,
39 %{scopes: ["read:chats"]} when action in [:messages, :index, :show]
42 plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError)
44 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation
46 def delete_message(%{assigns: %{user: %{id: user_id} = user}} = conn, %{
47 message_id: message_id,
50 with %MessageReference{} = cm_ref <-
51 MessageReference.get_by_id(message_id),
52 ^chat_id <- to_string(cm_ref.chat_id),
53 %Chat{user_id: ^user_id} <- Chat.get_by_id(chat_id),
54 {:ok, _} <- remove_or_delete(cm_ref, user) do
56 |> put_view(MessageReferenceView)
57 |> render("show.json", chat_message_reference: cm_ref)
60 {:error, :could_not_delete}
64 defp remove_or_delete(
65 %{object: %{data: %{"actor" => actor, "id" => id}}},
66 %{ap_id: actor} = user
68 with %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
69 CommonAPI.delete(activity.id, user)
73 defp remove_or_delete(cm_ref, _), do: MessageReference.delete(cm_ref)
75 def post_chat_message(
76 %{body_params: params, assigns: %{user: user}} = conn,
79 with {:ok, chat} <- Chat.get_by_user_and_id(user, id),
80 %User{} = recipient <- User.get_cached_by_ap_id(chat.recipient),
82 CommonAPI.post_chat_message(user, recipient, params[:content],
83 media_id: params[:media_id],
84 idempotency_key: idempotency_key(conn)
86 message <- Object.normalize(activity, false),
87 cm_ref <- MessageReference.for_chat_and_object(chat, message) do
89 |> put_view(MessageReferenceView)
90 |> render("show.json", chat_message_reference: cm_ref)
94 |> put_status(:unprocessable_entity)
95 |> json(%{error: message})
99 |> put_status(:bad_request)
100 |> json(%{error: message})
104 def mark_message_as_read(
105 %{assigns: %{user: %{id: user_id}}} = conn,
106 %{id: chat_id, message_id: message_id}
108 with %MessageReference{} = cm_ref <- MessageReference.get_by_id(message_id),
109 ^chat_id <- to_string(cm_ref.chat_id),
110 %Chat{user_id: ^user_id} <- Chat.get_by_id(chat_id),
111 {:ok, cm_ref} <- MessageReference.mark_as_read(cm_ref) do
113 |> put_view(MessageReferenceView)
114 |> render("show.json", chat_message_reference: cm_ref)
119 %{body_params: %{last_read_id: last_read_id}, assigns: %{user: user}} = conn,
122 with {:ok, chat} <- Chat.get_by_user_and_id(user, id),
123 {_n, _} <- MessageReference.set_all_seen_for_chat(chat, last_read_id) do
125 |> put_view(ChatView)
126 |> render("show.json", chat: chat)
130 def messages(%{assigns: %{user: user}} = conn, %{id: id} = params) do
131 with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do
134 |> MessageReference.for_chat_query()
135 |> Pagination.fetch_paginated(params)
138 |> add_link_headers(chat_message_refs)
139 |> put_view(MessageReferenceView)
140 |> render("index.json", chat_message_references: chat_message_refs)
144 def index(%{assigns: %{user: %{id: user_id} = user}} = conn, _params) do
145 blocked_ap_ids = User.blocked_users_ap_ids(user)
148 Chat.for_user_query(user_id)
149 |> where([c], c.recipient not in ^blocked_ap_ids)
153 |> put_view(ChatView)
154 |> render("index.json", chats: chats)
157 def create(%{assigns: %{user: user}} = conn, %{id: id}) do
158 with %User{ap_id: recipient} <- User.get_cached_by_id(id),
159 {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
161 |> put_view(ChatView)
162 |> render("show.json", chat: chat)
166 def show(%{assigns: %{user: user}} = conn, %{id: id}) do
167 with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do
169 |> put_view(ChatView)
170 |> render("show.json", chat: chat)
174 defp idempotency_key(conn) do
175 case get_req_header(conn, "idempotency-key") do