Add a workspace for tagging technique themes by family (#194)

An admin picks a technique theme and sees candidate endgames grouped by Key
with counts. Expanding a family previews its boards without starting a puzzle,
and selecting positions applies the theme in bulk as curated. Large families are
paginated so a 600-endgame group stays usable, and curated links can be removed
from the same surface.

Removal is scoped to curated links, so derived and community links are never
deleted by hand. Material themes are refused because derivation owns them.

Closes #187
This commit is contained in:
2026-11-17 21:55:37 +01:00
committed by GitHub
parent 946fc38756
commit e6f08e0ca6
8 changed files with 775 additions and 4 deletions
+44 -4
View File
@@ -111,8 +111,13 @@ defmodule Chesstrainer.Endgames do
@doc """
Returns `{key, count}` groups for endgames matching the filters, sorted by key.
Filters is a map with optional `:themes` (a list of theme ids; an endgame must
carry any one of them) and `:key` (an exact Key).
Filters is a map with optional:
* `:themes` - a list of theme ids; an endgame must carry any one of them
* `:key` - an exact Key
* `:with_theme` - `{theme_id, source}`; only endgames linked to the theme
with that source
* `:without_theme` - a theme id; only endgames not linked to the theme at all
"""
def list_key_groups(filters \\ %{}) do
Endgame
@@ -125,20 +130,31 @@ defmodule Chesstrainer.Endgames do
@doc """
Returns the endgames for one Key, matching the filters, with themes preloaded.
Accepts `:limit` and `:offset` options for paginating large Key groups.
"""
def list_endgames_for_key(key, filters \\ %{}) do
def list_endgames_for_key(key, filters \\ %{}, opts \\ []) do
skip = Keyword.get(opts, :offset, 0)
Endgame
|> apply_filters(filters)
|> where([e], e.key == ^key)
|> preload(:themes)
|> order_by([e], asc: e.fen)
|> order_by([e], asc: e.fen, asc: e.id)
|> limit_query(Keyword.get(opts, :limit))
|> offset(^skip)
|> Repo.all()
end
defp limit_query(query, nil), do: query
defp limit_query(query, limit), do: limit(query, ^limit)
defp apply_filters(query, filters) do
query
|> filter_themes(Map.get(filters, :themes, []))
|> filter_key(Map.get(filters, :key))
|> filter_with_theme(Map.get(filters, :with_theme))
|> filter_without_theme(Map.get(filters, :without_theme))
end
defp filter_themes(query, []), do: query
@@ -156,6 +172,30 @@ defmodule Chesstrainer.Endgames do
defp filter_key(query, key) when key in [nil, ""], do: query
defp filter_key(query, key), do: where(query, [e], e.key == ^key)
defp filter_with_theme(query, nil), do: query
defp filter_with_theme(query, {theme_id, source}) do
endgame_ids =
from(t in "endgames_themes",
where: t.theme_id == ^theme_id and t.source == ^to_string(source),
select: t.endgame_id
)
where(query, [e], e.id in subquery(endgame_ids))
end
defp filter_without_theme(query, nil), do: query
defp filter_without_theme(query, theme_id) do
endgame_ids =
from(t in "endgames_themes",
where: t.theme_id == ^theme_id,
select: t.endgame_id
)
where(query, [e], e.id not in subquery(endgame_ids))
end
@doc """
Gets a single endgame.
+55
View File
@@ -263,6 +263,61 @@ defmodule Chesstrainer.Themes do
:ok
end
@link_sources [:curated, :community]
@doc """
Links `theme` to many endgames at once with the given source.
Defaults to `:curated`, which is what the admin tagging workspace writes.
Existing links are left alone, so re-applying is safe. Returns the number of
endgames the link was attempted for.
"""
@spec tag_endgames(Theme.t(), [Ecto.UUID.t()], :curated | :community) ::
{:ok, non_neg_integer()}
def tag_endgames(%Theme{id: theme_id}, endgame_ids, source \\ :curated)
when is_list(endgame_ids) and source in @link_sources do
now = DateTime.utc_now() |> DateTime.truncate(:second)
endgame_ids
|> Enum.uniq()
|> Enum.map(fn endgame_id ->
%{
endgame_id: Ecto.UUID.dump!(endgame_id),
theme_id: theme_id,
source: to_string(source),
inserted_at: now
}
end)
|> Enum.chunk_every(5_000)
|> Enum.each(fn rows ->
Repo.insert_all("endgames_themes", rows, on_conflict: :nothing)
end)
{:ok, length(Enum.uniq(endgame_ids))}
end
@doc """
Removes a link from `theme` to one endgame, but only if it has the given
source.
The tagging workspace removes `:curated` links only, so a derived or
community link can never be deleted by hand. Returns the number removed.
"""
@spec untag_endgame(Theme.t(), Ecto.UUID.t(), :curated | :community) ::
{:ok, non_neg_integer()}
def untag_endgame(%Theme{id: theme_id}, endgame_id, source \\ :curated)
when source in @link_sources do
{count, _} =
Repo.delete_all(
from l in "endgames_themes",
where:
l.theme_id == ^theme_id and l.endgame_id == type(^endgame_id, :binary_id) and
l.source == ^to_string(source)
)
{:ok, count}
end
@doc """
Replaces the derived theme links for one endgame.