1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.ConversationController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
10 alias Pleroma.Conversation.Participation
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
14 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
16 plug(Pleroma.Web.ApiSpec.CastAndValidate)
17 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action == :index)
18 plug(OAuthScopesPlug, %{scopes: ["write:conversations"]} when action != :index)
20 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ConversationOperation
22 @doc "GET /api/v1/conversations"
23 def index(%{assigns: %{user: user}} = conn, params) do
24 participations = Participation.for_user_with_last_activity_id(user, params)
27 |> add_link_headers(participations)
28 |> render("participations.json", participations: participations, for: user)
31 @doc "POST /api/v1/conversations/:id/read"
32 def mark_as_read(%{assigns: %{user: user}} = conn, %{id: participation_id}) do
33 with %Participation{} = participation <-
34 Repo.get_by(Participation, id: participation_id, user_id: user.id),
35 {:ok, participation} <- Participation.mark_as_read(participation) do
36 render(conn, "participation.json", participation: participation, for: user)
40 @doc "DELETE /api/v1/conversations/:id"
41 def delete(%{assigns: %{user: user}} = conn, %{id: participation_id}) do
42 with %Participation{} = participation <-
43 Repo.get_by(Participation, id: participation_id, user_id: user.id),
44 {:ok, _} <- Participation.delete(participation) do