Remove activity/user representer; move logic to controller.

This commit is contained in:
Phil Hagelberg 2019-10-29 20:30:43 -07:00
parent 41fde63def
commit 33a26b61c3
3 changed files with 46 additions and 114 deletions

View File

@ -1,69 +0,0 @@
# 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
alias Pleroma.Web.Router.Helpers
def prepare_activity(%User{} = user, %Activity{} = activity) do
object = Object.normalize(activity.data["object"])
%{}
|> set_user(user)
|> set_object(object)
|> set_title(object)
|> set_content(object)
|> set_link(activity.id)
|> set_published(object)
|> set_sensitive(object)
|> set_attachment(object.data["attachment"])
|> 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)
defp set_attachment(data, attachment), do: Map.put(data, :attachment, attachment)
defp set_link(data, activity_id),
do: Map.put(data, :link, Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity_id))
defp set_published(data, %Object{data: %{"published" => published}}),
do: Map.put(data, :published, published)
defp set_sensitive(data, %Object{data: %{"sensitive" => sensitive}}),
do: Map.put(data, :sensitive, sensitive)
# 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_with_object(activity_id),
true <- Visibility.is_public?(activity),
{:ok, %User{} = user} <- User.get_or_fetch(activity.data["actor"]) do
{:ok, prepare_activity(user, activity)}
else
{:error, reason} -> {:error, reason}
_error -> {:error, "Not found"}
end
end
end

View File

@ -5,32 +5,65 @@
defmodule Pleroma.Web.StaticFE.StaticFEController do
use Pleroma.Web, :controller
alias Pleroma.Activity
alias Pleroma.Object
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.StaticFE.ActivityRepresenter
alias Pleroma.Web.StaticFE.UserRepresenter
alias Pleroma.Web.Router.Helpers
plug(:put_layout, :static_fe)
plug(:put_view, Pleroma.Web.StaticFE.StaticFEView)
plug(:assign_id)
action_fallback(:not_found)
defp get_title(%Object{data: %{"name" => name}}) when is_binary(name),
do: name
defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary),
do: summary
defp get_title(_), do: nil
def represent(%Activity{} = activity, %User{} = user, selected) do
%{
user: user,
title: get_title(activity.object),
content: activity.object.data["content"] || nil,
attachment: activity.object.data["attachment"],
link: Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity.id),
published: activity.object.data["published"],
sensitive: activity.object.data["sensitive"],
selected: selected
}
end
def represent(%Activity{} = activity, selected) do
{:ok, user} = User.get_or_fetch(activity.data["actor"])
represent(activity, user, selected)
end
def show_notice(%{assigns: %{notice_id: notice_id}} = conn, _params) do
with {:ok, data} <- ActivityRepresenter.represent(notice_id) do
context = data.object.data["context"]
activity = Activity.get_by_id_with_object(notice_id)
context = activity.object.data["context"]
activities = ActivityPub.fetch_activities_for_context(context, %{})
activities =
for a <- Enum.reverse(ActivityPub.fetch_activities_for_context(context, %{})) do
ActivityRepresenter.prepare_activity(data.user, a)
|> Map.put(:selected, a.object.id == data.object.id)
end
represented =
for a <- Enum.reverse(activities) do
represent(activity, a.object.id == activity.object.id)
end
render(conn, "conversation.html", activities: activities)
end
render(conn, "conversation.html", activities: represented)
end
def show_user(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do
{:ok, data} = UserRepresenter.represent(username_or_id)
render(conn, "profile.html", %{user: data.user, timeline: data.timeline})
%User{} = user = User.get_cached_by_nickname_or_id(username_or_id)
timeline =
for activity <- ActivityPub.fetch_user_activities(user, nil, %{}) do
represent(activity, user, false)
end
render(conn, "profile.html", %{user: user, timeline: timeline})
end
def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts),

View File

@ -1,32 +0,0 @@
# 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.UserRepresenter do
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.StaticFE.ActivityRepresenter
def prepare_user(%User{} = user) do
%{}
|> set_user(user)
|> set_timeline(user)
end
defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
defp set_timeline(data, %User{} = user) do
activities =
ActivityPub.fetch_user_activities(user, nil, %{})
|> Enum.map(fn activity -> ActivityRepresenter.prepare_activity(user, activity) end)
Map.put(data, :timeline, activities)
end
def represent(username_or_id) do
case User.get_cached_by_nickname_or_id(username_or_id) do
%User{} = user -> {:ok, prepare_user(user)}
nil -> {:error, "User not found"}
end
end
end