dashPi: a self-hosted home dashboard, its sync server and phone app
The dashboard (Raspberry Pi 3.5" screen + web pages), the end-to-end encrypted sync mailbox (PHP + SQLite) and the Android companion app (Flutter), with docs, clean-install checks and screenshots made from demo data. Built mostly with an AI coding assistant; not yet reviewed line by line by a human (see README and SECURITY.md). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
fb3c01cdd7
333 changed files with 45151 additions and 0 deletions
136
app/lib/notes/notes_model.dart
Normal file
136
app/lib/notes/notes_model.dart
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
// Notes: the dashboard's notes plugin (plugins/notes/db.py) on the phone — one synced row per note
|
||||
// in "notes.note": folder ("Work/Acme", '' = none), body (markdown), pinned, created / updated
|
||||
// (UTC), rev / revs (versions, for the sync's "keep both copies" — SyncStore.forks), origin (a
|
||||
// conflicted copy: the rev it keeps), trashed (in the trash since). Title = the first line, tags =
|
||||
// the #words — worked out on read, like the web page.
|
||||
import 'dart:math';
|
||||
|
||||
import '../sync/store.dart';
|
||||
import 'markdown.dart' show tagRe;
|
||||
|
||||
const noteTable = 'notes.note';
|
||||
const _revs = 200; // versions remembered per note (db.REVS)
|
||||
|
||||
String nowIso() => '${DateTime.now().toUtc().toIso8601String().substring(0, 19)}Z';
|
||||
|
||||
String newRev() {
|
||||
final r = Random.secure();
|
||||
return List.generate(6, (_) => r.nextInt(256).toRadixString(16).padLeft(2, '0')).join();
|
||||
}
|
||||
|
||||
String noteTitle(String body) {
|
||||
for (final l in body.split('\n')) {
|
||||
final s = l.trim().replaceFirst(RegExp(r'^#+'), '').trim();
|
||||
if (s.isNotEmpty) return s.length > 120 ? s.substring(0, 120) : s;
|
||||
}
|
||||
return 'Untitled';
|
||||
}
|
||||
|
||||
List<String> noteTags(String body) {
|
||||
final plain = body.replaceAll(RegExp(r'```[\s\S]*?(```|$)'), '').replaceAll(RegExp(r'`[^`\n]*`'), '');
|
||||
return {for (final m in tagRe.allMatches(plain)) m[2]!.toLowerCase()}.toList();
|
||||
}
|
||||
|
||||
String noteSnippet(String body) {
|
||||
final lines = body.split('\n');
|
||||
final first = lines.indexWhere((l) => l.trim().isNotEmpty);
|
||||
final text = lines.skip(first + 1).map((l) => l.replaceFirst(RegExp(r'^\s*(?:[-*+]|\d+[.)])\s+(?:\[[ xX]\]\s+)?'), '')).join(' ')
|
||||
.replaceAll(RegExp(r'[#>*_`~=\[\]]|\(http[^)]*\)'), '').replaceAll(RegExp(r'\s+'), ' ').trim();
|
||||
return text.length > 160 ? text.substring(0, 160) : text;
|
||||
}
|
||||
|
||||
/// " Work / Acme/ " → "Work/Acme".
|
||||
String cleanFolder(String f) => f.replaceAll('\\', '/').split('/').map((p) => p.trim()).where((p) => p.isNotEmpty).join('/');
|
||||
|
||||
/// Lower case without accents, for search.
|
||||
String fold(String s) {
|
||||
const from = 'áäàâãåčćďéěëèêíïìîľĺňñóöòôõőřŕšśťúůüùûűýÿžźżłąęß';
|
||||
const to = 'aaaaaaccdeeeeeiiiillnnoooooorrssstuuuuuuyyzzzlaes';
|
||||
final out = StringBuffer();
|
||||
for (final c in s.toLowerCase().split('')) {
|
||||
final i = from.indexOf(c);
|
||||
out.write(i < 0 ? c : to[i]);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
class Note {
|
||||
Note(this.r);
|
||||
final Map<String, dynamic> r;
|
||||
String get id => r['_u'] as String;
|
||||
String get body => (r['body'] as String?) ?? '';
|
||||
String get folder => (r['folder'] as String?) ?? '';
|
||||
bool get pinned => (r['pinned'] as num? ?? 0) != 0;
|
||||
String get updated => (r['updated'] as String?) ?? '';
|
||||
String get rev => (r['rev'] as String?) ?? '';
|
||||
String? get origin => r['origin'] as String?;
|
||||
String? get trashed => r['trashed'] as String?;
|
||||
String get title => noteTitle(body);
|
||||
List<String> get tags => noteTags(body);
|
||||
}
|
||||
|
||||
List<Note> allNotes(SyncStore s) => [for (final r in s.rows(noteTable)) Note(r)];
|
||||
|
||||
Note? getNote(SyncStore s, String id) {
|
||||
final r = s.row(noteTable, id);
|
||||
return r == null ? null : Note(r);
|
||||
}
|
||||
|
||||
/// Save the text typed on version [base] → (the note's id, conflict). A new note: no id. When the
|
||||
/// note changed meanwhile (a sync came in), the text becomes a copy beside it (db.save).
|
||||
(String, bool) saveNote(SyncStore s, {String? id, required String body, String? base, String folder = '', bool pinned = false}) {
|
||||
final t = nowIso();
|
||||
final cur = id == null ? null : s.row(noteTable, id);
|
||||
if (cur == null || (base != null && base != cur['rev'] && body != cur['body'])) {
|
||||
final rev = newRev(), nid = SyncStore.randomId();
|
||||
final copy = cur != null;
|
||||
s.put(noteTable, nid, {
|
||||
'folder': copy ? cur['folder'] : cleanFolder(folder), 'body': body, 'pinned': copy ? 0 : (pinned ? 1 : 0),
|
||||
'created': t, 'updated': t, 'rev': rev, 'revs': rev, 'origin': copy ? rev : null, 'trashed': null,
|
||||
});
|
||||
return (nid, copy);
|
||||
}
|
||||
if (body == cur['body']) return (id!, false);
|
||||
final row = {...cur, 'body': body, 'updated': t};
|
||||
if (s.known(noteTable, id!)) { // seen elsewhere: a new version (else typing goes on in this one)
|
||||
final rev = newRev();
|
||||
final revs = [...((cur['revs'] as String?) ?? '').split(' ').where((x) => x.isNotEmpty), rev];
|
||||
row['rev'] = rev;
|
||||
row['revs'] = revs.skip(max(0, revs.length - _revs)).join(' ');
|
||||
}
|
||||
s.put(noteTable, id, row);
|
||||
return (id, false);
|
||||
}
|
||||
|
||||
/// Pinned, folder, trashed, origin — no new version.
|
||||
void setNote(SyncStore s, String id, Map<String, dynamic> fields) {
|
||||
final cur = s.row(noteTable, id);
|
||||
if (cur != null) s.put(noteTable, id, {...cur, ...fields});
|
||||
}
|
||||
|
||||
void trashNote(SyncStore s, String id, bool on) => setNote(s, id, {'trashed': on ? nowIso() : null});
|
||||
|
||||
/// For good (from the trash).
|
||||
void deleteNote(SyncStore s, String id) => s.delete(noteTable, id);
|
||||
|
||||
int emptyTrash(SyncStore s) {
|
||||
final gone = allNotes(s).where((n) => n.trashed != null).toList();
|
||||
for (final n in gone) {
|
||||
s.delete(noteTable, n.id);
|
||||
}
|
||||
return gone.length;
|
||||
}
|
||||
|
||||
/// Every note in [from] and its sub-folders moves to [to] (same sub-paths).
|
||||
int renameFolder(SyncStore s, String from, String to) {
|
||||
from = cleanFolder(from);
|
||||
to = cleanFolder(to);
|
||||
var n = 0;
|
||||
for (final x in allNotes(s)) {
|
||||
if (x.folder == from || x.folder.startsWith('$from/')) {
|
||||
setNote(s, x.id, {'folder': cleanFolder(to + x.folder.substring(from.length))});
|
||||
n++;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue