1 defmodule Pleroma.Object do
3 alias Pleroma.{Repo, Object}
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
25 def get_by_ap_id(ap_id) do
26 Repo.one(from object in Object,
27 where: fragment("(?)->>'id' = ?", object.data, ^ap_id))
30 def get_cached_by_ap_id(ap_id) do
31 if Mix.env == :test do
34 key = "object:#{ap_id}"
35 Cachex.get!(:user_cache, key, fallback: fn(_) ->
36 object = get_by_ap_id(ap_id)
46 def context_mapping(context) do
47 Object.change(%Object{}, %{data: %{"id" => context}})