secubox-openwrt/package/secubox/luci-app-secubox-admin/htdocs/luci-static/resources/view/secubox-admin/apps.js
CyberMind-FR 77dbd3d499 feat: Multi-source AppStore with version management and updates UI (Phases 1-3)
Implement comprehensive multi-source catalog system with automatic fallback,
advanced version tracking, and rich update management interfaces.

## Phase 1: Backend Infrastructure (COMPLETE)

### UCI Configuration
- **New**: `/etc/config/secubox-appstore`
  - 4 source types: GitHub (remote), local web (remote), USB (local), embedded (fallback)
  - Priority-based fallback (1=highest, 999=embedded last resort)
  - Settings: auto_sync, force_source, check_updates_on_boot, notify_updates
  - Update checking with configurable intervals

### Catalog Sync Script
- **New**: `/usr/sbin/secubox-catalog-sync` (364 lines)
  - Automatic multi-source fallback by priority
  - Download tools: uclient-fetch, wget, curl (auto-detect)
  - HTTP caching: ETag support, 304 Not Modified handling
  - JSON validation before use
  - Metadata tracking with jq
  - Logging to syslog
  - Source types: remote (HTTPS/HTTP), local (filesystem), embedded (ROM)

### CLI Enhancement
- **Modified**: `/usr/sbin/secubox-appstore`
  - New commands: `sync [source]`, `check-updates [--json]`, `changelog <app> [version]`
  - `get_active_catalog()`: Reads from cache or embedded
  - `sync_catalog()`: Wrapper for secubox-catalog-sync
  - `check_updates()`: Version comparison with opkg
  - `get_changelog()`: Extracts from catalog JSON

### Metadata Structure
- **New**: `/usr/share/secubox/catalog-metadata.json.example`
  - Active source tracking
  - Source status (online/offline/error)
  - ETag cache for HTTP sources
  - Installed apps version tracking
  - Update statistics

### Makefile Updates
- **Modified**: `secubox-core/Makefile`
  - PKG_RELEASE: 5 → 6
  - Added conffiles: `/etc/config/secubox-appstore`
  - Install secubox-catalog-sync binary
  - Install catalog-metadata.json.example
  - Added dependency: +jq
  - postinst: Create cache directories (/var/cache/secubox/catalogs, /var/lib/secubox)

## Phase 2: RPCD Backend (COMPLETE)

### New RPC Methods
- **Modified**: `/usr/libexec/rpcd/luci.secubox`
  - `get_catalog_sources()`: List configured sources from UCI, status from metadata
  - `set_catalog_source(source)`: Configure force_source in UCI
  - `sync_catalog([source])`: Trigger catalog sync (auto-fallback or specific)
  - `check_updates()`: Compare installed vs catalog versions
  - `get_app_versions(app_id)`: Detailed version info (pkg, app, installed, catalog)
  - `get_changelog(app_id, from, to)`: Extract changelog from catalog
  - `get_widget_data(app_id)`: Widget metrics (Phase 5 prep)

All methods integrate with:
- UCI config parsing (`config_load`, `config_foreach`)
- Metadata file reading (`/var/lib/secubox/catalog-metadata.json`)
- Catalog reading (`/var/cache/secubox/catalogs/*.json` or embedded)
- opkg version checking

## Phase 3: Frontend LuCI Views (COMPLETE)

### API Module Enhancement
- **Modified**: `secubox-admin/api.js`
  - New RPC declarations: 7 new methods
  - Exports: `getCatalogSources`, `setCatalogSource`, `syncCatalog`,
            `checkUpdates`, `getAppVersions`, `getChangelog`, `getWidgetData`

### Catalog Sources Management
- **New**: `view/secubox-admin/catalog-sources.js` (370 lines)
  - Live source status display (online/offline/error)
  - Priority-based ordering
  - Active source indicator
  - Per-source actions: Sync, Test, Set Active, Enable/Disable
  - Summary stats: Total sources, active source, updates available
  - Auto-refresh every 30 seconds
  - Timestamp formatting (relative: "5 minutes ago", "2 days ago")

