Compare commits
2 Commits
10e2355817
...
95088f2d42
| Author | SHA1 | Date | |
|---|---|---|---|
| 95088f2d42 | |||
| 8c7698adbe |
30
CODESMELL.md
30
CODESMELL.md
@@ -436,8 +436,34 @@ Total: 2245 lines now live in focused submodules; the remaining 647 in `BDS.Gene
|
|||||||
**Remaining work in this priority:**
|
**Remaining work in this priority:**
|
||||||
|
|
||||||
- ✅ `BDS.Generation` — done (76% reduction, 647 lines remaining is acceptable for a coordinator).
|
- ✅ `BDS.Generation` — done (76% reduction, 647 lines remaining is acceptable for a coordinator).
|
||||||
- ⏳ `BDS.Desktop.ShellLive` (2607) — next target.
|
- 🔄 `BDS.Desktop.ShellLive` (2607 → 1545, 41% reduction). Submodules extracted under `lib/bds/desktop/shell_live/`:
|
||||||
- ⏳ `BDS.Posts` (1781).
|
|
||||||
|
| Module | Lines | Responsibility |
|
||||||
|
|---|---|---|
|
||||||
|
| `TitlebarMenu` | 181 | Menu group definition, dropdown items, open/close/hover/keydown |
|
||||||
|
| `CliSync` | 133 | CLI watcher entity-change application + tab refresh |
|
||||||
|
| `PanelRenderer` | 290 | Tasks/output/post-links/git-log panel rendering + editor toolbar |
|
||||||
|
| `TabHelpers` | 99 | Tab title/subtitle/icon, route atom mapping, post/media labels |
|
||||||
|
| `TaskLocalization` | 80 | Task status localization, editor-meta translation |
|
||||||
|
| `ChatSurface` | 233 | Chat-surface action dispatch, assistant message helpers |
|
||||||
|
| `SidebarCreate` | 131 | New post/media/script/template/import sidebar creation |
|
||||||
|
| `Layout` | 53 | Sync-layout, resize-panel, parse-width, ignore-shortcut |
|
||||||
|
| `ShellCommandRunner` | 95 | `apply_shell_command` + `apply_result` dispatch |
|
||||||
|
| `SessionUtil` | 49 | Workbench-session restore, project-name picker, task-result tracking |
|
||||||
|
|
||||||
|
Coordinator (`shell_live.ex`) now 1545 lines containing only `mount/3`, `render/1`, `handle_event/3`, `handle_info/2` clauses, plus thin dispatchers and small editor-assign helpers.
|
||||||
|
- ⏳ `BDS.Posts` (1781 → 569, 68% reduction). Submodules extracted under `lib/bds/posts/`:
|
||||||
|
|
||||||
|
| Module | Lines | Responsibility |
|
||||||
|
|---|---|---|
|
||||||
|
| `Slugs` | 86 | `slug_available/3`, `unique_slug_for_title/3`, `unique`, `unique_for_import`, `default_source` |
|
||||||
|
| `AutoTranslation` | 176 | `maybe_schedule/1`, missing-language detection, post + cascading media auto-translate task scheduling |
|
||||||
|
| `FileSync` | 146 | Post/translation relative-path computation, frontmatter serialization, body extraction, on-disk delete |
|
||||||
|
| `TranslationValidation` | 464 | `validate/2`, `fix_invalid/1`, invalid DB/FS issue classification, legacy report fields, canonical-language helpers, markdown-file recursion |
|
||||||
|
| `RebuildFromFiles` | 320 | `rebuild_posts_from_files/2`, `import_orphan_post_file/2`, `import_orphan_post_translation_file/2`, `parse_rebuild_file`, `upsert_post_from_file`, `upsert_post_from_rebuild_file`, `upsert_post_translation_from_rebuild_file`, `progress_callback/1`, `report_rebuild_started/3`, `report_rebuild_progress/4`, `parse_post_status`, `parse_translation_status` |
|
||||||
|
| `Translations` | 279 | `publish_post_translation/2`, `list_post_translations/1`, `upsert_post_translation/3`, `delete_post_translation/1`, `sync_post_translation_from_file/1`, `rewrite_published_post_translation/1`, `publish_translation/2`, `publish_post_translations/1`, `normalize_translation_updates`, `maybe_reopen_source_post_for_manual_translation` |
|
||||||
|
|
||||||
|
Public API on `BDS.Posts` preserved via `defdelegate` for: `slug_available/3`, `unique_slug_for_title/3`, `validate_translations/2`, `fix_invalid_translations/1`, `rebuild_posts_from_files/2`, `import_orphan_post_file/2`, `import_orphan_post_translation_file/2`, `publish_post_translation/2`, `list_post_translations/1`, `upsert_post_translation/3`, `delete_post_translation/1`, `sync_post_translation_from_file/1`, `rewrite_published_post_translation/1`. Remaining clusters in posts.ex are core CRUD (`create_post`, `update_post`, `publish_post`, `delete_post`, `archive_post`, `discard_post_changes`, `sync_post_from_file`, `rewrite_published_post`, `editor_body`), small stats (`dashboard_stats`, `post_counts_by_year_month`, ~40 lines extractable), and `rebuild_post_links` (~22 lines). Stats could be split next, but ~569 lines is a reasonable steady state.
|
||||||
- ⏳ `BDS.AI` (1711).
|
- ⏳ `BDS.AI` (1711).
|
||||||
- ⏳ `BDS.MCP` (677).
|
- ⏳ `BDS.MCP` (677).
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
233
lib/bds/desktop/shell_live/chat_surface.ex
Normal file
233
lib/bds/desktop/shell_live/chat_surface.ex
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.ChatSurface do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
import Phoenix.Component, only: [assign: 3]
|
||||||
|
|
||||||
|
alias BDS.Desktop.ShellData
|
||||||
|
alias BDS.Desktop.ShellLive.{ChatEditor, TabHelpers}
|
||||||
|
alias BDS.UI.Workbench
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Handle a chat-surface action from a chat message. Receives callbacks for
|
||||||
|
`reload_shell/2` and `open_sidebar_item/3` to remain decoupled from
|
||||||
|
`BDS.Desktop.ShellLive` private state.
|
||||||
|
"""
|
||||||
|
def handle_action(socket, params, callbacks) do
|
||||||
|
surface_id = Map.get(params, "surface-id", "")
|
||||||
|
|
||||||
|
payload =
|
||||||
|
params
|
||||||
|
|> Map.get("payload")
|
||||||
|
|> decode_payload()
|
||||||
|
|> maybe_put_form_data(socket, surface_id)
|
||||||
|
|
||||||
|
case normalize_action(Map.get(params, "action", "")) do
|
||||||
|
:open_post ->
|
||||||
|
case Map.get(payload, "postId") || Map.get(payload, "post_id") do
|
||||||
|
post_id when is_binary(post_id) and post_id != "" ->
|
||||||
|
socket
|
||||||
|
|> clear_action_error()
|
||||||
|
|> callbacks.open_sidebar.(
|
||||||
|
%{
|
||||||
|
"route" => "post",
|
||||||
|
"id" => post_id,
|
||||||
|
"title" => TabHelpers.post_title(post_id),
|
||||||
|
"subtitle" => TabHelpers.post_subtitle(post_id)
|
||||||
|
},
|
||||||
|
:pin
|
||||||
|
)
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
ChatEditor.set_action_error(
|
||||||
|
socket,
|
||||||
|
socket.assigns.current_tab.id,
|
||||||
|
"Invalid payload for openPost action",
|
||||||
|
callbacks.reload
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
:open_media ->
|
||||||
|
case Map.get(payload, "mediaId") || Map.get(payload, "media_id") do
|
||||||
|
media_id when is_binary(media_id) and media_id != "" ->
|
||||||
|
socket
|
||||||
|
|> clear_action_error()
|
||||||
|
|> callbacks.open_sidebar.(
|
||||||
|
%{
|
||||||
|
"route" => "media",
|
||||||
|
"id" => media_id,
|
||||||
|
"title" => TabHelpers.media_title(media_id),
|
||||||
|
"subtitle" => TabHelpers.media_subtitle(media_id)
|
||||||
|
},
|
||||||
|
:pin
|
||||||
|
)
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
ChatEditor.set_action_error(
|
||||||
|
socket,
|
||||||
|
socket.assigns.current_tab.id,
|
||||||
|
"Invalid payload for openMedia action",
|
||||||
|
callbacks.reload
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
:open_settings ->
|
||||||
|
socket
|
||||||
|
|> clear_action_error()
|
||||||
|
|> callbacks.open_sidebar.(
|
||||||
|
%{"route" => "settings", "id" => "settings-ai", "title" => "Settings", "subtitle" => "AI"},
|
||||||
|
:pin
|
||||||
|
)
|
||||||
|
|
||||||
|
:open_chat ->
|
||||||
|
chat_id =
|
||||||
|
Map.get(payload, "conversationId") || Map.get(payload, "conversation_id") ||
|
||||||
|
socket.assigns.current_tab.id
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> clear_action_error()
|
||||||
|
|> callbacks.open_sidebar.(
|
||||||
|
%{
|
||||||
|
"route" => "chat",
|
||||||
|
"id" => chat_id,
|
||||||
|
"title" => Map.get(payload, "title", "Chat"),
|
||||||
|
"subtitle" => Map.get(payload, "subtitle", "")
|
||||||
|
},
|
||||||
|
:pin
|
||||||
|
)
|
||||||
|
|
||||||
|
:switch_view ->
|
||||||
|
case safe_existing_atom(Map.get(payload, "view")) do
|
||||||
|
nil ->
|
||||||
|
ChatEditor.set_action_error(
|
||||||
|
socket,
|
||||||
|
socket.assigns.current_tab.id,
|
||||||
|
"Invalid payload for switchView action",
|
||||||
|
callbacks.reload
|
||||||
|
)
|
||||||
|
|
||||||
|
view ->
|
||||||
|
socket
|
||||||
|
|> clear_action_error()
|
||||||
|
|> callbacks.reload.(Workbench.click_activity(socket.assigns.workbench, view))
|
||||||
|
end
|
||||||
|
|
||||||
|
:toggle_sidebar ->
|
||||||
|
socket
|
||||||
|
|> clear_action_error()
|
||||||
|
|> callbacks.reload.(Workbench.toggle_sidebar(socket.assigns.workbench))
|
||||||
|
|
||||||
|
:toggle_panel ->
|
||||||
|
socket
|
||||||
|
|> clear_action_error()
|
||||||
|
|> callbacks.reload.(Workbench.toggle_panel(socket.assigns.workbench))
|
||||||
|
|
||||||
|
:toggle_assistant_sidebar ->
|
||||||
|
socket
|
||||||
|
|> clear_action_error()
|
||||||
|
|> callbacks.reload.(Workbench.toggle_assistant_sidebar(socket.assigns.workbench))
|
||||||
|
|
||||||
|
:unknown ->
|
||||||
|
ChatEditor.set_action_error(
|
||||||
|
socket,
|
||||||
|
socket.assigns.current_tab.id,
|
||||||
|
"Unsupported assistant action",
|
||||||
|
callbacks.reload
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def assistant_turn(prompt, socket) do
|
||||||
|
[
|
||||||
|
%{role: "user", content: prompt},
|
||||||
|
%{role: "assistant", content: assistant_reply(socket)}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
def assistant_project_name(nil), do: translated("Projects")
|
||||||
|
def assistant_project_name(project), do: project.name
|
||||||
|
|
||||||
|
def assistant_message_label("assistant"), do: translated("Assistant")
|
||||||
|
def assistant_message_label("user"), do: translated("You")
|
||||||
|
def assistant_message_label(_role), do: translated("Assistant")
|
||||||
|
|
||||||
|
def assistant_message_testid(role), do: "assistant-message-#{role}"
|
||||||
|
|
||||||
|
def update_shell_overlay(socket, updater) do
|
||||||
|
case socket.assigns[:shell_overlay] do
|
||||||
|
nil -> socket
|
||||||
|
overlay -> assign(socket, :shell_overlay, updater.(overlay))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def clear_action_error(%{assigns: %{current_tab: %{type: :chat, id: conversation_id}}} = socket) do
|
||||||
|
assign(socket, :chat_editor_action_errors, Map.delete(socket.assigns.chat_editor_action_errors, conversation_id))
|
||||||
|
end
|
||||||
|
|
||||||
|
def clear_action_error(socket), do: socket
|
||||||
|
|
||||||
|
defp decode_payload(nil), do: %{}
|
||||||
|
defp decode_payload(""), do: %{}
|
||||||
|
|
||||||
|
defp decode_payload(payload) when is_binary(payload) do
|
||||||
|
case Jason.decode(payload) do
|
||||||
|
{:ok, decoded} when is_map(decoded) -> decoded
|
||||||
|
_other -> %{}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp decode_payload(_payload), do: %{}
|
||||||
|
|
||||||
|
defp maybe_put_form_data(payload, socket, surface_id) when is_binary(surface_id) and surface_id != "" do
|
||||||
|
form_data = ChatEditor.current_surface_data(socket, surface_id)
|
||||||
|
|
||||||
|
if form_data == %{} do
|
||||||
|
payload
|
||||||
|
else
|
||||||
|
Map.put(payload, "formData", form_data)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_put_form_data(payload, _socket, _surface_id), do: payload
|
||||||
|
|
||||||
|
defp normalize_action(action) do
|
||||||
|
action
|
||||||
|
|> to_string()
|
||||||
|
|> String.replace("_", "")
|
||||||
|
|> String.downcase()
|
||||||
|
|> case do
|
||||||
|
"openpost" -> :open_post
|
||||||
|
"openmedia" -> :open_media
|
||||||
|
"opensettings" -> :open_settings
|
||||||
|
"openchat" -> :open_chat
|
||||||
|
"switchview" -> :switch_view
|
||||||
|
"setactiveview" -> :switch_view
|
||||||
|
"togglesidebar" -> :toggle_sidebar
|
||||||
|
"togglepanel" -> :toggle_panel
|
||||||
|
"openpanel" -> :toggle_panel
|
||||||
|
"toggleassistantsidebar" -> :toggle_assistant_sidebar
|
||||||
|
_other -> :unknown
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp safe_existing_atom(action) when is_binary(action) do
|
||||||
|
String.to_existing_atom(action)
|
||||||
|
rescue
|
||||||
|
ArgumentError -> nil
|
||||||
|
end
|
||||||
|
|
||||||
|
defp safe_existing_atom(_), do: nil
|
||||||
|
|
||||||
|
defp assistant_reply(socket) do
|
||||||
|
if socket.assigns.offline_mode do
|
||||||
|
ShellData.translate("Automatic AI actions stay gated by airplane mode.", %{}, socket.assigns.page_language)
|
||||||
|
else
|
||||||
|
ShellData.translate(
|
||||||
|
"The assistant sidebar chat surface is ready, but model execution is not connected yet.",
|
||||||
|
%{},
|
||||||
|
socket.assigns.page_language
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp translated(text), do: ShellData.translate(text, %{}, Process.get(:bds_ui_locale))
|
||||||
|
end
|
||||||
133
lib/bds/desktop/shell_live/cli_sync.ex
Normal file
133
lib/bds/desktop/shell_live/cli_sync.ex
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.CliSync do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
import Phoenix.Component, only: [assign: 3]
|
||||||
|
|
||||||
|
alias BDS.Media.Media
|
||||||
|
alias BDS.Posts.Post
|
||||||
|
alias BDS.Repo
|
||||||
|
alias BDS.UI.Workbench
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Apply a CLI entity change payload to the shell socket. `reload_fun` is
|
||||||
|
called with `(socket, workbench)` to refresh derived data.
|
||||||
|
"""
|
||||||
|
@spec apply_entity_change(Phoenix.LiveView.Socket.t(), map(),
|
||||||
|
(Phoenix.LiveView.Socket.t(), map() -> Phoenix.LiveView.Socket.t())) ::
|
||||||
|
Phoenix.LiveView.Socket.t()
|
||||||
|
def apply_entity_change(socket, payload, reload_fun) do
|
||||||
|
entity = Map.get(payload, :entity) || Map.get(payload, "entity") || Map.get(payload, :entity_type) || Map.get(payload, "entity_type")
|
||||||
|
|
||||||
|
entity_id =
|
||||||
|
Map.get(payload, :entity_id) || Map.get(payload, "entity_id") || Map.get(payload, :entityId) ||
|
||||||
|
Map.get(payload, "entityId")
|
||||||
|
|
||||||
|
action = normalize_action(Map.get(payload, :action) || Map.get(payload, "action"))
|
||||||
|
|
||||||
|
if is_binary(entity) and entity != "" and is_binary(entity_id) and entity_id != "" and
|
||||||
|
action in [:created, :updated, :deleted] do
|
||||||
|
{socket, workbench} = maybe_close_deleted_tab(socket, entity, entity_id, action)
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> maybe_refresh_tab_meta(entity, entity_id, action)
|
||||||
|
|> reload_fun.(workbench)
|
||||||
|
else
|
||||||
|
socket
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_close_deleted_tab(socket, "post", post_id, :deleted) do
|
||||||
|
workbench = Workbench.close_tab(socket.assigns.workbench, :post, post_id)
|
||||||
|
|
||||||
|
socket =
|
||||||
|
socket
|
||||||
|
|> assign(:workbench, workbench)
|
||||||
|
|> assign(:shell_overlay, nil)
|
||||||
|
|> assign(:tab_meta, Map.delete(socket.assigns.tab_meta, {:post, post_id}))
|
||||||
|
|> assign(:post_editor_drafts, Map.delete(socket.assigns.post_editor_drafts, post_id))
|
||||||
|
|> assign(:post_editor_active_languages, Map.delete(socket.assigns.post_editor_active_languages, post_id))
|
||||||
|
|> assign(:post_editor_tag_queries, Map.delete(socket.assigns.post_editor_tag_queries, post_id))
|
||||||
|
|> assign(:post_editor_category_queries, Map.delete(socket.assigns.post_editor_category_queries, post_id))
|
||||||
|
|> assign(:post_editor_quick_actions_open, Map.delete(socket.assigns.post_editor_quick_actions_open, post_id))
|
||||||
|
|> assign(:post_editor_modes, Map.delete(socket.assigns.post_editor_modes, post_id))
|
||||||
|
|> assign(:post_editor_expanded, Map.delete(socket.assigns.post_editor_expanded, post_id))
|
||||||
|
|> assign(:post_editor_save_states, Map.delete(socket.assigns.post_editor_save_states, post_id))
|
||||||
|
|
||||||
|
{socket, workbench}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_close_deleted_tab(socket, "media", media_id, :deleted) do
|
||||||
|
workbench = Workbench.close_tab(socket.assigns.workbench, :media, media_id)
|
||||||
|
|
||||||
|
socket =
|
||||||
|
socket
|
||||||
|
|> assign(:workbench, workbench)
|
||||||
|
|> assign(:shell_overlay, nil)
|
||||||
|
|> assign(:tab_meta, Map.delete(socket.assigns.tab_meta, {:media, media_id}))
|
||||||
|
|> assign(:media_editor_drafts, Map.delete(socket.assigns.media_editor_drafts, media_id))
|
||||||
|
|> assign(:media_editor_quick_actions_open, Map.delete(socket.assigns.media_editor_quick_actions_open, media_id))
|
||||||
|
|> assign(:media_editor_post_pickers_open, Map.delete(socket.assigns.media_editor_post_pickers_open, media_id))
|
||||||
|
|> assign(:media_editor_post_picker_queries, Map.delete(socket.assigns.media_editor_post_picker_queries, media_id))
|
||||||
|
|> assign(:media_editor_save_states, Map.delete(socket.assigns.media_editor_save_states, media_id))
|
||||||
|
|> assign(:media_editor_translation_forms, Map.delete(socket.assigns.media_editor_translation_forms, media_id))
|
||||||
|
|
||||||
|
{socket, workbench}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_close_deleted_tab(socket, _entity, _entity_id, _action), do: {socket, socket.assigns.workbench}
|
||||||
|
|
||||||
|
defp maybe_refresh_tab_meta(socket, "post", post_id, action) when action in [:created, :updated] do
|
||||||
|
maybe_put_tab_meta(socket, :post, post_id, fn ->
|
||||||
|
case Repo.get(Post, post_id) do
|
||||||
|
%Post{} = post -> %{title: post.title || post.slug || post.id, subtitle: Atom.to_string(post.status || :draft)}
|
||||||
|
_other -> nil
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_refresh_tab_meta(socket, "media", media_id, action) when action in [:created, :updated] do
|
||||||
|
maybe_put_tab_meta(socket, :media, media_id, fn ->
|
||||||
|
case Repo.get(Media, media_id) do
|
||||||
|
%Media{} = media -> %{title: media.title || media.filename || media.id, subtitle: media.filename || media.mime_type || "media"}
|
||||||
|
_other -> nil
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_refresh_tab_meta(socket, _entity, _entity_id, _action), do: socket
|
||||||
|
|
||||||
|
defp maybe_put_tab_meta(socket, route, entity_id, meta_fun) do
|
||||||
|
key = {route, entity_id}
|
||||||
|
|
||||||
|
if tab_present?(socket.assigns.workbench, key) or Map.has_key?(socket.assigns.tab_meta, key) do
|
||||||
|
case meta_fun.() do
|
||||||
|
%{} = fresh_meta ->
|
||||||
|
updated_meta = Map.update(socket.assigns.tab_meta, key, fresh_meta, &Map.merge(&1, fresh_meta))
|
||||||
|
assign(socket, :tab_meta, updated_meta)
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
socket
|
||||||
|
end
|
||||||
|
else
|
||||||
|
socket
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp tab_present?(%{tabs: tabs}, {route, entity_id}) do
|
||||||
|
Enum.any?(tabs, &(&1.type == route and &1.id == entity_id))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp normalize_action(action) when action in [:created, :updated, :deleted], do: action
|
||||||
|
|
||||||
|
defp normalize_action(action) do
|
||||||
|
action
|
||||||
|
|> to_string()
|
||||||
|
|> String.downcase()
|
||||||
|
|> case do
|
||||||
|
"created" -> :created
|
||||||
|
"updated" -> :updated
|
||||||
|
"deleted" -> :deleted
|
||||||
|
_other -> :unknown
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -41,14 +41,14 @@
|
|||||||
data-testid="window-titlebar-menu-dropdown"
|
data-testid="window-titlebar-menu-dropdown"
|
||||||
phx-click-away="close_titlebar_menu"
|
phx-click-away="close_titlebar_menu"
|
||||||
>
|
>
|
||||||
<%= for item <- titlebar_menu_dropdown_items(group) do %>
|
<%= for item <- BDS.Desktop.ShellLive.TitlebarMenu.dropdown_items(group) do %>
|
||||||
<%= if item.separator do %>
|
<%= if item.separator do %>
|
||||||
<div class="window-titlebar-menu-separator" role="separator"></div>
|
<div class="window-titlebar-menu-separator" role="separator"></div>
|
||||||
<% else %>
|
<% else %>
|
||||||
<button
|
<button
|
||||||
class={[
|
class={[
|
||||||
"window-titlebar-menu-item",
|
"window-titlebar-menu-item",
|
||||||
if(titlebar_menu_item_active?(group, item, @titlebar_menu_item_index), do: "is-keyboard-active")
|
if(BDS.Desktop.ShellLive.TitlebarMenu.item_active?(group, item, @titlebar_menu_item_index), do: "is-keyboard-active")
|
||||||
]}
|
]}
|
||||||
data-testid="window-titlebar-menu-item"
|
data-testid="window-titlebar-menu-item"
|
||||||
data-menu-action={item.id}
|
data-menu-action={item.id}
|
||||||
@@ -237,8 +237,8 @@
|
|||||||
phx-value-type={tab.type}
|
phx-value-type={tab.type}
|
||||||
phx-value-id={tab.id}
|
phx-value-id={tab.id}
|
||||||
>
|
>
|
||||||
<span class="tab-icon"><%= raw(ShellData.activity_icon(tab_icon_id(tab))) %></span>
|
<span class="tab-icon"><%= raw(ShellData.activity_icon(BDS.Desktop.ShellLive.TabHelpers.tab_icon_id(tab))) %></span>
|
||||||
<span class="tab-title"><%= tab_title(tab, @tab_meta) %></span>
|
<span class="tab-title"><%= BDS.Desktop.ShellLive.TabHelpers.tab_title(tab, @tab_meta) %></span>
|
||||||
</button>
|
</button>
|
||||||
<div class="tab-actions">
|
<div class="tab-actions">
|
||||||
<%= if Workbench.dirty?(@workbench, tab.type, tab.id) do %>
|
<%= if Workbench.dirty?(@workbench, tab.type, tab.id) do %>
|
||||||
@@ -383,7 +383,7 @@
|
|||||||
<% else %>
|
<% else %>
|
||||||
<%= cond do %>
|
<%= cond do %>
|
||||||
<% @current_tab.type == :post and @post_editor -> %>
|
<% @current_tab.type == :post and @post_editor -> %>
|
||||||
<PostEditor.post_editor post_editor={@post_editor} toolbar_buttons={editor_toolbar_buttons(@current_tab)} />
|
<PostEditor.post_editor post_editor={@post_editor} toolbar_buttons={BDS.Desktop.ShellLive.PanelRenderer.editor_toolbar_buttons(@current_tab)} />
|
||||||
|
|
||||||
<% @current_tab.type == :media and @media_editor -> %>
|
<% @current_tab.type == :media and @media_editor -> %>
|
||||||
<MediaEditor.media_editor media_editor={@media_editor} />
|
<MediaEditor.media_editor media_editor={@media_editor} />
|
||||||
@@ -418,14 +418,14 @@
|
|||||||
<% true -> %>
|
<% true -> %>
|
||||||
<div class="editor-frame">
|
<div class="editor-frame">
|
||||||
<section class="editor-main">
|
<section class="editor-main">
|
||||||
<div class="editor-kicker"><%= tab_route_label(@current_tab) %></div>
|
<div class="editor-kicker"><%= BDS.Desktop.ShellLive.TabHelpers.tab_route_label(@current_tab) %></div>
|
||||||
<h1 class="editor-title" data-testid="editor-title"><%= tab_title(@current_tab, @tab_meta) %></h1>
|
<h1 class="editor-title" data-testid="editor-title"><%= BDS.Desktop.ShellLive.TabHelpers.tab_title(@current_tab, @tab_meta) %></h1>
|
||||||
<p class="editor-subtitle"><%= tab_subtitle(@current_tab, @tab_meta) %></p>
|
<p class="editor-subtitle"><%= BDS.Desktop.ShellLive.TabHelpers.tab_subtitle(@current_tab, @tab_meta) %></p>
|
||||||
|
|
||||||
<%= render_editor_toolbar(assigns) %>
|
<%= BDS.Desktop.ShellLive.PanelRenderer.render_editor_toolbar(assigns) %>
|
||||||
|
|
||||||
<div class="editor-section">
|
<div class="editor-section">
|
||||||
<h2><%= tab_title(@current_tab, @tab_meta) %></h2>
|
<h2><%= BDS.Desktop.ShellLive.TabHelpers.tab_title(@current_tab, @tab_meta) %></h2>
|
||||||
<p>Desktop workbench content routed through the Elixir shell.</p>
|
<p>Desktop workbench content routed through the Elixir shell.</p>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
@@ -469,7 +469,7 @@
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="panel-content">
|
<div class="panel-content">
|
||||||
<%= render_panel_body(assigns) %>
|
<%= BDS.Desktop.ShellLive.PanelRenderer.render_panel_body(assigns) %>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
@@ -498,13 +498,13 @@
|
|||||||
<section class="assistant-sidebar-context" data-testid="assistant-context">
|
<section class="assistant-sidebar-context" data-testid="assistant-context">
|
||||||
<div class="assistant-sidebar-context-row">
|
<div class="assistant-sidebar-context-row">
|
||||||
<span class="assistant-sidebar-context-label"><%= translated("Project") %></span>
|
<span class="assistant-sidebar-context-label"><%= translated("Project") %></span>
|
||||||
<span class="assistant-sidebar-context-value"><%= assistant_project_name(@current_project) %></span>
|
<span class="assistant-sidebar-context-value"><%= BDS.Desktop.ShellLive.ChatSurface.assistant_project_name(@current_project) %></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="assistant-sidebar-context-row">
|
<div class="assistant-sidebar-context-row">
|
||||||
<span class="assistant-sidebar-context-label"><%= translated("Editor") %></span>
|
<span class="assistant-sidebar-context-label"><%= translated("Editor") %></span>
|
||||||
<span class="assistant-sidebar-context-value"><%= tab_title(@current_tab, @tab_meta) %></span>
|
<span class="assistant-sidebar-context-value"><%= BDS.Desktop.ShellLive.TabHelpers.tab_title(@current_tab, @tab_meta) %></span>
|
||||||
</div>
|
</div>
|
||||||
<p class="assistant-sidebar-context-text"><%= tab_subtitle(@current_tab, @tab_meta) %></p>
|
<p class="assistant-sidebar-context-text"><%= BDS.Desktop.ShellLive.TabHelpers.tab_subtitle(@current_tab, @tab_meta) %></p>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<form
|
<form
|
||||||
@@ -545,9 +545,9 @@
|
|||||||
<%= for message <- @assistant_messages do %>
|
<%= for message <- @assistant_messages do %>
|
||||||
<article
|
<article
|
||||||
class={["assistant-sidebar-message", message.role]}
|
class={["assistant-sidebar-message", message.role]}
|
||||||
data-testid={assistant_message_testid(message.role)}
|
data-testid={BDS.Desktop.ShellLive.ChatSurface.assistant_message_testid(message.role)}
|
||||||
>
|
>
|
||||||
<span class="assistant-sidebar-message-role"><%= assistant_message_label(message.role) %></span>
|
<span class="assistant-sidebar-message-role"><%= BDS.Desktop.ShellLive.ChatSurface.assistant_message_label(message.role) %></span>
|
||||||
<p class="assistant-sidebar-message-content"><%= message.content %></p>
|
<p class="assistant-sidebar-message-content"><%= message.content %></p>
|
||||||
</article>
|
</article>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
53
lib/bds/desktop/shell_live/layout.ex
Normal file
53
lib/bds/desktop/shell_live/layout.ex
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.Layout do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
alias BDS.UI.Workbench
|
||||||
|
|
||||||
|
def sync(workbench, params) do
|
||||||
|
workbench
|
||||||
|
|> maybe_set_sidebar_width(Map.get(params, "sidebar_width"))
|
||||||
|
|> maybe_set_assistant_width(Map.get(params, "assistant_sidebar_width"))
|
||||||
|
end
|
||||||
|
|
||||||
|
def resize(workbench, "sidebar", width) do
|
||||||
|
workbench
|
||||||
|
|> Workbench.set_sidebar_width(parse_width(width))
|
||||||
|
|> Map.put(:sidebar_visible, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def resize(workbench, "assistant", width) do
|
||||||
|
workbench
|
||||||
|
|> Workbench.set_assistant_sidebar_width(parse_width(width))
|
||||||
|
|> Map.put(:assistant_sidebar_visible, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def resize(workbench, _target, _width), do: workbench
|
||||||
|
|
||||||
|
def ignore_shortcut?(params) do
|
||||||
|
Map.get(params, "alt", false) or
|
||||||
|
Map.get(params, "contentEditable", false) or
|
||||||
|
Map.get(params, "content_editable", false) or
|
||||||
|
Map.get(params, "tag") in ["INPUT", "TEXTAREA", "SELECT"] or
|
||||||
|
Map.get(params, :tag) in ["INPUT", "TEXTAREA", "SELECT"]
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_set_sidebar_width(workbench, nil), do: workbench
|
||||||
|
defp maybe_set_sidebar_width(workbench, width),
|
||||||
|
do: Workbench.set_sidebar_width(workbench, parse_width(width))
|
||||||
|
|
||||||
|
defp maybe_set_assistant_width(workbench, nil), do: workbench
|
||||||
|
|
||||||
|
defp maybe_set_assistant_width(workbench, width),
|
||||||
|
do: Workbench.set_assistant_sidebar_width(workbench, parse_width(width))
|
||||||
|
|
||||||
|
defp parse_width(width) when is_integer(width), do: width
|
||||||
|
|
||||||
|
defp parse_width(width) when is_binary(width) do
|
||||||
|
case Integer.parse(width) do
|
||||||
|
{parsed, _rest} -> parsed
|
||||||
|
:error -> 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_width(_), do: 0
|
||||||
|
end
|
||||||
290
lib/bds/desktop/shell_live/panel_renderer.ex
Normal file
290
lib/bds/desktop/shell_live/panel_renderer.ex
Normal file
@@ -0,0 +1,290 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.PanelRenderer do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
use Phoenix.Component
|
||||||
|
|
||||||
|
alias BDS.Desktop.ShellData
|
||||||
|
alias BDS.Git
|
||||||
|
alias BDS.Media.Media
|
||||||
|
alias BDS.PostLinks
|
||||||
|
alias BDS.Posts.Post
|
||||||
|
alias BDS.Repo
|
||||||
|
|
||||||
|
@doc "Render the active panel tab body."
|
||||||
|
def render_panel_body(assigns) do
|
||||||
|
case assigns.workbench.panel.active_tab do
|
||||||
|
:tasks -> render_task_entries(assigns)
|
||||||
|
:output -> render_output_entries(assigns)
|
||||||
|
:post_links -> render_post_links(assigns)
|
||||||
|
:git_log -> render_git_log(assigns)
|
||||||
|
other -> render_generic_panel(assigns, other)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Render the editor toolbar for the current tab."
|
||||||
|
def render_editor_toolbar(assigns) do
|
||||||
|
buttons = editor_toolbar_buttons(assigns.current_tab)
|
||||||
|
assigns = assign(assigns, :editor_toolbar_buttons, buttons)
|
||||||
|
|
||||||
|
~H"""
|
||||||
|
<%= if Enum.any?(@editor_toolbar_buttons) do %>
|
||||||
|
<div class="editor-toolbar">
|
||||||
|
<%= for button <- @editor_toolbar_buttons do %>
|
||||||
|
<button
|
||||||
|
class={["editor-toolbar-button", if(button.destructive, do: "is-destructive")]}
|
||||||
|
data-testid="editor-toolbar-overlay-button"
|
||||||
|
type="button"
|
||||||
|
phx-click="open_overlay"
|
||||||
|
phx-value-kind={button.kind}
|
||||||
|
>
|
||||||
|
<%= translated(button.label) %>
|
||||||
|
</button>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp render_task_entries(assigns) do
|
||||||
|
~H"""
|
||||||
|
<%= if Enum.empty?(Map.get(@task_status, :tasks, [])) do %>
|
||||||
|
<div class="panel-entry panel-empty-state">
|
||||||
|
<strong><%= translated("Tasks") %></strong>
|
||||||
|
<span><%= translated("No background tasks running") %></span>
|
||||||
|
</div>
|
||||||
|
<% else %>
|
||||||
|
<div class="task-list">
|
||||||
|
<%= for task <- Map.get(@task_status, :tasks, []) do %>
|
||||||
|
<div class="panel-entry task-entry">
|
||||||
|
<div class="task-entry-header">
|
||||||
|
<strong><%= task.name %></strong>
|
||||||
|
<span class={"task-status task-status-#{task.status}"}><%= Map.get(task, :status_label, task.status |> to_string() |> String.capitalize()) %></span>
|
||||||
|
</div>
|
||||||
|
<span><%= task.message || task.group_name || "" %></span>
|
||||||
|
<%= if is_number(task.progress) do %>
|
||||||
|
<div class="task-progress-row">
|
||||||
|
<progress max="1" value={task.progress}></progress>
|
||||||
|
<span><%= Map.get(task, :progress_label, progress_percent(task.progress)) %></span>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp render_output_entries(assigns) do
|
||||||
|
~H"""
|
||||||
|
<%= if Enum.empty?(@output_entries) do %>
|
||||||
|
<div class="panel-entry panel-empty-state output-list">
|
||||||
|
<strong><%= translated("Output") %></strong>
|
||||||
|
<span><%= translated("No shell output yet") %></span>
|
||||||
|
</div>
|
||||||
|
<% else %>
|
||||||
|
<div class="output-list">
|
||||||
|
<%= for entry <- @output_entries do %>
|
||||||
|
<div class={[
|
||||||
|
"panel-entry",
|
||||||
|
"output-entry",
|
||||||
|
if(Map.get(entry, :level) == "error", do: "output-entry-error")
|
||||||
|
]}>
|
||||||
|
<strong><%= entry.title %></strong>
|
||||||
|
<span><%= entry.message %></span>
|
||||||
|
<%= if present?(entry.details) do %>
|
||||||
|
<span><%= entry.details %></span>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp render_post_links(assigns) do
|
||||||
|
links = post_link_entries(assigns)
|
||||||
|
|
||||||
|
assigns =
|
||||||
|
assigns
|
||||||
|
|> assign(:backlinks, Map.get(links, :backlinks, []))
|
||||||
|
|> assign(:outlinks, Map.get(links, :outlinks, []))
|
||||||
|
|
||||||
|
~H"""
|
||||||
|
<%= if Enum.empty?(@backlinks) and Enum.empty?(@outlinks) do %>
|
||||||
|
<div class="panel-entry panel-empty-state">
|
||||||
|
<strong><%= translated("Post Links") %></strong>
|
||||||
|
<span><%= translated("No post links yet") %></span>
|
||||||
|
</div>
|
||||||
|
<% else %>
|
||||||
|
<div class="git-log-list">
|
||||||
|
<%= if Enum.any?(@backlinks) do %>
|
||||||
|
<div class="panel-entry"><strong><%= translated("Backlinks") %></strong></div>
|
||||||
|
<%= for entry <- @backlinks do %>
|
||||||
|
<button
|
||||||
|
class="panel-entry task-entry"
|
||||||
|
type="button"
|
||||||
|
phx-click="pin_sidebar_item"
|
||||||
|
phx-value-route="post"
|
||||||
|
phx-value-id={entry.id}
|
||||||
|
phx-value-title={entry.title}
|
||||||
|
phx-value-subtitle="linked post"
|
||||||
|
>
|
||||||
|
<strong><%= entry.title %></strong>
|
||||||
|
<span><%= entry.text %></span>
|
||||||
|
</button>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<%= if Enum.any?(@outlinks) do %>
|
||||||
|
<div class="panel-entry"><strong><%= translated("Links To") %></strong></div>
|
||||||
|
<%= for entry <- @outlinks do %>
|
||||||
|
<button
|
||||||
|
class="panel-entry task-entry"
|
||||||
|
type="button"
|
||||||
|
phx-click="pin_sidebar_item"
|
||||||
|
phx-value-route="post"
|
||||||
|
phx-value-id={entry.id}
|
||||||
|
phx-value-title={entry.title}
|
||||||
|
phx-value-subtitle="linked post"
|
||||||
|
>
|
||||||
|
<strong><%= entry.title %></strong>
|
||||||
|
<span><%= entry.text %></span>
|
||||||
|
</button>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp render_git_log(assigns) do
|
||||||
|
entries = git_log_entries(assigns)
|
||||||
|
assigns = assign(assigns, :git_entries, entries)
|
||||||
|
|
||||||
|
~H"""
|
||||||
|
<%= if Enum.empty?(@git_entries) do %>
|
||||||
|
<div class="git-log-list">
|
||||||
|
<div class="panel-entry panel-empty-state">
|
||||||
|
<strong><%= translated("Git Log") %></strong>
|
||||||
|
<span><%= translated("No git history yet") %></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<% else %>
|
||||||
|
<div class="git-log-list">
|
||||||
|
<%= for entry <- @git_entries do %>
|
||||||
|
<div class="panel-entry task-entry">
|
||||||
|
<strong><%= short_commit_hash(entry.hash) %> <%= entry.subject || translated("No commit subject") %></strong>
|
||||||
|
<span><%= entry.hash %></span>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp render_generic_panel(assigns, tab) do
|
||||||
|
assigns = assign(assigns, :panel_label, ShellData.route_label(tab))
|
||||||
|
|
||||||
|
~H"""
|
||||||
|
<div class="panel-entry">
|
||||||
|
<strong><%= @panel_label %></strong>
|
||||||
|
<span><%= translated("The shared lower panel is available for tasks, output, git details, and editor-specific diagnostics.") %></span>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp post_link_entries(assigns) do
|
||||||
|
case assigns.current_tab do
|
||||||
|
%{type: :post, id: post_id} ->
|
||||||
|
%{
|
||||||
|
backlinks: related_posts(PostLinks.list_incoming_links(post_id), :source_post_id),
|
||||||
|
outlinks: related_posts(PostLinks.list_outgoing_links(post_id), :target_post_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
%{backlinks: [], outlinks: []}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp related_posts(links, key) do
|
||||||
|
Enum.map(links, fn link ->
|
||||||
|
case Repo.get(Post, Map.fetch!(link, key)) do
|
||||||
|
%Post{} = post -> %{id: post.id, title: post.title || post.slug || post.id, text: link.link_text || post.slug || post.id}
|
||||||
|
_other -> nil
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|> Enum.reject(&is_nil/1)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp git_log_entries(assigns) do
|
||||||
|
case git_history_target(assigns.current_tab) do
|
||||||
|
nil ->
|
||||||
|
[]
|
||||||
|
|
||||||
|
{project_id, file_path} ->
|
||||||
|
case Git.file_history(project_id, file_path) do
|
||||||
|
{:ok, %{commits: commits}} -> commits
|
||||||
|
_other -> []
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp git_history_target(%{type: :post, id: post_id}) do
|
||||||
|
case Repo.get(Post, post_id) do
|
||||||
|
%Post{project_id: project_id, file_path: file_path} when file_path not in [nil, ""] -> {project_id, file_path}
|
||||||
|
_other -> nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp git_history_target(%{type: :media, id: media_id}) do
|
||||||
|
case Repo.get(Media, media_id) do
|
||||||
|
%Media{project_id: project_id, file_path: file_path} when file_path not in [nil, ""] -> {project_id, file_path}
|
||||||
|
_other -> nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp git_history_target(_tab), do: nil
|
||||||
|
|
||||||
|
def editor_toolbar_buttons(nil), do: []
|
||||||
|
|
||||||
|
def editor_toolbar_buttons(%{type: :post}) do
|
||||||
|
[
|
||||||
|
%{kind: "ai_suggestions", label: "AI Suggestions", destructive: false},
|
||||||
|
%{kind: "insert_link", label: "Insert Link", destructive: false},
|
||||||
|
%{kind: "insert_media", label: "Insert Media", destructive: false},
|
||||||
|
%{kind: "language_picker", label: "Translate", destructive: false},
|
||||||
|
%{kind: "gallery", label: "Gallery", destructive: false}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
def editor_toolbar_buttons(%{type: :media}) do
|
||||||
|
[
|
||||||
|
%{kind: "ai_suggestions", label: "AI Suggestions", destructive: false},
|
||||||
|
%{kind: "language_picker", label: "Translate", destructive: false},
|
||||||
|
%{kind: "confirm_delete", label: "Delete Media", destructive: true}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
def editor_toolbar_buttons(%{type: :tags}) do
|
||||||
|
[
|
||||||
|
%{kind: "confirm_merge", label: "Merge Tags", destructive: false},
|
||||||
|
%{kind: "confirm_delete", label: "Delete Tag", destructive: true}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
def editor_toolbar_buttons(_tab), do: []
|
||||||
|
|
||||||
|
defp short_commit_hash(hash) when is_binary(hash), do: String.slice(hash, 0, 7)
|
||||||
|
defp short_commit_hash(_hash), do: "-------"
|
||||||
|
|
||||||
|
defp progress_percent(progress) when is_number(progress) do
|
||||||
|
rounded = progress |> Kernel.*(100) |> Float.round(0) |> trunc()
|
||||||
|
"#{rounded}%"
|
||||||
|
end
|
||||||
|
|
||||||
|
defp progress_percent(_), do: ""
|
||||||
|
|
||||||
|
defp present?(value), do: value not in [nil, ""]
|
||||||
|
|
||||||
|
defp translated(text, bindings \\ %{}), do: ShellData.translate(text, bindings, Process.get(:bds_ui_locale))
|
||||||
|
end
|
||||||
48
lib/bds/desktop/shell_live/session_util.ex
Normal file
48
lib/bds/desktop/shell_live/session_util.ex
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.SessionUtil do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
import Phoenix.Component, only: [assign: 3]
|
||||||
|
|
||||||
|
alias BDS.UI.{Session, Workbench}
|
||||||
|
|
||||||
|
@default_new_project_name "New Blog"
|
||||||
|
|
||||||
|
def restore_workbench_session(session_payload) do
|
||||||
|
Session.restore(session_payload)
|
||||||
|
rescue
|
||||||
|
_error -> Workbench.new()
|
||||||
|
end
|
||||||
|
|
||||||
|
def next_project_name(projects) do
|
||||||
|
existing_names = MapSet.new(Enum.map(projects, & &1.name))
|
||||||
|
|
||||||
|
Stream.iterate(1, &(&1 + 1))
|
||||||
|
|> Enum.find_value(fn index ->
|
||||||
|
candidate =
|
||||||
|
if index == 1, do: @default_new_project_name, else: "#{@default_new_project_name} #{index}"
|
||||||
|
|
||||||
|
if MapSet.member?(existing_names, candidate), do: nil, else: candidate
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def initial_handled_task_results do
|
||||||
|
BDS.Tasks.status_snapshot()
|
||||||
|
|> Map.get(:tasks, [])
|
||||||
|
|> Enum.filter(fn task -> task.status == :completed and is_map(task.result) end)
|
||||||
|
|> Enum.map(& &1.id)
|
||||||
|
|> MapSet.new()
|
||||||
|
end
|
||||||
|
|
||||||
|
def next_completed_task_result(socket, task_status) do
|
||||||
|
handled = Map.get(socket.assigns, :handled_task_results, MapSet.new())
|
||||||
|
|
||||||
|
Enum.find(Map.get(task_status, :tasks, []), fn task ->
|
||||||
|
task.status == :completed and is_map(task.result) and not MapSet.member?(handled, task.id)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def mark_task_result_handled(socket, task_id) do
|
||||||
|
handled = Map.get(socket.assigns, :handled_task_results, MapSet.new())
|
||||||
|
assign(socket, :handled_task_results, MapSet.put(handled, task_id))
|
||||||
|
end
|
||||||
|
end
|
||||||
95
lib/bds/desktop/shell_live/shell_command_runner.ex
Normal file
95
lib/bds/desktop/shell_live/shell_command_runner.ex
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.ShellCommandRunner do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
import Phoenix.Component, only: [assign: 3]
|
||||||
|
|
||||||
|
alias BDS.Desktop.ShellCommands
|
||||||
|
alias BDS.Desktop.ShellLive.{TabHelpers, TaskLocalization}
|
||||||
|
alias BDS.UI.Workbench
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Execute a shell command and apply its result to the socket.
|
||||||
|
|
||||||
|
`callbacks` requires:
|
||||||
|
* `:reload` — `(socket, workbench -> socket)`
|
||||||
|
* `:append_output` — `(socket, title, message, details, level -> socket)`
|
||||||
|
"""
|
||||||
|
def execute(socket, action, params \\ %{}, callbacks) do
|
||||||
|
case ShellCommands.execute(action, params) do
|
||||||
|
{:ok, result} ->
|
||||||
|
apply_result(socket, result, callbacks)
|
||||||
|
|
||||||
|
{:error, %{message: message}} ->
|
||||||
|
callbacks.append_output.(socket, TaskLocalization.command_title(action), message, nil, "error")
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
callbacks.append_output.(socket, TaskLocalization.command_title(action), inspect(reason), nil, "error")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def apply_result(socket, %{kind: "task_queued", title: title, message: message, panel_tab: panel_tab}, callbacks) do
|
||||||
|
workbench =
|
||||||
|
socket.assigns.workbench
|
||||||
|
|> Workbench.set_panel_visible(true)
|
||||||
|
|> Workbench.set_panel_tab(String.to_existing_atom(panel_tab))
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> callbacks.append_output.(
|
||||||
|
TaskLocalization.translate_for_socket(socket, title),
|
||||||
|
TaskLocalization.translate_for_socket(socket, message),
|
||||||
|
nil,
|
||||||
|
"info"
|
||||||
|
)
|
||||||
|
|> callbacks.reload.(workbench)
|
||||||
|
end
|
||||||
|
|
||||||
|
def apply_result(socket, %{kind: "output", title: title, message: message} = result, callbacks) do
|
||||||
|
callbacks.append_output.(
|
||||||
|
socket,
|
||||||
|
TaskLocalization.translate_for_socket(socket, title),
|
||||||
|
TaskLocalization.translate_for_socket(socket, message),
|
||||||
|
Map.get(result, :details),
|
||||||
|
Map.get(result, :level, "info")
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def apply_result(socket, %{kind: "open_url", title: title, message: message, url: url}, callbacks) do
|
||||||
|
callbacks.append_output.(
|
||||||
|
socket,
|
||||||
|
TaskLocalization.translate_for_socket(socket, title),
|
||||||
|
TaskLocalization.translate_for_socket(socket, message),
|
||||||
|
url,
|
||||||
|
"info"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def apply_result(socket, %{kind: "open_editor", route: route, title: title, subtitle: subtitle} = result, callbacks) do
|
||||||
|
route_atom = String.to_existing_atom(route)
|
||||||
|
tab_id = TabHelpers.tab_id_for_route(route_atom, route)
|
||||||
|
workbench = Workbench.open_tab(socket.assigns.workbench, route_atom, tab_id, :pin)
|
||||||
|
|
||||||
|
tab_meta =
|
||||||
|
Map.put(socket.assigns.tab_meta, {route_atom, tab_id}, %{
|
||||||
|
title: TaskLocalization.translate_for_socket(socket, title),
|
||||||
|
subtitle: TaskLocalization.translate_for_socket(socket, subtitle),
|
||||||
|
action: Map.get(result, :action),
|
||||||
|
payload: Map.get(result, :payload),
|
||||||
|
project_id: Map.get(result, :project_id),
|
||||||
|
editor_meta: TaskLocalization.translate_editor_meta(Map.get(result, :editorMeta, []), socket.assigns.page_language)
|
||||||
|
})
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> assign(:tab_meta, tab_meta)
|
||||||
|
|> callbacks.reload.(workbench)
|
||||||
|
end
|
||||||
|
|
||||||
|
def apply_result(socket, _result, _callbacks), do: socket
|
||||||
|
|
||||||
|
def safe_existing_atom(action) when is_binary(action) do
|
||||||
|
String.to_existing_atom(action)
|
||||||
|
rescue
|
||||||
|
ArgumentError -> nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def safe_existing_atom(_), do: nil
|
||||||
|
end
|
||||||
131
lib/bds/desktop/shell_live/sidebar_create.ex
Normal file
131
lib/bds/desktop/shell_live/sidebar_create.ex
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.SidebarCreate do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
alias BDS.Desktop.{FilePicker, ShellData}
|
||||||
|
alias BDS.ImportDefinitions
|
||||||
|
alias BDS.Scripts
|
||||||
|
alias BDS.Templates
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Create a new sidebar item of the given kind for the active project.
|
||||||
|
|
||||||
|
`callbacks` must contain:
|
||||||
|
* `:reload` — `(socket, workbench -> socket)`
|
||||||
|
* `:open_sidebar` — `(socket, params, intent -> socket)`
|
||||||
|
* `:append_output` — `(socket, title, message, details, level -> socket)`
|
||||||
|
"""
|
||||||
|
def create(socket, kind, callbacks) do
|
||||||
|
case socket.assigns.projects.active_project_id do
|
||||||
|
project_id when is_binary(project_id) -> create(socket, project_id, kind, callbacks)
|
||||||
|
_other -> callbacks.reload.(socket, socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create(socket, project_id, "post", callbacks) do
|
||||||
|
case BDS.Posts.create_post(%{project_id: project_id, title: "", content: "", tags: [], categories: []}) do
|
||||||
|
{:ok, _post} ->
|
||||||
|
callbacks.reload.(socket, socket.assigns.workbench)
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
socket
|
||||||
|
|> callbacks.append_output.(translated("sidebar.newPost"), inspect(reason), nil, "error")
|
||||||
|
|> callbacks.reload.(socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create(socket, project_id, "media", callbacks) do
|
||||||
|
case FilePicker.choose_file(translated("sidebar.importMedia")) do
|
||||||
|
{:ok, source_path} ->
|
||||||
|
case BDS.Media.import_media(%{project_id: project_id, source_path: source_path}) do
|
||||||
|
{:ok, _media} ->
|
||||||
|
callbacks.reload.(socket, socket.assigns.workbench)
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
socket
|
||||||
|
|> callbacks.append_output.(translated("sidebar.importMedia"), inspect(reason), nil, "error")
|
||||||
|
|> callbacks.reload.(socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
|
||||||
|
:cancel ->
|
||||||
|
callbacks.reload.(socket, socket.assigns.workbench)
|
||||||
|
|
||||||
|
{:error, %{message: message}} ->
|
||||||
|
socket
|
||||||
|
|> callbacks.append_output.(translated("sidebar.importMedia"), message, nil, "error")
|
||||||
|
|> callbacks.reload.(socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create(socket, project_id, "script", callbacks) do
|
||||||
|
case Scripts.create_script(%{
|
||||||
|
project_id: project_id,
|
||||||
|
title: translated("sidebar.scripts.newScript"),
|
||||||
|
kind: :utility,
|
||||||
|
content: "print(\"new script\")",
|
||||||
|
entrypoint: "main",
|
||||||
|
enabled: true
|
||||||
|
}) do
|
||||||
|
{:ok, script} ->
|
||||||
|
callbacks.open_sidebar.(
|
||||||
|
socket,
|
||||||
|
%{"route" => "scripts", "id" => script.id, "title" => script.title, "subtitle" => "Automation helpers"},
|
||||||
|
:pin
|
||||||
|
)
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
socket
|
||||||
|
|> callbacks.append_output.(translated("sidebar.scripts.newScript"), inspect(reason), nil, "error")
|
||||||
|
|> callbacks.reload.(socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create(socket, project_id, "template", callbacks) do
|
||||||
|
case Templates.create_template(%{
|
||||||
|
project_id: project_id,
|
||||||
|
title: translated("sidebar.templates.newTemplate"),
|
||||||
|
kind: :post,
|
||||||
|
content: "",
|
||||||
|
enabled: true
|
||||||
|
}) do
|
||||||
|
{:ok, template} ->
|
||||||
|
callbacks.open_sidebar.(
|
||||||
|
socket,
|
||||||
|
%{"route" => "templates", "id" => template.id, "title" => template.title, "subtitle" => "Site rendering"},
|
||||||
|
:pin
|
||||||
|
)
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
socket
|
||||||
|
|> callbacks.append_output.(translated("sidebar.templates.newTemplate"), inspect(reason), nil, "error")
|
||||||
|
|> callbacks.reload.(socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create(socket, project_id, "import", callbacks) do
|
||||||
|
case ImportDefinitions.create_definition(%{project_id: project_id, name: translated("sidebar.import.newDefinition")}) do
|
||||||
|
{:ok, definition} ->
|
||||||
|
callbacks.open_sidebar.(
|
||||||
|
socket,
|
||||||
|
%{"route" => "import", "id" => definition.id, "title" => definition.name, "subtitle" => "Import definitions"},
|
||||||
|
:pin
|
||||||
|
)
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
socket
|
||||||
|
|> callbacks.append_output.(translated("sidebar.import.newDefinition"), inspect(reason), nil, "error")
|
||||||
|
|> callbacks.reload.(socket.assigns.workbench)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create(socket, _project_id, _kind, callbacks),
|
||||||
|
do: callbacks.reload.(socket, socket.assigns.workbench)
|
||||||
|
|
||||||
|
def action(:posts), do: %{kind: "post", label: "sidebar.newPost"}
|
||||||
|
def action(:media), do: %{kind: "media", label: "sidebar.importMedia"}
|
||||||
|
def action(:scripts), do: %{kind: "script", label: "sidebar.scripts.newScript"}
|
||||||
|
def action(:templates), do: %{kind: "template", label: "sidebar.templates.newTemplate"}
|
||||||
|
def action(:import), do: %{kind: "import", label: "sidebar.import.newDefinition"}
|
||||||
|
def action(_view), do: nil
|
||||||
|
|
||||||
|
defp translated(text), do: ShellData.translate(text, %{}, Process.get(:bds_ui_locale))
|
||||||
|
end
|
||||||
99
lib/bds/desktop/shell_live/tab_helpers.ex
Normal file
99
lib/bds/desktop/shell_live/tab_helpers.ex
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.TabHelpers do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
alias BDS.Desktop.ShellData
|
||||||
|
alias BDS.Media.Media
|
||||||
|
alias BDS.Posts.Post
|
||||||
|
alias BDS.Repo
|
||||||
|
alias BDS.UI.Registry
|
||||||
|
|
||||||
|
def tab_title(nil, _tab_meta), do: translated("Dashboard")
|
||||||
|
|
||||||
|
def tab_title(tab, tab_meta) do
|
||||||
|
case Map.get(tab_meta, {tab.type, tab.id}) do
|
||||||
|
%{title: title} when is_binary(title) and title != "" -> title
|
||||||
|
_other -> default_tab_title(tab)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def tab_subtitle(nil, _tab_meta), do: translated("dashboard.subtitle")
|
||||||
|
|
||||||
|
def tab_subtitle(tab, tab_meta) do
|
||||||
|
case Map.get(tab_meta, {tab.type, tab.id}) do
|
||||||
|
%{subtitle: subtitle} when is_binary(subtitle) and subtitle != "" -> subtitle
|
||||||
|
_other -> "Desktop workbench content routed through the Elixir shell."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def default_tab_title(%{type: type, id: id}) do
|
||||||
|
case Registry.editor_route(type) do
|
||||||
|
%{singleton: true} -> ShellData.route_label(type)
|
||||||
|
_other -> id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def tab_route_label(nil), do: translated("Dashboard")
|
||||||
|
def tab_route_label(%{type: type}), do: ShellData.route_label(type)
|
||||||
|
|
||||||
|
def tab_icon_id(nil), do: "posts"
|
||||||
|
def tab_icon_id(%{type: :post}), do: "posts"
|
||||||
|
def tab_icon_id(%{type: :git_diff}), do: "git"
|
||||||
|
def tab_icon_id(%{type: :style}), do: "settings"
|
||||||
|
def tab_icon_id(%{type: type}), do: Atom.to_string(type)
|
||||||
|
|
||||||
|
def sidebar_route_atom(route) when is_atom(route), do: route
|
||||||
|
def sidebar_route_atom(route) when is_binary(route), do: String.to_existing_atom(route)
|
||||||
|
|
||||||
|
def tab_id_for_route(route, id) do
|
||||||
|
case Registry.editor_route(route) do
|
||||||
|
%{singleton: true} -> Atom.to_string(route)
|
||||||
|
_other -> id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def tab_intent(route, requested_intent) do
|
||||||
|
case Registry.editor_route(route) do
|
||||||
|
%{singleton: true} -> :pin
|
||||||
|
_other -> requested_intent
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def post_title(post_id) do
|
||||||
|
case Repo.get(Post, post_id) do
|
||||||
|
%Post{} = post -> post.title || post.slug || post.id
|
||||||
|
_other -> "Post"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def post_subtitle(post_id) do
|
||||||
|
case Repo.get(Post, post_id) do
|
||||||
|
%Post{} = post -> post.slug || "draft"
|
||||||
|
_other -> "draft"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def media_title(media_id) do
|
||||||
|
case Repo.get(Media, media_id) do
|
||||||
|
%Media{} = media -> media.title || media.filename || media.id
|
||||||
|
_other -> "Media"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def media_subtitle(media_id) do
|
||||||
|
case Repo.get(Media, media_id) do
|
||||||
|
%Media{} = media -> media.filename || media.mime_type || "media"
|
||||||
|
_other -> "media"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_integer(value) when is_integer(value), do: value
|
||||||
|
|
||||||
|
def parse_integer(value) do
|
||||||
|
case Integer.parse(to_string(value || "0")) do
|
||||||
|
{parsed, _rest} -> parsed
|
||||||
|
:error -> 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp translated(text), do: ShellData.translate(text, %{}, Process.get(:bds_ui_locale))
|
||||||
|
end
|
||||||
80
lib/bds/desktop/shell_live/task_localization.ex
Normal file
80
lib/bds/desktop/shell_live/task_localization.ex
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.TaskLocalization do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
alias BDS.Desktop.ShellData
|
||||||
|
|
||||||
|
def localize_task_status(task_status, locale) do
|
||||||
|
tasks = Enum.map(Map.get(task_status, :tasks, []), &localize_task(&1, locale))
|
||||||
|
active = Enum.filter(tasks, &(&1.status in [:running, :pending]))
|
||||||
|
|
||||||
|
task_status
|
||||||
|
|> Map.put(:tasks, tasks)
|
||||||
|
|> Map.put(:running_task_message, localized_running_task_message(active, locale))
|
||||||
|
end
|
||||||
|
|
||||||
|
def translate_editor_meta(items, locale) do
|
||||||
|
Enum.map(items, fn item ->
|
||||||
|
item
|
||||||
|
|> Map.update(:label, nil, &ShellData.translate(&1, %{}, locale))
|
||||||
|
|> Map.update(:value, nil, &translate_editor_meta_value(&1, locale))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def translate_for_socket(socket, text) when is_binary(text),
|
||||||
|
do: ShellData.translate(text, %{}, socket.assigns.page_language)
|
||||||
|
|
||||||
|
def translate_for_socket(_socket, text), do: text
|
||||||
|
|
||||||
|
def progress_percent(progress) when is_number(progress) do
|
||||||
|
percentage = progress |> Kernel.*(100) |> round()
|
||||||
|
Integer.to_string(percentage) <> "%"
|
||||||
|
end
|
||||||
|
|
||||||
|
def command_title(action) do
|
||||||
|
action
|
||||||
|
|> to_string()
|
||||||
|
|> String.replace("_", " ")
|
||||||
|
|> String.split()
|
||||||
|
|> Enum.map_join(" ", &String.capitalize/1)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp localize_task(task, locale) do
|
||||||
|
progress = Map.get(task, :progress)
|
||||||
|
|
||||||
|
task
|
||||||
|
|> Map.put(:name, ShellData.translate(task.name, %{}, locale))
|
||||||
|
|> Map.put(:message, localize_task_message(Map.get(task, :message), locale))
|
||||||
|
|> Map.put(:group_name, localize_task_group(Map.get(task, :group_name), locale))
|
||||||
|
|> Map.put(:status_label, localize_task_status_label(task.status, locale))
|
||||||
|
|> Map.put(:progress_label, if(is_number(progress), do: progress_percent(progress), else: nil))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp localize_task_message(nil, _locale), do: nil
|
||||||
|
defp localize_task_message("", _locale), do: ""
|
||||||
|
defp localize_task_message(message, locale), do: ShellData.translate(message, %{}, locale)
|
||||||
|
|
||||||
|
defp localize_task_group(nil, _locale), do: nil
|
||||||
|
defp localize_task_group(group, locale), do: ShellData.translate(group, %{}, locale)
|
||||||
|
|
||||||
|
defp localize_task_status_label(status, locale) do
|
||||||
|
status
|
||||||
|
|> to_string()
|
||||||
|
|> String.capitalize()
|
||||||
|
|> ShellData.translate(%{}, locale)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp localized_running_task_message([], _locale), do: nil
|
||||||
|
|
||||||
|
defp localized_running_task_message([task | _rest], locale) do
|
||||||
|
cond do
|
||||||
|
task.status == :pending -> ShellData.translate("Queued", %{}, locale) <> ": " <> task.name
|
||||||
|
is_binary(task.message) and task.message != "" -> task.name <> ": " <> task.message
|
||||||
|
true -> task.name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp translate_editor_meta_value(value, locale) when is_binary(value),
|
||||||
|
do: ShellData.translate(value, %{}, locale)
|
||||||
|
|
||||||
|
defp translate_editor_meta_value(value, _locale), do: value
|
||||||
|
end
|
||||||
181
lib/bds/desktop/shell_live/titlebar_menu.ex
Normal file
181
lib/bds/desktop/shell_live/titlebar_menu.ex
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
defmodule BDS.Desktop.ShellLive.TitlebarMenu do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
use Phoenix.Component
|
||||||
|
alias BDS.Desktop.MenuBar, as: DesktopMenuBar
|
||||||
|
|
||||||
|
@spec groups() :: [map()]
|
||||||
|
def groups do
|
||||||
|
DesktopMenuBar.groups(dev_mode?: Application.get_env(:bds, :dev_routes, false))
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec dropdown_items(map()) :: [map()]
|
||||||
|
def dropdown_items(group) do
|
||||||
|
group.items
|
||||||
|
|> Enum.map_reduce(0, fn item, keyboard_index ->
|
||||||
|
if Map.get(item, :separator, false) do
|
||||||
|
{%{separator: true}, keyboard_index}
|
||||||
|
else
|
||||||
|
{Map.put(item, :keyboard_index, keyboard_index), keyboard_index + 1}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|> elem(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec item_active?(map(), map(), non_neg_integer() | nil) :: boolean()
|
||||||
|
def item_active?(group, item, current_index) do
|
||||||
|
cond do
|
||||||
|
is_nil(current_index) ->
|
||||||
|
false
|
||||||
|
|
||||||
|
Map.get(item, :separator, false) ->
|
||||||
|
false
|
||||||
|
|
||||||
|
true ->
|
||||||
|
group.items
|
||||||
|
|> Enum.reject(&Map.get(&1, :separator, false))
|
||||||
|
|> Enum.find_index(&(&1.id == item.id))
|
||||||
|
|> Kernel.==(current_index)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec active_group(map()) :: map() | nil
|
||||||
|
def active_group(assigns) do
|
||||||
|
Enum.find(assigns.menu_groups || [], fn group -> Atom.to_string(group.id) == assigns.titlebar_menu_group end)
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec active_items(map()) :: [map()]
|
||||||
|
def active_items(assigns) do
|
||||||
|
assigns
|
||||||
|
|> active_group()
|
||||||
|
|> case do
|
||||||
|
nil -> []
|
||||||
|
group -> Enum.reject(group.items, &Map.get(&1, :separator, false))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec open(Phoenix.LiveView.Socket.t(), String.t()) :: Phoenix.LiveView.Socket.t()
|
||||||
|
def open(socket, group) do
|
||||||
|
socket
|
||||||
|
|> assign(:titlebar_menu_group, group)
|
||||||
|
|> assign(:titlebar_menu_item_index, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec close(Phoenix.LiveView.Socket.t()) :: Phoenix.LiveView.Socket.t()
|
||||||
|
def close(socket) do
|
||||||
|
socket
|
||||||
|
|> assign(:titlebar_menu_group, nil)
|
||||||
|
|> assign(:titlebar_menu_item_index, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec toggle(Phoenix.LiveView.Socket.t(), String.t()) :: Phoenix.LiveView.Socket.t()
|
||||||
|
def toggle(socket, group) do
|
||||||
|
if socket.assigns.titlebar_menu_group == group do
|
||||||
|
close(socket)
|
||||||
|
else
|
||||||
|
open(socket, group)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec hover(Phoenix.LiveView.Socket.t(), String.t()) :: Phoenix.LiveView.Socket.t()
|
||||||
|
def hover(socket, group) do
|
||||||
|
if socket.assigns.titlebar_menu_group do
|
||||||
|
open(socket, group)
|
||||||
|
else
|
||||||
|
socket
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Handle a keydown event on an open titlebar menu. `invoke_fun` is called
|
||||||
|
with the action id (string) when the user activates an item.
|
||||||
|
"""
|
||||||
|
@spec handle_keydown(Phoenix.LiveView.Socket.t(), String.t(), (Phoenix.LiveView.Socket.t(), String.t() -> Phoenix.LiveView.Socket.t())) ::
|
||||||
|
Phoenix.LiveView.Socket.t()
|
||||||
|
def handle_keydown(socket, key, invoke_fun) do
|
||||||
|
if socket.assigns.titlebar_menu_group do
|
||||||
|
case key do
|
||||||
|
"Escape" -> close(socket)
|
||||||
|
"ArrowRight" -> rotate_group(socket, 1)
|
||||||
|
"ArrowLeft" -> rotate_group(socket, -1)
|
||||||
|
"ArrowDown" -> advance_item_index(socket, 1)
|
||||||
|
"ArrowUp" -> advance_item_index(socket, -1)
|
||||||
|
"Home" -> set_first_item_index(socket)
|
||||||
|
"End" -> set_last_item_index(socket)
|
||||||
|
"Enter" -> invoke_active_item(socket, invoke_fun)
|
||||||
|
" " -> invoke_active_item(socket, invoke_fun)
|
||||||
|
_other -> socket
|
||||||
|
end
|
||||||
|
else
|
||||||
|
socket
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp rotate_group(socket, offset) do
|
||||||
|
groups = socket.assigns.menu_groups || []
|
||||||
|
current_group = socket.assigns.titlebar_menu_group
|
||||||
|
current_index = Enum.find_index(groups, fn group -> Atom.to_string(group.id) == current_group end)
|
||||||
|
|
||||||
|
if is_nil(current_index) or groups == [] do
|
||||||
|
socket
|
||||||
|
else
|
||||||
|
next_index = rem(current_index + offset + length(groups), length(groups))
|
||||||
|
next_group = Enum.at(groups, next_index)
|
||||||
|
open(socket, Atom.to_string(next_group.id))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp advance_item_index(socket, offset) do
|
||||||
|
items = active_items(socket.assigns)
|
||||||
|
current_index = socket.assigns[:titlebar_menu_item_index]
|
||||||
|
|
||||||
|
cond do
|
||||||
|
items == [] ->
|
||||||
|
socket
|
||||||
|
|
||||||
|
current_index == nil and offset > 0 ->
|
||||||
|
assign(socket, :titlebar_menu_item_index, 0)
|
||||||
|
|
||||||
|
current_index == nil and offset < 0 ->
|
||||||
|
assign(socket, :titlebar_menu_item_index, length(items) - 1)
|
||||||
|
|
||||||
|
true ->
|
||||||
|
next_index = rem(current_index + offset + length(items), length(items))
|
||||||
|
assign(socket, :titlebar_menu_item_index, next_index)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp set_last_item_index(socket) do
|
||||||
|
items = active_items(socket.assigns)
|
||||||
|
|
||||||
|
if items == [] do
|
||||||
|
socket
|
||||||
|
else
|
||||||
|
assign(socket, :titlebar_menu_item_index, length(items) - 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp set_first_item_index(socket) do
|
||||||
|
items = active_items(socket.assigns)
|
||||||
|
|
||||||
|
if items == [] do
|
||||||
|
socket
|
||||||
|
else
|
||||||
|
assign(socket, :titlebar_menu_item_index, 0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp invoke_active_item(socket, invoke_fun) do
|
||||||
|
items = active_items(socket.assigns)
|
||||||
|
|
||||||
|
case Enum.at(items, socket.assigns[:titlebar_menu_item_index]) do
|
||||||
|
%{id: id} ->
|
||||||
|
socket
|
||||||
|
|> close()
|
||||||
|
|> invoke_fun.(Atom.to_string(id))
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
socket
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
1316
lib/bds/posts.ex
1316
lib/bds/posts.ex
File diff suppressed because it is too large
Load Diff
176
lib/bds/posts/auto_translation.ex
Normal file
176
lib/bds/posts/auto_translation.ex
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
defmodule BDS.Posts.AutoTranslation do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
alias BDS.AI
|
||||||
|
alias BDS.Media
|
||||||
|
alias BDS.Metadata
|
||||||
|
alias BDS.Posts.Post
|
||||||
|
alias BDS.Posts.PostMedia
|
||||||
|
alias BDS.Posts.Translation
|
||||||
|
alias BDS.Repo
|
||||||
|
alias BDS.Tasks
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Schedule background auto-translation tasks for any missing target languages.
|
||||||
|
|
||||||
|
Returns `:ok` even when nothing is scheduled (offline mode, no metadata, etc.).
|
||||||
|
"""
|
||||||
|
@spec maybe_schedule(Post.t()) :: :ok
|
||||||
|
def maybe_schedule(%Post{do_not_translate: true}), do: :ok
|
||||||
|
|
||||||
|
def maybe_schedule(%Post{} = post) do
|
||||||
|
with true <- configured?(),
|
||||||
|
{:ok, metadata} <- Metadata.get_project_metadata(post.project_id) do
|
||||||
|
post
|
||||||
|
|> missing_languages(metadata)
|
||||||
|
|> Enum.each(&queue_post(post, &1))
|
||||||
|
else
|
||||||
|
_other -> :ok
|
||||||
|
end
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def missing_languages(%Post{} = post, metadata) do
|
||||||
|
source_language = normalize_language(post.language || metadata.main_language)
|
||||||
|
|
||||||
|
configured_languages =
|
||||||
|
([metadata.main_language] ++ (metadata.blog_languages || []))
|
||||||
|
|> Enum.map(&normalize_language/1)
|
||||||
|
|> Enum.reject(&(&1 in [nil, ""]))
|
||||||
|
|> Enum.uniq()
|
||||||
|
|
||||||
|
existing_languages =
|
||||||
|
Repo.all(
|
||||||
|
from translation in Translation,
|
||||||
|
where: translation.translation_for == ^post.id,
|
||||||
|
select: translation.language
|
||||||
|
)
|
||||||
|
|
||||||
|
configured_languages
|
||||||
|
|> Enum.reject(&(&1 == source_language or &1 in existing_languages))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp queue_post(%Post{} = post, language) do
|
||||||
|
_ =
|
||||||
|
Tasks.submit_task(
|
||||||
|
"Auto-translate Post to #{language}",
|
||||||
|
fn report ->
|
||||||
|
report.(0.05, "Translating post to #{language}")
|
||||||
|
|
||||||
|
with {:ok, translation} <- AI.translate_post(post.id, language, ai_opts()),
|
||||||
|
{:ok, saved_translation} <-
|
||||||
|
BDS.Posts.upsert_post_translation(post.id, language, %{
|
||||||
|
title: translation.title,
|
||||||
|
excerpt: translation.excerpt,
|
||||||
|
content: translation.content,
|
||||||
|
auto_generated: true
|
||||||
|
}) do
|
||||||
|
report.(0.85, "Post translation saved")
|
||||||
|
:ok = queue_media_cascade(post, language)
|
||||||
|
report.(1.0, "Post translation complete")
|
||||||
|
%{post_id: post.id, translation_id: saved_translation.id, language: language}
|
||||||
|
else
|
||||||
|
{:error, reason} -> {:error, reason}
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
task_attrs(post)
|
||||||
|
)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp queue_media_cascade(%Post{} = post, language) do
|
||||||
|
linked_media_ids(post.id)
|
||||||
|
|> Enum.each(fn media_id ->
|
||||||
|
if media_needed?(media_id, language) do
|
||||||
|
queue_media(post, media_id, language)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp queue_media(%Post{} = post, media_id, language) do
|
||||||
|
_ =
|
||||||
|
Tasks.submit_task(
|
||||||
|
"Auto-translate Media to #{language}",
|
||||||
|
fn report ->
|
||||||
|
report.(0.05, "Translating media to #{language}")
|
||||||
|
|
||||||
|
with {:ok, translation} <- AI.translate_media(media_id, language, ai_opts()),
|
||||||
|
{:ok, saved_translation} <-
|
||||||
|
Media.upsert_media_translation(media_id, language, %{
|
||||||
|
title: translation.title,
|
||||||
|
alt: translation.alt,
|
||||||
|
caption: translation.caption
|
||||||
|
}) do
|
||||||
|
report.(1.0, "Media translation complete")
|
||||||
|
%{media_id: media_id, translation_id: saved_translation.id, language: language}
|
||||||
|
else
|
||||||
|
{:error, reason} -> {:error, reason}
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
task_attrs(post)
|
||||||
|
)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp media_needed?(media_id, language) do
|
||||||
|
case Repo.get(Media.Media, media_id) do
|
||||||
|
%Media.Media{language: source_language} when source_language not in [nil, ""] and source_language != language ->
|
||||||
|
not Repo.exists?(
|
||||||
|
from translation in Media.Translation,
|
||||||
|
where: translation.translation_for == ^media_id and translation.language == ^language
|
||||||
|
)
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp task_attrs(%Post{} = post), do: %{group_id: post.project_id, group_name: "AI"}
|
||||||
|
|
||||||
|
defp ai_opts do
|
||||||
|
Application.get_env(:bds, :posts, [])
|
||||||
|
|> Keyword.get(:auto_translation_ai_opts, [])
|
||||||
|
end
|
||||||
|
|
||||||
|
defp configured? do
|
||||||
|
mode = if AI.airplane_mode?(), do: :airplane, else: :online
|
||||||
|
|
||||||
|
case AI.get_endpoint(mode) do
|
||||||
|
{:ok, %{url: url, model: model} = endpoint}
|
||||||
|
when is_binary(url) and url != "" and is_binary(model) and model != "" ->
|
||||||
|
mode == :airplane or present?(Map.get(endpoint, :api_key))
|
||||||
|
|
||||||
|
_other ->
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp linked_media_ids(post_id) do
|
||||||
|
Repo.all(
|
||||||
|
from pm in PostMedia,
|
||||||
|
where: pm.post_id == ^post_id,
|
||||||
|
order_by: [asc: pm.sort_order, asc: pm.media_id],
|
||||||
|
select: pm.media_id
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp normalize_language(nil), do: ""
|
||||||
|
|
||||||
|
defp normalize_language(language) do
|
||||||
|
language
|
||||||
|
|> to_string()
|
||||||
|
|> String.trim()
|
||||||
|
|> String.downcase()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp present?(value) when is_binary(value), do: String.trim(value) != ""
|
||||||
|
defp present?(value), do: not is_nil(value)
|
||||||
|
end
|
||||||
146
lib/bds/posts/file_sync.ex
Normal file
146
lib/bds/posts/file_sync.ex
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
defmodule BDS.Posts.FileSync do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
alias BDS.Frontmatter
|
||||||
|
alias BDS.Persistence
|
||||||
|
alias BDS.Posts.Post
|
||||||
|
alias BDS.Posts.Translation
|
||||||
|
alias BDS.Projects
|
||||||
|
|
||||||
|
@doc "Compute the canonical relative path for a published post."
|
||||||
|
@spec post_relative_path(String.t(), integer()) :: String.t()
|
||||||
|
def post_relative_path(slug, created_at) do
|
||||||
|
datetime = Persistence.from_unix_ms!(created_at)
|
||||||
|
year = Integer.to_string(datetime.year)
|
||||||
|
month = datetime.month |> Integer.to_string() |> String.pad_leading(2, "0")
|
||||||
|
Path.join(["posts", year, month, "#{slug}.md"])
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Compute the canonical relative path for a translation file."
|
||||||
|
@spec translation_relative_path(Post.t(), String.t()) :: String.t()
|
||||||
|
def translation_relative_path(post, language) do
|
||||||
|
datetime = Persistence.from_unix_ms!(post.created_at)
|
||||||
|
year = Integer.to_string(datetime.year)
|
||||||
|
month = datetime.month |> Integer.to_string() |> String.pad_leading(2, "0")
|
||||||
|
Path.join(["posts", year, month, "#{post.slug}.#{language}.md"])
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Resolve the body to publish for a post, falling back to its existing file."
|
||||||
|
@spec publishable_post_body(Post.t(), String.t(), term()) :: String.t()
|
||||||
|
def publishable_post_body(%Post{content: content}, _full_path, _project)
|
||||||
|
when is_binary(content), do: content
|
||||||
|
|
||||||
|
def publishable_post_body(%Post{file_path: file_path} = post, full_path, project) do
|
||||||
|
source_path =
|
||||||
|
if file_path in [nil, ""] do
|
||||||
|
full_path
|
||||||
|
else
|
||||||
|
Path.join(Projects.project_data_dir(project), file_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
published_post_body(post, source_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Read the body of a previously-published post (DB content first, file fallback)."
|
||||||
|
@spec published_post_body(Post.t(), String.t()) :: String.t()
|
||||||
|
def published_post_body(%Post{content: content}, _full_path) when is_binary(content),
|
||||||
|
do: content
|
||||||
|
|
||||||
|
def published_post_body(_post, full_path), do: read_markdown_body(full_path)
|
||||||
|
|
||||||
|
@doc "Read the body section (after frontmatter) from a markdown file on disk."
|
||||||
|
@spec read_markdown_body(String.t()) :: String.t()
|
||||||
|
def read_markdown_body(path) do
|
||||||
|
case File.read(path) do
|
||||||
|
{:ok, contents} ->
|
||||||
|
case String.split(contents, "\n---\n", parts: 2) do
|
||||||
|
[_frontmatter, body] -> String.trim_trailing(body, "\n")
|
||||||
|
_parts -> ""
|
||||||
|
end
|
||||||
|
|
||||||
|
{:error, _reason} ->
|
||||||
|
""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Serialize a post to a frontmatter+body string for the published file."
|
||||||
|
@spec serialize_post_file(Post.t(), integer()) :: String.t()
|
||||||
|
def serialize_post_file(post, published_at) do
|
||||||
|
Frontmatter.serialize_document(
|
||||||
|
[
|
||||||
|
{"id", post.id},
|
||||||
|
{"title", post.title},
|
||||||
|
{"slug", post.slug},
|
||||||
|
{"excerpt", post.excerpt},
|
||||||
|
{"status", :published},
|
||||||
|
{"author", post.author},
|
||||||
|
{"language", post.language},
|
||||||
|
{"doNotTranslate", post.do_not_translate},
|
||||||
|
{"templateSlug", post.template_slug},
|
||||||
|
{"createdAt", post.created_at},
|
||||||
|
{"updatedAt", post.updated_at},
|
||||||
|
{"publishedAt", published_at},
|
||||||
|
{"tags", post.tags || []},
|
||||||
|
{"categories", post.categories || []}
|
||||||
|
],
|
||||||
|
post.content
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Serialize a translation row to a frontmatter+body string."
|
||||||
|
@spec serialize_translation_file(Translation.t(), integer()) :: String.t()
|
||||||
|
def serialize_translation_file(translation, published_at) do
|
||||||
|
Frontmatter.serialize_document(
|
||||||
|
[
|
||||||
|
{"id", translation.id},
|
||||||
|
{"translationFor", translation.translation_for},
|
||||||
|
{"language", translation.language},
|
||||||
|
{"title", translation.title},
|
||||||
|
{"excerpt", translation.excerpt},
|
||||||
|
{"status", :published},
|
||||||
|
{"createdAt", translation.created_at},
|
||||||
|
{"updatedAt", translation.updated_at},
|
||||||
|
{"publishedAt", published_at}
|
||||||
|
],
|
||||||
|
translation.content
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Resolve the body of a translation, falling back to its existing file."
|
||||||
|
@spec publishable_translation_body(Translation.t(), String.t()) :: String.t()
|
||||||
|
def publishable_translation_body(%Translation{content: content}, _full_path)
|
||||||
|
when is_binary(content), do: content
|
||||||
|
|
||||||
|
def publishable_translation_body(_translation, full_path) do
|
||||||
|
read_markdown_body(full_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Delete a published post's file on disk (no-op if it has none)."
|
||||||
|
@spec delete_post_file(Post.t()) :: :ok | {:error, term()}
|
||||||
|
def delete_post_file(%Post{file_path: file_path}) when file_path in [nil, ""], do: :ok
|
||||||
|
|
||||||
|
def delete_post_file(%Post{} = post) do
|
||||||
|
project = Projects.get_project!(post.project_id)
|
||||||
|
full_path = Path.join(Projects.project_data_dir(project), post.file_path)
|
||||||
|
rm_quiet(full_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Delete a translation's file on disk (no-op if it has none)."
|
||||||
|
@spec delete_translation_file(Translation.t()) :: :ok | {:error, term()}
|
||||||
|
def delete_translation_file(%Translation{file_path: file_path}) when file_path in [nil, ""],
|
||||||
|
do: :ok
|
||||||
|
|
||||||
|
def delete_translation_file(%Translation{} = translation) do
|
||||||
|
project = Projects.get_project!(translation.project_id)
|
||||||
|
full_path = Path.join(Projects.project_data_dir(project), translation.file_path)
|
||||||
|
rm_quiet(full_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp rm_quiet(full_path) do
|
||||||
|
case File.rm(full_path) do
|
||||||
|
:ok -> :ok
|
||||||
|
{:error, :enoent} -> :ok
|
||||||
|
{:error, reason} -> {:error, reason}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
320
lib/bds/posts/rebuild_from_files.ex
Normal file
320
lib/bds/posts/rebuild_from_files.ex
Normal file
@@ -0,0 +1,320 @@
|
|||||||
|
defmodule BDS.Posts.RebuildFromFiles do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
alias BDS.DocumentFields
|
||||||
|
alias BDS.Embeddings
|
||||||
|
alias BDS.Frontmatter
|
||||||
|
alias BDS.Persistence
|
||||||
|
alias BDS.Posts.Post
|
||||||
|
alias BDS.Posts.Slugs
|
||||||
|
alias BDS.Posts.Translation
|
||||||
|
alias BDS.Posts.TranslationValidation
|
||||||
|
alias BDS.Projects
|
||||||
|
alias BDS.Rebuild
|
||||||
|
alias BDS.Repo
|
||||||
|
alias BDS.Search
|
||||||
|
|
||||||
|
@spec rebuild_posts_from_files(String.t(), keyword()) :: {:ok, [Post.t()]}
|
||||||
|
def rebuild_posts_from_files(project_id, opts \\ []) do
|
||||||
|
project = Projects.get_project!(project_id)
|
||||||
|
on_progress = progress_callback(opts)
|
||||||
|
|
||||||
|
rebuild_files =
|
||||||
|
project
|
||||||
|
|> Projects.project_data_dir()
|
||||||
|
|> Path.join("posts")
|
||||||
|
|> TranslationValidation.list_matching_files("*.md")
|
||||||
|
|> Rebuild.parallel_map(&parse_rebuild_file(project, &1))
|
||||||
|
|
||||||
|
total_files = length(rebuild_files)
|
||||||
|
:ok = report_rebuild_started(on_progress, total_files, "post files")
|
||||||
|
|
||||||
|
{translation_files, post_files} =
|
||||||
|
Enum.split_with(rebuild_files, &TranslationValidation.translation_rebuild_file?/1)
|
||||||
|
|
||||||
|
posts =
|
||||||
|
post_files
|
||||||
|
|> Enum.with_index(1)
|
||||||
|
|> Enum.map(fn {file, index} ->
|
||||||
|
post =
|
||||||
|
upsert_post_from_rebuild_file(project_id, file,
|
||||||
|
sync_search: false,
|
||||||
|
sync_embeddings: false
|
||||||
|
)
|
||||||
|
|
||||||
|
:ok = report_rebuild_progress(on_progress, index, total_files, "post files")
|
||||||
|
post
|
||||||
|
end)
|
||||||
|
|
||||||
|
translation_files
|
||||||
|
|> Enum.with_index(length(post_files) + 1)
|
||||||
|
|> Enum.each(fn {file, index} ->
|
||||||
|
upsert_post_translation_from_rebuild_file(project_id, file, sync_search: false)
|
||||||
|
:ok = report_rebuild_progress(on_progress, index, total_files, "post files")
|
||||||
|
end)
|
||||||
|
|
||||||
|
if Keyword.get(opts, :reindex_search, true) do
|
||||||
|
:ok = report_rebuild_phase(on_progress, 0.97, "Refreshing post search index")
|
||||||
|
|
||||||
|
:ok =
|
||||||
|
Search.reindex_posts(project_id,
|
||||||
|
on_progress: scaled_progress_reporter(on_progress, 0.97, 0.99)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
if Keyword.get(opts, :rebuild_embeddings, true) do
|
||||||
|
:ok = report_rebuild_phase(on_progress, 0.99, "Refreshing post embeddings")
|
||||||
|
|
||||||
|
{:ok, _rebuilt_post_ids} =
|
||||||
|
Embeddings.rebuild_project(project_id,
|
||||||
|
on_progress: scaled_progress_reporter(on_progress, 0.99, 1.0)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
{:ok, posts}
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec import_orphan_post_file(String.t(), String.t()) ::
|
||||||
|
{:ok, Post.t()} | {:error, :not_found | :unsupported_file}
|
||||||
|
def import_orphan_post_file(project_id, relative_path) do
|
||||||
|
project = Projects.get_project!(project_id)
|
||||||
|
full_path = Path.join(Projects.project_data_dir(project), relative_path)
|
||||||
|
|
||||||
|
if File.exists?(full_path) do
|
||||||
|
rebuild_file = parse_rebuild_file(project, full_path)
|
||||||
|
|
||||||
|
if TranslationValidation.translation_rebuild_file?(rebuild_file) do
|
||||||
|
{:error, :unsupported_file}
|
||||||
|
else
|
||||||
|
fields =
|
||||||
|
rebuild_file.fields
|
||||||
|
|> Map.put("id", unique_post_id(Map.get(rebuild_file.fields, "id")))
|
||||||
|
|> Map.put(
|
||||||
|
"slug",
|
||||||
|
Slugs.unique_for_import(project_id, Map.fetch!(rebuild_file.fields, "slug"))
|
||||||
|
)
|
||||||
|
|
||||||
|
{:ok, upsert_post_from_rebuild_file(project_id, %{rebuild_file | fields: fields})}
|
||||||
|
end
|
||||||
|
else
|
||||||
|
{:error, :not_found}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec import_orphan_post_translation_file(String.t(), String.t()) ::
|
||||||
|
{:ok, Translation.t()} | {:error, :not_found | :unsupported_file | :conflict}
|
||||||
|
def import_orphan_post_translation_file(project_id, relative_path) do
|
||||||
|
project = Projects.get_project!(project_id)
|
||||||
|
full_path = Path.join(Projects.project_data_dir(project), relative_path)
|
||||||
|
|
||||||
|
if File.exists?(full_path) do
|
||||||
|
rebuild_file = parse_rebuild_file(project, full_path)
|
||||||
|
|
||||||
|
if TranslationValidation.translation_rebuild_file?(rebuild_file) do
|
||||||
|
source_post_id = Map.fetch!(rebuild_file.fields, "translationFor")
|
||||||
|
language = TranslationValidation.normalize_language(Map.fetch!(rebuild_file.fields, "language"))
|
||||||
|
|
||||||
|
case Repo.get(Post, source_post_id) do
|
||||||
|
nil ->
|
||||||
|
{:error, :not_found}
|
||||||
|
|
||||||
|
%Post{} = post ->
|
||||||
|
if TranslationValidation.normalize_language(post.language) == language or
|
||||||
|
Repo.get_by(Translation, translation_for: source_post_id, language: language) do
|
||||||
|
{:error, :conflict}
|
||||||
|
else
|
||||||
|
fields = Map.put(rebuild_file.fields, "id", Ecto.UUID.generate())
|
||||||
|
|
||||||
|
{:ok,
|
||||||
|
upsert_post_translation_from_rebuild_file(
|
||||||
|
project_id,
|
||||||
|
%{rebuild_file | fields: fields},
|
||||||
|
sync_search: true
|
||||||
|
)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
{:error, :unsupported_file}
|
||||||
|
end
|
||||||
|
else
|
||||||
|
{:error, :not_found}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def upsert_post_from_file(project_id, project, path) do
|
||||||
|
rebuild_file = parse_rebuild_file(project, path)
|
||||||
|
upsert_post_from_rebuild_file(project_id, rebuild_file)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def upsert_post_from_rebuild_file(project_id, rebuild_file, opts \\ []) do
|
||||||
|
fields = rebuild_file.fields
|
||||||
|
now = Persistence.now_ms()
|
||||||
|
|
||||||
|
attrs = %{
|
||||||
|
id: DocumentFields.get(fields, "id") || Ecto.UUID.generate(),
|
||||||
|
project_id: project_id,
|
||||||
|
title: DocumentFields.get(fields, "title") || "",
|
||||||
|
slug: DocumentFields.fetch!(fields, "slug"),
|
||||||
|
excerpt: Map.get(fields, "excerpt"),
|
||||||
|
content: nil,
|
||||||
|
status: parse_post_status(DocumentFields.get(fields, "status", "published")),
|
||||||
|
author: Map.get(fields, "author"),
|
||||||
|
created_at: DocumentFields.get(fields, "createdAt", now),
|
||||||
|
updated_at: DocumentFields.get(fields, "updatedAt", now),
|
||||||
|
published_at: DocumentFields.get(fields, "publishedAt"),
|
||||||
|
file_path: rebuild_file.relative_path,
|
||||||
|
checksum: nil,
|
||||||
|
tags: Map.get(fields, "tags", []),
|
||||||
|
categories: Map.get(fields, "categories", []),
|
||||||
|
template_slug: DocumentFields.get(fields, "templateSlug"),
|
||||||
|
language: Map.get(fields, "language"),
|
||||||
|
do_not_translate: DocumentFields.get(fields, "doNotTranslate", false),
|
||||||
|
published_title: nil,
|
||||||
|
published_content: nil,
|
||||||
|
published_tags: nil,
|
||||||
|
published_categories: nil,
|
||||||
|
published_excerpt: nil
|
||||||
|
}
|
||||||
|
|
||||||
|
post =
|
||||||
|
Repo.get(Post, attrs.id) ||
|
||||||
|
Repo.get_by(Post, project_id: project_id, file_path: rebuild_file.relative_path) ||
|
||||||
|
Repo.get_by(Post, project_id: project_id, slug: attrs.slug) || %Post{}
|
||||||
|
|
||||||
|
post =
|
||||||
|
post
|
||||||
|
|> Post.changeset(attrs)
|
||||||
|
|> Repo.insert_or_update!()
|
||||||
|
|
||||||
|
if Keyword.get(opts, :sync_search, true) do
|
||||||
|
:ok = Search.sync_post(post)
|
||||||
|
end
|
||||||
|
|
||||||
|
if Keyword.get(opts, :sync_embeddings, true) do
|
||||||
|
:ok = Embeddings.sync_post(post)
|
||||||
|
end
|
||||||
|
|
||||||
|
post
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def upsert_post_translation_from_rebuild_file(project_id, rebuild_file, opts) do
|
||||||
|
fields = rebuild_file.fields
|
||||||
|
source_post_id = DocumentFields.fetch!(fields, "translationFor")
|
||||||
|
source_post = Repo.get_by!(Post, project_id: project_id, id: source_post_id)
|
||||||
|
now = Persistence.now_ms()
|
||||||
|
language = TranslationValidation.normalize_language(DocumentFields.fetch!(fields, "language"))
|
||||||
|
|
||||||
|
translation =
|
||||||
|
Repo.get_by(Translation, translation_for: source_post_id, language: language) ||
|
||||||
|
%Translation{}
|
||||||
|
|
||||||
|
attrs = %{
|
||||||
|
id: DocumentFields.get(fields, "id") || Ecto.UUID.generate(),
|
||||||
|
project_id: project_id,
|
||||||
|
translation_for: source_post_id,
|
||||||
|
language: language,
|
||||||
|
title: DocumentFields.get(fields, "title") || "",
|
||||||
|
excerpt: Map.get(fields, "excerpt"),
|
||||||
|
content: nil,
|
||||||
|
status: parse_translation_status(DocumentFields.get(fields, "status", "published")),
|
||||||
|
created_at: DocumentFields.get(fields, "createdAt", source_post.created_at || now),
|
||||||
|
updated_at:
|
||||||
|
DocumentFields.get(
|
||||||
|
fields,
|
||||||
|
"updatedAt",
|
||||||
|
source_post.updated_at || source_post.created_at || now
|
||||||
|
),
|
||||||
|
published_at: DocumentFields.get(fields, "publishedAt", source_post.published_at),
|
||||||
|
file_path: rebuild_file.relative_path,
|
||||||
|
checksum: nil
|
||||||
|
}
|
||||||
|
|
||||||
|
translation
|
||||||
|
|> Translation.changeset(attrs)
|
||||||
|
|> Repo.insert_or_update!()
|
||||||
|
|> tap(fn _translation ->
|
||||||
|
if Keyword.get(opts, :sync_search, true) do
|
||||||
|
:ok = Search.sync_post(source_post_id)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def parse_rebuild_file(project, path) do
|
||||||
|
contents = File.read!(path)
|
||||||
|
{:ok, %{fields: fields}} = Frontmatter.parse_document(contents)
|
||||||
|
|
||||||
|
%{
|
||||||
|
path: path,
|
||||||
|
relative_path: Path.relative_to(path, Projects.project_data_dir(project)),
|
||||||
|
fields: fields
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def parse_post_status(status) when is_atom(status), do: status
|
||||||
|
def parse_post_status(status), do: String.to_existing_atom(status)
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def parse_translation_status(status) when is_atom(status), do: status
|
||||||
|
def parse_translation_status(status), do: String.to_existing_atom(status)
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def progress_callback(opts) do
|
||||||
|
case Keyword.get(opts, :on_progress) do
|
||||||
|
callback when is_function(callback, 2) -> callback
|
||||||
|
_other -> nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def report_rebuild_started(nil, _total, _label), do: :ok
|
||||||
|
|
||||||
|
def report_rebuild_started(callback, 0, label) do
|
||||||
|
callback.(1.0, "No #{label} found")
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
def report_rebuild_started(callback, total, label) do
|
||||||
|
callback.(0.05, "Rebuilding #{label} (0/#{total})")
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def report_rebuild_progress(nil, _current, _total, _label), do: :ok
|
||||||
|
def report_rebuild_progress(_callback, _current, 0, _label), do: :ok
|
||||||
|
|
||||||
|
def report_rebuild_progress(callback, current, total, label) do
|
||||||
|
callback.(0.05 + 0.95 * (current / total), "Rebuilding #{label} (#{current}/#{total})")
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp scaled_progress_reporter(nil, _start_value, _end_value), do: nil
|
||||||
|
|
||||||
|
defp scaled_progress_reporter(report, start_value, end_value) when is_function(report, 2) do
|
||||||
|
fn value, message ->
|
||||||
|
scaled_value = start_value + (end_value - start_value) * value
|
||||||
|
report.(scaled_value, message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp report_rebuild_phase(nil, _progress, _message), do: :ok
|
||||||
|
|
||||||
|
defp report_rebuild_phase(callback, progress, message) do
|
||||||
|
callback.(progress, message)
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp unique_post_id(nil), do: Ecto.UUID.generate()
|
||||||
|
|
||||||
|
defp unique_post_id(id) do
|
||||||
|
if Repo.get(Post, id) || Repo.get(Translation, id) do
|
||||||
|
Ecto.UUID.generate()
|
||||||
|
else
|
||||||
|
id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
86
lib/bds/posts/slugs.ex
Normal file
86
lib/bds/posts/slugs.ex
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
defmodule BDS.Posts.Slugs do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
alias BDS.Posts.Post
|
||||||
|
alias BDS.Repo
|
||||||
|
alias BDS.Slug
|
||||||
|
|
||||||
|
@spec available(String.t(), String.t(), String.t() | nil) :: boolean()
|
||||||
|
def available(project_id, slug, exclude_post_id \\ nil) do
|
||||||
|
normalized_slug = slug |> to_string() |> String.trim()
|
||||||
|
|
||||||
|
query =
|
||||||
|
from(post in Post,
|
||||||
|
where: post.project_id == ^project_id and post.slug == ^normalized_slug,
|
||||||
|
select: post.id,
|
||||||
|
limit: 1
|
||||||
|
)
|
||||||
|
|
||||||
|
case Repo.one(query) do
|
||||||
|
nil -> true
|
||||||
|
^exclude_post_id -> true
|
||||||
|
_other -> false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec unique_for_title(String.t(), String.t(), String.t() | nil) :: String.t()
|
||||||
|
def unique_for_title(project_id, title, exclude_post_id \\ nil) do
|
||||||
|
base_slug = title |> default_source() |> Slug.slugify()
|
||||||
|
|
||||||
|
if available(project_id, base_slug, exclude_post_id) do
|
||||||
|
base_slug
|
||||||
|
else
|
||||||
|
Stream.iterate(2, &(&1 + 1))
|
||||||
|
|> Enum.find_value(fn counter ->
|
||||||
|
candidate = "#{base_slug}-#{counter}"
|
||||||
|
if available(project_id, candidate, exclude_post_id), do: candidate, else: nil
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Pick a free slug, falling back to `untitled` for blank input."
|
||||||
|
@spec unique(String.t(), String.t()) :: String.t()
|
||||||
|
def unique(project_id, base_slug) do
|
||||||
|
normalized = if base_slug == "", do: "untitled", else: base_slug
|
||||||
|
|
||||||
|
if available?(project_id, normalized) do
|
||||||
|
normalized
|
||||||
|
else
|
||||||
|
find_unique(project_id, normalized, 2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Pick a free slug for an imported post by re-slugifying the source value."
|
||||||
|
@spec unique_for_import(String.t(), String.t()) :: String.t()
|
||||||
|
def unique_for_import(project_id, slug) do
|
||||||
|
normalized = slug |> default_source() |> Slug.slugify()
|
||||||
|
|
||||||
|
if available?(project_id, normalized) do
|
||||||
|
normalized
|
||||||
|
else
|
||||||
|
find_unique(project_id, normalized, 2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec default_source(String.t()) :: String.t()
|
||||||
|
def default_source(""), do: "untitled"
|
||||||
|
def default_source(title), do: title
|
||||||
|
|
||||||
|
defp find_unique(project_id, base_slug, suffix) do
|
||||||
|
candidate = "#{base_slug}-#{suffix}"
|
||||||
|
|
||||||
|
if available?(project_id, candidate) do
|
||||||
|
candidate
|
||||||
|
else
|
||||||
|
find_unique(project_id, base_slug, suffix + 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp available?(project_id, slug) do
|
||||||
|
not Repo.exists?(
|
||||||
|
from post in Post, where: post.project_id == ^project_id and post.slug == ^slug
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
464
lib/bds/posts/translation_validation.ex
Normal file
464
lib/bds/posts/translation_validation.ex
Normal file
@@ -0,0 +1,464 @@
|
|||||||
|
defmodule BDS.Posts.TranslationValidation do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
alias BDS.DocumentFields
|
||||||
|
alias BDS.Frontmatter
|
||||||
|
alias BDS.Metadata
|
||||||
|
alias BDS.Posts.Post
|
||||||
|
alias BDS.Posts.RebuildFromFiles
|
||||||
|
alias BDS.Posts.Translation
|
||||||
|
alias BDS.Posts.Translations
|
||||||
|
alias BDS.Projects
|
||||||
|
alias BDS.Repo
|
||||||
|
alias BDS.Search
|
||||||
|
|
||||||
|
@type report :: %{
|
||||||
|
required(:checked_database_row_count) => non_neg_integer(),
|
||||||
|
required(:checked_filesystem_file_count) => non_neg_integer(),
|
||||||
|
required(:invalid_database_rows) => [map()],
|
||||||
|
required(:invalid_filesystem_files) => [map()],
|
||||||
|
required(:missing) => [map()],
|
||||||
|
required(:orphan_files) => [String.t()],
|
||||||
|
required(:do_not_translate_posts) => [String.t()]
|
||||||
|
}
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Validate translation rows + on-disk translation files for a project.
|
||||||
|
|
||||||
|
The result map preserves both the modern invalid-item shape
|
||||||
|
(`invalid_database_rows`, `invalid_filesystem_files`, etc.) and the legacy
|
||||||
|
summary fields (`missing`, `orphan_files`, `do_not_translate_posts`).
|
||||||
|
"""
|
||||||
|
@spec validate(String.t(), keyword()) :: {:ok, report()}
|
||||||
|
def validate(project_id, opts \\ []) do
|
||||||
|
project = Projects.get_project!(project_id)
|
||||||
|
{:ok, metadata} = Metadata.get_project_metadata(project_id)
|
||||||
|
on_progress = RebuildFromFiles.progress_callback(opts)
|
||||||
|
|
||||||
|
source_posts =
|
||||||
|
Repo.all(
|
||||||
|
from post in Post,
|
||||||
|
where: post.project_id == ^project_id,
|
||||||
|
order_by: [asc: post.created_at, asc: post.slug]
|
||||||
|
)
|
||||||
|
|
||||||
|
source_post_map = Map.new(source_posts, &{&1.id, &1})
|
||||||
|
|
||||||
|
translation_rows =
|
||||||
|
Repo.all(
|
||||||
|
from translation in Translation,
|
||||||
|
where: translation.project_id == ^project_id,
|
||||||
|
order_by: [asc: translation.translation_for, asc: translation.language, asc: translation.id]
|
||||||
|
)
|
||||||
|
|
||||||
|
project_data_dir = Projects.project_data_dir(project)
|
||||||
|
|
||||||
|
markdown_files =
|
||||||
|
project_data_dir
|
||||||
|
|> Path.join("posts")
|
||||||
|
|> list_markdown_files_recursive()
|
||||||
|
|
||||||
|
total_items = length(translation_rows) + length(markdown_files)
|
||||||
|
:ok = RebuildFromFiles.report_rebuild_started(on_progress, total_items, "translations")
|
||||||
|
|
||||||
|
invalid_database_rows =
|
||||||
|
translation_rows
|
||||||
|
|> Enum.with_index(1)
|
||||||
|
|> Enum.flat_map(fn {translation, index} ->
|
||||||
|
:ok = RebuildFromFiles.report_rebuild_progress(on_progress, index, total_items, "translations")
|
||||||
|
|
||||||
|
case invalid_database_translation_issue(translation, source_post_map, metadata) do
|
||||||
|
nil -> []
|
||||||
|
issue -> [issue]
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|> Enum.sort_by(&issue_sort_key/1)
|
||||||
|
|
||||||
|
{checked_filesystem_file_count, invalid_filesystem_files} =
|
||||||
|
markdown_files
|
||||||
|
|> Enum.with_index(length(translation_rows) + 1)
|
||||||
|
|> Enum.reduce({0, []}, fn {file_path, index}, {count, issues} ->
|
||||||
|
:ok = RebuildFromFiles.report_rebuild_progress(on_progress, index, total_items, "translations")
|
||||||
|
|
||||||
|
case invalid_filesystem_translation_issue(file_path, source_post_map, metadata) do
|
||||||
|
{:ok, nil} -> {count + 1, issues}
|
||||||
|
{:ok, issue} -> {count + 1, [issue | issues]}
|
||||||
|
:skip -> {count, issues}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
missing = legacy_missing_entries(source_posts, translation_rows, metadata)
|
||||||
|
orphan_files = legacy_orphan_files(invalid_filesystem_files, project_data_dir)
|
||||||
|
do_not_translate_posts = legacy_do_not_translate_posts(source_posts)
|
||||||
|
|
||||||
|
{:ok,
|
||||||
|
%{
|
||||||
|
checked_database_row_count: length(translation_rows),
|
||||||
|
checked_filesystem_file_count: checked_filesystem_file_count,
|
||||||
|
invalid_database_rows: invalid_database_rows,
|
||||||
|
invalid_filesystem_files:
|
||||||
|
invalid_filesystem_files |> Enum.reverse() |> Enum.sort_by(&issue_sort_key/1),
|
||||||
|
missing: missing,
|
||||||
|
orphan_files: orphan_files,
|
||||||
|
do_not_translate_posts: do_not_translate_posts
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Apply fixes for the issues described in a validation `report`."
|
||||||
|
@spec fix_invalid(map()) ::
|
||||||
|
{:ok,
|
||||||
|
%{
|
||||||
|
deleted_database_rows: non_neg_integer(),
|
||||||
|
deleted_files: non_neg_integer(),
|
||||||
|
flushed_translations: non_neg_integer()
|
||||||
|
}}
|
||||||
|
def fix_invalid(report) when is_map(report) do
|
||||||
|
normalized_report = normalize_report(report)
|
||||||
|
|
||||||
|
{deleted_database_rows, flushed_translations, synced_post_ids} =
|
||||||
|
Enum.reduce(normalized_report.invalid_database_rows, {0, 0, MapSet.new()}, fn issue, {deleted, flushed, synced_ids} ->
|
||||||
|
case fix_invalid_database_row(issue) do
|
||||||
|
{:deleted, post_id} -> {deleted + 1, flushed, maybe_put_synced_post(synced_ids, post_id)}
|
||||||
|
{:flushed, post_id} -> {deleted, flushed + 1, maybe_put_synced_post(synced_ids, post_id)}
|
||||||
|
:noop -> {deleted, flushed, synced_ids}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
deleted_files =
|
||||||
|
Enum.reduce(normalized_report.invalid_filesystem_files, 0, fn issue, count ->
|
||||||
|
if delete_validation_file(issue.file_path), do: count + 1, else: count
|
||||||
|
end)
|
||||||
|
|
||||||
|
Enum.each(synced_post_ids, &Search.sync_post/1)
|
||||||
|
|
||||||
|
{:ok,
|
||||||
|
%{
|
||||||
|
deleted_database_rows: deleted_database_rows,
|
||||||
|
deleted_files: deleted_files,
|
||||||
|
flushed_translations: flushed_translations
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "True if the parsed rebuild file represents a translation (`translationFor` set, no `slug`)."
|
||||||
|
@spec translation_rebuild_file?(map()) :: boolean()
|
||||||
|
def translation_rebuild_file?(%{fields: fields}) do
|
||||||
|
DocumentFields.has_key?(fields, "translationFor") and
|
||||||
|
not DocumentFields.has_key?(fields, "slug")
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Recursively list `.md`/`.markdown`/`.mdx` files under `dir`."
|
||||||
|
@spec list_markdown_files_recursive(String.t()) :: [String.t()]
|
||||||
|
def list_markdown_files_recursive(dir) do
|
||||||
|
["*.md", "*.markdown", "*.mdx"]
|
||||||
|
|> Enum.flat_map(&list_matching_files(dir, &1))
|
||||||
|
|> Enum.uniq()
|
||||||
|
|> Enum.sort()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "List files in `dir` matching `pattern` (recursive glob)."
|
||||||
|
@spec list_matching_files(String.t(), String.t()) :: [String.t()]
|
||||||
|
def list_matching_files(dir, pattern) do
|
||||||
|
if File.dir?(dir) do
|
||||||
|
Path.join([dir, "**", pattern])
|
||||||
|
|> Path.wildcard()
|
||||||
|
|> Enum.sort()
|
||||||
|
else
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def normalize_language(value), do: do_normalize_language(value)
|
||||||
|
|
||||||
|
# ----- internals -----
|
||||||
|
|
||||||
|
defp invalid_database_translation_issue(%Translation{} = translation, source_post_map, metadata) do
|
||||||
|
source_post = Map.get(source_post_map, translation.translation_for)
|
||||||
|
normalized_language = do_normalize_language(translation.language)
|
||||||
|
|
||||||
|
cond do
|
||||||
|
is_nil(source_post) ->
|
||||||
|
issue(%{
|
||||||
|
issue: "missing-source-post",
|
||||||
|
translation_id: translation.id,
|
||||||
|
translation_for: translation.translation_for,
|
||||||
|
translation_language: normalized_language,
|
||||||
|
title: translation.title,
|
||||||
|
file_path: blank_to_nil(translation.file_path)
|
||||||
|
})
|
||||||
|
|
||||||
|
canonical_language?(source_post, normalized_language, metadata) ->
|
||||||
|
issue(%{
|
||||||
|
issue: "same-language-as-canonical",
|
||||||
|
translation_id: translation.id,
|
||||||
|
translation_for: translation.translation_for,
|
||||||
|
canonical_language: canonical_language(source_post, metadata),
|
||||||
|
translation_language: normalized_language,
|
||||||
|
title: translation.title,
|
||||||
|
file_path: blank_to_nil(translation.file_path)
|
||||||
|
})
|
||||||
|
|
||||||
|
source_post.do_not_translate ->
|
||||||
|
issue(%{
|
||||||
|
issue: "do-not-translate-has-translations",
|
||||||
|
translation_id: translation.id,
|
||||||
|
translation_for: translation.translation_for,
|
||||||
|
translation_language: normalized_language,
|
||||||
|
title: translation.title,
|
||||||
|
file_path: blank_to_nil(translation.file_path)
|
||||||
|
})
|
||||||
|
|
||||||
|
translation.status == :published and present?(translation.content) ->
|
||||||
|
issue(%{
|
||||||
|
issue: "content-in-database",
|
||||||
|
translation_id: translation.id,
|
||||||
|
translation_for: translation.translation_for,
|
||||||
|
translation_language: normalized_language,
|
||||||
|
title: translation.title,
|
||||||
|
file_path: blank_to_nil(translation.file_path)
|
||||||
|
})
|
||||||
|
|
||||||
|
true ->
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp invalid_filesystem_translation_issue(file_path, source_post_map, metadata) do
|
||||||
|
with {:ok, contents} <- File.read(file_path),
|
||||||
|
{:ok, %{fields: fields}} <- Frontmatter.parse_document(contents),
|
||||||
|
true <- translation_rebuild_file?(%{fields: fields}) do
|
||||||
|
translation_for = DocumentFields.get(fields, "translationFor")
|
||||||
|
source_post = Map.get(source_post_map, translation_for)
|
||||||
|
normalized_language = do_normalize_language(DocumentFields.get(fields, "language"))
|
||||||
|
title = DocumentFields.get(fields, "title")
|
||||||
|
|
||||||
|
result =
|
||||||
|
cond do
|
||||||
|
is_nil(source_post) ->
|
||||||
|
issue(%{
|
||||||
|
issue: "missing-source-post",
|
||||||
|
translation_for: translation_for,
|
||||||
|
translation_language: normalized_language,
|
||||||
|
title: title,
|
||||||
|
file_path: file_path
|
||||||
|
})
|
||||||
|
|
||||||
|
canonical_language?(source_post, normalized_language, metadata) ->
|
||||||
|
issue(%{
|
||||||
|
issue: "same-language-as-canonical",
|
||||||
|
translation_for: translation_for,
|
||||||
|
canonical_language: canonical_language(source_post, metadata),
|
||||||
|
translation_language: normalized_language,
|
||||||
|
title: title,
|
||||||
|
file_path: file_path
|
||||||
|
})
|
||||||
|
|
||||||
|
source_post.do_not_translate ->
|
||||||
|
issue(%{
|
||||||
|
issue: "do-not-translate-has-translations",
|
||||||
|
translation_for: translation_for,
|
||||||
|
translation_language: normalized_language,
|
||||||
|
title: title,
|
||||||
|
file_path: file_path
|
||||||
|
})
|
||||||
|
|
||||||
|
true ->
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
{:ok, result}
|
||||||
|
else
|
||||||
|
false -> :skip
|
||||||
|
_other -> :skip
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp normalize_report(report) do
|
||||||
|
%{
|
||||||
|
checked_database_row_count: map_value(report, :checked_database_row_count, 0),
|
||||||
|
checked_filesystem_file_count: map_value(report, :checked_filesystem_file_count, 0),
|
||||||
|
invalid_database_rows:
|
||||||
|
report |> map_value(:invalid_database_rows, []) |> Enum.map(&normalize_issue/1),
|
||||||
|
invalid_filesystem_files:
|
||||||
|
report |> map_value(:invalid_filesystem_files, []) |> Enum.map(&normalize_issue/1)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp legacy_missing_entries(source_posts, translation_rows, metadata) do
|
||||||
|
configured_languages =
|
||||||
|
([Map.get(metadata, :main_language)] ++ Map.get(metadata, :blog_languages, []))
|
||||||
|
|> Enum.map(&do_normalize_language/1)
|
||||||
|
|> Enum.reject(&(&1 in [nil, ""]))
|
||||||
|
|> Enum.uniq()
|
||||||
|
|
||||||
|
existing_languages_by_post =
|
||||||
|
Enum.reduce(translation_rows, %{}, fn translation, acc ->
|
||||||
|
Map.update(
|
||||||
|
acc,
|
||||||
|
translation.translation_for,
|
||||||
|
MapSet.new([do_normalize_language(translation.language)]),
|
||||||
|
&MapSet.put(&1, do_normalize_language(translation.language))
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
|
source_posts
|
||||||
|
|> Enum.filter(&(&1.status == :published and not &1.do_not_translate))
|
||||||
|
|> Enum.flat_map(fn post ->
|
||||||
|
canonical = canonical_language(post, metadata)
|
||||||
|
existing_languages = Map.get(existing_languages_by_post, post.id, MapSet.new())
|
||||||
|
|
||||||
|
configured_languages
|
||||||
|
|> Enum.reject(&(&1 == canonical or MapSet.member?(existing_languages, &1)))
|
||||||
|
|> Enum.map(&%{post_id: post.id, language: &1})
|
||||||
|
end)
|
||||||
|
|> Enum.sort_by(&{&1.post_id, &1.language})
|
||||||
|
end
|
||||||
|
|
||||||
|
defp legacy_orphan_files(invalid_filesystem_files, project_data_dir) do
|
||||||
|
invalid_filesystem_files
|
||||||
|
|> Enum.filter(&(Map.get(&1, :issue) == "missing-source-post"))
|
||||||
|
|> Enum.map(fn issue ->
|
||||||
|
issue
|
||||||
|
|> Map.get(:file_path)
|
||||||
|
|> relative_project_data_path(project_data_dir)
|
||||||
|
end)
|
||||||
|
|> Enum.reject(&is_nil/1)
|
||||||
|
|> Enum.sort()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp legacy_do_not_translate_posts(source_posts) do
|
||||||
|
source_posts
|
||||||
|
|> Enum.filter(&(&1.status == :published and &1.do_not_translate))
|
||||||
|
|> Enum.map(& &1.id)
|
||||||
|
|> Enum.sort()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp normalize_issue(issue) when is_map(issue) do
|
||||||
|
%{
|
||||||
|
issue: map_value(issue, :issue),
|
||||||
|
translation_id: blank_to_nil(map_value(issue, :translation_id)),
|
||||||
|
translation_for: map_value(issue, :translation_for),
|
||||||
|
canonical_language: blank_to_nil(map_value(issue, :canonical_language)),
|
||||||
|
translation_language: map_value(issue, :translation_language),
|
||||||
|
title: blank_to_nil(map_value(issue, :title)),
|
||||||
|
file_path: blank_to_nil(map_value(issue, :file_path))
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp fix_invalid_database_row(%{issue: "content-in-database", translation_id: translation_id})
|
||||||
|
when is_binary(translation_id) do
|
||||||
|
case Repo.get(Translation, translation_id) do
|
||||||
|
%Translation{} = translation ->
|
||||||
|
case Repo.get(Post, translation.translation_for) do
|
||||||
|
%Post{} = post ->
|
||||||
|
:ok = Translations.publish_translation(post, translation)
|
||||||
|
{:flushed, translation.translation_for}
|
||||||
|
|
||||||
|
nil ->
|
||||||
|
:noop
|
||||||
|
end
|
||||||
|
|
||||||
|
nil ->
|
||||||
|
:noop
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp fix_invalid_database_row(%{translation_id: translation_id, translation_for: translation_for})
|
||||||
|
when is_binary(translation_id) do
|
||||||
|
case Repo.get(Translation, translation_id) do
|
||||||
|
%Translation{} = translation ->
|
||||||
|
Repo.delete!(translation)
|
||||||
|
{:deleted, translation_for}
|
||||||
|
|
||||||
|
nil ->
|
||||||
|
:noop
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp fix_invalid_database_row(_issue), do: :noop
|
||||||
|
|
||||||
|
defp delete_validation_file(file_path) when file_path in [nil, ""], do: false
|
||||||
|
|
||||||
|
defp delete_validation_file(file_path) do
|
||||||
|
case File.rm(file_path) do
|
||||||
|
:ok -> true
|
||||||
|
{:error, :enoent} -> false
|
||||||
|
{:error, _reason} -> false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp issue(attrs) do
|
||||||
|
%{
|
||||||
|
issue: Map.get(attrs, :issue),
|
||||||
|
translation_id: Map.get(attrs, :translation_id),
|
||||||
|
translation_for: Map.get(attrs, :translation_for),
|
||||||
|
canonical_language: Map.get(attrs, :canonical_language),
|
||||||
|
translation_language: Map.get(attrs, :translation_language),
|
||||||
|
title: Map.get(attrs, :title),
|
||||||
|
file_path: Map.get(attrs, :file_path)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp issue_sort_key(issue) do
|
||||||
|
[Map.get(issue, :translation_for), Map.get(issue, :translation_id), Map.get(issue, :file_path)]
|
||||||
|
|> Enum.map(&to_string(&1 || ""))
|
||||||
|
|> Enum.join(":")
|
||||||
|
end
|
||||||
|
|
||||||
|
defp canonical_language(source_post, metadata) do
|
||||||
|
language = do_normalize_language(source_post.language)
|
||||||
|
|
||||||
|
if language == "" do
|
||||||
|
do_normalize_language(Map.get(metadata, :main_language))
|
||||||
|
else
|
||||||
|
language
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp canonical_language?(source_post, language, metadata) do
|
||||||
|
canonical = canonical_language(source_post, metadata)
|
||||||
|
canonical != "" and canonical == do_normalize_language(language)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_normalize_language(nil), do: ""
|
||||||
|
|
||||||
|
defp do_normalize_language(language) do
|
||||||
|
language
|
||||||
|
|> to_string()
|
||||||
|
|> String.downcase()
|
||||||
|
|> String.split("-", parts: 2)
|
||||||
|
|> hd()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp map_value(map, key, default \\ nil) when is_map(map) do
|
||||||
|
Map.get(map, key, Map.get(map, Atom.to_string(key), default))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp blank_to_nil(value) when is_binary(value) do
|
||||||
|
case String.trim(value) do
|
||||||
|
"" -> nil
|
||||||
|
trimmed -> trimmed
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp blank_to_nil(value), do: value
|
||||||
|
|
||||||
|
defp relative_project_data_path(nil, _project_data_dir), do: nil
|
||||||
|
|
||||||
|
defp relative_project_data_path(file_path, project_data_dir) do
|
||||||
|
case Path.relative_to(file_path, project_data_dir) do
|
||||||
|
relative_path when relative_path == file_path -> file_path
|
||||||
|
relative_path -> relative_path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_put_synced_post(set, post_id) when is_binary(post_id) and post_id != "",
|
||||||
|
do: MapSet.put(set, post_id)
|
||||||
|
|
||||||
|
defp maybe_put_synced_post(set, _post_id), do: set
|
||||||
|
|
||||||
|
defp present?(value) when is_binary(value), do: String.trim(value) != ""
|
||||||
|
defp present?(value), do: not is_nil(value)
|
||||||
|
end
|
||||||
279
lib/bds/posts/translations.ex
Normal file
279
lib/bds/posts/translations.ex
Normal file
@@ -0,0 +1,279 @@
|
|||||||
|
defmodule BDS.Posts.Translations do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
alias BDS.Persistence
|
||||||
|
alias BDS.Posts
|
||||||
|
alias BDS.Posts.FileSync
|
||||||
|
alias BDS.Posts.Post
|
||||||
|
alias BDS.Posts.RebuildFromFiles
|
||||||
|
alias BDS.Posts.Translation
|
||||||
|
alias BDS.Projects
|
||||||
|
alias BDS.Repo
|
||||||
|
alias BDS.Search
|
||||||
|
|
||||||
|
@type attrs :: %{optional(atom()) => term(), optional(String.t()) => term()}
|
||||||
|
|
||||||
|
@spec publish_post_translation(String.t(), String.t() | atom()) ::
|
||||||
|
{:ok, Translation.t()} | {:error, :not_found | term()}
|
||||||
|
def publish_post_translation(post_id, language) do
|
||||||
|
normalized_language = language |> to_string() |> String.trim() |> String.downcase()
|
||||||
|
|
||||||
|
case Repo.get_by(Translation, translation_for: post_id, language: normalized_language) do
|
||||||
|
nil ->
|
||||||
|
{:error, :not_found}
|
||||||
|
|
||||||
|
%Translation{} ->
|
||||||
|
with {:ok, _post} <- Posts.publish_post(post_id),
|
||||||
|
%Translation{} = translation <-
|
||||||
|
Repo.get_by(Translation, translation_for: post_id, language: normalized_language) do
|
||||||
|
{:ok, translation}
|
||||||
|
else
|
||||||
|
nil -> {:error, :not_found}
|
||||||
|
error -> error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec list_post_translations(String.t()) :: {:ok, [Translation.t()]}
|
||||||
|
def list_post_translations(post_id) do
|
||||||
|
{:ok,
|
||||||
|
Repo.all(
|
||||||
|
from(translation in Translation,
|
||||||
|
where: translation.translation_for == ^post_id,
|
||||||
|
order_by: [asc: translation.language]
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec upsert_post_translation(String.t(), String.t() | atom(), attrs()) ::
|
||||||
|
{:ok, Translation.t()} | {:error, :not_found | Ecto.Changeset.t()}
|
||||||
|
def upsert_post_translation(post_id, language, attrs) do
|
||||||
|
case Repo.get(Post, post_id) do
|
||||||
|
nil ->
|
||||||
|
{:error, :not_found}
|
||||||
|
|
||||||
|
%Post{do_not_translate: true} = post ->
|
||||||
|
{:error,
|
||||||
|
post
|
||||||
|
|> Post.changeset(%{})
|
||||||
|
|> Ecto.Changeset.add_error(
|
||||||
|
:do_not_translate,
|
||||||
|
"cannot add translations when do_not_translate is true"
|
||||||
|
)}
|
||||||
|
|
||||||
|
%Post{} = post ->
|
||||||
|
now = Persistence.now_ms()
|
||||||
|
normalized_language = normalize_language(language)
|
||||||
|
|
||||||
|
translation =
|
||||||
|
Repo.get_by(Translation, translation_for: post.id, language: normalized_language) ||
|
||||||
|
%Translation{}
|
||||||
|
|
||||||
|
updates =
|
||||||
|
normalize_translation_updates(post, translation, normalized_language, attrs, now)
|
||||||
|
|
||||||
|
translation
|
||||||
|
|> Translation.changeset(updates)
|
||||||
|
|> Repo.insert_or_update()
|
||||||
|
|> case do
|
||||||
|
{:ok, saved_translation} ->
|
||||||
|
{:ok, _post} = maybe_reopen_source_post_for_manual_translation(post, attrs)
|
||||||
|
:ok = Search.sync_post(post.id)
|
||||||
|
{:ok, saved_translation}
|
||||||
|
|
||||||
|
error ->
|
||||||
|
error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec delete_post_translation(String.t()) :: {:ok, :deleted} | {:error, :not_found}
|
||||||
|
def delete_post_translation(translation_id) do
|
||||||
|
case Repo.get(Translation, translation_id) do
|
||||||
|
nil ->
|
||||||
|
{:error, :not_found}
|
||||||
|
|
||||||
|
%Translation{} = translation ->
|
||||||
|
:ok = FileSync.delete_translation_file(translation)
|
||||||
|
Repo.delete!(translation)
|
||||||
|
:ok = Search.sync_post(translation.translation_for)
|
||||||
|
{:ok, :deleted}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec sync_post_translation_from_file(String.t()) ::
|
||||||
|
{:ok, Translation.t()} | {:error, :not_found}
|
||||||
|
def sync_post_translation_from_file(translation_id) do
|
||||||
|
case Repo.get(Translation, translation_id) do
|
||||||
|
nil ->
|
||||||
|
{:error, :not_found}
|
||||||
|
|
||||||
|
%Translation{file_path: file_path} when file_path in [nil, ""] ->
|
||||||
|
{:error, :not_found}
|
||||||
|
|
||||||
|
%Translation{} = translation ->
|
||||||
|
project = Projects.get_project!(translation.project_id)
|
||||||
|
full_path = Path.join(Projects.project_data_dir(project), translation.file_path)
|
||||||
|
|
||||||
|
if File.exists?(full_path) do
|
||||||
|
rebuild_file = RebuildFromFiles.parse_rebuild_file(project, full_path)
|
||||||
|
|
||||||
|
{:ok,
|
||||||
|
RebuildFromFiles.upsert_post_translation_from_rebuild_file(
|
||||||
|
translation.project_id,
|
||||||
|
rebuild_file,
|
||||||
|
sync_search: true
|
||||||
|
)}
|
||||||
|
else
|
||||||
|
{:error, :not_found}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@spec rewrite_published_post_translation(String.t()) ::
|
||||||
|
{:ok, Translation.t()} | {:error, :not_found}
|
||||||
|
def rewrite_published_post_translation(translation_id) do
|
||||||
|
case Repo.get(Translation, translation_id) do
|
||||||
|
nil ->
|
||||||
|
{:error, :not_found}
|
||||||
|
|
||||||
|
%Translation{file_path: file_path, status: status} = translation
|
||||||
|
when file_path not in [nil, ""] and status == :published ->
|
||||||
|
post = Repo.get!(Post, translation.translation_for)
|
||||||
|
:ok = publish_translation(post, translation)
|
||||||
|
{:ok, Repo.get!(Translation, translation_id)}
|
||||||
|
|
||||||
|
%Translation{} ->
|
||||||
|
{:error, :not_found}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def publish_post_translations(%Post{} = post) do
|
||||||
|
Repo.all(from(translation in Translation, where: translation.translation_for == ^post.id))
|
||||||
|
|> Enum.each(fn translation ->
|
||||||
|
if translation.status == :draft do
|
||||||
|
publish_translation(post, translation)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def publish_translation(%Post{} = post, %Translation{} = translation) do
|
||||||
|
project = Projects.get_project!(post.project_id)
|
||||||
|
published_at = translation.published_at || Persistence.now_ms()
|
||||||
|
relative_path = FileSync.translation_relative_path(post, translation.language)
|
||||||
|
full_path = Path.join(Projects.project_data_dir(project), relative_path)
|
||||||
|
updated_at = Persistence.now_ms()
|
||||||
|
body = FileSync.publishable_translation_body(translation, full_path)
|
||||||
|
|
||||||
|
:ok =
|
||||||
|
Persistence.atomic_write(
|
||||||
|
full_path,
|
||||||
|
FileSync.serialize_translation_file(
|
||||||
|
%{translation | updated_at: updated_at, content: body},
|
||||||
|
published_at
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
translation
|
||||||
|
|> Translation.changeset(%{
|
||||||
|
status: :published,
|
||||||
|
published_at: published_at,
|
||||||
|
file_path: relative_path,
|
||||||
|
content: nil,
|
||||||
|
updated_at: updated_at
|
||||||
|
})
|
||||||
|
|> Repo.update!()
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp normalize_translation_updates(post, %Translation{} = translation, language, attrs, now) do
|
||||||
|
requested_status =
|
||||||
|
case attr(attrs, :status) do
|
||||||
|
nil -> nil
|
||||||
|
status -> RebuildFromFiles.parse_translation_status(status)
|
||||||
|
end
|
||||||
|
|
||||||
|
updates =
|
||||||
|
%{}
|
||||||
|
|> maybe_put(:title, attr(attrs, :title))
|
||||||
|
|> maybe_put(:excerpt, attr(attrs, :excerpt))
|
||||||
|
|> maybe_put(:content, attr(attrs, :content))
|
||||||
|
|
||||||
|
reopened? =
|
||||||
|
translation.status == :published and translation_content_change?(translation, updates)
|
||||||
|
|
||||||
|
status = if(reopened?, do: :draft, else: requested_status || translation.status || :draft)
|
||||||
|
|
||||||
|
%{
|
||||||
|
id: translation.id || Ecto.UUID.generate(),
|
||||||
|
project_id: post.project_id,
|
||||||
|
translation_for: post.id,
|
||||||
|
language: language,
|
||||||
|
title: Map.get(updates, :title, translation.title),
|
||||||
|
excerpt: Map.get(updates, :excerpt, translation.excerpt),
|
||||||
|
content: Map.get(updates, :content, translation.content),
|
||||||
|
status: status,
|
||||||
|
created_at: translation.created_at || now,
|
||||||
|
updated_at: now,
|
||||||
|
published_at: translation.published_at || if(status == :published, do: now, else: nil),
|
||||||
|
file_path: translation.file_path || "",
|
||||||
|
checksum: translation.checksum
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp translation_content_change?(translation, updates) do
|
||||||
|
Enum.any?([:title, :excerpt, :content], fn field ->
|
||||||
|
case Map.fetch(updates, field) do
|
||||||
|
{:ok, value} -> value != Map.get(translation, field)
|
||||||
|
:error -> false
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_reopen_source_post_for_manual_translation(%Post{} = post, attrs) do
|
||||||
|
if attr(attrs, :auto_generated) == true or post.status != :published or
|
||||||
|
post.file_path in [nil, ""] do
|
||||||
|
{:ok, post}
|
||||||
|
else
|
||||||
|
project = Projects.get_project!(post.project_id)
|
||||||
|
full_path = Path.join(Projects.project_data_dir(project), post.file_path)
|
||||||
|
restored_content = FileSync.published_post_body(post, full_path)
|
||||||
|
|
||||||
|
post
|
||||||
|
|> Post.changeset(%{
|
||||||
|
status: :draft,
|
||||||
|
content: restored_content,
|
||||||
|
updated_at: Persistence.now_ms()
|
||||||
|
})
|
||||||
|
|> Repo.update()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp normalize_language(nil), do: ""
|
||||||
|
|
||||||
|
defp normalize_language(language) do
|
||||||
|
language
|
||||||
|
|> to_string()
|
||||||
|
|> String.downcase()
|
||||||
|
|> String.split("-", parts: 2)
|
||||||
|
|> hd()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_put(map, _key, nil), do: map
|
||||||
|
defp maybe_put(map, key, value), do: Map.put(map, key, value)
|
||||||
|
|
||||||
|
defp attr(attrs, key) do
|
||||||
|
cond do
|
||||||
|
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
||||||
|
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
||||||
|
true -> nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -121,12 +121,14 @@ defmodule BDS.UI.ShellTest do
|
|||||||
test "desktop shell assets persist workbench layout per project" do
|
test "desktop shell assets persist workbench layout per project" do
|
||||||
live_js = File.read!("/Users/gb/Projects/bDS2/priv/ui/live.js")
|
live_js = File.read!("/Users/gb/Projects/bDS2/priv/ui/live.js")
|
||||||
live_ex = File.read!("/Users/gb/Projects/bDS2/lib/bds/desktop/shell_live.ex")
|
live_ex = File.read!("/Users/gb/Projects/bDS2/lib/bds/desktop/shell_live.ex")
|
||||||
|
session_util_ex =
|
||||||
|
File.read!("/Users/gb/Projects/bDS2/lib/bds/desktop/shell_live/session_util.ex")
|
||||||
|
|
||||||
assert live_js =~ "bds-workbench-"
|
assert live_js =~ "bds-workbench-"
|
||||||
assert live_js =~ "restore_workbench_session"
|
assert live_js =~ "restore_workbench_session"
|
||||||
assert live_js =~ "dataset.workbenchSession"
|
assert live_js =~ "dataset.workbenchSession"
|
||||||
assert live_ex =~ ~s(def handle_event("restore_workbench_session")
|
assert live_ex =~ ~s(def handle_event("restore_workbench_session")
|
||||||
assert live_ex =~ "Session.restore"
|
assert session_util_ex =~ "Session.restore"
|
||||||
assert live_ex =~ "encoded_workbench_session"
|
assert live_ex =~ "encoded_workbench_session"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user