-
Notifications
You must be signed in to change notification settings - Fork 9
/
object.ex
146 lines (116 loc) · 3.33 KB
/
object.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
defmodule ActivityPub.Object do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias ActivityPub.Fetcher
alias ActivityPub.Object
@repo Application.get_env(:activity_pub, :repo)
@type t :: %__MODULE__{}
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "ap_object" do
field(:data, :map)
field(:local, :boolean, default: true)
field(:public, :boolean)
belongs_to(:pointer, Pointers.Pointer)
timestamps()
end
def get_by_id(id), do: @repo.get(Object, id)
def get_by_ap_id(ap_id) do
@repo.one(from(object in Object, where: fragment("(?)->>'id' = ?", object.data, ^ap_id)))
end
def get_by_pointer_id(pointer_id), do: @repo.get_by(Object, pointer_id: pointer_id)
def get_cached_by_ap_id(ap_id) do
key = "ap_id:#{ap_id}"
Cachex.fetch!(:ap_object_cache, key, fn _ ->
object = get_by_ap_id(ap_id)
if object do
{:commit, object}
else
{:ignore, object}
end
end)
end
def get_cached_by_pointer_id(pointer_id) do
key = "pointer_id:#{pointer_id}"
Cachex.fetch!(:ap_object_cache, key, fn _ ->
object = get_by_pointer_id(pointer_id)
if object do
{:commit, object}
else
{:ignore, object}
end
end)
end
def set_cache(%Object{data: %{"id" => ap_id}} = object) do
Cachex.put(:ap_object_cache, "ap_id:#{ap_id}", object)
if object.pointer_id do
Cachex.put(:ap_object_cache, "pointer_id:#{object.pointer_id}", object)
end
{:ok, object}
end
def invalidate_cache(%Object{data: %{"id" => ap_id}} = object) do
with {:ok, true} <- Cachex.del(:ap_object_cache, "ap_id:#{ap_id}"),
{:ok, true} <- Cachex.del(:ap_object_cache, "pointer_id:#{object.pointer_id}") do
:ok
end
end
def insert(attrs) do
attrs
|> changeset()
|> @repo.insert()
end
def changeset(attrs) do
%__MODULE__{}
|> changeset(attrs)
end
def changeset(object, attrs) do
object
|> cast(attrs, [:data, :local, :public, :pointer_id])
|> validate_required(:data)
end
def update(object, attrs) do
object
|> change(attrs)
|> update_and_set_cache()
end
def update_and_set_cache(changeset) do
with {:ok, object} <- @repo.update(changeset) do
set_cache(object)
else
e -> e
end
end
def normalize(_, fetch_remote \\ true)
def normalize(%Object{} = object, _), do: object
def normalize(%{"id" => ap_id}, fetch_remote), do: normalize(ap_id, fetch_remote)
def normalize(ap_id, false) when is_binary(ap_id), do: get_cached_by_ap_id(ap_id)
def normalize(ap_id, true) when is_binary(ap_id) do
with {:ok, object} <- Fetcher.fetch_object_from_id(ap_id) do
object
else
_e -> nil
end
end
def normalize(_, _), do: nil
def make_tombstone(%Object{data: %{"id" => id, "type" => type}}, deleted \\ DateTime.utc_now()) do
%{
"id" => id,
"formerType" => type,
"deleted" => deleted,
"type" => "Tombstone"
}
end
def swap_object_with_tombstone(object) do
tombstone = make_tombstone(object)
object
|> Object.changeset(%{data: tombstone})
|> @repo.update()
end
def delete(%Object{} = object) do
with {:ok, _obj} <- swap_object_with_tombstone(object),
:ok <- invalidate_cache(object) do
{:ok, object}
end
end
end