### Updates Manager
- **New**: `view/secubox-admin/updates.js` (380 lines)
  - Available updates list with version comparison
  - Changelog preview in update cards
  - Version arrows: "0.3.0-1 → 0.4.0-2"
  - Per-app actions: Update Now, View Full Changelog, Skip Version
  - Batch update: "Update All" button
  - Check for Updates: Sync + check flow
  - Auto-refresh every 60 seconds
  - No updates state: Checkmark with message

### Apps Manager Enhancement
- **Modified**: `view/secubox-admin/apps.js`
  - Load update info on page load
  - Update available badges (warning style)
  - Version display with tooltip (installed → available)
  - Visual indicators: `.has-update`, `.version-outdated` classes
  - New filter: "Updates Available" / "Installed" / "Not Installed"
  - Changelog button on all apps (installed or not)
  - Update button for apps with available updates
  - `updateApp()`: Shows changelog before update
  - `viewChangelog()`: Modal with version history
  - `filterByStatus()`: Filter by update/install status

### Menu Integration
- **Modified**: `menu.d/luci-app-secubox-admin.json`
  - New entries:
    - "Updates" (order: 25) → `/admin/secubox/admin/updates`
    - "Catalog Sources" (order: 27) → `/admin/secubox/admin/catalog-sources`
  - Placed between Apps Manager and App Settings

## Data Flow Architecture

```
User Action (Web UI)
  ↓
LuCI View (catalog-sources.js, updates.js, apps.js)
  ↓
API Module (api.js RPC calls)
  ↓
RPCD Backend (luci.secubox)
  ↓
CLI Scripts (secubox-appstore, secubox-catalog-sync)
  ↓
Data Layer
  ├── UCI Config (/etc/config/secubox-appstore)
  ├── Cache (/var/cache/secubox/catalogs/*.json)
  ├── Metadata (/var/lib/secubox/catalog-metadata.json)
  └── Embedded (/usr/share/secubox/catalog.json)
```

## Fallback Logic

1. User triggers sync (or auto-sync)
2. secubox-catalog-sync reads UCI config
3. Sorts sources by priority (1 = GitHub, 2 = Local Web, 3 = USB, 999 = Embedded)
4. Attempts each source in order:
   - GitHub HTTPS → timeout/fail → Next
   - Local Web → unreachable → Next
   - USB → not mounted → Next
   - Embedded → Always succeeds (ROM)
5. First successful source becomes active
6. Metadata updated with status, ETag, timestamp
7. Cache written to `/var/cache/secubox/catalogs/<source>.json`

## Version Tracking

- **PKG_VERSION**: OpenWrt package version (e.g., "0.4.0")
- **PKG_RELEASE**: Build release number (e.g., "2")
- **pkg_version**: Full package string "0.4.0-2" (in catalog)
- **app_version**: Underlying app version (e.g., "0.4.0")
- **installed_version**: From `opkg list-installed`
- **catalog_version**: From active catalog JSON
- **Comparison**: Uses `opkg compare-versions` for semantic versioning

## Storage Layout

```
/etc/config/secubox-appstore              # UCI configuration
/var/cache/secubox/catalogs/              # Downloaded catalogs (755/644)
  ├── github.json
  ├── local_web.json
  └── usb.json
/var/lib/secubox/                         # Runtime metadata (700/600)
  └── catalog-metadata.json
/usr/share/secubox/catalog.json           # Embedded fallback (ROM)
```

## Key Features

 **Multi-source support**: GitHub + Web + USB + Embedded
 **Automatic fallback**: Priority-based with retry logic
 **HTTP optimization**: ETag caching, 304 Not Modified
 **Version management**: PKG + App versions, changelog tracking
 **Update notifications**: Badges, filters, dedicated updates page
 **Offline capable**: USB and embedded sources work without internet
 **Live status**: Auto-refresh, real-time source health
 **User control**: Manual sync, force specific source, enable/disable sources

