1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 7]
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
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
21 |> put_view(ConversationView)
22 |> render("participation.json", %{participation: participation, for: user})
26 def conversation_statuses(
27 %{assigns: %{user: user}} = conn,
28 %{"id" => participation_id} = params
32 |> Map.put("blocking_user", user)
33 |> Map.put("muting_user", user)
34 |> Map.put("user", user)
38 |> Participation.get(preload: [:conversation])
40 if user.id == participation.user_id do
42 participation.conversation.ap_id
43 |> ActivityPub.fetch_activities_for_context(params)
48 :conversation_statuses,
55 |> put_view(StatusView)
56 |> render("index.json", %{activities: activities, for: user, as: :activity})
60 def update_conversation(
61 %{assigns: %{user: user}} = conn,
62 %{"id" => participation_id, "recipients" => recipients}
66 |> Participation.get()
68 with true <- user.id == participation.user_id,
69 {:ok, participation} <- Participation.set_recipients(participation, recipients) do
71 |> put_view(ConversationView)
72 |> render("participation.json", %{participation: participation, for: user})
76 def read_notification(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
77 with {:ok, notification} <- Notification.read_one(user, notification_id) do
79 |> put_view(NotificationView)
80 |> render("show.json", %{notification: notification, for: user})
84 |> put_status(:bad_request)
85 |> json(%{"error" => message})
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)
94 |> put_view(NotificationView)
95 |> render("index.json", %{notifications: notifications, for: user})