Prevent non-local notices from rendering.
[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.Metadata
13 alias Pleroma.Web.Router.Helpers
14
15 plug(:put_layout, :static_fe)
16 plug(:put_view, Pleroma.Web.StaticFE.StaticFEView)
17 plug(:assign_id)
18
19 @page_keys ["max_id", "min_id", "limit", "since_id", "order"]
20
21 defp get_title(%Object{data: %{"name" => name}}) when is_binary(name),
22 do: name
23
24 defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary),
25 do: summary
26
27 defp get_title(_), do: nil
28
29 def get_counts(%Activity{} = activity) do
30 %Object{data: data} = Object.normalize(activity)
31
32 %{
33 likes: data["like_count"] || 0,
34 replies: data["repliesCount"] || 0,
35 announces: data["announcement_count"] || 0
36 }
37 end
38
39 def represent(%Activity{} = activity), do: represent(activity, false)
40
41 def represent(%Activity{object: %Object{data: data}} = activity, selected) do
42 {:ok, user} = User.get_or_fetch(activity.object.data["actor"])
43
44 link =
45 case user.local do
46 true -> Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
47 _ -> data["url"] || data["external_url"] || data["id"]
48 end
49
50 %{
51 user: user,
52 title: get_title(activity.object),
53 content: data["content"] || nil,
54 attachment: data["attachment"],
55 link: link,
56 published: data["published"],
57 sensitive: data["sensitive"],
58 selected: selected,
59 counts: get_counts(activity),
60 id: activity.id
61 }
62 end
63
64 def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do
65 case Activity.get_by_id_with_object(notice_id) do
66 %Activity{local: true} = activity ->
67 %User{} = user = User.get_by_ap_id(activity.object.data["actor"])
68 meta = Metadata.build_tags(%{activity_id: notice_id, object: activity.object, user: user})
69
70 timeline =
71 activity.object.data["context"]
72 |> ActivityPub.fetch_activities_for_context(%{})
73 |> Enum.reverse()
74 |> Enum.map(&represent(&1, &1.object.id == activity.object.id))
75
76 render(conn, "conversation.html", %{activities: timeline, meta: meta})
77
78 _ ->
79 conn
80 |> put_status(404)
81 |> render("error.html", %{message: "Post not found.", meta: ""})
82 end
83 end
84
85 def show(%{assigns: %{username_or_id: username_or_id}} = conn, params) do
86 case User.get_cached_by_nickname_or_id(username_or_id) do
87 %User{} = user ->
88 meta = Metadata.build_tags(%{user: user})
89
90 timeline =
91 ActivityPub.fetch_user_activities(user, nil, Map.take(params, @page_keys))
92 |> Enum.map(&represent/1)
93
94 prev_page_id =
95 (params["min_id"] || params["max_id"]) &&
96 List.first(timeline) && List.first(timeline).id
97
98 next_page_id = List.last(timeline) && List.last(timeline).id
99
100 render(conn, "profile.html", %{
101 user: user,
102 timeline: timeline,
103 prev_page_id: prev_page_id,
104 next_page_id: next_page_id,
105 meta: meta
106 })
107
108 _ ->
109 conn
110 |> put_status(404)
111 |> render("error.html", %{message: "User not found.", meta: ""})
112 end
113 end
114
115 def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
116 do: assign(conn, :notice_id, notice_id)
117
118 def assign_id(%{path_info: ["users", user_id]} = conn, _opts),
119 do: assign(conn, :username_or_id, user_id)
120
121 def assign_id(conn, _opts), do: conn
122 end