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, %Activity{} = activity) do
13 object = Object.normalize(activity.data["object"])
19 |> set_content(object)
20 |> set_link(activity.id)
21 |> set_published(object)
22 |> set_sensitive(object)
23 |> set_attachment(object.data["attachment"])
24 |> set_attachments(object)
27 defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
29 defp set_object(data, %Object{} = object), do: Map.put(data, :object, object)
31 defp set_title(data, %Object{data: %{"name" => name}}) when is_binary(name),
32 do: Map.put(data, :title, name)
34 defp set_title(data, %Object{data: %{"summary" => summary}}) when is_binary(summary),
35 do: Map.put(data, :title, summary)
37 defp set_title(data, _), do: Map.put(data, :title, nil)
39 defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(content),
40 do: Map.put(data, :content, content)
42 defp set_content(data, _), do: Map.put(data, :content, nil)
44 defp set_attachment(data, attachment), do: Map.put(data, :attachment, attachment)
46 defp set_link(data, activity_id),
47 do: Map.put(data, :link, Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity_id))
49 defp set_published(data, %Object{data: %{"published" => published}}),
50 do: Map.put(data, :published, published)
52 defp set_sensitive(data, %Object{data: %{"sensitive" => sensitive}}),
53 do: Map.put(data, :sensitive, sensitive)
56 defp set_attachments(data, _), do: Map.put(data, :attachments, [])
58 def represent(activity_id) do
59 with %Activity{data: %{"type" => "Create"}} = activity <-
60 Activity.get_by_id_with_object(activity_id),
61 true <- Visibility.is_public?(activity),
62 {:ok, %User{} = user} <- User.get_or_fetch(activity.data["actor"]) do
63 {:ok, prepare_activity(user, activity)}