1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2024 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.BookmarkFolder do
11 alias Pleroma.BookmarkFolder
16 @type t :: %__MODULE__{}
17 @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
19 schema "bookmark_folders" do
21 field(:emoji, :string)
23 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
28 def get_by_id(id), do: Repo.get_by(BookmarkFolder, id: id)
30 def create(user_id, name, emoji \\ nil) do
38 [:user_id, :name, :emoji]
40 |> validate_required([:user_id, :name])
43 |> unique_constraint([:user_id, :name])
47 def update(folder_id, name, emoji \\ nil) do
58 |> unique_constraint([:user_id, :name])
62 defp fix_emoji(changeset) do
63 with {:emoji_field, emoji} when is_binary(emoji) <-
64 {:emoji_field, get_field(changeset, :emoji)},
65 {:fixed_emoji, emoji} <-
68 |> Pleroma.Emoji.fully_qualify_emoji()
69 |> Pleroma.Emoji.maybe_quote()} do
70 put_change(changeset, :emoji, emoji)
72 {:emoji_field, _} -> changeset
76 defp validate_emoji(changeset) do
77 validate_change(changeset, :emoji, fn
82 if Emoji.unicode?(emoji) or valid_local_custom_emoji?(emoji) do
85 [emoji: "Invalid emoji"]
90 defp valid_local_custom_emoji?(emoji) do
91 with %{file: _path} <- Emoji.get(emoji) do
98 def delete(folder_id) do
100 |> Repo.get_by(id: folder_id)
104 def for_user(user_id) do
106 |> where(user_id: ^user_id)
110 def belongs_to_user?(folder_id, user_id) do
112 |> where(id: ^folder_id, user_id: ^user_id)