Add option to keep id for follows.
[akkoma] / lib / pleroma / object.ex
1 defmodule Pleroma.Object do
2 use Ecto.Schema
3 alias Pleroma.{Repo, Object}
4 import Ecto.{Query, Changeset}
5
6 schema "objects" do
7 field :data, :map
8
9 timestamps()
10 end
11
12 def change(struct, params \\ %{}) do
13 changeset = struct
14 |> cast(params, [:data])
15 |> validate_required([:data])
16 |> unique_constraint(:ap_id, name: :objects_unique_apid_index)
17 end
18
19 def get_by_ap_id(ap_id) do
20 Repo.one(from object in Object,
21 where: fragment("? @> ?", object.data, ^%{id: ap_id}))
22 end
23
24 def get_cached_by_ap_id(ap_id) do
25 if Mix.env == :test do
26 get_by_ap_id(ap_id)
27 else
28 key = "object:#{ap_id}"
29 Cachex.get!(:user_cache, key, fallback: fn(_) ->
30 object = get_by_ap_id(ap_id)
31 if object do
32 {:commit, object}
33 else
34 {:ignore, object}
35 end
36 end)
37 end
38 end
39
40 def context_mapping(context) do
41 %Object{data: %{"id" => context}}
42 end
43 end