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.Delivery do
16 schema "deliveries" do
17 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
18 belongs_to(:object, Object)
21 def changeset(delivery, params \\ %{}) do
23 |> cast(params, [:user_id, :object_id])
24 |> validate_required([:user_id, :object_id])
25 |> foreign_key_constraint(:object_id)
26 |> foreign_key_constraint(:user_id)
27 |> unique_constraint(:user_id, name: :deliveries_user_id_object_id_index)
30 def create(object_id, user_id) do
32 |> changeset(%{user_id: user_id, object_id: object_id})
33 |> Repo.insert(on_conflict: :nothing)
36 def get(object_id, user_id) do
37 from(d in Delivery, where: d.user_id == ^user_id and d.object_id == ^object_id)
41 # A hack because user delete activities have a fake id for whatever reason
42 # TODO: Get rid of this
43 def delete_all_by_object_id("pleroma:fake_object_id"), do: {0, []}
45 def delete_all_by_object_id(object_id) do
46 from(d in Delivery, where: d.object_id == ^object_id)