For remote notices, redirect to the original instead of 404.
[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 def get_counts(%Activity{} = activity) do
31 %Object{data: data} = Object.normalize(activity)
32
33 %{
34 likes: data["like_count"] || 0,
35 replies: data["repliesCount"] || 0,
36 announces: data["announcement_count"] || 0
37 }
38 end
39
40 def represent(%Activity{} = activity), do: represent(activity, false)
41
42 def represent(%Activity{object: %Object{data: data}} = activity, selected) do
43 {:ok, user} = User.get_or_fetch(activity.object.data["actor"])
44
45 link =
46 case user.local do
47 true -> Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
48 _ -> data["url"] || data["external_url"] || data["id"]
49 end
50
51 %{
52 user: user,
53 title: get_title(activity.object),
54 content: data["content"] || nil,
55 attachment: data["attachment"],
56 link: link,
57 published: data["published"],
58 sensitive: data["sensitive"],
59 selected: selected,
60 counts: get_counts(activity),
61 id: activity.id
62 }
63 end
64
65 def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do
66 with %Activity{local: true} = activity <-
67 Activity.get_by_id_with_object(notice_id),
68 true <- Visibility.is_public?(activity.object),
69 %User{} = user <- User.get_by_ap_id(activity.object.data["actor"]) do
70 meta = Metadata.build_tags(%{activity_id: notice_id, object: activity.object, user: user})
71
72 timeline =
73 activity.object.data["context"]
74 |> ActivityPub.fetch_activities_for_context(%{})
75 |> Enum.reverse()
76 |> Enum.map(&represent(&1, &1.object.id == activity.object.id))
77
78 render(conn, "conversation.html", %{activities: timeline, meta: meta})
79 else
80 %Activity{object: %Object{data: data}} ->
81 conn
82 |> put_status(:found)
83 |> redirect(external: data["url"] || data["external_url"] || data["id"])
84
85 _ ->
86 conn
87 |> put_status(404)
88 |> render("error.html", %{message: "Post not found.", meta: ""})
89 end
90 end
91
92 def show(%{assigns: %{username_or_id: username_or_id}} = conn, params) do
93 case User.get_cached_by_nickname_or_id(username_or_id) do
94 %User{} = user ->
95 meta = Metadata.build_tags(%{user: user})
96
97 timeline =
98 ActivityPub.fetch_user_activities(user, nil, Map.take(params, @page_keys))
99 |> Enum.map(&represent/1)
100
101 prev_page_id =
102 (params["min_id"] || params["max_id"]) &&
103 List.first(timeline) && List.first(timeline).id
104
105 next_page_id = List.last(timeline) && List.last(timeline).id
106
107 render(conn, "profile.html", %{
108 user: user,
109 timeline: timeline,
110 prev_page_id: prev_page_id,
111 next_page_id: next_page_id,
112 meta: meta
113 })
114
115 _ ->
116 conn
117 |> put_status(404)
118 |> render("error.html", %{message: "User not found.", meta: ""})
119 end
120 end
121
122 def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
123 do: assign(conn, :notice_id, notice_id)
124
125 def assign_id(%{path_info: ["users", user_id]} = conn, _opts),
126 do: assign(conn, :username_or_id, user_id)
127
128 def assign_id(conn, _opts), do: conn
129 end