1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
16 plug(:put_layout, :static_fe)
17 plug(:put_view, Pleroma.Web.StaticFE.StaticFEView)
20 @page_keys ["max_id", "min_id", "limit", "since_id", "order"]
22 defp get_title(%Object{data: %{"name" => name}}) when is_binary(name),
25 defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary),
28 defp get_title(_), do: nil
30 def get_counts(%Activity{} = activity) do
31 %Object{data: data} = Object.normalize(activity)
34 likes: data["like_count"] || 0,
35 replies: data["repliesCount"] || 0,
36 announces: data["announcement_count"] || 0
40 def represent(%Activity{} = activity), do: represent(activity, false)
42 def represent(%Activity{object: %Object{data: data}} = activity, selected) do
43 {:ok, user} = User.get_or_fetch(activity.object.data["actor"])
47 true -> Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
48 _ -> data["url"] || data["external_url"] || data["id"]
53 title: get_title(activity.object),
54 content: data["content"] || nil,
55 attachment: data["attachment"],
57 published: data["published"],
58 sensitive: data["sensitive"],
60 counts: get_counts(activity),
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})
73 activity.object.data["context"]
74 |> ActivityPub.fetch_activities_for_context(%{})
76 |> Enum.map(&represent(&1, &1.object.id == activity.object.id))
78 render(conn, "conversation.html", %{activities: timeline, meta: meta})
80 %Activity{object: %Object{data: data}} ->
83 |> redirect(external: data["url"] || data["external_url"] || data["id"])
88 |> render("error.html", %{message: "Post not found.", meta: ""})
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
95 meta = Metadata.build_tags(%{user: user})
98 ActivityPub.fetch_user_activities(user, nil, Map.take(params, @page_keys))
99 |> Enum.map(&represent/1)
102 (params["min_id"] || params["max_id"]) &&
103 List.first(timeline) && List.first(timeline).id
105 next_page_id = List.last(timeline) && List.last(timeline).id
107 render(conn, "profile.html", %{
110 prev_page_id: prev_page_id,
111 next_page_id: next_page_id,
118 |> render("error.html", %{message: "User not found.", meta: ""})
122 def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
123 do: assign(conn, :notice_id, notice_id)
125 def assign_id(%{path_info: ["users", user_id]} = conn, _opts),
126 do: assign(conn, :username_or_id, user_id)
128 def assign_id(conn, _opts), do: conn