1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.StaticFE.StaticFEController do
6 use Pleroma.Web, :controller
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Visibility
13 alias Pleroma.Web.Metadata
14 alias Pleroma.Web.Router.Helpers
18 @page_keys ["max_id", "min_id", "limit", "since_id", "order"]
20 @doc "Renders requested local public activity or public activities of requested user"
21 def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do
22 with %Activity{local: true} = activity <-
23 Activity.get_by_id_with_object(notice_id),
24 true <- Visibility.public?(activity.object),
25 {_, true} <- {:visible?, Visibility.visible_for_user?(activity, _reading_user = nil)},
26 %User{} = user <- User.get_by_ap_id(activity.object.data["actor"]) do
27 url = Helpers.url(conn) <> conn.request_path
30 Metadata.build_tags(%{
31 activity_id: notice_id,
32 object: activity.object,
38 activity.object.data["context"]
39 |> ActivityPub.fetch_activities_for_context(%{})
41 |> Enum.map(&represent(&1, &1.object.id == activity.object.id))
43 render(conn, "conversation.html", %{activities: timeline, meta: meta})
45 %Activity{object: %Object{data: data}} ->
48 |> redirect(external: data["url"] || data["external_url"] || data["id"])
51 not_found(conn, "Post not found.")
55 def show(%{assigns: %{username_or_id: username_or_id}} = conn, params) do
56 with {_, %User{local: true} = user} <-
57 {:fetch_user, User.get_cached_by_nickname_or_id(username_or_id)},
58 {_, :visible} <- {:visibility, User.visible_for(user, _reading_user = nil)} do
59 meta = Metadata.build_tags(%{user: user})
63 |> Map.take(@page_keys)
64 |> Map.new(fn {k, v} -> {String.to_existing_atom(k), v} end)
68 |> ActivityPub.fetch_user_activities(_reading_user = nil, params)
69 |> Enum.map(&represent/1)
72 (params["min_id"] || params["max_id"]) &&
73 List.first(timeline) && List.first(timeline).id
75 next_page_id = List.last(timeline) && List.last(timeline).id
77 render(conn, "profile.html", %{
78 user: User.sanitize_html(user),
80 prev_page_id: prev_page_id,
81 next_page_id: next_page_id,
86 not_found(conn, "User not found.")
90 def show(%{assigns: %{object_id: _}} = conn, _params) do
91 url = Helpers.url(conn) <> conn.request_path
93 case Activity.get_create_by_object_ap_id_with_object(url) do
94 %Activity{} = activity ->
95 to = Helpers.o_status_path(Pleroma.Web.Endpoint, :notice, activity)
96 redirect(conn, to: to)
99 not_found(conn, "Post not found.")
103 def show(%{assigns: %{activity_id: _}} = conn, _params) do
104 url = Helpers.url(conn) <> conn.request_path
106 case Activity.get_by_ap_id(url) do
107 %Activity{} = activity ->
108 to = Helpers.o_status_path(Pleroma.Web.Endpoint, :notice, activity)
109 redirect(conn, to: to)
112 not_found(conn, "Post not found.")
116 defp get_title(%Object{data: %{"name" => name}}) when is_binary(name),
119 defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary),
122 defp get_title(_), do: nil
124 defp not_found(conn, message) do
127 |> render("error.html", %{message: message, meta: ""})
130 defp get_counts(%Activity{} = activity) do
131 %Object{data: data} = Object.normalize(activity, fetch: false)
134 likes: data["like_count"] || 0,
135 replies: data["repliesCount"] || 0,
136 announces: data["announcement_count"] || 0
140 defp represent(%Activity{} = activity), do: represent(activity, false)
142 defp represent(%Activity{object: %Object{data: data}} = activity, selected) do
143 {:ok, user} = User.get_or_fetch(activity.object.data["actor"])
147 true -> Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
148 _ -> data["url"] || data["external_url"] || data["id"]
152 if data["content"] do
154 |> Pleroma.HTML.filter_tags()
155 |> Pleroma.Emoji.Formatter.emojify(Map.get(data, "emoji", %{}))
161 user: User.sanitize_html(user),
162 title: get_title(activity.object),
164 attachment: data["attachment"],
166 published: data["published"],
167 sensitive: data["sensitive"],
169 counts: get_counts(activity),
174 defp assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
175 do: assign(conn, :notice_id, notice_id)
177 defp assign_id(%{path_info: ["users", user_id]} = conn, _opts),
178 do: assign(conn, :username_or_id, user_id)
180 defp assign_id(%{path_info: ["objects", object_id]} = conn, _opts),
181 do: assign(conn, :object_id, object_id)
183 defp assign_id(%{path_info: ["activities", activity_id]} = conn, _opts),
184 do: assign(conn, :activity_id, activity_id)
186 defp assign_id(conn, _opts), do: conn