Make many of the improvements suggested in review.
[akkoma] / lib / pleroma / web / static_fe / static_fe_controller.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.StaticFE.StaticFEController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Activity
9 alias Pleroma.Object
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.Router.Helpers
13
14 plug(:put_layout, :static_fe)
15 plug(:put_view, Pleroma.Web.StaticFE.StaticFEView)
16 plug(:assign_id)
17
18 defp get_title(%Object{data: %{"name" => name}}) when is_binary(name),
19 do: name
20
21 defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary),
22 do: summary
23
24 defp get_title(_), do: nil
25
26 def get_counts(%Activity{} = activity) do
27 %Object{data: data} = Object.normalize(activity)
28
29 %{
30 likes: data["like_count"] || 0,
31 replies: data["repliesCount"] || 0,
32 announces: data["announcement_count"] || 0
33 }
34 end
35
36 def represent(%Activity{} = activity), do: represent(activity, false)
37
38 def represent(%Activity{object: %Object{data: data}} = activity, selected) do
39 {:ok, user} = User.get_or_fetch(activity.object.data["actor"])
40
41 link =
42 case user.local do
43 true -> Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
44 _ -> data["url"] || data["external_url"] || data["id"]
45 end
46
47 %{
48 user: user,
49 title: get_title(activity.object),
50 content: data["content"] || nil,
51 attachment: data["attachment"],
52 link: link,
53 published: data["published"],
54 sensitive: data["sensitive"],
55 selected: selected,
56 counts: get_counts(activity)
57 }
58 end
59
60 def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do
61 activity = Activity.get_by_id_with_object(notice_id)
62 timeline =
63 activity.object.data["context"]
64 |> ActivityPub.fetch_activities_for_context(%{})
65 |> Enum.reverse()
66 |> Enum.map(&represent(&1, &1.object.id == activity.object.id))
67
68 render(conn, "conversation.html", %{activities: timeline})
69 end
70
71 def show(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do
72 %User{} = user = User.get_cached_by_nickname_or_id(username_or_id)
73
74 timeline =
75 ActivityPub.fetch_user_activities(user, nil, %{})
76 |> Enum.map(&represent/1)
77
78 render(conn, "profile.html", %{user: user, timeline: timeline})
79 end
80
81 def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
82 do: assign(conn, :notice_id, notice_id)
83
84 def assign_id(%{path_info: ["users", user_id]} = conn, _opts),
85 do: assign(conn, :username_or_id, user_id)
86
87 def assign_id(conn, _opts), do: conn
88 end