1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Object do
11 alias Pleroma.Activity
14 alias Pleroma.Object.Fetcher
15 alias Pleroma.ObjectTombstone
18 alias Pleroma.Workers.AttachmentsCleanupWorker
22 @type t() :: %__MODULE__{}
24 @derive {Jason.Encoder, only: [:data]}
32 def with_joined_activity(query, activity_type \\ "Create", join_type \\ :inner) do
33 object_position = Map.get(query.aliases, :object, 0)
35 join(query, join_type, [{object, object_position}], a in Activity,
38 "COALESCE(?->'object'->>'id', ?->>'object') = (? ->> 'id') AND (?->>'type' = ?) ",
50 Object.change(%Object{}, %{data: data})
54 def change(struct, params \\ %{}) do
56 |> cast(params, [:data])
57 |> validate_required([:data])
58 |> unique_constraint(:ap_id, name: :objects_unique_apid_index)
61 def get_by_id(nil), do: nil
62 def get_by_id(id), do: Repo.get(Object, id)
64 def get_by_id_and_maybe_refetch(id, opts \\ []) do
65 %{updated_at: updated_at} = object = get_by_id(id)
68 NaiveDateTime.diff(NaiveDateTime.utc_now(), updated_at) > opts[:interval] do
69 case Fetcher.refetch_object(object) do
70 {:ok, %Object{} = object} ->
74 Logger.error("Couldn't refresh #{object.data["id"]}:\n#{inspect(e)}")
82 def get_by_ap_id(nil), do: nil
84 def get_by_ap_id(ap_id) do
85 Repo.one(from(object in Object, where: fragment("(?)->>'id' = ?", object.data, ^ap_id)))
89 Get a single attachment by it's name and href
91 @spec get_attachment_by_name_and_href(String.t(), String.t()) :: Object.t() | nil
92 def get_attachment_by_name_and_href(name, href) do
95 where: fragment("(?)->>'name' = ?", o.data, ^name),
96 where: fragment("(?)->>'href' = ?", o.data, ^href)
102 defp warn_on_no_object_preloaded(ap_id) do
103 "Object.normalize() called without preloaded object (#{inspect(ap_id)}). Consider preloading the object"
106 Logger.debug("Backtrace: #{inspect(Process.info(:erlang.self(), :current_stacktrace))}")
109 def normalize(_, fetch_remote \\ true, options \\ [])
111 # If we pass an Activity to Object.normalize(), we can try to use the preloaded object.
112 # Use this whenever possible, especially when walking graphs in an O(N) loop!
113 def normalize(%Object{} = object, _, _), do: object
114 def normalize(%Activity{object: %Object{} = object}, _, _), do: object
116 # A hack for fake activities
117 def normalize(%Activity{data: %{"object" => %{"fake" => true} = data}}, _, _) do
118 %Object{id: "pleroma:fake_object_id", data: data}
121 # No preloaded object
122 def normalize(%Activity{data: %{"object" => %{"id" => ap_id}}}, fetch_remote, _) do
123 warn_on_no_object_preloaded(ap_id)
124 normalize(ap_id, fetch_remote)
127 # No preloaded object
128 def normalize(%Activity{data: %{"object" => ap_id}}, fetch_remote, _) do
129 warn_on_no_object_preloaded(ap_id)
130 normalize(ap_id, fetch_remote)
133 # Old way, try fetching the object through cache.
134 def normalize(%{"id" => ap_id}, fetch_remote, _), do: normalize(ap_id, fetch_remote)
135 def normalize(ap_id, false, _) when is_binary(ap_id), do: get_cached_by_ap_id(ap_id)
137 def normalize(ap_id, true, options) when is_binary(ap_id) do
138 Fetcher.fetch_object_from_id!(ap_id, options)
141 def normalize(_, _, _), do: nil
143 # Owned objects can only be accessed by their owner
144 def authorize_access(%Object{data: %{"actor" => actor}}, %User{ap_id: ap_id}) do
152 # Legacy objects can be accessed by anybody
153 def authorize_access(%Object{}, %User{}), do: :ok
155 @spec get_cached_by_ap_id(String.t()) :: Object.t() | nil
156 def get_cached_by_ap_id(ap_id) do
157 key = "object:#{ap_id}"
159 with {:ok, nil} <- Cachex.get(:object_cache, key),
160 object when not is_nil(object) <- get_by_ap_id(ap_id),
161 {:ok, true} <- Cachex.put(:object_cache, key, object) do
164 {:ok, object} -> object
169 def context_mapping(context) do
170 Object.change(%Object{}, %{data: %{"id" => context}})
173 def make_tombstone(%Object{data: %{"id" => id, "type" => type}}, deleted \\ DateTime.utc_now()) do
182 def swap_object_with_tombstone(object) do
183 tombstone = make_tombstone(object)
186 |> Object.change(%{data: tombstone})
190 def delete(%Object{data: %{"id" => id}} = object) do
191 with {:ok, _obj} = swap_object_with_tombstone(object),
192 deleted_activity = Activity.delete_all_by_object_ap_id(id),
193 {:ok, _} <- invalid_object_cache(object) do
195 Config.get([:instance, :cleanup_attachments]),
196 %{"object" => object}
199 {:ok, object, deleted_activity}
203 @spec cleanup_attachments(boolean(), %{required(:object) => map()}) ::
204 {:ok, Oban.Job.t() | nil}
205 def cleanup_attachments(true, %{"object" => _} = params) do
206 AttachmentsCleanupWorker.enqueue("cleanup_attachments", params)
209 def cleanup_attachments(_, _), do: {:ok, nil}
211 def prune(%Object{data: %{"id" => _id}} = object) do
212 with {:ok, object} <- Repo.delete(object),
213 {:ok, _} <- invalid_object_cache(object) do
218 def invalid_object_cache(%Object{data: %{"id" => id}}) do
219 with {:ok, true} <- Cachex.del(:object_cache, "object:#{id}") do
220 Cachex.del(:web_resp_cache, URI.parse(id).path)
224 def set_cache(%Object{data: %{"id" => ap_id}} = object) do
225 Cachex.put(:object_cache, "object:#{ap_id}", object)
229 def update_and_set_cache(changeset) do
230 with {:ok, object} <- Repo.update(changeset) do
235 def increase_replies_count(ap_id) do
237 |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
243 safe_jsonb_set(?, '{repliesCount}',
244 (coalesce((?->>'repliesCount')::int, 0) + 1)::varchar::jsonb, true)
251 |> Repo.update_all([])
253 {1, [object]} -> set_cache(object)
254 _ -> {:error, "Not found"}
258 defp poll_is_multiple?(%Object{data: %{"anyOf" => [_ | _]}}), do: true
260 defp poll_is_multiple?(_), do: false
262 def decrease_replies_count(ap_id) do
264 |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
270 safe_jsonb_set(?, '{repliesCount}',
271 (greatest(0, (?->>'repliesCount')::int - 1))::varchar::jsonb, true)
278 |> Repo.update_all([])
280 {1, [object]} -> set_cache(object)
281 _ -> {:error, "Not found"}
285 def increase_vote_count(ap_id, name, actor) do
286 with %Object{} = object <- Object.normalize(ap_id),
287 "Question" <- object.data["type"] do
288 key = if poll_is_multiple?(object), do: "anyOf", else: "oneOf"
293 %{"name" => ^name} = option ->
294 Kernel.update_in(option["replies"]["totalItems"], &(&1 + 1))
300 voters = [actor | object.data["voters"] || []] |> Enum.uniq()
304 |> Map.put(key, options)
305 |> Map.put("voters", voters)
308 |> Object.change(%{data: data})
309 |> update_and_set_cache()
315 @doc "Updates data field of an object"
316 def update_data(%Object{data: data} = object, attrs \\ %{}) do
318 |> Object.change(%{data: Map.merge(data || %{}, attrs)})
322 def local?(%Object{data: %{"id" => id}}) do
323 String.starts_with?(id, Pleroma.Web.base_url() <> "/")
326 def replies(object, opts \\ []) do
327 object = Object.normalize(object)
333 fragment("(?)->>'inReplyTo' = ?", o.data, ^object.data["id"])
335 |> order_by([o], asc: o.id)
337 if opts[:self_only] do
338 actor = object.data["actor"]
339 where(query, [o], fragment("(?)->>'actor' = ?", o.data, ^actor))
345 def self_replies(object, opts \\ []),
346 do: replies(object, Keyword.put(opts, :self_only, true))