static fe: use a generic activity representer to render activities
This commit is contained in:
parent
ff8d0902f3
commit
8f08da750a
2 changed files with 55 additions and 10 deletions
52
lib/pleroma/web/static_fe/activity_representer.ex
Normal file
52
lib/pleroma/web/static_fe/activity_representer.ex
Normal file
|
@ -0,0 +1,52 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.StaticFE.ActivityRepresenter do
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.Object
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ActivityPub.Visibility
|
||||
|
||||
def prepare_activity(%User{} = user, %Object{} = object) do
|
||||
%{}
|
||||
|> set_user(user)
|
||||
|> set_object(object)
|
||||
|> set_title(object)
|
||||
|> set_content(object)
|
||||
|> set_attachments(object)
|
||||
end
|
||||
|
||||
defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
|
||||
|
||||
defp set_object(data, %Object{} = object), do: Map.put(data, :object, object)
|
||||
|
||||
defp set_title(data, %Object{data: %{"name" => name}}) when is_binary(name),
|
||||
do: Map.put(data, :title, name)
|
||||
|
||||
defp set_title(data, %Object{data: %{"summary" => summary}}) when is_binary(summary),
|
||||
do: Map.put(data, :title, summary)
|
||||
|
||||
defp set_title(data, _), do: Map.put(data, :title, nil)
|
||||
|
||||
defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(content),
|
||||
do: Map.put(data, :content, content)
|
||||
|
||||
defp set_content(data, _), do: Map.put(data, :content, nil)
|
||||
|
||||
# TODO: attachments
|
||||
defp set_attachments(data, _), do: Map.put(data, :attachments, [])
|
||||
|
||||
def represent(activity_id) do
|
||||
with %Activity{data: %{"type" => "Create"}} = activity <- Activity.get_by_id(activity_id),
|
||||
true <- Visibility.is_public?(activity),
|
||||
%Object{} = object <- Object.normalize(activity.data["object"]),
|
||||
%User{} = user <- User.get_or_fetch(activity.data["actor"]),
|
||||
data <- prepare_activity(user, object) do
|
||||
{:ok, data}
|
||||
else
|
||||
e ->
|
||||
{:error, e}
|
||||
end
|
||||
end
|
||||
end
|
|
@ -5,24 +5,17 @@
|
|||
defmodule Pleroma.Web.StaticFE.StaticFEController do
|
||||
use Pleroma.Web, :controller
|
||||
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.Object
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ActivityPub.Visibility
|
||||
alias Pleroma.Web.StaticFE.ActivityRepresenter
|
||||
|
||||
require Logger
|
||||
|
||||
def show_notice(conn, %{"notice_id" => notice_id}) do
|
||||
with %Activity{} = activity <- Repo.get(Activity, notice_id),
|
||||
true <- Visibility.is_public?(activity),
|
||||
%User{} = user <- User.get_or_fetch(activity.data["actor"]),
|
||||
%Object{} = object <- Object.normalize(activity.data["object"]) do
|
||||
with {:ok, data} <- ActivityRepresenter.represent(notice_id) do
|
||||
conn
|
||||
|> put_layout(:static_fe)
|
||||
|> put_status(200)
|
||||
|> put_view(Pleroma.Web.StaticFE.StaticFEView)
|
||||
|> render("notice.html", %{notice: activity, object: object, user: user})
|
||||
|> render("notice.html", data)
|
||||
else
|
||||
_ ->
|
||||
conn
|
||||
|
|
Loading…
Reference in a new issue