1 defmodule Pleroma.Object do
3 alias Pleroma.{Repo, Object, Activity}
4 import Ecto.{Query, Changeset}
13 Object.change(%Object{}, %{data: data})
17 def change(struct, params \\ %{}) do
19 |> cast(params, [:data])
20 |> validate_required([:data])
21 |> unique_constraint(:ap_id, name: :objects_unique_apid_index)
24 def get_by_ap_id(nil), do: nil
26 def get_by_ap_id(ap_id) do
27 Repo.one(from(object in Object, where: fragment("(?)->>'id' = ?", object.data, ^ap_id)))
30 def normalize(obj) when is_map(obj), do: Object.get_by_ap_id(obj["id"])
31 def normalize(ap_id) when is_binary(ap_id), do: Object.get_by_ap_id(ap_id)
32 def normalize(_), do: nil
34 def get_cached_by_ap_id(ap_id) do
35 if Mix.env() == :test do
38 key = "object:#{ap_id}"
40 Cachex.fetch!(:object_cache, key, fn _ ->
41 object = get_by_ap_id(ap_id)
52 def context_mapping(context) do
53 Object.change(%Object{}, %{data: %{"id" => context}})
56 def delete(%Object{data: %{"id" => id}} = object) do
57 with Repo.delete(object),
58 Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
59 {:ok, true} <- Cachex.del(:object_cache, "object:#{id}") do