Merge remote-tracking branch 'upstream/develop' into feature/move-activity
[akkoma] / lib / pleroma / web / mastodon_api / views / conversation_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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", as: :participation, for: user)
16 end
17
18 def render("participation.json", %{participation: participation, for: user}) do
19 participation = Repo.preload(participation, conversation: [], recipients: [])
20
21 last_activity_id =
22 with nil <- participation.last_activity_id do
23 ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{
24 "user" => user,
25 "blocking_user" => user
26 })
27 end
28
29 activity = Activity.get_by_id_with_object(last_activity_id)
30 # Conversations return all users except the current user.
31 users = Enum.reject(participation.recipients, &(&1.id == user.id))
32
33 %{
34 id: participation.id |> to_string(),
35 accounts: render(AccountView, "index.json", users: users, as: :user),
36 unread: !participation.read,
37 last_status:
38 render(StatusView, "show.json",
39 activity: activity,
40 direct_conversation_id: participation.id
41 )
42 }
43 end
44 end