Merge pull request 'purge chat and shout endpoints' (#97) from purge-chat into develop
[akkoma] / lib / pleroma / web / mastodon_api / controllers / conversation_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.ConversationController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
9
10 alias Pleroma.Conversation.Participation
11 alias Pleroma.Repo
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
13
14 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
15
16 plug(Pleroma.Web.ApiSpec.CastAndValidate)
17 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action == :index)
18 plug(OAuthScopesPlug, %{scopes: ["write:conversations"]} when action != :index)
19
20 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ConversationOperation
21
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)
25
26 conn
27 |> add_link_headers(participations)
28 |> render("participations.json", participations: participations, for: user)
29 end
30
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)
37 end
38 end
39
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
45 json(conn, %{})
46 end
47 end
48 end