2019-09-12 18:37:36 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 22:44:49 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-09-12 18:37:36 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Delivery do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
alias Pleroma.Delivery
|
|
|
|
alias Pleroma.Object
|
2019-09-13 09:06:31 +00:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
2019-09-12 18:37:36 +00:00
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
schema "deliveries" do
|
2019-09-18 14:54:31 +00:00
|
|
|
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
2019-09-12 18:37:36 +00:00
|
|
|
belongs_to(:object, Object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def changeset(delivery, params \\ %{}) do
|
|
|
|
delivery
|
|
|
|
|> cast(params, [:user_id, :object_id])
|
2019-09-13 16:28:35 +00:00
|
|
|
|> validate_required([:user_id, :object_id])
|
2019-09-12 18:37:36 +00:00
|
|
|
|> foreign_key_constraint(:object_id)
|
|
|
|
|> foreign_key_constraint(:user_id)
|
|
|
|
|> unique_constraint(:user_id, name: :deliveries_user_id_object_id_index)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(object_id, user_id) do
|
|
|
|
%Delivery{}
|
|
|
|
|> changeset(%{user_id: user_id, object_id: object_id})
|
2019-09-13 15:23:26 +00:00
|
|
|
|> Repo.insert(on_conflict: :nothing)
|
2019-09-12 18:37:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get(object_id, user_id) do
|
|
|
|
from(d in Delivery, where: d.user_id == ^user_id and d.object_id == ^object_id)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2019-09-13 08:36:49 +00:00
|
|
|
# A hack because user delete activities have a fake id for whatever reason
|
|
|
|
# TODO: Get rid of this
|
|
|
|
def delete_all_by_object_id("pleroma:fake_object_id"), do: {0, []}
|
|
|
|
|
2019-09-12 18:37:36 +00:00
|
|
|
def delete_all_by_object_id(object_id) do
|
|
|
|
from(d in Delivery, where: d.object_id == ^object_id)
|
|
|
|
|> Repo.delete_all()
|
|
|
|
end
|
|
|
|
end
|