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.Bookmark do
11 alias Pleroma.Activity
12 alias Pleroma.Bookmark
16 @type t :: %__MODULE__{}
19 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
20 belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
25 @spec create(FlakeId.Ecto.CompatType.t(), FlakeId.Ecto.CompatType.t()) ::
26 {:ok, Bookmark.t()} | {:error, Changeset.t()}
27 def create(user_id, activity_id) do
30 activity_id: activity_id
34 |> cast(attrs, [:user_id, :activity_id])
35 |> validate_required([:user_id, :activity_id])
36 |> unique_constraint(:activity_id, name: :bookmarks_user_id_activity_id_index)
40 @spec for_user_query(FlakeId.Ecto.CompatType.t()) :: Ecto.Query.t()
41 def for_user_query(user_id) do
43 |> where(user_id: ^user_id)
44 |> join(:inner, [b], activity in assoc(b, :activity))
45 |> preload([b, a], activity: a)
48 def get(user_id, activity_id) do
50 |> where(user_id: ^user_id)
51 |> where(activity_id: ^activity_id)
55 @spec destroy(FlakeId.Ecto.CompatType.t(), FlakeId.Ecto.CompatType.t()) ::
56 {:ok, Bookmark.t()} | {:error, Changeset.t()}
57 def destroy(user_id, activity_id) do
59 where: b.user_id == ^user_id,
60 where: b.activity_id == ^activity_id