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
26 def with_joined_activity(query, activity_type \\ "Create", join_type \\ :inner) do
27 object_position = Map.get(query.aliases, :object, 0)
29 join(query, join_type, [{object, object_position}], a in Activity,
32 "COALESCE(?->'object'->>'id', ?->>'object') = (? ->> 'id') AND (?->>'type' = ?) ",
44 Object.change(%Object{}, %{data: data})
48 def change(struct, params \\ %{}) do
50 |> cast(params, [:data])
51 |> validate_required([:data])
52 |> unique_constraint(:ap_id, name: :objects_unique_apid_index)
55 def get_by_id(nil), do: nil
56 def get_by_id(id), do: Repo.get(Object, id)
58 def get_by_id_and_maybe_refetch(id, opts \\ []) do
59 %{updated_at: updated_at} = object = get_by_id(id)
62 NaiveDateTime.diff(NaiveDateTime.utc_now(), updated_at) > opts[:interval] do
63 case Fetcher.refetch_object(object) do
64 {:ok, %Object{} = object} ->
68 Logger.error("Couldn't refresh #{object.data["id"]}:\n#{inspect(e)}")
76 def get_by_ap_id(nil), do: nil
78 def get_by_ap_id(ap_id) do
79 Repo.one(from(object in Object, where: fragment("(?)->>'id' = ?", object.data, ^ap_id)))
82 defp warn_on_no_object_preloaded(ap_id) do
83 "Object.normalize() called without preloaded object (#{inspect(ap_id)}). Consider preloading the object"
86 Logger.debug("Backtrace: #{inspect(Process.info(:erlang.self(), :current_stacktrace))}")
89 def normalize(_, fetch_remote \\ true, options \\ [])
91 # If we pass an Activity to Object.normalize(), we can try to use the preloaded object.
92 # Use this whenever possible, especially when walking graphs in an O(N) loop!
93 def normalize(%Object{} = object, _, _), do: object
94 def normalize(%Activity{object: %Object{} = object}, _, _), do: object
96 # A hack for fake activities
97 def normalize(%Activity{data: %{"object" => %{"fake" => true} = data}}, _, _) do
98 %Object{id: "pleroma:fake_object_id", data: data}
101 # No preloaded object
102 def normalize(%Activity{data: %{"object" => %{"id" => ap_id}}}, fetch_remote, _) do
103 warn_on_no_object_preloaded(ap_id)
104 normalize(ap_id, fetch_remote)
107 # No preloaded object
108 def normalize(%Activity{data: %{"object" => ap_id}}, fetch_remote, _) do
109 warn_on_no_object_preloaded(ap_id)
110 normalize(ap_id, fetch_remote)
113 # Old way, try fetching the object through cache.
114 def normalize(%{"id" => ap_id}, fetch_remote, _), do: normalize(ap_id, fetch_remote)
115 def normalize(ap_id, false, _) when is_binary(ap_id), do: get_cached_by_ap_id(ap_id)
117 def normalize(ap_id, true, options) when is_binary(ap_id) do
118 Fetcher.fetch_object_from_id!(ap_id, options)
121 def normalize(_, _, _), do: nil
123 # Owned objects can only be mutated by their owner
124 def authorize_mutation(%Object{data: %{"actor" => actor}}, %User{ap_id: ap_id}),
127 # Legacy objects can be mutated by anybody
128 def authorize_mutation(%Object{}, %User{}), do: true
130 def get_cached_by_ap_id(ap_id) do
131 key = "object:#{ap_id}"
133 Cachex.fetch!(:object_cache, key, fn _ ->
134 object = get_by_ap_id(ap_id)
144 def context_mapping(context) do
145 Object.change(%Object{}, %{data: %{"id" => context}})
148 def make_tombstone(%Object{data: %{"id" => id, "type" => type}}, deleted \\ DateTime.utc_now()) do
157 def swap_object_with_tombstone(object) do
158 tombstone = make_tombstone(object)
161 |> Object.change(%{data: tombstone})
165 def delete(%Object{data: %{"id" => id}} = object) do
166 with {:ok, _obj} = swap_object_with_tombstone(object),
167 deleted_activity = Activity.delete_all_by_object_ap_id(id),
168 {:ok, true} <- Cachex.del(:object_cache, "object:#{id}"),
169 {:ok, _} <- Cachex.del(:web_resp_cache, URI.parse(id).path) do
170 {:ok, object, deleted_activity}
174 def prune(%Object{data: %{"id" => id}} = object) do
175 with {:ok, object} <- Repo.delete(object),
176 {:ok, true} <- Cachex.del(:object_cache, "object:#{id}"),
177 {:ok, _} <- Cachex.del(:web_resp_cache, URI.parse(id).path) do
182 def set_cache(%Object{data: %{"id" => ap_id}} = object) do
183 Cachex.put(:object_cache, "object:#{ap_id}", object)
187 def update_and_set_cache(changeset) do
188 with {:ok, object} <- Repo.update(changeset) do
193 def increase_replies_count(ap_id) do
195 |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
201 safe_jsonb_set(?, '{repliesCount}',
202 (coalesce((?->>'repliesCount')::int, 0) + 1)::varchar::jsonb, true)
209 |> Repo.update_all([])
211 {1, [object]} -> set_cache(object)
212 _ -> {:error, "Not found"}
216 def decrease_replies_count(ap_id) do
218 |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
224 safe_jsonb_set(?, '{repliesCount}',
225 (greatest(0, (?->>'repliesCount')::int - 1))::varchar::jsonb, true)
232 |> Repo.update_all([])
234 {1, [object]} -> set_cache(object)
235 _ -> {:error, "Not found"}
239 def increase_vote_count(ap_id, name) do
240 with %Object{} = object <- Object.normalize(ap_id),
241 "Question" <- object.data["type"] do
242 multiple = Map.has_key?(object.data, "anyOf")
245 (object.data["anyOf"] || object.data["oneOf"] || [])
247 %{"name" => ^name} = option ->
248 Kernel.update_in(option["replies"]["totalItems"], &(&1 + 1))
256 Map.put(object.data, "anyOf", options)
258 Map.put(object.data, "oneOf", options)
262 |> Object.change(%{data: data})
263 |> update_and_set_cache()
269 @doc "Updates data field of an object"
270 def update_data(%Object{data: data} = object, attrs \\ %{}) do
272 |> Object.change(%{data: Map.merge(data || %{}, attrs)})
276 def local?(%Object{data: %{"id" => id}}) do
277 String.starts_with?(id, Pleroma.Web.base_url() <> "/")