object.ex 942 Bytes
Newer Older
lain's avatar
lain committed
1 2
defmodule Pleroma.Object do
  use Ecto.Schema
3
  alias Pleroma.{Repo, Object}
lain's avatar
lain committed
4
  import Ecto.{Query, Changeset}
lain's avatar
lain committed
5 6 7 8 9 10

  schema "objects" do
    field :data, :map

    timestamps()
  end
11

lain's avatar
lain committed
12 13 14 15 16 17 18
  def change(struct, params \\ %{}) do
    changeset = struct
    |> cast(params, [:data])
    |> validate_required([:data])
    |> unique_constraint(:ap_id, name: :objects_unique_apid_index)
  end

19 20 21 22
  def get_by_ap_id(ap_id) do
    Repo.one(from object in Object,
      where: fragment("? @> ?", object.data, ^%{id: ap_id}))
  end
23

lain's avatar
lain committed
24 25 26 27 28
  def get_cached_by_ap_id(ap_id) do
    if Mix.env == :test do
      get_by_ap_id(ap_id)
    else
      key = "object:#{ap_id}"
29 30 31 32 33 34 35 36
      Cachex.get!(:user_cache, key, fallback: fn(_) ->
        object = get_by_ap_id(ap_id)
        if object do
          {:commit, object}
        else
          {:ignore, object}
        end
      end)
lain's avatar
lain committed
37 38 39
    end
  end

40 41 42
  def context_mapping(context) do
    %Object{data: %{"id" => context}}
  end
lain's avatar
lain committed
43
end