Pagination for user profiles.
[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 @page_keys ["max_id", "min_id", "limit", "since_id", "order"]
19
20 defp get_title(%Object{data: %{"name" => name}}) when is_binary(name),
21 do: name
22
23 defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary),
24 do: summary
25
26 defp get_title(_), do: nil
27
28 def get_counts(%Activity{} = activity) do
29 %Object{data: data} = Object.normalize(activity)
30
31 %{
32 likes: data["like_count"] || 0,
33 replies: data["repliesCount"] || 0,
34 announces: data["announcement_count"] || 0
35 }
36 end
37
38 def represent(%Activity{} = activity), do: represent(activity, false)
39
40 def represent(%Activity{object: %Object{data: data}} = activity, selected) do
41 {:ok, user} = User.get_or_fetch(activity.object.data["actor"])
42
43 link =
44 case user.local do
45 true -> Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
46 _ -> data["url"] || data["external_url"] || data["id"]
47 end
48
49 %{
50 user: user,
51 title: get_title(activity.object),
52 content: data["content"] || nil,
53 attachment: data["attachment"],
54 link: link,
55 published: data["published"],
56 sensitive: data["sensitive"],
57 selected: selected,
58 counts: get_counts(activity),
59 id: activity.id
60 }
61 end
62
63 def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do
64 activity = Activity.get_by_id_with_object(notice_id)
65 timeline =
66 activity.object.data["context"]
67 |> ActivityPub.fetch_activities_for_context(%{})
68 |> Enum.reverse()
69 |> Enum.map(&represent(&1, &1.object.id == activity.object.id))
70
71 render(conn, "conversation.html", %{activities: timeline})
72 end
73
74 def show(%{assigns: %{username_or_id: username_or_id}} = conn, params) do
75 %User{} = user = User.get_cached_by_nickname_or_id(username_or_id)
76
77 timeline =
78 ActivityPub.fetch_user_activities(user, nil, Map.take(params, @page_keys))
79 |> Enum.map(&represent/1)
80
81 prev_page_id =
82 (params["min_id"] || params["max_id"]) &&
83 List.first(timeline) && List.first(timeline).id
84
85 next_page_id = List.last(timeline) && List.last(timeline).id
86
87 render(conn, "profile.html", %{
88 user: user,
89 timeline: timeline,
90 prev_page_id: prev_page_id,
91 next_page_id: next_page_id
92 })
93 end
94
95 def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
96 do: assign(conn, :notice_id, notice_id)
97
98 def assign_id(%{path_info: ["users", user_id]} = conn, _opts),
99 do: assign(conn, :username_or_id, user_id)
100
101 def assign_id(conn, _opts), do: conn
102 end