AccountController: Federate user account changes.
[akkoma] / lib / pleroma / web / mastodon_api / controllers / conversation_controller.ex
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
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.Plugs.OAuthScopesPlug
12 alias Pleroma.Repo
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 params = stringify_pagination_params(params)
25 participations = Participation.for_user_with_last_activity_id(user, params)
26
27 conn
28 |> add_link_headers(participations)
29 |> render("participations.json", participations: participations, for: user)
30 end
31
32 @doc "POST /api/v1/conversations/:id/read"
33 def mark_as_read(%{assigns: %{user: user}} = conn, %{id: participation_id}) do
34 with %Participation{} = participation <-
35 Repo.get_by(Participation, id: participation_id, user_id: user.id),
36 {:ok, participation} <- Participation.mark_as_read(participation) do
37 render(conn, "participation.json", participation: participation, for: user)
38 end
39 end
40
41 defp stringify_pagination_params(params) do
42 atom_keys =
43 Pleroma.Pagination.page_keys()
44 |> Enum.map(&String.to_atom(&1))
45
46 str_keys =
47 params
48 |> Map.take(atom_keys)
49 |> Enum.map(fn {key, value} -> {to_string(key), value} end)
50 |> Enum.into(%{})
51
52 params
53 |> Map.delete(atom_keys)
54 |> Map.merge(str_keys)
55 end
56 end