Skip to content

Commit

Permalink
https://github.com/bonfire-networks/bonfire-app/issues/831
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Jan 25, 2024
1 parent adde7f6 commit 697104f
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 161 deletions.
12 changes: 7 additions & 5 deletions lib/categories.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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, [
Expand All @@ -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
Expand Down
33 changes: 26 additions & 7 deletions lib/category.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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")
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions lib/category_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions lib/tree.ex
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand All @@ -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))
Expand Down
16 changes: 1 addition & 15 deletions lib/web/classify_live_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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")}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
>
<li
class="create_new_topic"
module={maybe_component(Bonfire.UI.Topics.NewTopicLive, @__context__) and
Bonfire.Boundaries.can?(@__context__, :edit, @category) && current_user_id(@__context__) &&
:if={Bonfire.Boundaries.can?(@__context__, :edit, @category) && current_user_id(@__context__) &&
@showing_within == :group}
>
<StatelessComponent
Expand Down Expand Up @@ -50,7 +49,7 @@
</li> --}
<li :if={current_user_id(@__context__)}>
<LiveRedirect
to={path(@category, :settings) <> "/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" />
Expand Down
25 changes: 0 additions & 25 deletions lib/web/components/create_old/new_label_live.ex

This file was deleted.

57 changes: 0 additions & 57 deletions lib/web/components/create_old/new_label_live.sface

This file was deleted.

9 changes: 5 additions & 4 deletions lib/web/components/hero/category_hero_live.sface
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
class="mb-5 normal-case btn btn-outline btn-sm border-base-content/70"
to={category_link(e(@category, :parent_category, :parent_category, nil))}
>
{e(@category, :parent_category, :parent_category, :profile, :name, "")}
{e(@category, :parent_category, :parent_category, :name, nil) ||
e(@category, :parent_category, :parent_category, :profile, :name, nil)}
</LiveRedirect>
{/if}

Expand All @@ -13,7 +14,7 @@
class="mb-5 normal-case btn btn-outline btn-sm border-base-content/70"
to={category_link(e(@category, :parent_category, nil))}
>
{e(@category, :parent_category, :profile, :name, "")}
{e(@category, :parent_category, :name, nil) || e(@category, :parent_category, :profile, :name, nil)}
</LiveRedirect>
{/if}
<div class="flex items-center justify-between w-full">
Expand All @@ -23,7 +24,7 @@
style={"background-image: url('#{Media.image_url(@category) || Media.banner_url(@category)}')"}
/>
<div class="flex items-center gap-2 text-xl font-semibold responsive">
<span>{e(@category, :profile, :name, "")}</span>
<span>{e(@category, :name, nil) || e(@category, :profile, :name, nil)}</span>
</div>
{!-- <Bonfire.Classify.Web.BreadcrumbsLive
category={@category}
Expand Down Expand Up @@ -70,7 +71,7 @@
else: ~p"/labels/#{e(category, :id, nil)}"}
class="text-sm !h-[2rem] hover:bg-base-content/5 tracking-wide badge-outline opacity-60 rounded badge badge-lg"
>
{e(category, :profile, :name, l("Untitled topic"))}
{e(category, :name, nil) || e(category, :profile, :name, l("Untitled topic"))}
</LiveRedirect>
</div>
</div>
6 changes: 0 additions & 6 deletions lib/web/components/modal/tag_modal_live.ex

This file was deleted.

33 changes: 0 additions & 33 deletions lib/web/components/modal/tag_modal_live.sface

This file was deleted.

0 comments on commit 697104f

Please sign in to comment.