mirror of
https://github.com/MarekWo/mc-webui.git
synced 2026-05-05 04:52:34 +02:00
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:
@@ -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
|
||||
|
||||
@@ -1000,14 +1000,13 @@ class DeviceManager:
|
||||
return self.db.get_contacts() # return cached
|
||||
|
||||
def delete_contact(self, pubkey: str) -> Dict:
|
||||
"""Delete a contact from device. Keep DB record to preserve DM history."""
|
||||
"""Delete a contact from device and soft-delete in database."""
|
||||
if not self.is_connected:
|
||||
return {'success': False, 'error': 'Device not connected'}
|
||||
|
||||
try:
|
||||
self.execute(self.mc.commands.remove_contact(pubkey))
|
||||
# Don't delete from DB — ON DELETE SET NULL would orphan all DMs.
|
||||
# Contact stays in DB for historical reference; upsert updates on re-add.
|
||||
self.db.delete_contact(pubkey) # soft-delete: sets source='deleted'
|
||||
# Also remove from in-memory contacts cache
|
||||
if self.mc.contacts and pubkey in self.mc.contacts:
|
||||
del self.mc.contacts[pubkey]
|
||||
|
||||
Reference in New Issue
Block a user