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: 2]
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
30 participation = Participation.get(participation_id, preload: [:conversation])
32 if user.id == participation.user_id do
35 |> Map.put("blocking_user", user)
36 |> Map.put("muting_user", user)
37 |> Map.put("user", user)
40 participation.conversation.ap_id
41 |> ActivityPub.fetch_activities_for_context(params)
45 |> add_link_headers(activities)
46 |> put_view(StatusView)
47 |> render("index.json", %{activities: activities, for: user, as: :activity})
51 def update_conversation(
52 %{assigns: %{user: user}} = conn,
53 %{"id" => participation_id, "recipients" => recipients}
57 |> Participation.get()
59 with true <- user.id == participation.user_id,
60 {:ok, participation} <- Participation.set_recipients(participation, recipients) do
62 |> put_view(ConversationView)
63 |> render("participation.json", %{participation: participation, for: user})
67 def read_notification(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
68 with {:ok, notification} <- Notification.read_one(user, notification_id) do
70 |> put_view(NotificationView)
71 |> render("show.json", %{notification: notification, for: user})
75 |> put_status(:bad_request)
76 |> json(%{"error" => message})
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)
85 |> put_view(NotificationView)
86 |> render("index.json", %{notifications: notifications, for: user})