## Files Modified (8)
- package/secubox/secubox-core/Makefile
- package/secubox/secubox-core/root/usr/libexec/rpcd/luci.secubox
- package/secubox/secubox-core/root/usr/sbin/secubox-appstore
- package/secubox/luci-app-secubox-admin/htdocs/luci-static/resources/secubox-admin/api.js
- package/secubox/luci-app-secubox-admin/htdocs/luci-static/resources/view/secubox-admin/apps.js
- package/secubox/luci-app-secubox-admin/root/usr/share/luci/menu.d/luci-app-secubox-admin.json

## Files Created (4)
- package/secubox/secubox-core/root/etc/config/secubox-appstore
- package/secubox/secubox-core/root/usr/sbin/secubox-catalog-sync
- package/secubox/secubox-core/root/usr/share/secubox/catalog-metadata.json.example
- package/secubox/luci-app-secubox-admin/htdocs/luci-static/resources/view/secubox-admin/catalog-sources.js
- package/secubox/luci-app-secubox-admin/htdocs/luci-static/resources/view/secubox-admin/updates.js

## Next Steps (Phase 4-5)

- Phase 4: Enrich catalog.json with changelog sections
- Phase 5: Widget system (renderer + templates for security/network/monitoring)
- Phase 6: Auto-sync service with cron
- Phase 7: Optimizations (signature validation, compression, CDN)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-04 10:53:57 +01:00

356 lines
10 KiB
JavaScript

