2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 23:13:23 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-04-14 06:45:56 -06:00
|
|
|
defmodule Pleroma.Bookmark do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Bookmark
|
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{}
|
|
|
|
|
|
|
|
schema "bookmarks" do
|
2019-09-18 08:54:31 -06:00
|
|
|
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
|
|
|
belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
|
2019-04-14 06:45:56 -06:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2019-09-18 08:54:31 -06:00
|
|
|
@spec create(FlakeId.Ecto.CompatType.t(), FlakeId.Ecto.CompatType.t()) ::
|
|
|
|
{:ok, Bookmark.t()} | {:error, Changeset.t()}
|
2019-04-14 06:45:56 -06:00
|
|
|
def create(user_id, activity_id) do
|
|
|
|
attrs = %{
|
|
|
|
user_id: user_id,
|
|
|
|
activity_id: activity_id
|
|
|
|
}
|
|
|
|
|
|
|
|
%Bookmark{}
|
|
|
|
|> cast(attrs, [:user_id, :activity_id])
|
|
|
|
|> validate_required([:user_id, :activity_id])
|
|
|
|
|> unique_constraint(:activity_id, name: :bookmarks_user_id_activity_id_index)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
2019-09-18 08:54:31 -06:00
|
|
|
@spec for_user_query(FlakeId.Ecto.CompatType.t()) :: Ecto.Query.t()
|
2019-04-14 06:45:56 -06:00
|
|
|
def for_user_query(user_id) do
|
|
|
|
Bookmark
|
|
|
|
|> where(user_id: ^user_id)
|
|
|
|
|> join(:inner, [b], activity in assoc(b, :activity))
|
|
|
|
|> preload([b, a], activity: a)
|
|
|
|
end
|
|
|
|
|
2019-04-27 14:06:46 -06:00
|
|
|
def get(user_id, activity_id) do
|
|
|
|
Bookmark
|
|
|
|
|> where(user_id: ^user_id)
|
|
|
|
|> where(activity_id: ^activity_id)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2019-09-18 08:54:31 -06:00
|
|
|
@spec destroy(FlakeId.Ecto.CompatType.t(), FlakeId.Ecto.CompatType.t()) ::
|
|
|
|
{:ok, Bookmark.t()} | {:error, Changeset.t()}
|
2019-04-14 06:45:56 -06:00
|
|
|
def destroy(user_id, activity_id) do
|
|
|
|
from(b in Bookmark,
|
|
|
|
where: b.user_id == ^user_id,
|
|
|
|
where: b.activity_id == ^activity_id
|
|
|
|
)
|
|
|
|
|> Repo.one()
|
|
|
|
|> Repo.delete()
|
|
|
|
end
|
|
|
|
end
|