Merge remote-tracking branch 'remotes/origin/develop' into 1560-non-federating-instan...
[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(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action == :index)
17 plug(OAuthScopesPlug, %{scopes: ["write:conversations"]} when action == :read)
18
19 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
20
21 @doc "GET /api/v1/conversations"
22 def index(%{assigns: %{user: user}} = conn, params) do
23 participations = Participation.for_user_with_last_activity_id(user, params)
24
25 conn
26 |> add_link_headers(participations)
27 |> render("participations.json", participations: participations, for: user)
28 end
29
30 @doc "POST /api/v1/conversations/:id/read"
31 def read(%{assigns: %{user: user}} = conn, %{"id" => participation_id}) do
32 with %Participation{} = participation <-
33 Repo.get_by(Participation, id: participation_id, user_id: user.id),
34 {:ok, participation} <- Participation.mark_as_read(participation) do
35 render(conn, "participation.json", participation: participation, for: user)
36 end
37 end
38 end