First
[anni] / lib / pleroma / web / embed_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.EmbedController do
6   use Pleroma.Web, :controller
7
8   alias Pleroma.Activity
9   alias Pleroma.Object
10   alias Pleroma.User
11
12   alias Pleroma.Web.ActivityPub.Visibility
13
14   plug(:put_layout, :embed)
15
16   def show(conn, %{"id" => id}) do
17     with %Activity{local: true} = activity <-
18            Activity.get_by_id_with_object(id),
19          true <- Visibility.is_public?(activity.object) do
20       {:ok, author} = User.get_or_fetch(activity.object.data["actor"])
21
22       conn
23       |> delete_resp_header("x-frame-options")
24       |> delete_resp_header("content-security-policy")
25       |> render("show.html",
26         activity: activity,
27         author: User.sanitize_html(author),
28         counts: get_counts(activity)
29       )
30     end
31   end
32
33   defp get_counts(%Activity{} = activity) do
34     %Object{data: data} = Object.normalize(activity, fetch: false)
35
36     %{
37       likes: Map.get(data, "like_count", 0),
38       replies: Map.get(data, "repliesCount", 0),
39       announces: Map.get(data, "announcement_count", 0)
40     }
41   end
42 end