2020-03-20 11:19:34 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-03-20 11:19:34 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.EmbedController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.User
|
|
|
|
|
|
|
|
alias Pleroma.Web.ActivityPub.Visibility
|
|
|
|
|
|
|
|
def show(conn, %{"id" => id}) do
|
2023-07-17 12:18:21 -06:00
|
|
|
with {:activity, %Activity{} = activity} <-
|
|
|
|
{:activity, Activity.get_by_id_with_object(id)},
|
|
|
|
{:local, true} <- {:local, activity.local},
|
|
|
|
{:visible, true} <- {:visible, Visibility.visible_for_user?(activity, nil)} do
|
2020-03-20 11:19:34 -06:00
|
|
|
{:ok, author} = User.get_or_fetch(activity.object.data["actor"])
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> delete_resp_header("x-frame-options")
|
|
|
|
|> delete_resp_header("content-security-policy")
|
2023-07-17 12:18:21 -06:00
|
|
|
|> put_view(Pleroma.Web.EmbedView)
|
2020-03-20 11:19:34 -06:00
|
|
|
|> render("show.html",
|
|
|
|
activity: activity,
|
|
|
|
author: User.sanitize_html(author),
|
|
|
|
counts: get_counts(activity)
|
|
|
|
)
|
2023-07-17 12:18:21 -06:00
|
|
|
else
|
|
|
|
{:activity, _} ->
|
|
|
|
render_error(conn, :not_found, "Post not found")
|
|
|
|
|
|
|
|
{:local, false} ->
|
|
|
|
render_error(conn, :unauthorized, "Federated posts cannot be embedded")
|
|
|
|
|
|
|
|
{:visible, false} ->
|
|
|
|
render_error(conn, :unauthorized, "Not authorized to view this post")
|
2020-03-20 11:19:34 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_counts(%Activity{} = activity) do
|
2021-01-04 05:38:31 -07:00
|
|
|
%Object{data: data} = Object.normalize(activity, fetch: false)
|
2020-03-20 11:19:34 -06:00
|
|
|
|
|
|
|
%{
|
|
|
|
likes: Map.get(data, "like_count", 0),
|
|
|
|
replies: Map.get(data, "repliesCount", 0),
|
|
|
|
announces: Map.get(data, "announcement_count", 0)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|