1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.ConversationController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
10 alias Pleroma.Conversation.Participation
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.MastodonAPI.StatusView
13 alias Pleroma.Web.Plugs.OAuthScopesPlug
15 plug(Pleroma.Web.ApiSpec.CastAndValidate)
16 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:show, :statuses])
20 %{scopes: ["write:conversations"]} when action in [:update, :mark_as_read]
23 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaConversationOperation
25 def show(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: participation_id}) do
26 with %Participation{user_id: ^user_id} = participation <- Participation.get(participation_id) do
27 render(conn, "participation.json", participation: participation, for: user)
31 |> put_status(:not_found)
32 |> json(%{"error" => "Unknown conversation id"})
37 %{assigns: %{user: %{id: user_id} = user}} = conn,
38 %{id: participation_id} = params
40 with %Participation{user_id: ^user_id} = participation <-
41 Participation.get(participation_id, preload: [:conversation]) do
44 |> Map.put(:blocking_user, user)
45 |> Map.put(:muting_user, user)
46 |> Map.put(:user, user)
49 participation.conversation.ap_id
50 |> ActivityPub.fetch_activities_for_context_query(params)
51 |> Pleroma.Pagination.fetch_paginated(Map.put(params, :total, false))
55 |> add_link_headers(activities)
56 |> put_view(StatusView)
57 |> render("index.json", activities: activities, for: user, as: :activity)
61 |> put_status(:not_found)
62 |> json(%{"error" => "Unknown conversation id"})
67 %{assigns: %{user: %{id: user_id} = user}} = conn,
68 %{id: participation_id, recipients: recipients}
70 with %Participation{user_id: ^user_id} = participation <- Participation.get(participation_id),
71 {:ok, participation} <- Participation.set_recipients(participation, recipients) do
72 render(conn, "participation.json", participation: participation, for: user)
76 |> put_status(:bad_request)
77 |> json(%{"error" => message})
81 |> put_status(:not_found)
82 |> json(%{"error" => "Unknown conversation id"})
86 def mark_as_read(%{assigns: %{user: user}} = conn, _params) do
87 with {:ok, _, participations} <- Participation.mark_all_as_read(user) do
89 |> add_link_headers(participations)
90 |> render("participations.json", participations: participations, for: user)