Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

51 linhas
1.4KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Delivery do
  5. use Ecto.Schema
  6. alias Pleroma.Delivery
  7. alias Pleroma.Object
  8. alias Pleroma.Repo
  9. alias Pleroma.User
  10. alias Pleroma.User
  11. import Ecto.Changeset
  12. import Ecto.Query
  13. schema "deliveries" do
  14. belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
  15. belongs_to(:object, Object)
  16. end
  17. def changeset(delivery, params \\ %{}) do
  18. delivery
  19. |> cast(params, [:user_id, :object_id])
  20. |> validate_required([:user_id, :object_id])
  21. |> foreign_key_constraint(:object_id)
  22. |> foreign_key_constraint(:user_id)
  23. |> unique_constraint(:user_id, name: :deliveries_user_id_object_id_index)
  24. end
  25. def create(object_id, user_id) do
  26. %Delivery{}
  27. |> changeset(%{user_id: user_id, object_id: object_id})
  28. |> Repo.insert(on_conflict: :nothing)
  29. end
  30. def get(object_id, user_id) do
  31. from(d in Delivery, where: d.user_id == ^user_id and d.object_id == ^object_id)
  32. |> Repo.one()
  33. end
  34. # A hack because user delete activities have a fake id for whatever reason
  35. # TODO: Get rid of this
  36. def delete_all_by_object_id("pleroma:fake_object_id"), do: {0, []}
  37. def delete_all_by_object_id(object_id) do
  38. from(d in Delivery, where: d.object_id == ^object_id)
  39. |> Repo.delete_all()
  40. end
  41. end