Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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.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 participation = Participation.get(participation_id, preload: [:conversation])
31
32 if user.id == participation.user_id do
33 params =
34 params
35 |> Map.put("blocking_user", user)
36 |> Map.put("muting_user", user)
37 |> Map.put("user", user)
38
39 activities =
40 participation.conversation.ap_id
41 |> ActivityPub.fetch_activities_for_context(params)
42 |> Enum.reverse()
43
44 conn
45 |> add_link_headers(activities)
46 |> put_view(StatusView)
47 |> render("index.json", %{activities: activities, for: user, as: :activity})
48 end
49 end
50
51 def update_conversation(
52 %{assigns: %{user: user}} = conn,
53 %{"id" => participation_id, "recipients" => recipients}
54 ) do
55 participation =
56 participation_id
57 |> Participation.get()
58
59 with true <- user.id == participation.user_id,
60 {:ok, participation} <- Participation.set_recipients(participation, recipients) do
61 conn
62 |> put_view(ConversationView)
63 |> render("participation.json", %{participation: participation, for: user})
64 end
65 end
66
67 def read_notification(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
68 with {:ok, notification} <- Notification.read_one(user, notification_id) do
69 conn
70 |> put_view(NotificationView)
71 |> render("show.json", %{notification: notification, for: user})
72 else
73 {:error, message} ->
74 conn
75 |> put_status(:bad_request)
76 |> json(%{"error" => message})
77 end
78 end
79
80 def read_notification(%{assigns: %{user: user}} = conn, %{"max_id" => max_id}) do
81 with notifications <- Notification.set_read_up_to(user, max_id) do
82 notifications = Enum.take(notifications, 80)
83
84 conn
85 |> put_view(NotificationView)
86 |> render("index.json", %{notifications: notifications, for: user})
87 end
88 end
89 end