1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.CounterCache do
6 alias Pleroma.CounterCache
12 schema "counter_cache" do
13 field(:instance, :string)
14 field(:public, :integer)
15 field(:unlisted, :integer)
16 field(:private, :integer)
17 field(:direct, :integer)
20 def changeset(struct, params) do
22 |> cast(params, [:instance, :public, :unlisted, :private, :direct])
23 |> validate_required([:instance])
24 |> unique_constraint(:instance)
27 def get_by_instance(instance) do
31 "unlisted" => c.unlisted,
32 "private" => c.private,
35 |> where([c], c.instance == ^instance)
38 nil -> %{"public" => 0, "unlisted" => 0, "private" => 0, "direct" => 0}
46 "public" => type(sum(c.public), :integer),
47 "unlisted" => type(sum(c.unlisted), :integer),
48 "private" => type(sum(c.private), :integer),
49 "direct" => type(sum(c.direct), :integer)
54 def set(instance, values) do
57 ["public", "private", "unlisted", "direct"],
58 %{"instance" => instance},
60 Map.put_new(acc, param, Map.get(values, param, 0))
69 public: params["public"],
70 private: params["private"],
71 unlisted: params["unlisted"],
72 direct: params["direct"]
76 conflict_target: :instance