Friendship ended with Postgresql now Cachex is my best friend
[akkoma] / lib / pleroma / object.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Object do
6 use Ecto.Schema
7 alias Pleroma.{Repo, Object, User, Activity, HTML}
8 import Ecto.{Query, Changeset}
9
10 schema "objects" do
11 field(:data, :map)
12
13 timestamps()
14 end
15
16 def create(data) do
17 Object.change(%Object{}, %{data: data})
18 |> Repo.insert()
19 end
20
21 def change(struct, params \\ %{}) do
22 struct
23 |> cast(params, [:data])
24 |> validate_required([:data])
25 |> unique_constraint(:ap_id, name: :objects_unique_apid_index)
26 end
27
28 def get_by_ap_id(nil), do: nil
29
30 def get_by_ap_id(ap_id) do
31 Repo.one(from(object in Object, where: fragment("(?)->>'id' = ?", object.data, ^ap_id)))
32 end
33
34 def normalize(obj) when is_map(obj), do: Object.get_by_ap_id(obj["id"])
35 def normalize(ap_id) when is_binary(ap_id), do: Object.get_by_ap_id(ap_id)
36 def normalize(_), do: nil
37
38 # Owned objects can only be mutated by their owner
39 def authorize_mutation(%Object{data: %{"actor" => actor}}, %User{ap_id: ap_id}),
40 do: actor == ap_id
41
42 # Legacy objects can be mutated by anybody
43 def authorize_mutation(%Object{}, %User{}), do: true
44
45 if Mix.env() == :test do
46 def get_cached_by_ap_id(ap_id) do
47 get_by_ap_id(ap_id)
48 end
49 else
50 def get_cached_by_ap_id(ap_id) do
51 key = "object:#{ap_id}"
52
53 Cachex.fetch!(:object_cache, key, fn _ ->
54 object = get_by_ap_id(ap_id)
55
56 if object do
57 {:commit, object}
58 else
59 {:ignore, object}
60 end
61 end)
62 end
63 end
64
65 def context_mapping(context) do
66 Object.change(%Object{}, %{data: %{"id" => context}})
67 end
68
69 def delete(%Object{data: %{"id" => id}} = object) do
70 with Repo.delete(object),
71 Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
72 {:ok, true} <- Cachex.del(:object_cache, "object:#{id}") do
73 {:ok, object}
74 end
75 end
76
77 def get_cached_scrubbed_html(content, scrubbers, object) do
78 key = "#{generate_scrubber_signature(scrubbers)}|#{object.id}"
79 Cachex.fetch!(:scrubber_cache, key, fn(_key) -> ensure_scrubbed_html(content, scrubbers) end )
80 end
81
82 def get_cached_stripped_html(content, object) do
83 get_cached_scrubbed_html(content, HtmlSanitizeEx.Scrubber.StripTags, object)
84 end
85
86 def ensure_scrubbed_html(
87 content,
88 scrubbers
89 ) do
90 {:commit, HTML.filter_tags(content, scrubbers)}
91 end
92
93 defp generate_scrubber_signature(scrubber) when is_atom(scrubber) do
94 generate_scrubber_signature([scrubber])
95 end
96
97 defp generate_scrubber_signature(scrubbers) do
98 Enum.reduce(scrubbers, "", fn scrubber, signature ->
99 # If a scrubber does not have a version(e.g HtmlSanitizeEx.Scrubber) it is assumed it is always 0)
100 version = if Kernel.function_exported?(scrubber, :version, 0) do
101 scrubber.version
102 else
103 0
104 end
105 "#{signature}#{to_string(scrubber)}#{version}"
106 end)
107 end
108 end