static fe: reformat activity representer
[akkoma] / lib / pleroma / web / static_fe / activity_representer.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.StaticFE.ActivityRepresenter do
6 alias Pleroma.Activity
7 alias Pleroma.Object
8 alias Pleroma.User
9 alias Pleroma.Web.ActivityPub.Visibility
10
11 def prepare_activity(%User{} = user, %Object{} = object) do
12 %{}
13 |> set_user(user)
14 |> set_object(object)
15 |> set_title(object)
16 |> set_content(object)
17 |> set_attachments(object)
18 end
19
20 def prepare_activity(%User{} = user, %Activity{} = activity),
21 do: prepare_activity(user, Object.normalize(activity.data["object"]))
22
23 defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
24
25 defp set_object(data, %Object{} = object), do: Map.put(data, :object, object)
26
27 defp set_title(data, %Object{data: %{"name" => name}}) when is_binary(name),
28 do: Map.put(data, :title, name)
29
30 defp set_title(data, %Object{data: %{"summary" => summary}}) when is_binary(summary),
31 do: Map.put(data, :title, summary)
32
33 defp set_title(data, _), do: Map.put(data, :title, nil)
34
35 defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(content),
36 do: Map.put(data, :content, content)
37
38 defp set_content(data, _), do: Map.put(data, :content, nil)
39
40 # TODO: attachments
41 defp set_attachments(data, _), do: Map.put(data, :attachments, [])
42
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
49 {:ok, data}
50 else
51 e ->
52 {:error, e}
53 end
54 end
55 end