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
10 alias Pleroma.Web.Router.Helpers
12 def prepare_activity(%User{} = user, %Object{} = object, activity_id) do
17 |> set_content(object)
18 |> set_link(activity_id)
19 |> set_published(object)
20 |> set_attachments(object)
23 def prepare_activity(%User{} = user, %Activity{} = activity),
24 do: prepare_activity(user, Object.normalize(activity.data["object"]), activity.id)
26 defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
28 defp set_object(data, %Object{} = object), do: Map.put(data, :object, object)
30 defp set_title(data, %Object{data: %{"name" => name}}) when is_binary(name),
31 do: Map.put(data, :title, name)
33 defp set_title(data, %Object{data: %{"summary" => summary}}) when is_binary(summary),
34 do: Map.put(data, :title, summary)
36 defp set_title(data, _), do: Map.put(data, :title, nil)
38 defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(content),
39 do: Map.put(data, :content, content)
41 defp set_content(data, _), do: Map.put(data, :content, nil)
43 defp set_link(data, activity_id),
44 do: Map.put(data, :link, Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity_id))
46 defp set_published(data, %Object{data: %{"published" => published}}),
47 do: Map.put(data, :published, published)
50 defp set_attachments(data, _), do: Map.put(data, :attachments, [])
52 def represent(activity_id) do
53 with %Activity{data: %{"type" => "Create"}} = activity <- Activity.get_by_id(activity_id),
54 true <- Visibility.is_public?(activity),
55 %Object{} = object <- Object.normalize(activity.data["object"]),
56 %User{} = user <- User.get_or_fetch(activity.data["actor"]),
57 data <- prepare_activity(user, object, activity_id) do