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-27 23:37:57 +00:00
|
|
|
def prepare_activity(%User{} = user, %Object{} = object, activity_id) do
|
2019-03-02 20:10:30 +00:00
|
|
|
%{}
|
|
|
|
|> set_user(user)
|
|
|
|
|> set_object(object)
|
|
|
|
|> set_title(object)
|
|
|
|
|> set_content(object)
|
2019-10-27 23:37:57 +00:00
|
|
|
|> set_link(activity_id)
|
|
|
|
|> set_published(object)
|
2019-03-02 20:10:30 +00:00
|
|
|
|> set_attachments(object)
|
|
|
|
end
|
|
|
|
|
2019-03-02 21:40:02 +00:00
|
|
|
def prepare_activity(%User{} = user, %Activity{} = activity),
|
2019-10-27 23:37:57 +00:00
|
|
|
do: prepare_activity(user, Object.normalize(activity.data["object"]), activity.id)
|
2019-03-02 21:12:38 +00:00
|
|
|
|
2019-03-02 20:10:30 +00:00
|
|
|
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-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-03-02 20:10:30 +00:00
|
|
|
# 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"]),
|
2019-10-27 23:37:57 +00:00
|
|
|
data <- prepare_activity(user, object, activity_id) do
|
2019-03-02 20:10:30 +00:00
|
|
|
{:ok, data}
|
|
|
|
else
|
|
|
|
e ->
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|