1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.StaticFE.ActivityRepresenter do
9 alias Pleroma.Web.ActivityPub.Visibility
11 def prepare_activity(%User{} = user, %Object{} = object) do
16 |> set_content(object)
17 |> set_attachments(object)
20 def prepare_activity(%User{} = user, %Activity{} = activity), do:
21 prepare_activity(user, Object.normalize(activity.data["object"]))
23 defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
25 defp set_object(data, %Object{} = object), do: Map.put(data, :object, object)
27 defp set_title(data, %Object{data: %{"name" => name}}) when is_binary(name),
28 do: Map.put(data, :title, name)
30 defp set_title(data, %Object{data: %{"summary" => summary}}) when is_binary(summary),
31 do: Map.put(data, :title, summary)
33 defp set_title(data, _), do: Map.put(data, :title, nil)
35 defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(content),
36 do: Map.put(data, :content, content)
38 defp set_content(data, _), do: Map.put(data, :content, nil)
41 defp set_attachments(data, _), do: Map.put(data, :attachments, [])
43 def represent(activity_id) do
44 with %Activity{data: %{"type" => "Create"}} = activity <- Activity.get_by_id(activity_id),
45 true <- Visibility.is_public?(activity),
46 %Object{} = object <- Object.normalize(activity.data["object"]),
47 %User{} = user <- User.get_or_fetch(activity.data["actor"]),
48 data <- prepare_activity(user, object) do