Support redirecting by activity UUID in static FE as well.
[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.ActivityPub.Visibility
13 alias Pleroma.Web.Metadata
14 alias Pleroma.Web.Router.Helpers
15
16 plug(:put_layout, :static_fe)
17 plug(:put_view, Pleroma.Web.StaticFE.StaticFEView)
18 plug(:assign_id)
19
20 @page_keys ["max_id", "min_id", "limit", "since_id", "order"]
21
22 defp get_title(%Object{data: %{"name" => name}}) when is_binary(name),
23 do: name
24
25 defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary),
26 do: summary
27
28 defp get_title(_), do: nil
29
30 defp not_found(conn, message) do
31 conn
32 |> put_status(404)
33 |> render("error.html", %{message: message, meta: ""})
34 end
35
36 def get_counts(%Activity{} = activity) do
37 %Object{data: data} = Object.normalize(activity)
38
39 %{
40 likes: data["like_count"] || 0,
41 replies: data["repliesCount"] || 0,
42 announces: data["announcement_count"] || 0
43 }
44 end
45
46 def represent(%Activity{} = activity), do: represent(activity, false)
47
48 def represent(%Activity{object: %Object{data: data}} = activity, selected) do
49 {:ok, user} = User.get_or_fetch(activity.object.data["actor"])
50
51 link =
52 case user.local do
53 true -> Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
54 _ -> data["url"] || data["external_url"] || data["id"]
55 end
56
57 %{
58 user: user,
59 title: get_title(activity.object),
60 content: data["content"] || nil,
61 attachment: data["attachment"],
62 link: link,
63 published: data["published"],
64 sensitive: data["sensitive"],
65 selected: selected,
66 counts: get_counts(activity),
67 id: activity.id
68 }
69 end
70
71 def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do
72 with %Activity{local: true} = activity <-
73 Activity.get_by_id_with_object(notice_id),
74 true <- Visibility.is_public?(activity.object),
75 %User{} = user <- User.get_by_ap_id(activity.object.data["actor"]) do
76 meta = Metadata.build_tags(%{activity_id: notice_id, object: activity.object, user: user})
77
78 timeline =
79 activity.object.data["context"]
80 |> ActivityPub.fetch_activities_for_context(%{})
81 |> Enum.reverse()
82 |> Enum.map(&represent(&1, &1.object.id == activity.object.id))
83
84 render(conn, "conversation.html", %{activities: timeline, meta: meta})
85 else
86 %Activity{object: %Object{data: data}} ->
87 conn
88 |> put_status(:found)
89 |> redirect(external: data["url"] || data["external_url"] || data["id"])
90
91 _ ->
92 not_found(conn, "Post not found.")
93 end
94 end
95
96 def show(%{assigns: %{username_or_id: username_or_id}} = conn, params) do
97 case User.get_cached_by_nickname_or_id(username_or_id) do
98 %User{} = user ->
99 meta = Metadata.build_tags(%{user: user})
100
101 timeline =
102 ActivityPub.fetch_user_activities(user, nil, Map.take(params, @page_keys))
103 |> Enum.map(&represent/1)
104
105 prev_page_id =
106 (params["min_id"] || params["max_id"]) &&
107 List.first(timeline) && List.first(timeline).id
108
109 next_page_id = List.last(timeline) && List.last(timeline).id
110
111 render(conn, "profile.html", %{
112 user: user,
113 timeline: timeline,
114 prev_page_id: prev_page_id,
115 next_page_id: next_page_id,
116 meta: meta
117 })
118
119 _ ->
120 not_found(conn, "User not found.")
121 end
122 end
123
124 def show(%{assigns: %{object_id: _}} = conn, _params) do
125 url = Helpers.url(conn) <> conn.request_path
126
127 case Activity.get_create_by_object_ap_id_with_object(url) do
128 %Activity{} = activity ->
129 to = Helpers.o_status_path(Pleroma.Web.Endpoint, :notice, activity)
130 redirect(conn, to: to)
131
132 _ ->
133 not_found(conn, "Post not found.")
134 end
135 end
136
137 def show(%{assigns: %{activity_id: _}} = conn, _params) do
138 url = Helpers.url(conn) <> conn.request_path
139
140 case Activity.get_by_ap_id(url) do
141 %Activity{} = activity ->
142 to = Helpers.o_status_path(Pleroma.Web.Endpoint, :notice, activity)
143 redirect(conn, to: to)
144
145 _ ->
146 not_found(conn, "Post not found.")
147 end
148 end
149
150 def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
151 do: assign(conn, :notice_id, notice_id)
152
153 def assign_id(%{path_info: ["users", user_id]} = conn, _opts),
154 do: assign(conn, :username_or_id, user_id)
155
156 def assign_id(%{path_info: ["objects", object_id]} = conn, _opts),
157 do: assign(conn, :object_id, object_id)
158
159 def assign_id(%{path_info: ["activities", activity_id]} = conn, _opts),
160 do: assign(conn, :activity_id, activity_id)
161
162 def assign_id(conn, _opts), do: conn
163 end