From 697104fe96990687d9b7c212f8c24d9db379e0bb Mon Sep 17 00:00:00 2001 From: Mayel de Borniol Date: Thu, 25 Jan 2024 08:50:25 +0000 Subject: [PATCH] https://github.com/bonfire-networks/bonfire-app/issues/831 --- lib/categories.ex | 12 ++-- lib/category.ex | 33 ++++++++--- lib/category_queries.ex | 13 +++-- lib/tree.ex | 9 +++ lib/web/classify_live_handler.ex | 16 +----- .../category_header_aside_live.sface | 5 +- .../components/create_old/new_label_live.ex | 25 -------- .../create_old/new_label_live.sface | 57 ------------------- .../components/hero/category_hero_live.sface | 9 +-- lib/web/components/modal/tag_modal_live.ex | 6 -- lib/web/components/modal/tag_modal_live.sface | 33 ----------- 11 files changed, 57 insertions(+), 161 deletions(-) delete mode 100644 lib/web/components/create_old/new_label_live.ex delete mode 100644 lib/web/components/create_old/new_label_live.sface delete mode 100644 lib/web/components/modal/tag_modal_live.ex delete mode 100644 lib/web/components/modal/tag_modal_live.sface diff --git a/lib/categories.ex b/lib/categories.ex index b1b8670..657a262 100644 --- a/lib/categories.ex +++ b/lib/categories.ex @@ -95,7 +95,7 @@ defmodule Bonfire.Classify.Categories do def create(creator, %{facet: facet} = params, is_local?) when not is_nil(facet) do - with attrs <- attrs_prepare(creator, params, is_local?) do + with attrs <- attrs_prepare(creator, params, is_local?) |> debug("attrs prepared") do do_create(creator, attrs, is_local?) end end @@ -108,7 +108,9 @@ defmodule Bonfire.Classify.Categories do # TODO: check that the category doesn't already exist (same name and parent) # debug(is_local?) - cs = Category.create_changeset(creator, attrs, is_local?) + cs = + Category.create_changeset(creator, attrs, is_local?) + |> debug() repo().transact_with(fn -> with {:ok, category} <- repo().insert(cs) do @@ -132,7 +134,7 @@ defmodule Bonfire.Classify.Categories do category ) - if is_local? do + if is_local? && creator do if attrs[:without_character] not in [true, "true"], do: Utils.maybe_apply(Bonfire.Social.Graph.Follows, :follow, [ @@ -141,8 +143,8 @@ defmodule Bonfire.Classify.Categories do skip_boundary_check: true ]) - # add to my own to likes by default - Utils.maybe_apply(Bonfire.Social.Likes, :do_like, [ + # add to my own to bookmarls by default + Utils.maybe_apply(Bonfire.Social.Bookmarks, :bookmark, [ creator, category, skip_boundary_check: true diff --git a/lib/category.ex b/lib/category.ex index 33a636f..644636a 100644 --- a/lib/category.ex +++ b/lib/category.ex @@ -29,7 +29,7 @@ defmodule Bonfire.Classify.Category do # pointable_schema do # field(:id, Needle.ULID, autogenerate: true) - field :type, Ecto.Enum, values: [group: 1, topic: 2] + field :type, Ecto.Enum, values: [group: 1, topic: 2, label: 3] # materialized path for trees has_one(:tree, Tree, foreign_key: :id, on_replace: :update) @@ -78,9 +78,7 @@ defmodule Bonfire.Classify.Category do # flex_schema(:bonfire_classify) end - def create_changeset(creator, attrs, is_local? \\ true) - - def create_changeset(nil, attrs, is_local?) do + def base_create_changeset(attrs, is_local?) do %Category{} |> Changesets.cast(attrs, @cast) |> Changeset.change( @@ -90,8 +88,16 @@ defmodule Bonfire.Classify.Category do |> common_changeset(attrs, is_local?) end + def create_changeset(creator, attrs, is_local? \\ true) + + def create_changeset(nil, attrs, is_local?) do + base_create_changeset(attrs, is_local?) + |> Tree.put_tree(attrs[:custodian], attrs[:parent_category]) + |> debug("cswithtree") + end + def create_changeset(creator, attrs, is_local?) do - create_changeset(nil, attrs, is_local?) + base_create_changeset(attrs, is_local?) |> Changesets.put_assoc(:created, %{creator_id: Map.get(creator, :id, nil)}) |> Tree.put_tree(attrs[:custodian] || creator, attrs[:parent_category]) |> debug("cswithtree") @@ -142,8 +148,21 @@ defmodule Bonfire.Classify.Category do defp common_changeset(changeset, attrs, is_local? \\ true) + defp common_changeset(changeset, %{without_character: without_character} = attrs, _is_local?) + when without_character in [true, "true"] do + # debug(attrs) + + changeset + |> Changeset.cast_assoc(:character, + #  to allow for non-character labels + required: false, + with: &Bonfire.Me.Characters.changeset/2 + ) + |> more_common_changeset(attrs) + end + defp common_changeset(changeset, attrs, _is_local? = true) do - debug(attrs) + # debug(attrs) changeset |> Changeset.cast_assoc(:character, @@ -156,7 +175,7 @@ defmodule Bonfire.Classify.Category do defp common_changeset(changeset, attrs, _is_local? = false) do changeset |> Changeset.cast_assoc(:character, - required: true, + required: false, with: &Bonfire.Me.Characters.remote_changeset/2 ) |> more_common_changeset(attrs) diff --git a/lib/category_queries.ex b/lib/category_queries.ex index 7f41257..88d0806 100644 --- a/lib/category_queries.ex +++ b/lib/category_queries.ex @@ -7,6 +7,7 @@ defmodule Bonfire.Classify.Category.Queries do alias Bonfire.Classify.Tree import Bonfire.Common.Repo.Utils, only: [match_admin: 0] + alias Bonfire.Common.Types def query(q \\ Category) @@ -103,12 +104,12 @@ defmodule Bonfire.Classify.Category.Queries do ## by field values - def filter(q, {:id, id}) when is_binary(id) do - where(q, [category: f], f.id == ^id) + def filter(q, {:id, id}) when is_binary(id) or is_map(id) do + where(q, [category: f], f.id == ^Types.ulid(id)) end def filter(q, {:id, ids}) when is_list(ids) do - where(q, [category: f], f.id in ^ids) + where(q, [category: f], f.id in ^Types.ulids(ids)) end def filter(q, {:username, username}) when is_binary(username) do @@ -136,11 +137,11 @@ defmodule Bonfire.Classify.Category.Queries do do: where(q, [category: c], c.id <= ^id) # get children of category - def filter(q, {:parent_category, id}) when is_binary(id), - do: where(q, [tree: t], t.parent_id == ^id) + def filter(q, {:parent_category, id}) when is_binary(id) or is_map(id), + do: where(q, [tree: t], t.parent_id == ^Types.ulid(id)) def filter(q, {:parent_category, ids}) when is_list(ids), - do: where(q, [tree: t], t.parent_id in ^ids) + do: where(q, [tree: t], t.parent_id in ^Types.ulids(ids)) # get by caretaker # def filter(q, {:caretaker, id}) when is_binary(id), diff --git a/lib/tree.ex b/lib/tree.ex index ffc628e..7cf5336 100644 --- a/lib/tree.ex +++ b/lib/tree.ex @@ -1,4 +1,5 @@ defmodule Bonfire.Classify.Tree do + @moduledoc "A mixin used to record parent/child relationships between categories (eg. a topic that belongs to a group) and between objects and categories (eg. a post was published in a topic)" use Needle.Mixin, otp_app: :bonfire_classify, source: "category_tree" @@ -16,7 +17,9 @@ defmodule Bonfire.Classify.Tree do alias Ecto.Changeset mixin_schema do + # parent is always a category (keeping in mind that means one of: Group, Topic, Label...) belongs_to(:parent, Category, foreign_key: :parent_id) + # custodian can be for example a user or category belongs_to(:custodian, Pointer, foreign_key: :custodian_id) # Kept updated by triggers. Total replies = direct replies + nested replies. field(:direct_children_count, :integer, default: 0) @@ -29,10 +32,16 @@ defmodule Bonfire.Classify.Tree do @cast [:parent_id, :custodian_id] # @required [:parent_id] + def put_tree(changeset, custodian, parent) + def put_tree(changeset, %Category{} = custodian_group, nil) do put_tree(changeset, custodian_group, custodian_group) end + def put_tree(changeset, nil, %Category{} = custodian_group) do + put_tree(changeset, custodian_group, custodian_group) + end + def put_tree(changeset, custodian, nil) do changeset |> Changesets.put_assoc(:tree, new_tree(changeset, custodian)) diff --git a/lib/web/classify_live_handler.ex b/lib/web/classify_live_handler.ex index 3227f3c..fe71ee9 100644 --- a/lib/web/classify_live_handler.ex +++ b/lib/web/classify_live_handler.ex @@ -28,7 +28,7 @@ defmodule Bonfire.Classify.LiveHandler do [:default, preload: :follow_count], current_user: current_user ]) do - if category.id == Bonfire.UI.Topics.LabelsLive.label_id() do + if category.id == maybe_apply(Bonfire.Label.Web.LabelsLive, :label_id) do {:ok, socket |> redirect_to(~p"/labels")} @@ -352,20 +352,6 @@ defmodule Bonfire.Classify.LiveHandler do new(attrs, socket) end - def handle_event("autocomplete", %{"input" => input}, socket) do - suggestions = - Bonfire.Tag.Autocomplete.tag_lookup_public( - input, - Bonfire.Classify.Category - ) - |> debug() - - {:noreply, - assign(socket, - autocomplete: (e(socket.assigns, :autocomplete, []) ++ suggestions) |> Enum.uniq() - )} - end - def handle_event("input_category", attrs, socket) do Bonfire.UI.Common.SmartInput.LiveHandler.assign_open(socket.assigns[:__context__], create_object_type: :category, diff --git a/lib/web/components/category_header_aside/category_header_aside_live.sface b/lib/web/components/category_header_aside/category_header_aside_live.sface index d138202..9b88681 100644 --- a/lib/web/components/category_header_aside/category_header_aside_live.sface +++ b/lib/web/components/category_header_aside/category_header_aside_live.sface @@ -17,8 +17,7 @@ >
  • --}
  • "/general"} + to={"#{path(@category, :settings)}/general"} class="flex items-center gap-2 text-sm text-base-content/70" > <#Icon iconify="ph:gear-fill" class="w-4 h-4 shrink-0" /> diff --git a/lib/web/components/create_old/new_label_live.ex b/lib/web/components/create_old/new_label_live.ex deleted file mode 100644 index 425bace..0000000 --- a/lib/web/components/create_old/new_label_live.ex +++ /dev/null @@ -1,25 +0,0 @@ -defmodule Bonfire.Classify.Web.NewLabelLive do - use Bonfire.UI.Common.Web, :stateless_component - - prop category, :any, default: nil - # prop object, :any, default: nil - prop context_id, :any, default: nil - - prop smart_input_opts, :map, default: %{} - prop textarea_class, :css_class, required: false - # unused but workaround surface "invalid value for property" issue - prop textarea_container_class, :css_class - prop to_boundaries, :any, default: nil - prop open_boundaries, :boolean, default: false - prop create_object_type, :any, default: :category - prop to_circles, :list, default: [] - prop exclude_circles, :list, default: [] - prop showing_within, :atom, default: nil - prop uploads, :any, default: nil - prop uploaded_files, :list, default: nil - - slot header - - @behaviour Bonfire.UI.Common.SmartInputModule - def smart_input_module, do: [:label] -end diff --git a/lib/web/components/create_old/new_label_live.sface b/lib/web/components/create_old/new_label_live.sface deleted file mode 100644 index a5d6789..0000000 --- a/lib/web/components/create_old/new_label_live.sface +++ /dev/null @@ -1,57 +0,0 @@ -
    - <#slot {@header} /> - -
    -
    - - - - {!--
    - -
    --} - -
    -
    {l("Define a label")}
    - -