Merge branch 'develop' into feature/reports-groups-and-multiple-state-update
[akkoma] / lib / pleroma / web / pleroma_api / controllers / pleroma_api_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.PleromaAPIController 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.Notification
12 alias Pleroma.Plugs.OAuthScopesPlug
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.MastodonAPI.ConversationView
15 alias Pleroma.Web.MastodonAPI.NotificationView
16 alias Pleroma.Web.MastodonAPI.StatusView
17
18 plug(
19 OAuthScopesPlug,
20 %{scopes: ["read:statuses"]} when action in [:conversation, :conversation_statuses]
21 )
22
23 plug(
24 OAuthScopesPlug,
25 %{scopes: ["write:conversations"]} when action == :update_conversation
26 )
27
28 plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action == :read_notification)
29
30 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
31
32 def conversation(%{assigns: %{user: user}} = conn, %{"id" => participation_id}) do
33 with %Participation{} = participation <- Participation.get(participation_id),
34 true <- user.id == participation.user_id do
35 conn
36 |> put_view(ConversationView)
37 |> render("participation.json", %{participation: participation, for: user})
38 end
39 end
40
41 def conversation_statuses(
42 %{assigns: %{user: user}} = conn,
43 %{"id" => participation_id} = params
44 ) do
45 participation = Participation.get(participation_id, preload: [:conversation])
46
47 if user.id == participation.user_id do
48 params =
49 params
50 |> Map.put("blocking_user", user)
51 |> Map.put("muting_user", user)
52 |> Map.put("user", user)
53
54 activities =
55 participation.conversation.ap_id
56 |> ActivityPub.fetch_activities_for_context(params)
57 |> Enum.reverse()
58
59 conn
60 |> add_link_headers(activities)
61 |> put_view(StatusView)
62 |> render("index.json", %{activities: activities, for: user, as: :activity})
63 end
64 end
65
66 def update_conversation(
67 %{assigns: %{user: user}} = conn,
68 %{"id" => participation_id, "recipients" => recipients}
69 ) do
70 participation =
71 participation_id
72 |> Participation.get()
73
74 with true <- user.id == participation.user_id,
75 {:ok, participation} <- Participation.set_recipients(participation, recipients) do
76 conn
77 |> put_view(ConversationView)
78 |> render("participation.json", %{participation: participation, for: user})
79 end
80 end
81
82 def read_conversations(%{assigns: %{user: user}} = conn, _params) do
83 with {:ok, _, participations} <- Participation.mark_all_as_read(user) do
84 conn
85 |> add_link_headers(participations)
86 |> put_view(ConversationView)
87 |> render("participations.json", participations: participations, for: user)
88 end
89 end
90
91 def read_notification(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
92 with {:ok, notification} <- Notification.read_one(user, notification_id) do
93 conn
94 |> put_view(NotificationView)
95 |> render("show.json", %{notification: notification, for: user})
96 else
97 {:error, message} ->
98 conn
99 |> put_status(:bad_request)
100 |> json(%{"error" => message})
101 end
102 end
103
104 def read_notification(%{assigns: %{user: user}} = conn, %{"max_id" => max_id}) do
105 with notifications <- Notification.set_read_up_to(user, max_id) do
106 notifications = Enum.take(notifications, 80)
107
108 conn
109 |> put_view(NotificationView)
110 |> render("index.json", %{notifications: notifications, for: user})
111 end
112 end
113 end