Migrate Pleroma.Web to phoenix 1.6 formats
[akkoma] / lib / pleroma / web / mastodon_api / views / conversation_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.ConversationView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.Activity
9 alias Pleroma.Repo
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.MastodonAPI.StatusView
13
14 def render("participations.json", %{participations: participations, for: user}) do
15 render_many(participations, __MODULE__, "participation.json", %{
16 as: :participation,
17 for: user
18 })
19 end
20
21 def render("participation.json", %{participation: participation, for: user}) do
22 participation = Repo.preload(participation, conversation: [], recipients: [])
23
24 last_activity_id =
25 with nil <- participation.last_activity_id do
26 ActivityPub.fetch_latest_direct_activity_id_for_context(
27 participation.conversation.ap_id,
28 %{
29 user: user,
30 blocking_user: user
31 }
32 )
33 end
34
35 activity = Activity.get_by_id_with_object(last_activity_id)
36
37 # Conversations return all users except the current user,
38 # except when the current user is the only participant
39 users =
40 if length(participation.recipients) > 1 do
41 Enum.reject(participation.recipients, &(&1.id == user.id))
42 else
43 participation.recipients
44 end
45
46 %{
47 id: participation.id |> to_string(),
48 accounts: render(AccountView, "index.json", users: users, for: user),
49 unread: !participation.read,
50 last_status:
51 render(StatusView, "show.json",
52 activity: activity,
53 direct_conversation_id: participation.id,
54 for: user
55 )
56 }
57 end
58 end