Merge branch 'fix/credo-issues' into 'develop'
[akkoma] / lib / pleroma / object.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Object do
6 use Ecto.Schema
7
8 alias Pleroma.Repo
9 alias Pleroma.Object
10 alias Pleroma.User
11 alias Pleroma.Activity
12 alias Pleroma.ObjectTombstone
13
14 import Ecto.Query
15 import Ecto.Changeset
16
17 schema "objects" do
18 field(:data, :map)
19
20 timestamps()
21 end
22
23 def insert_or_get(cng) do
24 {_, data} = fetch_field(cng, :data)
25 id = data["id"] || data[:id]
26 key = "object:#{id}"
27
28 fetcher = fn _ ->
29 with nil <- get_by_ap_id(id),
30 {:ok, object} <- Repo.insert(cng) do
31 {:commit, object}
32 else
33 %Object{} = object -> {:commit, object}
34 e -> {:ignore, e}
35 end
36 end
37
38 with {state, object} when state in [:commit, :ok] <- Cachex.fetch(:object_cache, key, fetcher) do
39 {:ok, object}
40 end
41 end
42
43 def create(data) do
44 Object.change(%Object{}, %{data: data})
45 |> insert_or_get()
46 end
47
48 def change(struct, params \\ %{}) do
49 struct
50 |> cast(params, [:data])
51 |> validate_required([:data])
52 |> unique_constraint(:ap_id, name: :objects_unique_apid_index)
53 end
54
55 def get_by_ap_id(nil), do: nil
56
57 def get_by_ap_id(ap_id) do
58 Repo.one(from(object in Object, where: fragment("(?)->>'id' = ?", object.data, ^ap_id)))
59 end
60
61 def normalize(%{"id" => ap_id}), do: normalize(ap_id)
62 def normalize(ap_id) when is_binary(ap_id), do: get_cached_by_ap_id(ap_id)
63 def normalize(_), do: nil
64
65 # Owned objects can only be mutated by their owner
66 def authorize_mutation(%Object{data: %{"actor" => actor}}, %User{ap_id: ap_id}),
67 do: actor == ap_id
68
69 # Legacy objects can be mutated by anybody
70 def authorize_mutation(%Object{}, %User{}), do: true
71
72 def get_cached_by_ap_id(ap_id) do
73 key = "object:#{ap_id}"
74
75 Cachex.fetch!(:object_cache, key, fn _ ->
76 object = get_by_ap_id(ap_id)
77
78 if object do
79 {:commit, object}
80 else
81 {:ignore, object}
82 end
83 end)
84 end
85
86 def context_mapping(context) do
87 Object.change(%Object{}, %{data: %{"id" => context}})
88 end
89
90 def make_tombstone(%Object{data: %{"id" => id, "type" => type}}, deleted \\ DateTime.utc_now()) do
91 %ObjectTombstone{
92 id: id,
93 formerType: type,
94 deleted: deleted
95 }
96 |> Map.from_struct()
97 end
98
99 def swap_object_with_tombstone(object) do
100 tombstone = make_tombstone(object)
101
102 object
103 |> Object.change(%{data: tombstone})
104 |> Repo.update()
105 end
106
107 def delete(%Object{data: %{"id" => id}} = object) do
108 with {:ok, _obj} = swap_object_with_tombstone(object),
109 Repo.delete_all(Activity.by_object_ap_id(id)),
110 {:ok, true} <- Cachex.del(:object_cache, "object:#{id}") do
111 {:ok, object}
112 end
113 end
114
115 def set_cache(%Object{data: %{"id" => ap_id}} = object) do
116 Cachex.put(:object_cache, "object:#{ap_id}", object)
117 {:ok, object}
118 end
119
120 def update_and_set_cache(changeset) do
121 with {:ok, object} <- Repo.update(changeset) do
122 set_cache(object)
123 else
124 e -> e
125 end
126 end
127 end