1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Object do
10 alias Pleroma.Object.Fetcher
11 alias Pleroma.ObjectTombstone
27 Object.change(%Object{}, %{data: data})
31 def change(struct, params \\ %{}) do
33 |> cast(params, [:data])
34 |> validate_required([:data])
35 |> unique_constraint(:ap_id, name: :objects_unique_apid_index)
38 def get_by_ap_id(nil), do: nil
40 def get_by_ap_id(ap_id) do
41 Repo.one(from(object in Object, where: fragment("(?)->>'id' = ?", object.data, ^ap_id)))
44 def normalize(_, fetch_remote \\ true)
45 # If we pass an Activity to Object.normalize(), we can try to use the preloaded object.
46 # Use this whenever possible, especially when walking graphs in an O(N) loop!
47 def normalize(%Object{} = object, _), do: object
48 def normalize(%Activity{object: %Object{} = object}, _), do: object
50 # A hack for fake activities
51 def normalize(%Activity{data: %{"object" => %{"fake" => true} = data}}, _) do
52 %Object{id: "pleroma:fake_object_id", data: data}
55 # Catch and log Object.normalize() calls where the Activity's child object is not
57 def normalize(%Activity{data: %{"object" => %{"id" => ap_id}}}, fetch_remote) do
59 "Object.normalize() called without preloaded object (#{ap_id}). Consider preloading the object!"
62 Logger.debug("Backtrace: #{inspect(Process.info(:erlang.self(), :current_stacktrace))}")
64 normalize(ap_id, fetch_remote)
67 def normalize(%Activity{data: %{"object" => ap_id}}, fetch_remote) do
69 "Object.normalize() called without preloaded object (#{ap_id}). Consider preloading the object!"
72 Logger.debug("Backtrace: #{inspect(Process.info(:erlang.self(), :current_stacktrace))}")
74 normalize(ap_id, fetch_remote)
77 # Old way, try fetching the object through cache.
78 def normalize(%{"id" => ap_id}, fetch_remote), do: normalize(ap_id, fetch_remote)
79 def normalize(ap_id, false) when is_binary(ap_id), do: get_cached_by_ap_id(ap_id)
80 def normalize(ap_id, true) when is_binary(ap_id), do: Fetcher.fetch_object_from_id!(ap_id)
81 def normalize(_, _), do: nil
83 # Owned objects can only be mutated by their owner
84 def authorize_mutation(%Object{data: %{"actor" => actor}}, %User{ap_id: ap_id}),
87 # Legacy objects can be mutated by anybody
88 def authorize_mutation(%Object{}, %User{}), do: true
90 def get_cached_by_ap_id(ap_id) do
91 key = "object:#{ap_id}"
93 Cachex.fetch!(:object_cache, key, fn _ ->
94 object = get_by_ap_id(ap_id)
104 def context_mapping(context) do
105 Object.change(%Object{}, %{data: %{"id" => context}})
108 def make_tombstone(%Object{data: %{"id" => id, "type" => type}}, deleted \\ DateTime.utc_now()) do
117 def swap_object_with_tombstone(object) do
118 tombstone = make_tombstone(object)
121 |> Object.change(%{data: tombstone})
125 def delete(%Object{data: %{"id" => id}} = object) do
126 with {:ok, _obj} = swap_object_with_tombstone(object),
127 deleted_activity = Activity.delete_by_ap_id(id),
128 {:ok, true} <- Cachex.del(:object_cache, "object:#{id}") do
129 {:ok, object, deleted_activity}
133 def set_cache(%Object{data: %{"id" => ap_id}} = object) do
134 Cachex.put(:object_cache, "object:#{ap_id}", object)
138 def update_and_set_cache(changeset) do
139 with {:ok, object} <- Repo.update(changeset) do
146 def increase_replies_count(ap_id) do
148 |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
154 jsonb_set(?, '{repliesCount}',
155 (coalesce((?->>'repliesCount')::int, 0) + 1)::varchar::jsonb, true)
162 |> Repo.update_all([])
164 {1, [object]} -> set_cache(object)
165 _ -> {:error, "Not found"}
169 def decrease_replies_count(ap_id) do
171 |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
177 jsonb_set(?, '{repliesCount}',
178 (greatest(0, (?->>'repliesCount')::int - 1))::varchar::jsonb, true)
185 |> Repo.update_all([])
187 {1, [object]} -> set_cache(object)
188 _ -> {:error, "Not found"}