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_id(nil), do: nil
39 def get_by_id(id), do: Repo.get(Object, id)
41 def get_by_id_and_maybe_refetch(id, opts \\ []) do
42 %{updated_at: updated_at} = object = get_by_id(id)
45 NaiveDateTime.diff(NaiveDateTime.utc_now(), updated_at) > opts[:interval] do
46 case Fetcher.refetch_object(object) do
47 {:ok, %Object{} = object} ->
51 Logger.error("Couldn't refresh #{object.data["id"]}:\n#{inspect(e)}")
59 def get_by_ap_id(nil), do: nil
61 def get_by_ap_id(ap_id) do
62 Repo.one(from(object in Object, where: fragment("(?)->>'id' = ?", object.data, ^ap_id)))
65 defp warn_on_no_object_preloaded(ap_id) do
66 "Object.normalize() called without preloaded object (#{inspect(ap_id)}). Consider preloading the object"
69 Logger.debug("Backtrace: #{inspect(Process.info(:erlang.self(), :current_stacktrace))}")
72 def normalize(_, fetch_remote \\ true, options \\ [])
74 # If we pass an Activity to Object.normalize(), we can try to use the preloaded object.
75 # Use this whenever possible, especially when walking graphs in an O(N) loop!
76 def normalize(%Object{} = object, _, _), do: object
77 def normalize(%Activity{object: %Object{} = object}, _, _), do: object
79 # A hack for fake activities
80 def normalize(%Activity{data: %{"object" => %{"fake" => true} = data}}, _, _) do
81 %Object{id: "pleroma:fake_object_id", data: data}
85 def normalize(%Activity{data: %{"object" => %{"id" => ap_id}}}, fetch_remote, _) do
86 warn_on_no_object_preloaded(ap_id)
87 normalize(ap_id, fetch_remote)
91 def normalize(%Activity{data: %{"object" => ap_id}}, fetch_remote, _) do
92 warn_on_no_object_preloaded(ap_id)
93 normalize(ap_id, fetch_remote)
96 # Old way, try fetching the object through cache.
97 def normalize(%{"id" => ap_id}, fetch_remote, _), do: normalize(ap_id, fetch_remote)
98 def normalize(ap_id, false, _) when is_binary(ap_id), do: get_cached_by_ap_id(ap_id)
100 def normalize(ap_id, true, options) when is_binary(ap_id) do
101 Fetcher.fetch_object_from_id!(ap_id, options)
104 def normalize(_, _, _), do: nil
106 # Owned objects can only be mutated by their owner
107 def authorize_mutation(%Object{data: %{"actor" => actor}}, %User{ap_id: ap_id}),
110 # Legacy objects can be mutated by anybody
111 def authorize_mutation(%Object{}, %User{}), do: true
113 def get_cached_by_ap_id(ap_id) do
114 key = "object:#{ap_id}"
116 Cachex.fetch!(:object_cache, key, fn _ ->
117 object = get_by_ap_id(ap_id)
127 def context_mapping(context) do
128 Object.change(%Object{}, %{data: %{"id" => context}})
131 def make_tombstone(%Object{data: %{"id" => id, "type" => type}}, deleted \\ DateTime.utc_now()) do
140 def swap_object_with_tombstone(object) do
141 tombstone = make_tombstone(object)
144 |> Object.change(%{data: tombstone})
148 def delete(%Object{data: %{"id" => id}} = object) do
149 with {:ok, _obj} = swap_object_with_tombstone(object),
150 deleted_activity = Activity.delete_by_ap_id(id),
151 {:ok, true} <- Cachex.del(:object_cache, "object:#{id}"),
152 {:ok, _} <- Cachex.del(:web_resp_cache, URI.parse(id).path) do
153 {:ok, object, deleted_activity}
157 def prune(%Object{data: %{"id" => id}} = object) do
158 with {:ok, object} <- Repo.delete(object),
159 {:ok, true} <- Cachex.del(:object_cache, "object:#{id}"),
160 {:ok, _} <- Cachex.del(:web_resp_cache, URI.parse(id).path) do
165 def set_cache(%Object{data: %{"id" => ap_id}} = object) do
166 Cachex.put(:object_cache, "object:#{ap_id}", object)
170 def update_and_set_cache(changeset) do
171 with {:ok, object} <- Repo.update(changeset) do
176 def increase_replies_count(ap_id) do
178 |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
184 safe_jsonb_set(?, '{repliesCount}',
185 (coalesce((?->>'repliesCount')::int, 0) + 1)::varchar::jsonb, true)
192 |> Repo.update_all([])
194 {1, [object]} -> set_cache(object)
195 _ -> {:error, "Not found"}
199 def decrease_replies_count(ap_id) do
201 |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
207 safe_jsonb_set(?, '{repliesCount}',
208 (greatest(0, (?->>'repliesCount')::int - 1))::varchar::jsonb, true)
215 |> Repo.update_all([])
217 {1, [object]} -> set_cache(object)
218 _ -> {:error, "Not found"}
222 def increase_vote_count(ap_id, name) do
223 with %Object{} = object <- Object.normalize(ap_id),
224 "Question" <- object.data["type"] do
225 multiple = Map.has_key?(object.data, "anyOf")
228 (object.data["anyOf"] || object.data["oneOf"] || [])
230 %{"name" => ^name} = option ->
231 Kernel.update_in(option["replies"]["totalItems"], &(&1 + 1))
239 Map.put(object.data, "anyOf", options)
241 Map.put(object.data, "oneOf", options)
245 |> Object.change(%{data: data})
246 |> update_and_set_cache()
252 @doc "Updates data field of an object"
253 def update_data(%Object{data: data} = object, attrs \\ %{}) do
255 |> Object.change(%{data: Map.merge(data || %{}, attrs)})
259 def local?(%Object{data: %{"id" => id}}) do
260 String.starts_with?(id, Pleroma.Web.base_url() <> "/")