1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Activity.Ir.Topics do
7 alias Pleroma.Web.ActivityPub.Visibility
9 def get_activity_topics(activity) do
11 |> Object.normalize(fetch: false)
12 |> generate_topics(activity)
16 defp generate_topics(%{data: %{"type" => "ChatMessage"}}, %{data: %{"type" => "Delete"}}) do
17 ["user", "user:pleroma_chat"]
20 defp generate_topics(%{data: %{"type" => "ChatMessage"}}, %{data: %{"type" => "Create"}}) do
24 defp generate_topics(%{data: %{"type" => "Answer"}}, _) do
28 defp generate_topics(object, activity) do
29 ["user", "list"] ++ visibility_tags(object, activity)
32 defp visibility_tags(object, %{data: %{"type" => type}} = activity) when type != "Announce" do
33 case Visibility.get_visibility(activity) do
36 ["public", "public:local"]
40 |> item_creation_tags(object, activity)
44 |> item_creation_tags(object, activity)
54 defp visibility_tags(_object, _activity) do
58 defp item_creation_tags(tags, object, %{data: %{"type" => "Create"}} = activity) do
60 remote_topics(activity) ++ hashtags_to_topics(object) ++ attachment_topics(object, activity)
63 defp item_creation_tags(tags, _, _) do
67 defp hashtags_to_topics(object) do
70 |> Enum.map(fn hashtag -> "hashtag:" <> hashtag end)
73 defp remote_topics(%{local: true}), do: []
75 defp remote_topics(%{actor: actor}) when is_binary(actor),
76 do: ["public:remote:" <> URI.parse(actor).host]
78 defp remote_topics(_), do: []
80 defp attachment_topics(%{data: %{"attachment" => []}}, _act), do: []
82 defp attachment_topics(_object, %{local: true} = activity) do
83 case Visibility.get_visibility(activity) do
85 ["public:media", "public:local:media"]
88 ["public:local:media"]
95 defp attachment_topics(_object, %{actor: actor}) when is_binary(actor),
96 do: ["public:media", "public:remote:media:" <> URI.parse(actor).host]
98 defp attachment_topics(_object, _act), do: ["public:media"]