static fe: use a generic activity representer to render activities
[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 defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
21
22 defp set_object(data, %Object{} = object), do: Map.put(data, :object, object)
23
24 defp set_title(data, %Object{data: %{"name" => name}}) when is_binary(name),
25 do: Map.put(data, :title, name)
26
27 defp set_title(data, %Object{data: %{"summary" => summary}}) when is_binary(summary),
28 do: Map.put(data, :title, summary)
29
30 defp set_title(data, _), do: Map.put(data, :title, nil)
31
32 defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(content),
33 do: Map.put(data, :content, content)
34
35 defp set_content(data, _), do: Map.put(data, :content, nil)
36
37 # TODO: attachments
38 defp set_attachments(data, _), do: Map.put(data, :attachments, [])
39
40 def represent(activity_id) do
41 with %Activity{data: %{"type" => "Create"}} = activity <- Activity.get_by_id(activity_id),
42 true <- Visibility.is_public?(activity),
43 %Object{} = object <- Object.normalize(activity.data["object"]),
44 %User{} = user <- User.get_or_fetch(activity.data["actor"]),
45 data <- prepare_activity(user, object) do
46 {:ok, data}
47 else
48 e ->
49 {:error, e}
50 end
51 end
52 end