fix(contacts): soft-delete contacts to preserve DM history

- delete_contact() now sets source='deleted' instead of SQL DELETE
- get_contacts() filters out deleted contacts (hidden from UI)
- upsert_contact() on re-add overwrites source, auto-undeleting
- DM FK references stay intact, no more orphaned messages

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
MarekWo
2026-03-07 08:29:44 +01:00
parent 66fa261151
commit 53928390c8
2 changed files with 9 additions and 5 deletions
+7 -2
View File
@@ -116,7 +116,7 @@ class Database:
def get_contacts(self) -> List[Dict]:
with self._connect() as conn:
rows = conn.execute(
"SELECT * FROM contacts ORDER BY last_seen DESC"
"SELECT * FROM contacts WHERE source != 'deleted' ORDER BY last_seen DESC"
).fetchall()
return [dict(r) for r in rows]
@@ -147,9 +147,14 @@ class Database:
return dict(row) if row else None
def delete_contact(self, public_key: str) -> bool:
"""Soft-delete: mark as 'deleted' instead of removing.
Keeps the row so FK references in direct_messages stay intact.
upsert_contact() overwrites source on re-add, auto-undeleting.
"""
with self._connect() as conn:
cursor = conn.execute(
"DELETE FROM contacts WHERE public_key = ?",
"UPDATE contacts SET source = 'deleted', lastmod = datetime('now') WHERE public_key = ?",
(public_key.lower(),)
)
return cursor.rowcount > 0