Fix activity_representer to work with User.get_or_fetch returning tuple.
[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, %Activity{} = activity) do
13 object = Object.normalize(activity.data["object"])
14
15 %{}
16 |> set_user(user)
17 |> set_object(object)
18 |> set_title(object)
19 |> set_content(object)
20 |> set_link(activity.id)
21 |> set_published(object)
22 |> set_attachments(object)
23 end
24
25 defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
26
27 defp set_object(data, %Object{} = object), do: Map.put(data, :object, object)
28
29 defp set_title(data, %Object{data: %{"name" => name}}) when is_binary(name),
30 do: Map.put(data, :title, name)
31
32 defp set_title(data, %Object{data: %{"summary" => summary}}) when is_binary(summary),
33 do: Map.put(data, :title, summary)
34
35 defp set_title(data, _), do: Map.put(data, :title, nil)
36
37 defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(content),
38 do: Map.put(data, :content, content)
39
40 defp set_content(data, _), do: Map.put(data, :content, nil)
41
42 defp set_link(data, activity_id),
43 do: Map.put(data, :link, Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity_id))
44
45 defp set_published(data, %Object{data: %{"published" => published}}),
46 do: Map.put(data, :published, published)
47
48 # TODO: attachments
49 defp set_attachments(data, _), do: Map.put(data, :attachments, [])
50
51 def represent(activity_id) do
52 with %Activity{data: %{"type" => "Create"}} = activity <- Activity.get_by_id(activity_id),
53 true <- Visibility.is_public?(activity),
54 {:ok, %User{} = user} <- User.get_or_fetch(activity.data["actor"]) do
55 {:ok, prepare_activity(user, activity)}
56 else
57 e ->
58 {:error, e}
59 end
60 end
61 end