fix formatting
[akkoma] / lib / pleroma / web / pleroma_api / 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: 7]
9
10 alias Pleroma.Conversation.Participation
11 alias Pleroma.Notification
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.MastodonAPI.ConversationView
14 alias Pleroma.Web.MastodonAPI.NotificationView
15 alias Pleroma.Web.MastodonAPI.StatusView
16
17 def conversation(%{assigns: %{user: user}} = conn, %{"id" => participation_id}) do
18 with %Participation{} = participation <- Participation.get(participation_id),
19 true <- user.id == participation.user_id do
20 conn
21 |> put_view(ConversationView)
22 |> render("participation.json", %{participation: participation, for: user})
23 end
24 end
25
26 def conversation_statuses(
27 %{assigns: %{user: user}} = conn,
28 %{"id" => participation_id} = params
29 ) do
30 params =
31 params
32 |> Map.put("blocking_user", user)
33 |> Map.put("muting_user", user)
34 |> Map.put("user", user)
35
36 participation =
37 participation_id
38 |> Participation.get(preload: [:conversation])
39
40 if user.id == participation.user_id do
41 activities =
42 participation.conversation.ap_id
43 |> ActivityPub.fetch_activities_for_context(params)
44 |> Enum.reverse()
45
46 conn
47 |> add_link_headers(
48 :conversation_statuses,
49 activities,
50 participation_id,
51 params,
52 nil,
53 &pleroma_api_url/4
54 )
55 |> put_view(StatusView)
56 |> render("index.json", %{activities: activities, for: user, as: :activity})
57 end
58 end
59
60 def update_conversation(
61 %{assigns: %{user: user}} = conn,
62 %{"id" => participation_id, "recipients" => recipients}
63 ) do
64 participation =
65 participation_id
66 |> Participation.get()
67
68 with true <- user.id == participation.user_id,
69 {:ok, participation} <- Participation.set_recipients(participation, recipients) do
70 conn
71 |> put_view(ConversationView)
72 |> render("participation.json", %{participation: participation, for: user})
73 end
74 end
75
76 def read_notification(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
77 with {:ok, notification} <- Notification.read_one(user, notification_id) do
78 conn
79 |> put_view(NotificationView)
80 |> render("show.json", %{notification: notification, for: user})
81 else
82 {:error, message} ->
83 conn
84 |> put_status(:bad_request)
85 |> json(%{"error" => message})
86 end
87 end
88
89 def read_notification(%{assigns: %{user: user}} = conn, %{"max_id" => max_id}) do
90 with notifications <- Notification.set_read_up_to(user, max_id) do
91 notifications = Enum.take(notifications, 80)
92
93 conn
94 |> put_view(NotificationView)
95 |> render("index.json", %{notifications: notifications, for: user})
96 end
97 end
98 end