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.Metadata
13 alias Pleroma.Web.Router.Helpers
15 plug(:put_layout, :static_fe)
16 plug(:put_view, Pleroma.Web.StaticFE.StaticFEView)
19 @page_keys ["max_id", "min_id", "limit", "since_id", "order"]
21 defp get_title(%Object{data: %{"name" => name}}) when is_binary(name),
24 defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary),
27 defp get_title(_), do: nil
29 def get_counts(%Activity{} = activity) do
30 %Object{data: data} = Object.normalize(activity)
33 likes: data["like_count"] || 0,
34 replies: data["repliesCount"] || 0,
35 announces: data["announcement_count"] || 0
39 def represent(%Activity{} = activity), do: represent(activity, false)
41 def represent(%Activity{object: %Object{data: data}} = activity, selected) do
42 {:ok, user} = User.get_or_fetch(activity.object.data["actor"])
46 true -> Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
47 _ -> data["url"] || data["external_url"] || data["id"]
52 title: get_title(activity.object),
53 content: data["content"] || nil,
54 attachment: data["attachment"],
56 published: data["published"],
57 sensitive: data["sensitive"],
59 counts: get_counts(activity),
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})
71 activity.object.data["context"]
72 |> ActivityPub.fetch_activities_for_context(%{})
74 |> Enum.map(&represent(&1, &1.object.id == activity.object.id))
76 render(conn, "conversation.html", %{activities: timeline, meta: meta})
81 |> render("error.html", %{message: "Post not found.", meta: ""})
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
88 meta = Metadata.build_tags(%{user: user})
91 ActivityPub.fetch_user_activities(user, nil, Map.take(params, @page_keys))
92 |> Enum.map(&represent/1)
95 (params["min_id"] || params["max_id"]) &&
96 List.first(timeline) && List.first(timeline).id
98 next_page_id = List.last(timeline) && List.last(timeline).id
100 render(conn, "profile.html", %{
103 prev_page_id: prev_page_id,
104 next_page_id: next_page_id,
111 |> render("error.html", %{message: "User not found.", meta: ""})
115 def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),
116 do: assign(conn, :notice_id, notice_id)
118 def assign_id(%{path_info: ["users", user_id]} = conn, _opts),
119 do: assign(conn, :username_or_id, user_id)
121 def assign_id(conn, _opts), do: conn