'use strict';
'require view';
'require secubox-admin.api as API';
'require secubox-admin.components as Components';
'require ui';
'require form';
return view.extend({
load: function() {
return Promise.all([
API.getApps(),
API.getModules(),
L.resolveDefault(API.checkUpdates(), {})
]);
},
render: function(data) {
var apps = data[0].apps || [];
var modules = data[1].modules || {};
var updateInfo = data[2] || {};
var self = this;
// Create updates lookup map
var updatesMap = {};
if (updateInfo.updates) {
updateInfo.updates.forEach(function(update) {
updatesMap[update.app_id] = update;
});
}
var container = E('div', { 'class': 'secubox-apps-manager' }, [
E('link', { 'rel': 'stylesheet',
'href': L.resource('secubox-admin/common.css') }),
E('link', { 'rel': 'stylesheet',
'href': L.resource('secubox-admin/admin.css') }),
E('h2', {}, 'Apps Manager'),
E('p', {}, 'Browse and manage SecuBox applications from the catalog'),
// Filters
E('div', { 'class': 'app-filters' }, [
E('input', {
'type': 'text',
'class': 'search-box',
'placeholder': 'Search apps...',
'keyup': function(ev) {
self.filterApps(ev.target.value);
}
}),
E('select', {
'class': 'category-filter',
'change': function(ev) {
self.filterByCategory(ev.target.value);
}
}, [
E('option', { 'value': '' }, 'All Categories'),
E('option', { 'value': 'security' }, 'Security'),
E('option', { 'value': 'network' }, 'Network'),
E('option', { 'value': 'hosting' }, 'Hosting'),
E('option', { 'value': 'productivity' }, 'Productivity')
]),
E('select', {
'class': 'status-filter',
'change': function(ev) {
self.filterByStatus(ev.target.value);
}
}, [
E('option', { 'value': '' }, 'All Apps'),
E('option', { 'value': 'update-available' }, 'Updates Available'),
E('option', { 'value': 'installed' }, 'Installed'),
E('option', { 'value': 'not-installed' }, 'Not Installed')
])
]),
// Apps grid
E('div', { 'class': 'apps-grid', 'id': 'apps-grid' },
apps.map(function(app) {
var status = API.getAppStatus(app, modules);
var updateAvailable = updatesMap[app.id];
return self.renderAppCard(app, status, updateAvailable);
})
)
]);
return container;
},
renderAppCard: function(app, status, updateInfo) {
var self = this;
var hasUpdate = updateInfo && updateInfo.update_available;
var cardClasses = 'app-card';
if (status.installed) cardClasses += ' installed';
if (hasUpdate) cardClasses += ' has-update';
return E('div', {
'class': cardClasses,
'data-category': app.category,
'data-update-status': hasUpdate ? 'update-available' : '',
'data-install-status': status.installed ? 'installed' : 'not-installed'
}, [
E('div', { 'class': 'app-icon' }, app.icon || '📦'),
E('div', { 'class': 'app-info' }, [
E('div', { 'class': 'app-title-row' }, [
E('h3', {}, app.name),
hasUpdate ? E('span', { 'class': 'badge badge-warning update-badge' }, 'Update') : null
]),
E('p', { 'class': 'app-description' }, app.description),
E('div', { 'class': 'app-meta' }, [
E('span', { 'class': 'app-category' }, app.category),
E('span', {
'class': 'app-version' + (hasUpdate ? ' version-outdated' : ''),
'title': hasUpdate ?
'Installed: ' + updateInfo.installed_version + ' → Available: ' + updateInfo.catalog_version :
''
}, 'v' + (app.pkg_version || app.version || '1.0')),
Components.renderStatusBadge(status.status)
])
]),
E('div', { 'class': 'app-actions' },
status.installed ? [
hasUpdate ? E('button', {
'class': 'btn btn-sm btn-warning',
'click': function() { self.updateApp(app, updateInfo); }
}, 'Update') : null,
E('button', {
'class': 'btn btn-sm btn-secondary',
'click': function() { self.viewChangelog(app); }
}, 'Changelog'),
E('button', {
'class': 'btn btn-sm btn-primary',
'click': function() { self.configureApp(app); }
}, 'Configure'),
E('button', {
'class': 'btn btn-sm btn-danger',
'click': function() { self.removeApp(app); }
}, 'Remove')
] : [
E('button', {
'class': 'btn btn-sm btn-secondary',
'click': function() { self.viewChangelog(app); }
}, 'Changelog'),
E('button', {
'class': 'btn btn-sm btn-success',
'click': function() { self.installApp(app); }
}, 'Install')
]
)
]);
},
installApp: function(app) {
var self = this;
ui.showModal('Install ' + app.name, [
E('p', {}, 'Are you sure you want to install ' + app.name + '?'),
E('div', { 'class': 'right' }, [
E('button', {
'class': 'btn',
'click': ui.hideModal
}, 'Cancel'),
E('button', {
'class': 'btn btn-primary',
'click': function() {
ui.hideModal();
ui.showModal('Installing...', [
Components.renderLoader('Installing ' + app.name + '...')
]);
API.installApp(app.id).then(function(result) {
ui.hideModal();
if (result.success) {
ui.addNotification(null, E('p', app.name + ' installed successfully'), 'info');
window.location.reload();
} else {
ui.addNotification(null, E('p', 'Failed to install ' + app.name), 'error');
}
});
}
}, 'Install')
])
]);
},
removeApp: function(app) {
var self = this;
ui.showModal('Remove ' + app.name, [
E('p', {}, 'Are you sure you want to remove ' + app.name + '?'),
E('div', { 'class': 'right' }, [
E('button', {
'class': 'btn',
'click': ui.hideModal
}, 'Cancel'),
E('button', {
'class': 'btn btn-danger',
'click': function() {
ui.hideModal();
ui.showModal('Removing...', [
Components.renderLoader('Removing ' + app.name + '...')
]);
API.removeApp(app.id).then(function(result) {
ui.hideModal();
if (result.success) {
ui.addNotification(null, E('p', app.name + ' removed successfully'), 'info');
window.location.reload();
} else {
ui.addNotification(null, E('p', 'Failed to remove ' + app.name), 'error');
}
});
}
}, 'Remove')
])
]);
},
configureApp: function(app) {
window.location = L.url('admin/secubox/admin/settings');
},
filterApps: function(query) {
var cards = document.querySelectorAll('.app-card');
query = query.toLowerCase();
cards.forEach(function(card) {
var name = card.querySelector('h3').textContent.toLowerCase();
var desc = card.querySelector('.app-description').textContent.toLowerCase();
if (name.includes(query) || desc.includes(query)) {
card.style.display = '';
} else {
card.style.display = 'none';
}
});
},
filterByCategory: function(category) {
var cards = document.querySelectorAll('.app-card');
cards.forEach(function(card) {
if (!category || card.dataset.category === category) {
card.style.display = '';
} else {
card.style.display = 'none';
}
});
},
filterByStatus: function(status) {
var cards = document.querySelectorAll('.app-card');
cards.forEach(function(card) {
if (!status) {
card.style.display = '';
} else if (status === 'update-available') {
card.style.display = card.dataset.updateStatus === 'update-available' ? '' : 'none';
} else if (status === 'installed') {
card.style.display = card.dataset.installStatus === 'installed' ? '' : 'none';
} else if (status === 'not-installed') {
card.style.display = card.dataset.installStatus === 'not-installed' ? '' : 'none';
}
});
},
updateApp: function(app, updateInfo) {
var self = this;
ui.showModal('Update ' + app.name, [
E('p', {}, 'Update ' + app.name + ' from v' +
updateInfo.installed_version + ' to v' + updateInfo.catalog_version + '?'),
updateInfo.changelog ? E('div', { 'class': 'update-changelog' }, [
E('h4', {}, 'What\'s New:'),
E('div', {},
Array.isArray(updateInfo.changelog) ?
E('ul', {},
updateInfo.changelog.map(function(item) {
return E('li', {}, item);
})
) :
E('p', {}, updateInfo.changelog)
)
]) : null,
E('div', { 'class': 'right' }, [
E('button', {
'class': 'btn',
'click': ui.hideModal
}, 'Cancel'),
E('button', {
'class': 'btn btn-warning',
'click': function() {
ui.hideModal();
ui.showModal('Updating...', [
Components.renderLoader('Updating ' + app.name + '...')
]);
API.installApp(app.id).then(function(result) {
ui.hideModal();
if (result.success) {
ui.addNotification(null, E('p', app.name + ' updated successfully'), 'success');
window.location.reload();
} else {
ui.addNotification(null, E('p', 'Failed to update ' + app.name), 'error');
}
});
}
}, 'Update')
])
]);
},
viewChangelog: function(app) {
ui.showModal('Changelog: ' + app.name, [
E('p', { 'class': 'spinning' }, 'Loading changelog...')
]);
API.getChangelog(app.id, null, null).then(function(changelog) {
var content = E('div', { 'class': 'changelog-viewer' });
if (changelog && changelog.changelog) {
var versions = Object.keys(changelog.changelog);
versions.forEach(function(version) {
var versionData = changelog.changelog[version];
content.appendChild(E('div', { 'class': 'changelog-version' }, [
E('h4', {}, 'Version ' + version),
versionData.date ? E('p', { 'class': 'changelog-date' }, versionData.date) : null,
E('ul', {},
(versionData.changes || []).map(function(change) {
return E('li', {}, change);
})
)
]));
});
} else if (typeof changelog === 'string') {
content.appendChild(E('pre', {}, changelog));
} else {
content.appendChild(E('p', {}, 'No changelog available'));
}
ui.showModal('Changelog: ' + app.name, [
content,
E('div', { 'class': 'right' }, [
E('button', {
'class': 'btn',
'click': ui.hideModal
}, 'Close')
])
]);
}).catch(function(err) {
ui.showModal('Changelog: ' + app.name, [
E('p', {}, 'Failed to load changelog: ' + err.message),
E('div', { 'class': 'right' }, [
E('button', {
'class': 'btn',
'click': ui.hideModal
}, 'Close')
])
]);
});
},
handleSaveApply: null,
handleSave: null,
handleReset: null
});