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
10 alias Pleroma.Pagination
11 alias Pleroma.Plugs.OAuthScopesPlug
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.PleromaAPI.ChatMessageView
16 alias Pleroma.Web.PleromaAPI.ChatView
19 import Pleroma.Web.ActivityPub.ObjectValidator, only: [stringify_keys: 1]
26 %{scopes: ["write:statuses"]}
27 when action in [:post_chat_message, :create, :mark_as_read, :delete_message]
32 %{scopes: ["read:statuses"]} when action in [:messages, :index, :show]
35 plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError)
37 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation
39 def delete_message(%{assigns: %{user: %{ap_id: actor} = user}} = conn, %{
47 "type" => "ChatMessage"
49 } = message <- Object.get_by_id(id),
50 %Chat{} = chat <- Chat.get(user.id, recipient),
51 %Activity{} = activity <- Activity.get_create_by_object_ap_id(object),
52 {:ok, _delete} <- CommonAPI.delete(activity.id, user) do
54 |> put_view(ChatMessageView)
55 |> render("show.json", for: user, object: message, chat: chat)
59 def post_chat_message(
60 %{body_params: %{content: content} = params, assigns: %{user: %{id: user_id} = user}} =
66 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id),
67 %User{} = recipient <- User.get_cached_by_ap_id(chat.recipient),
69 CommonAPI.post_chat_message(user, recipient, content, media_id: params[:media_id]),
70 message <- Object.normalize(activity) do
72 |> put_view(ChatMessageView)
73 |> render("show.json", for: user, object: message, chat: chat)
77 def mark_as_read(%{assigns: %{user: %{id: user_id}}} = conn, %{id: id}) do
78 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id),
79 {:ok, chat} <- Chat.mark_as_read(chat) do
82 |> render("show.json", chat: chat)
86 def messages(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: id} = params) do
87 with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id) do
90 |> Chat.messages_for_chat_query()
91 |> Pagination.fetch_paginated(params |> stringify_keys())
94 |> put_view(ChatMessageView)
95 |> render("index.json", for: user, objects: messages, chat: chat)
99 |> put_status(:not_found)
100 |> json(%{error: "not found"})
104 def index(%{assigns: %{user: %{id: user_id}}} = conn, params) do
107 where: c.user_id == ^user_id,
108 order_by: [desc: c.updated_at]
110 |> Pagination.fetch_paginated(params |> stringify_keys)
113 |> put_view(ChatView)
114 |> render("index.json", chats: chats)
117 def create(%{assigns: %{user: user}} = conn, params) do
118 with %User{ap_id: recipient} <- User.get_by_id(params[:id]),
119 {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
121 |> put_view(ChatView)
122 |> render("show.json", chat: chat)
126 def show(%{assigns: %{user: user}} = conn, params) do
127 with %Chat{} = chat <- Repo.get_by(Chat, user_id: user.id, id: params[:id]) do
129 |> put_view(ChatView)
130 |> render("show.json", chat: chat)