2022-07-25 20:08:40 -04:00
|
|
|
defmodule Memex.Notes do
|
|
|
|
@moduledoc """
|
|
|
|
The Notes context.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Ecto.Query, warn: false
|
2022-11-17 22:38:52 -05:00
|
|
|
alias Memex.{Accounts.User, Notes.Note, Repo}
|
2022-07-25 20:08:40 -04:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of notes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
iex> list_notes(%User{id: 123})
|
|
|
|
[%Note{}, ...]
|
|
|
|
|
|
|
|
iex> list_notes("my note", %User{id: 123})
|
2022-11-26 14:51:18 -05:00
|
|
|
[%Note{slug: "my note"}, ...]
|
2022-11-17 22:38:52 -05:00
|
|
|
|
|
|
|
"""
|
|
|
|
@spec list_notes(User.t()) :: [Note.t()]
|
2022-11-19 00:21:14 -05:00
|
|
|
@spec list_notes(search :: String.t() | nil, User.t()) :: [Note.t()]
|
|
|
|
def list_notes(search \\ nil, user)
|
|
|
|
|
|
|
|
def list_notes(search, %{id: user_id}) when search |> is_nil() or search == "" do
|
2022-11-26 14:51:18 -05:00
|
|
|
Repo.all(from n in Note, where: n.user_id == ^user_id, order_by: n.slug)
|
2022-11-17 22:38:52 -05:00
|
|
|
end
|
|
|
|
|
2022-11-19 00:21:14 -05:00
|
|
|
def list_notes(search, %{id: user_id}) when search |> is_binary() do
|
|
|
|
trimmed_search = String.trim(search)
|
|
|
|
|
|
|
|
Repo.all(
|
|
|
|
from n in Note,
|
|
|
|
where: n.user_id == ^user_id,
|
|
|
|
where:
|
|
|
|
fragment(
|
2022-11-27 21:10:54 -05:00
|
|
|
"search @@ websearch_to_tsquery('english', ?)",
|
2022-11-19 00:21:14 -05:00
|
|
|
^trimmed_search
|
|
|
|
),
|
|
|
|
order_by: {
|
|
|
|
:desc,
|
|
|
|
fragment(
|
2022-11-27 21:10:54 -05:00
|
|
|
"ts_rank_cd(search, websearch_to_tsquery('english', ?), 4)",
|
2022-11-19 00:21:14 -05:00
|
|
|
^trimmed_search
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
@doc """
|
|
|
|
Returns the list of public notes for viewing
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> list_public_notes()
|
2022-07-25 20:08:40 -04:00
|
|
|
[%Note{}, ...]
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
iex> list_public_notes("my note")
|
2022-11-26 14:51:18 -05:00
|
|
|
[%Note{slug: "my note"}, ...]
|
2022-07-25 20:08:40 -04:00
|
|
|
"""
|
2022-11-17 22:38:52 -05:00
|
|
|
@spec list_public_notes() :: [Note.t()]
|
2022-11-19 00:21:14 -05:00
|
|
|
@spec list_public_notes(search :: String.t() | nil) :: [Note.t()]
|
|
|
|
def list_public_notes(search \\ nil)
|
|
|
|
|
|
|
|
def list_public_notes(search) when search |> is_nil() or search == "" do
|
2022-11-26 14:51:18 -05:00
|
|
|
Repo.all(from n in Note, where: n.visibility == :public, order_by: n.slug)
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
2022-11-19 00:21:14 -05:00
|
|
|
def list_public_notes(search) when search |> is_binary() do
|
|
|
|
trimmed_search = String.trim(search)
|
|
|
|
|
|
|
|
Repo.all(
|
|
|
|
from n in Note,
|
|
|
|
where: n.visibility == :public,
|
|
|
|
where:
|
|
|
|
fragment(
|
2022-11-27 21:10:54 -05:00
|
|
|
"search @@ websearch_to_tsquery('english', ?)",
|
2022-11-19 00:21:14 -05:00
|
|
|
^trimmed_search
|
|
|
|
),
|
|
|
|
order_by: {
|
|
|
|
:desc,
|
|
|
|
fragment(
|
2022-11-27 21:10:54 -05:00
|
|
|
"ts_rank_cd(search, websearch_to_tsquery('english', ?), 4)",
|
2022-11-19 00:21:14 -05:00
|
|
|
^trimmed_search
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-07-25 20:08:40 -04:00
|
|
|
@doc """
|
|
|
|
Gets a single note.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Note does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
iex> get_note!(123, %User{id: 123})
|
2022-07-25 20:08:40 -04:00
|
|
|
%Note{}
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
iex> get_note!(456, %User{id: 123})
|
2022-07-25 20:08:40 -04:00
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
2022-11-17 22:38:52 -05:00
|
|
|
@spec get_note!(Note.id(), User.t()) :: Note.t()
|
|
|
|
def get_note!(id, %{id: user_id}) do
|
|
|
|
Repo.one!(
|
|
|
|
from n in Note,
|
|
|
|
where: n.id == ^id,
|
|
|
|
where: n.user_id == ^user_id or n.visibility in [:public, :unlisted]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_note!(id, _invalid_user) do
|
|
|
|
Repo.one!(
|
|
|
|
from n in Note,
|
|
|
|
where: n.id == ^id,
|
|
|
|
where: n.visibility in [:public, :unlisted]
|
|
|
|
)
|
|
|
|
end
|
2022-07-25 20:08:40 -04:00
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
@doc """
|
|
|
|
Gets a single note by slug.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Note does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_note_by_slug("my-note", %User{id: 123})
|
|
|
|
%Note{}
|
|
|
|
|
|
|
|
iex> get_note_by_slug("my-note", %User{id: 123})
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
|
|
|
@spec get_note_by_slug(Note.slug(), User.t()) :: Note.t() | nil
|
|
|
|
def get_note_by_slug(slug, %{id: user_id}) do
|
|
|
|
Repo.one(
|
|
|
|
from n in Note,
|
|
|
|
where: n.slug == ^slug,
|
|
|
|
where: n.user_id == ^user_id or n.visibility in [:public, :unlisted]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_note_by_slug(slug, _invalid_user) do
|
|
|
|
Repo.one(
|
|
|
|
from n in Note,
|
|
|
|
where: n.slug == ^slug,
|
|
|
|
where: n.visibility in [:public, :unlisted]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-07-25 20:08:40 -04:00
|
|
|
@doc """
|
|
|
|
Creates a note.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
iex> create_note(%{field: value}, %User{id: 123})
|
2022-07-25 20:08:40 -04:00
|
|
|
{:ok, %Note{}}
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
iex> create_note(%{field: bad_value}, %User{id: 123})
|
2022-07-25 20:08:40 -04:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
2022-11-17 22:38:52 -05:00
|
|
|
@spec create_note(User.t()) :: {:ok, Note.t()} | {:error, Note.changeset()}
|
|
|
|
@spec create_note(attrs :: map(), User.t()) :: {:ok, Note.t()} | {:error, Note.changeset()}
|
|
|
|
def create_note(attrs \\ %{}, user) do
|
|
|
|
Note.create_changeset(attrs, user) |> Repo.insert()
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a note.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
iex> update_note(note, %{field: new_value}, %User{id: 123})
|
2022-07-25 20:08:40 -04:00
|
|
|
{:ok, %Note{}}
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
iex> update_note(note, %{field: bad_value}, %User{id: 123})
|
2022-07-25 20:08:40 -04:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
2022-11-17 22:38:52 -05:00
|
|
|
@spec update_note(Note.t(), attrs :: map(), User.t()) ::
|
|
|
|
{:ok, Note.t()} | {:error, Note.changeset()}
|
|
|
|
def update_note(%Note{} = note, attrs, user) do
|
2022-07-25 20:08:40 -04:00
|
|
|
note
|
2022-11-17 22:38:52 -05:00
|
|
|
|> Note.update_changeset(attrs, user)
|
2022-07-25 20:08:40 -04:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a note.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
iex> delete_note(%Note{user_id: 123}, %User{id: 123})
|
|
|
|
{:ok, %Note{}}
|
|
|
|
|
|
|
|
iex> delete_note(%Note{}, %User{role: :admin})
|
2022-07-25 20:08:40 -04:00
|
|
|
{:ok, %Note{}}
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
iex> delete_note(%Note{}, %User{id: 123})
|
2022-07-25 20:08:40 -04:00
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
2022-11-17 22:38:52 -05:00
|
|
|
@spec delete_note(Note.t(), User.t()) :: {:ok, Note.t()} | {:error, Note.changeset()}
|
|
|
|
def delete_note(%Note{user_id: user_id} = note, %{id: user_id}) do
|
|
|
|
note |> Repo.delete()
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_note(%Note{} = note, %{role: :admin}) do
|
|
|
|
note |> Repo.delete()
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an `%Ecto.Changeset{}` for tracking note changes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
iex> change_note(note, %User{id: 123})
|
2022-07-25 20:08:40 -04:00
|
|
|
%Ecto.Changeset{data: %Note{}}
|
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
iex> change_note(note, %{slug: "new slug"}, %User{id: 123})
|
2022-11-17 22:38:52 -05:00
|
|
|
%Ecto.Changeset{data: %Note{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
@spec change_note(Note.t(), User.t()) :: Note.changeset()
|
|
|
|
@spec change_note(Note.t(), attrs :: map(), User.t()) :: Note.changeset()
|
|
|
|
def change_note(%Note{} = note, attrs \\ %{}, user) do
|
|
|
|
note |> Note.update_changeset(attrs, user)
|
|
|
|
end
|
2023-03-18 23:09:36 -04:00
|
|
|
|
2024-02-23 22:17:56 -05:00
|
|
|
@spec owner_or_admin?(Note.t(), User.t()) :: boolean()
|
|
|
|
def owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true
|
|
|
|
def owner_or_admin?(_context, %{role: :admin}), do: true
|
|
|
|
def owner_or_admin?(_context, _other_user), do: false
|
2023-03-18 23:09:36 -04:00
|
|
|
|
2024-02-23 22:18:25 -05:00
|
|
|
@spec owner?(Note.t(), User.t()) :: boolean()
|
|
|
|
def owner?(%{user_id: user_id}, %{id: user_id}), do: true
|
|
|
|
def owner?(_context, _other_user), do: false
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|