8249cbe27127791079e88f1f0086b5f220725c12
[anni] / lib / pleroma / activity / ir / topics.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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(fetch: false)
12     |> generate_topics(activity)
13     |> List.flatten()
14   end
15
16   defp generate_topics(%{data: %{"type" => "ChatMessage"}}, %{data: %{"type" => "Delete"}}) do
17     ["user", "user:pleroma_chat"]
18   end
19
20   defp generate_topics(%{data: %{"type" => "ChatMessage"}}, %{data: %{"type" => "Create"}}) do
21     []
22   end
23
24   defp generate_topics(%{data: %{"type" => "Answer"}}, _) do
25     []
26   end
27
28   defp generate_topics(object, activity) do
29     ["user", "list"] ++ visibility_tags(object, activity)
30   end
31
32   defp visibility_tags(object, %{data: %{"type" => type}} = activity) when type != "Announce" do
33     case Visibility.get_visibility(activity) do
34       "public" ->
35         if activity.local do
36           ["public", "public:local"]
37         else
38           ["public"]
39         end
40         |> item_creation_tags(object, activity)
41
42       "local" ->
43         ["public:local"]
44         |> item_creation_tags(object, activity)
45
46       "direct" ->
47         ["direct"]
48
49       _ ->
50         []
51     end
52   end
53
54   defp visibility_tags(_object, _activity) do
55     []
56   end
57
58   defp item_creation_tags(tags, object, %{data: %{"type" => "Create"}} = activity) do
59     tags ++
60       remote_topics(activity) ++ hashtags_to_topics(object) ++ attachment_topics(object, activity)
61   end
62
63   defp item_creation_tags(tags, _, _) do
64     tags
65   end
66
67   defp hashtags_to_topics(object) do
68     object
69     |> Object.hashtags()
70     |> Enum.map(fn hashtag -> "hashtag:" <> hashtag end)
71   end
72
73   defp remote_topics(%{local: true}), do: []
74
75   defp remote_topics(%{actor: actor}) when is_binary(actor),
76     do: ["public:remote:" <> URI.parse(actor).host]
77
78   defp remote_topics(_), do: []
79
80   defp attachment_topics(%{data: %{"attachment" => []}}, _act), do: []
81
82   defp attachment_topics(_object, %{local: true} = activity) do
83     case Visibility.get_visibility(activity) do
84       "public" ->
85         ["public:media", "public:local:media"]
86
87       "local" ->
88         ["public:local:media"]
89
90       _ ->
91         []
92     end
93   end
94
95   defp attachment_topics(_object, %{actor: actor}) when is_binary(actor),
96     do: ["public:media", "public:remote:media:" <> URI.parse(actor).host]
97
98   defp attachment_topics(_object, _act), do: ["public:media"]
99 end