e383b8415f96903207e97d8b7ec71939646fca44
[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 alias Pleroma.Web.Router.Helpers
11
12 def prepare_activity(%User{} = user, %Object{} = object, activity_id) do
13 %{}
14 |> set_user(user)
15 |> set_object(object)
16 |> set_title(object)
17 |> set_content(object)
18 |> set_link(activity_id)
19 |> set_published(object)
20 |> set_attachments(object)
21 end
22
23 def prepare_activity(%User{} = user, %Activity{} = activity),
24 do: prepare_activity(user, Object.normalize(activity.data["object"]), activity.id)
25
26 defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
27
28 defp set_object(data, %Object{} = object), do: Map.put(data, :object, object)
29
30 defp set_title(data, %Object{data: %{"name" => name}}) when is_binary(name),
31 do: Map.put(data, :title, name)
32
33 defp set_title(data, %Object{data: %{"summary" => summary}}) when is_binary(summary),
34 do: Map.put(data, :title, summary)
35
36 defp set_title(data, _), do: Map.put(data, :title, nil)
37
38 defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(content),
39 do: Map.put(data, :content, content)
40
41 defp set_content(data, _), do: Map.put(data, :content, nil)
42
43 defp set_link(data, activity_id),
44 do: Map.put(data, :link, Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity_id))
45
46 defp set_published(data, %Object{data: %{"published" => published}}),
47 do: Map.put(data, :published, published)
48
49 # TODO: attachments
50 defp set_attachments(data, _), do: Map.put(data, :attachments, [])
51
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
58 {:ok, data}
59 else
60 e ->
61 {:error, e}
62 end
63 end
64 end