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.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
20 %{scopes: ["read:statuses"]} when action in [:conversation, :conversation_statuses]
25 %{scopes: ["write:conversations"]} when action == :update_conversation
28 plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action == :read_notification)
30 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
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
36 |> put_view(ConversationView)
37 |> render("participation.json", %{participation: participation, for: user})
41 def conversation_statuses(
42 %{assigns: %{user: user}} = conn,
43 %{"id" => participation_id} = params
45 participation = Participation.get(participation_id, preload: [:conversation])
47 if user.id == participation.user_id do
50 |> Map.put("blocking_user", user)
51 |> Map.put("muting_user", user)
52 |> Map.put("user", user)
55 participation.conversation.ap_id
56 |> ActivityPub.fetch_activities_for_context(params)
60 |> add_link_headers(activities)
61 |> put_view(StatusView)
62 |> render("index.json", %{activities: activities, for: user, as: :activity})
66 def update_conversation(
67 %{assigns: %{user: user}} = conn,
68 %{"id" => participation_id, "recipients" => recipients}
72 |> Participation.get()
74 with true <- user.id == participation.user_id,
75 {:ok, participation} <- Participation.set_recipients(participation, recipients) do
77 |> put_view(ConversationView)
78 |> render("participation.json", %{participation: participation, for: user})
82 def read_conversations(%{assigns: %{user: user}} = conn, _params) do
83 with {:ok, _, participations} <- Participation.mark_all_as_read(user) do
85 |> add_link_headers(participations)
86 |> put_view(ConversationView)
87 |> render("participations.json", participations: participations, for: user)
91 def read_notification(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
92 with {:ok, notification} <- Notification.read_one(user, notification_id) do
94 |> put_view(NotificationView)
95 |> render("show.json", %{notification: notification, for: user})
99 |> put_status(:bad_request)
100 |> json(%{"error" => message})
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)
109 |> put_view(NotificationView)
110 |> render("index.json", %{notifications: notifications, for: user})