2019-07-10 07:13:23 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 21:38:50 +01:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-07-10 07:13:23 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-04-14 14:45:56 +02:00
|
|
|
defmodule Pleroma.Bookmark do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Bookmark
|
2024-02-26 23:45:02 +01:00
|
|
|
alias Pleroma.BookmarkFolder
|
2019-04-14 14:45:56 +02:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{}
|
|
|
|
|
|
|
|
schema "bookmarks" do
|
2019-09-18 16:54:31 +02:00
|
|
|
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
|
|
|
belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
|
2024-02-26 23:45:02 +01:00
|
|
|
belongs_to(:folder, BookmarkFolder, type: FlakeId.Ecto.CompatType)
|
2019-04-14 14:45:56 +02:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2024-01-20 22:36:01 +01:00
|
|
|
@spec create(Ecto.UUID.t(), Ecto.UUID.t()) ::
|
2024-01-20 22:47:50 +01:00
|
|
|
{:ok, Bookmark.t()} | {:error, Ecto.Changeset.t()}
|
2024-02-26 23:45:02 +01:00
|
|
|
def create(user_id, activity_id, folder_id \\ nil) do
|
2019-04-14 14:45:56 +02:00
|
|
|
attrs = %{
|
|
|
|
user_id: user_id,
|
2024-02-26 23:45:02 +01:00
|
|
|
activity_id: activity_id,
|
|
|
|
folder_id: folder_id
|
2019-04-14 14:45:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
%Bookmark{}
|
2024-02-26 23:45:02 +01:00
|
|
|
|> cast(attrs, [:user_id, :activity_id, :folder_id])
|
2019-04-14 14:45:56 +02:00
|
|
|
|> validate_required([:user_id, :activity_id])
|
|
|
|
|> unique_constraint(:activity_id, name: :bookmarks_user_id_activity_id_index)
|
2024-02-26 23:45:02 +01:00
|
|
|
|> Repo.insert(
|
|
|
|
on_conflict: [set: [folder_id: folder_id]],
|
|
|
|
conflict_target: [:user_id, :activity_id]
|
|
|
|
)
|
2019-04-14 14:45:56 +02:00
|
|
|
end
|
|
|
|
|
2024-01-20 22:36:01 +01:00
|
|
|
@spec for_user_query(Ecto.UUID.t()) :: Ecto.Query.t()
|
2024-02-26 23:45:02 +01:00
|
|
|
def for_user_query(user_id, folder_id \\ nil) do
|
2019-04-14 14:45:56 +02:00
|
|
|
Bookmark
|
|
|
|
|> where(user_id: ^user_id)
|
2024-02-26 23:45:02 +01:00
|
|
|
|> maybe_filter_by_folder(folder_id)
|
2019-04-14 14:45:56 +02:00
|
|
|
|> join(:inner, [b], activity in assoc(b, :activity))
|
|
|
|
|> preload([b, a], activity: a)
|
|
|
|
end
|
|
|
|
|
2024-02-26 23:45:02 +01:00
|
|
|
defp maybe_filter_by_folder(query, nil), do: query
|
|
|
|
|
|
|
|
defp maybe_filter_by_folder(query, folder_id) do
|
|
|
|
query
|
|
|
|
|> where(folder_id: ^folder_id)
|
|
|
|
end
|
|
|
|
|
2019-04-27 22:06:46 +02:00
|
|
|
def get(user_id, activity_id) do
|
|
|
|
Bookmark
|
|
|
|
|> where(user_id: ^user_id)
|
|
|
|
|> where(activity_id: ^activity_id)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2024-01-20 22:36:01 +01:00
|
|
|
@spec destroy(Ecto.UUID.t(), Ecto.UUID.t()) ::
|
2024-01-20 22:47:50 +01:00
|
|
|
{:ok, Bookmark.t()} | {:error, Ecto.Changeset.t()}
|
2019-04-14 14:45:56 +02: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
|
2024-02-26 23:45:02 +01:00
|
|
|
|
|
|
|
def set_folder(bookmark, folder_id) do
|
|
|
|
bookmark
|
|
|
|
|> cast(%{folder_id: folder_id}, [:folder_id])
|
|
|
|
|> validate_required([:folder_id])
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
2019-04-14 14:45:56 +02:00
|
|
|
end
|