fe2e8cb5c900d7b84f0e92bc1ba42a74c85d685b
[akkoma] / lib / pleroma / activity / ir / topics.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Activity.Ir.Topics do
6 alias Pleroma.Object
7 alias Pleroma.Web.ActivityPub.Visibility
8
9 def get_activity_topics(activity) do
10 activity
11 |> Object.normalize()
12 |> generate_topics(activity)
13 |> List.flatten()
14 end
15
16 defp generate_topics(%{data: %{"type" => "Answer"}}, _) do
17 []
18 end
19
20 defp generate_topics(object, activity) do
21 ["user", "list"] ++ visibility_tags(object, activity)
22 end
23
24 defp visibility_tags(object, activity) do
25 case Visibility.get_visibility(activity) do
26 "public" ->
27 if activity.local do
28 ["public", "public:local"]
29 else
30 ["public"]
31 end
32 |> item_creation_tags(object, activity)
33
34 "direct" ->
35 ["direct"]
36
37 _ ->
38 []
39 end
40 end
41
42 defp item_creation_tags(tags, object, %{data: %{"type" => "Create"}} = activity) do
43 tags ++
44 remote_topics(activity) ++ hashtags_to_topics(object) ++ attachment_topics(object, activity)
45 end
46
47 defp item_creation_tags(tags, _, _) do
48 tags
49 end
50
51 defp hashtags_to_topics(%{data: %{"tag" => tags}}) do
52 tags
53 |> Enum.filter(&is_bitstring(&1))
54 |> Enum.map(fn tag -> "hashtag:" <> tag end)
55 end
56
57 defp hashtags_to_topics(_), do: []
58
59 defp remote_topics(%{local: true}), do: []
60
61 defp remote_topics(%{actor: actor}) when is_binary(actor),
62 do: ["public:remote:" <> URI.parse(actor).host]
63
64 defp remote_topics(_), do: []
65
66 defp attachment_topics(%{data: %{"attachment" => []}}, _act), do: []
67
68 defp attachment_topics(_object, %{local: true}), do: ["public:media", "public:local:media"]
69
70 defp attachment_topics(_object, %{actor: actor}) when is_binary(actor),
71 do: ["public:media", "public:remote:media:" <> URI.parse(actor).host]
72
73 defp attachment_topics(_object, _act), do: ["public:media"]
74 end