Merge pull request 'metrics' (#375) from stats into develop
[akkoma] / lib / pleroma / web / pleroma_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.PleromaAPI.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.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.MastodonAPI.StatusView
13 alias Pleroma.Web.Plugs.OAuthScopesPlug
14
15 plug(Pleroma.Web.ApiSpec.CastAndValidate)
16 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:show, :statuses])
17
18 plug(
19 OAuthScopesPlug,
20 %{scopes: ["write:conversations"]} when action in [:update, :mark_as_read]
21 )
22
23 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaConversationOperation
24
25 def show(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: participation_id}) do
26 with %Participation{user_id: ^user_id} = participation <- Participation.get(participation_id) do
27 render(conn, "participation.json", participation: participation, for: user)
28 else
29 _error ->
30 conn
31 |> put_status(:not_found)
32 |> json(%{"error" => "Unknown conversation id"})
33 end
34 end
35
36 def statuses(
37 %{assigns: %{user: %{id: user_id} = user}} = conn,
38 %{id: participation_id} = params
39 ) do
40 with %Participation{user_id: ^user_id} = participation <-
41 Participation.get(participation_id, preload: [:conversation]) do
42 params =
43 params
44 |> Map.put(:blocking_user, user)
45 |> Map.put(:muting_user, user)
46 |> Map.put(:user, user)
47
48 activities =
49 participation.conversation.ap_id
50 |> ActivityPub.fetch_activities_for_context_query(params)
51 |> Pleroma.Pagination.fetch_paginated(Map.put(params, :total, false))
52 |> Enum.reverse()
53
54 conn
55 |> add_link_headers(activities)
56 |> put_view(StatusView)
57 |> render("index.json", activities: activities, for: user, as: :activity)
58 else
59 _error ->
60 conn
61 |> put_status(:not_found)
62 |> json(%{"error" => "Unknown conversation id"})
63 end
64 end
65
66 def update(
67 %{assigns: %{user: %{id: user_id} = user}} = conn,
68 %{id: participation_id, recipients: recipients}
69 ) do
70 with %Participation{user_id: ^user_id} = participation <- Participation.get(participation_id),
71 {:ok, participation} <- Participation.set_recipients(participation, recipients) do
72 render(conn, "participation.json", participation: participation, for: user)
73 else
74 {:error, message} ->
75 conn
76 |> put_status(:bad_request)
77 |> json(%{"error" => message})
78
79 _error ->
80 conn
81 |> put_status(:not_found)
82 |> json(%{"error" => "Unknown conversation id"})
83 end
84 end
85
86 def mark_as_read(%{assigns: %{user: user}} = conn, _params) do
87 with {:ok, _, participations} <- Participation.mark_all_as_read(user) do
88 conn
89 |> add_link_headers(participations)
90 |> render("participations.json", participations: participations, for: user)
91 end
92 end
93 end