1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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(:put_view, Pleroma.Web.MastodonAPI.ConversationView)
17 plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:show, :statuses])
21 %{scopes: ["write:conversations"]} when action in [:update, :mark_as_read]
24 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaConversationOperation
26 def show(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: participation_id}) do
27 with %Participation{user_id: ^user_id} = participation <- Participation.get(participation_id) do
28 render(conn, "participation.json", participation: participation, for: user)
32 |> put_status(:not_found)
33 |> json(%{"error" => "Unknown conversation id"})
38 %{assigns: %{user: %{id: user_id} = user}} = conn,
39 %{id: participation_id} = params
41 with %Participation{user_id: ^user_id} = participation <-
42 Participation.get(participation_id, preload: [:conversation]) do
45 |> Map.put(:blocking_user, user)
46 |> Map.put(:muting_user, user)
47 |> Map.put(:user, user)
50 participation.conversation.ap_id
51 |> ActivityPub.fetch_activities_for_context_query(params)
52 |> Pleroma.Pagination.fetch_paginated(Map.put(params, :total, false))
56 |> add_link_headers(activities)
57 |> put_view(StatusView)
58 |> render("index.json", activities: activities, for: user, as: :activity)
62 |> put_status(:not_found)
63 |> json(%{"error" => "Unknown conversation id"})
68 %{assigns: %{user: %{id: user_id} = user}} = conn,
69 %{id: participation_id, recipients: recipients}
71 with %Participation{user_id: ^user_id} = participation <- Participation.get(participation_id),
72 {:ok, participation} <- Participation.set_recipients(participation, recipients) do
73 render(conn, "participation.json", participation: participation, for: user)
77 |> put_status(:bad_request)
78 |> json(%{"error" => message})
82 |> put_status(:not_found)
83 |> json(%{"error" => "Unknown conversation id"})
87 def mark_as_read(%{assigns: %{user: user}} = conn, _params) do
88 with {:ok, _, participations} <- Participation.mark_all_as_read(user) do
90 |> add_link_headers(participations)
91 |> render("participations.json", participations: participations, for: user)