+ end
+ end
+
+ def increase_replies_count(ap_id) do
+ Object
+ |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
+ |> update([o],
+ set: [
+ data:
+ fragment(
+ """
+ safe_jsonb_set(?, '{repliesCount}',
+ (coalesce((?->>'repliesCount')::int, 0) + 1)::varchar::jsonb, true)
+ """,
+ o.data,
+ o.data
+ )
+ ]
+ )
+ |> Repo.update_all([])
+ |> case do
+ {1, [object]} -> set_cache(object)
+ _ -> {:error, "Not found"}
+ end
+ end
+
+ def decrease_replies_count(ap_id) do
+ Object
+ |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
+ |> update([o],
+ set: [
+ data:
+ fragment(
+ """
+ safe_jsonb_set(?, '{repliesCount}',
+ (greatest(0, (?->>'repliesCount')::int - 1))::varchar::jsonb, true)
+ """,
+ o.data,
+ o.data
+ )
+ ]
+ )
+ |> Repo.update_all([])
+ |> case do
+ {1, [object]} -> set_cache(object)
+ _ -> {:error, "Not found"}
+ end
+ end
+
+ def increase_vote_count(ap_id, name) do
+ with %Object{} = object <- Object.normalize(ap_id),
+ "Question" <- object.data["type"] do
+ multiple = Map.has_key?(object.data, "anyOf")
+
+ options =
+ (object.data["anyOf"] || object.data["oneOf"] || [])
+ |> Enum.map(fn
+ %{"name" => ^name} = option ->
+ Kernel.update_in(option["replies"]["totalItems"], &(&1 + 1))
+
+ option ->
+ option
+ end)
+
+ data =
+ if multiple do
+ Map.put(object.data, "anyOf", options)
+ else
+ Map.put(object.data, "oneOf", options)
+ end
+
+ object
+ |> Object.change(%{data: data})
+ |> update_and_set_cache()
+ else
+ _ -> :noop
+ end
+ end
+
+ @doc "Updates data field of an object"
+ def update_data(%Object{data: data} = object, attrs \\ %{}) do
+ object
+ |> Object.change(%{data: Map.merge(data || %{}, attrs)})
+ |> Repo.update()
+ end
+
+ def local?(%Object{data: %{"id" => id}}) do
+ String.starts_with?(id, Pleroma.Web.base_url() <> "/")
+ end
+
+ def replies(object, opts \\ []) do
+ object = Object.normalize(object)
+
+ query =
+ Object
+ |> where(
+ [o],
+ fragment("(?)->>'inReplyTo' = ?", o.data, ^object.data["id"])
+ )
+ |> order_by([o], asc: o.id)
+
+ if opts[:self_only] do
+ actor = object.data["actor"]
+ where(query, [o], fragment("(?)->>'actor' = ?", o.data, ^actor))