1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
17 @type t :: %__MODULE__{}
20 belongs_to(:user, User, type: FlakeId)
21 belongs_to(:activity, Activity, type: FlakeId)
26 @spec create(FlakeId.t(), FlakeId.t()) :: {: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.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.t(), FlakeId.t()) :: {:ok, Bookmark.t()} | {:error, Changeset.t()}
56 def destroy(user_id, activity_id) do
58 where: b.user_id == ^user_id,
59 where: b.activity_id == ^activity_id