Show counts for replies, likes, and announces for selected notice.
[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 action_fallback(:not_found)
18
19 defp get_title(%Object{data: %{"name" => name}}) when is_binary(name),
20 do: name
21
22 defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary),
23 do: summary
24
25 defp get_title(_), do: nil
26
27 def get_counts(%Activity{} = activity) do
28 %Object{data: data} = Object.normalize(activity)
29
30 %{
31 likes: data["like_count"] || 0,
32 replies: data["repliesCount"] || 0,
33 announces: data["announcement_count"] || 0
34 }
35 end
36
37 def represent(%Activity{} = activity, %User{} = user, selected) do
38 %{
39 user: user,
40 title: get_title(activity.object),
41 content: activity.object.data["content"] || nil,
42 attachment: activity.object.data["attachment"],
43 link: Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity.id),
44 published: activity.object.data["published"],
45 sensitive: activity.object.data["sensitive"],
46 selected: selected,
47 counts: get_counts(activity)
48 }
49 end
50
51 def represent(%Activity{} = activity, selected) do
52 {:ok, user} = User.get_or_fetch(activity.data["actor"])
53 represent(activity, user, selected)
54 end
55
56 def show_notice(%{assigns: %{notice_id: notice_id}} = conn, _params) do
57 instance_name = Pleroma.Config.get([:instance, :name], "Pleroma")
58 activity = Activity.get_by_id_with_object(notice_id)
59 context = activity.object.data["context"]
60 activities = ActivityPub.fetch_activities_for_context(context, %{})
61
62 represented =
63 for a <- Enum.reverse(activities) do
64 represent(a, a.object.id == activity.object.id)
65 end
66
67 render(conn, "conversation.html", %{activities: represented, instance_name: instance_name})
68 end
69
70 def show_user(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do
71 instance_name = Pleroma.Config.get([:instance, :name], "Pleroma")
72 %User{} = user = User.get_cached_by_nickname_or_id(username_or_id)
73
74 timeline =
75 for activity <- ActivityPub.fetch_user_activities(user, nil, %{}) do
76 represent(activity, user, false)
77 end
78
79 render(conn, "profile.html", %{user: user, timeline: timeline, instance_name: instance_name})
80 end
81
82 def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
83 do: assign(conn, :notice_id, notice_id)
84
85 def assign_id(%{path_info: ["users", user_id]} = conn, _opts),
86 do: assign(conn, :username_or_id, user_id)
87
88 def assign_id(%{path_info: [user_id]} = conn, _opts),
89 do: assign(conn, :username_or_id, user_id)
90
91 def assign_id(conn, _opts), do: conn
92
93 # Fallback for unhandled types
94 def not_found(conn, _opts) do
95 conn
96 |> put_status(404)
97 |> text("Not found")
98 end
99 end