🚧 Add message delete

This commit is contained in:
Jonas Bengtsson
2026-09-06 22:35:24 +02:00
parent 64847b9ca1
commit f3fc394c0c
7 changed files with 797 additions and 3 deletions
+7 -2
View File
@@ -116,6 +116,7 @@ Authentication is layered:
| Read messages/devices/attributes/users | ✓ | ✓ | ✓ | ✓ |
| Write devices, settings, attribute timelines, configs, tokens | — 403 | ✓ | ✓ | ✓ |
| Import messages | — 403 | ✓ | ✓ | ✓ |
| Delete / restore messages | — 403 | ✓ | ✓ | ✓ |
| Add users | — 403 | ✓ — only role `user` | ✓ any role | ✓ |
| Modify/delete users | — 403 | ✓ — only `user`-role accounts | ✓ (never the last admin) | ✓ |
@@ -128,6 +129,10 @@ The first admin is seeded from `BOOTSTRAP_ADMIN_USER`/`BOOTSTRAP_ADMIN_PASSWORD`
| `GET /messages` | token | List messages, newest first. The device's **current settings document** (from `imei_map`) is merged into each item's `payload`; on duplicate keys the stored payload wins. Each item also carries an **`attributes`** sibling field with the device's time-varying attributes **resolved as of the message's device time** (`payload_time`, `received_at` as the fallback) — apply the offsets to the payload values yourself (e.g. `payload[k] + attributes[k]`). Query parameters: `imei`, `device` (exact-match filters), `from`/`to` (ISO 8601 range on `received_at` — server receipt time; inclusive; naive values treated as UTC), `payload_from`/`payload_to` (same but on `payload_time` — the device timestamp extracted from the JSON; messages without one are excluded; ordering switches to `payload_time DESC`), `limit` (default 100, capped at `API_MAX_LIMIT`), `offset` for paging. The response carries a **filter-aware `total`** (the count over the same filters as `items`, so pagers can show "page X of Y" and jump to the last page) alongside `limit`/`offset`. Reversed ranges return 422 |
| `GET /messages/export` | token | **Export selected messages as stored** (raw rows, no read-time settings merge, no resolved attributes). Query: repeatable `ids` (e.g. `?ids=1&ids=2`; at least one, capped at `API_MAX_LIMIT`). Returns a JSON array of `{id, received_at, topic, imei, device, payload, payload_time}` in the order the ids were given; unknown ids are skipped. Empty/oversized id lists → 422 |
| `POST /messages/import` | superuser+ | **Import previously exported messages**: body is the JSON array from `GET /messages/export` (a single message object is accepted too). Rows are inserted as-is (original payload/timestamps/topic; `topic` defaults to `/import/<imei>`, absent `received_at` becomes now). The exported **`id` is ignored** — the database assigns fresh BIGSERIAL ids, so re-importing a file (or importing into the source database) never collides. Per-row validation → 422 with `row N` positions; max 10000 rows per request. Returns `{"status": "imported", "imported": n}` |
| `POST /messages/delete` | superuser+ | **Soft-delete the selected messages**: moves them (same ids) into retention where they stay restorable for `RETENTION_DAYS` (env, default 30), then are purged. Body `{"ids": [...]}` (at least one, ≤ 10000; unknown ids skipped). The session username is recorded as `deleted_by` (NULL for app tokens). Returns `{"status": "deleted", "deleted": n}` |
| `GET /messages/retention/count` | token | `{"count": n}` — retained messages right now (the Messages view banner); expired rows are purged first |
| `GET /messages/retention` | token | One page of retained messages (newest deletions first): export shape plus `deleted_at`, `deleted_by`, `expires_at`, with a `total` for the pager. `limit` (default 100, capped at `API_MAX_LIMIT`) and `offset` |
| `POST /messages/retention/restore` | superuser+ | Move retained messages back into `mqtt_messages` **with their original ids**. Body `{"ids": [...]}`; unknown ids (e.g. already purged) are skipped. Returns `{"status": "restored", "restored": n}` |
| `GET /configs/{name}` | token | Read a config as JSON. `name`: `app_tokens` (secrets are not recoverable — app names with `null` placeholders) or `imei_map` (all device settings documents) |
| `PUT/POST /configs/{name}` | token | **Bulk replace** a config in the database (delete + insert, transactional). Body **must be a JSON object**; per-config schema is validated (`imei_map` keys must be 15-digit IMEIs, values are settings documents — flat `"name"` values are promoted to `{"device": "name"}`; `app_tokens` must keep the `{"tokens": {name: secret}}` shape). Invalid payloads → 422, unknown names → 404. `app_tokens` changes take effect immediately; `imei_map` changes are picked up by the listener within ~5 seconds |
| `GET /devices` | token | All devices, each as `{"imei": ..., **settings}` |
@@ -257,9 +262,9 @@ Example response:
- [ ] Change messages in tables to display information about the entire messege, not only the payload.
- [X] Fix facet counts on Messages show 1 per device.
- [x] Add ability to select how many rows to show in tables except the last messages table in the dashboard. Make it possible to select 10, 25, 50, 100, 200 or manual entry. Default to 25. (→ shared `Pager.vue` footer on Messages (server-side), Devices and Users (client-side via `useTable`); presets 10/25/50/100/200 + "custom…" manual entry clamped to `API_MAX_LIMIT`, default 25, persisted in localStorage; the dashboard's fixed 5-row list is untouched)
- [ ] Add operations button in messages view for import/export or deletion of selected messages. (→ export + import landed: per-row checkboxes + header select-all + "Export JSON" download; superuser+ "Import" dialog (paste/file, array or single object). Delete operation still open)
- [X] Add operations button in messages view for import/export or deletion of selected messages. (→ all three landed: per-row checkboxes + header select-all, "Export JSON" download, superuser+ "Import" dialog (paste/file, array or single object) and superuser+ **Delete** with confirm. The delete moves messages to retention — see the next line)
- [x] Export option will export selected messages as they are stored in the database, without added parameters/features. (→ `GET /messages/export?ids=…` returns raw `mqtt_messages` rows — no settings merge, no attributes — and the UI downloads the JSON array; ids keep the caller's order)
- [ ] Delete option will, after warning and user confirmation, move the deleted messages to retention. Where to see and be able to select messages to restore from retention? Rention lasts for 30 days. Add some kind of notification in the message view if messages are retained. Make it possible to view retained messages. Make it possible to recover retained messages.
- [x] Delete option will, after warning and user confirmation, move the deleted messages to retention. Where to see and be able to select messages to restore from retention? Rention lasts for 30 days. Add some kind of notification in the message view if messages are retained. Make it possible to view retained messages in the messages view. Make it possible to recover retained messages. (→ **Delete** button in the selection toolbar (superuser+), confirm dialog, moves rows to `mqtt_messages_retention` keeping their ids; retention period = `RETENTION_DAYS` env (default 30), expired rows purged automatically. A **banner** in the Messages view shows the retained count and opens a **retention dialog**: the retained rows (deleted at/by, purge date, payload) with per-row selection, header select-all and **Restore selected** (superuser+). Viewers see the banner and the dialog but no delete/restore controls)
- [ ] Add payload timestamp as column in messages view. The from/to field would be able to select if it is received or payload timestamp that is used for framing the loaded messages.
- [x] Add table navigastion buttons for jumping to first or last page. Add manual navigation textfield/dropdown to all tables exept dashboard last messages table. (→ the shared `Pager.vue`: « first / ‹ prev / page-number textfield (clamped, Enter or blur to apply) / › next / » last buttons + "page X of Y" + "n–m of total"; works on Messages (server-side offset/limit) and the client-side tables (Devices/Users via `useTable` pagination). `GET /messages` now returns a filter-aware `total` to power it)
- [ ] Add versioning of UI and API, show versions on bottom left.