# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Object do
Logger.debug("Backtrace: #{inspect(Process.info(:erlang.self(), :current_stacktrace))}")
end
- def normalize(_, fetch_remote \\ true, options \\ [])
+ def normalize(_, options \\ [fetch: false])
# If we pass an Activity to Object.normalize(), we can try to use the preloaded object.
# Use this whenever possible, especially when walking graphs in an O(N) loop!
- def normalize(%Object{} = object, _, _), do: object
- def normalize(%Activity{object: %Object{} = object}, _, _), do: object
+ def normalize(%Object{} = object, _), do: object
+ def normalize(%Activity{object: %Object{} = object}, _), do: object
# A hack for fake activities
- def normalize(%Activity{data: %{"object" => %{"fake" => true} = data}}, _, _) do
+ def normalize(%Activity{data: %{"object" => %{"fake" => true} = data}}, _) do
%Object{id: "pleroma:fake_object_id", data: data}
end
# No preloaded object
- def normalize(%Activity{data: %{"object" => %{"id" => ap_id}}}, fetch_remote, _) do
+ def normalize(%Activity{data: %{"object" => %{"id" => ap_id}}}, options) do
warn_on_no_object_preloaded(ap_id)
- normalize(ap_id, fetch_remote)
+ normalize(ap_id, options)
end
# No preloaded object
- def normalize(%Activity{data: %{"object" => ap_id}}, fetch_remote, _) do
+ def normalize(%Activity{data: %{"object" => ap_id}}, options) do
warn_on_no_object_preloaded(ap_id)
- normalize(ap_id, fetch_remote)
+ normalize(ap_id, options)
end
# Old way, try fetching the object through cache.
- def normalize(%{"id" => ap_id}, fetch_remote, _), do: normalize(ap_id, fetch_remote)
- def normalize(ap_id, false, _) when is_binary(ap_id), do: get_cached_by_ap_id(ap_id)
+ def normalize(%{"id" => ap_id}, options), do: normalize(ap_id, options)
- def normalize(ap_id, true, options) when is_binary(ap_id) do
- Fetcher.fetch_object_from_id!(ap_id, options)
+ def normalize(ap_id, options) when is_binary(ap_id) do
+ if Keyword.get(options, :fetch) do
+ Fetcher.fetch_object_from_id!(ap_id, options)
+ else
+ get_cached_by_ap_id(ap_id)
+ end
end
- def normalize(_, _, _), do: nil
+ def normalize(_, _), do: nil
# Owned objects can only be accessed by their owner
def authorize_access(%Object{data: %{"actor" => actor}}, %User{ap_id: ap_id}) do
end
def increase_vote_count(ap_id, name, actor) do
- with %Object{} = object <- Object.normalize(ap_id),
+ with %Object{} = object <- Object.normalize(ap_id, fetch: false),
"Question" <- object.data["type"] do
key = if poll_is_multiple?(object), do: "anyOf", else: "oneOf"
end
def replies(object, opts \\ []) do
- object = Object.normalize(object)
+ object = Object.normalize(object, fetch: false)
query =
Object
defp embedded_hashtags(_), do: []
- defp object_data_hashtags(%{"tag" => tags}) when is_list(tags) do
- # Note: AS2 map-type elements are ignored
- Enum.filter(tags, &is_bitstring(&1))
- end
-
- defp object_data_hashtags(_), do: []
+ def object_data_hashtags(%{"tag" => tags}) when is_list(tags) do
+ tags
+ |> Enum.filter(fn
+ %{"type" => "Hashtag"} = data -> Map.has_key?(data, "name")
+ plain_text when is_bitstring(plain_text) -> true
+ _ -> false
+ end)
+ |> Enum.map(fn
+ %{"name" => "#" <> hashtag} -> String.downcase(hashtag)
+ %{"name" => hashtag} -> String.downcase(hashtag)
+ hashtag when is_bitstring(hashtag) -> String.downcase(hashtag)
+ end)
+ |> Enum.uniq()
+ # Note: "" elements (plain text) might occur in `data.tag` for incoming objects
+ |> Enum.filter(&(&1 not in [nil, ""]))
+ end
+
+ def object_data_hashtags(_), do: []
end