2019-03-02 20:10:30 +00:00
|
|
|
# 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
|
2019-10-27 23:37:57 +00:00
|
|
|
alias Pleroma.Web.Router.Helpers
|
2019-03-02 20:10:30 +00:00
|
|
|
|
2019-10-28 02:02:19 +00:00
|
|
|
def prepare_activity(%User{} = user, %Activity{} = activity) do
|
|
|
|
object = Object.normalize(activity.data["object"])
|
|
|
|
|
2019-03-02 20:10:30 +00:00
|
|
|
%{}
|
|
|
|
|> set_user(user)
|
|
|
|
|> set_object(object)
|
|
|
|
|> set_title(object)
|
|
|
|
|> set_content(object)
|
2019-10-28 02:02:19 +00:00
|
|
|
|> set_link(activity.id)
|
2019-10-27 23:37:57 +00:00
|
|
|
|> set_published(object)
|
2019-10-29 02:05:45 +00:00
|
|
|
|> set_sensitive(object)
|
|
|
|
|> set_attachment(object.data["attachment"])
|
2019-03-02 20:10:30 +00:00
|
|
|
|> 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)
|
|
|
|
|
2019-10-29 02:05:45 +00:00
|
|
|
defp set_attachment(data, attachment), do: Map.put(data, :attachment, attachment)
|
|
|
|
|
2019-10-27 23:37:57 +00:00
|
|
|
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)
|
|
|
|
|
2019-10-29 02:05:45 +00:00
|
|
|
defp set_sensitive(data, %Object{data: %{"sensitive" => sensitive}}),
|
|
|
|
do: Map.put(data, :sensitive, sensitive)
|
|
|
|
|
2019-03-02 20:10:30 +00:00
|
|
|
# TODO: attachments
|
|
|
|
defp set_attachments(data, _), do: Map.put(data, :attachments, [])
|
|
|
|
|
|
|
|
def represent(activity_id) do
|
2019-10-29 02:05:45 +00:00
|
|
|
with %Activity{data: %{"type" => "Create"}} = activity <-
|
|
|
|
Activity.get_by_id_with_object(activity_id),
|
2019-03-02 20:10:30 +00:00
|
|
|
true <- Visibility.is_public?(activity),
|
2019-10-28 02:02:19 +00:00
|
|
|
{:ok, %User{} = user} <- User.get_or_fetch(activity.data["actor"]) do
|
|
|
|
{:ok, prepare_activity(user, activity)}
|
2019-03-02 20:10:30 +00:00
|
|
|
else
|
|
|
|
e ->
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|