|
| 1 | +"""API routes for vCard conflict management.""" |
| 2 | + |
| 3 | +import uuid |
| 4 | +from datetime import datetime, timezone |
| 5 | + |
| 6 | +from fastapi import APIRouter, HTTPException |
| 7 | +from sqlmodel import select |
| 8 | + |
| 9 | +from app.api.deps import CurrentUser, SessionDep |
| 10 | +from app.models import Contact |
| 11 | +from app.models_vcard_conflict import ( |
| 12 | + VCardConflict, |
| 13 | + VCardConflictPublic, |
| 14 | + VCardConflictsPublic, |
| 15 | +) |
| 16 | + |
| 17 | +router = APIRouter(prefix="/vcard-conflicts", tags=["vCard Conflicts"]) |
| 18 | + |
| 19 | + |
| 20 | +@router.get("/", response_model=VCardConflictsPublic) |
| 21 | +def list_vcard_conflicts( |
| 22 | + session: SessionDep, |
| 23 | + current_user: CurrentUser, |
| 24 | + skip: int = 0, |
| 25 | + limit: int = 100, |
| 26 | +) -> VCardConflictsPublic: |
| 27 | + """List all unresolved vCard conflicts for the current user.""" |
| 28 | + # Join with Contact to ensure we only return conflicts for this user's contacts |
| 29 | + stmt = ( |
| 30 | + select(VCardConflict) |
| 31 | + .join(Contact, VCardConflict.contact_id == Contact.id) |
| 32 | + .where( |
| 33 | + Contact.owner_id == current_user.id, |
| 34 | + VCardConflict.resolved_at.is_(None), |
| 35 | + ) |
| 36 | + .order_by(VCardConflict.created_at.desc()) |
| 37 | + .offset(skip) |
| 38 | + .limit(limit) |
| 39 | + ) |
| 40 | + conflicts = session.exec(stmt).all() |
| 41 | + |
| 42 | + # Also get the local vcard_raw for each conflict |
| 43 | + result = [] |
| 44 | + for conflict in conflicts: |
| 45 | + conflict_public = VCardConflictPublic( |
| 46 | + id=conflict.id, |
| 47 | + contact_id=conflict.contact_id, |
| 48 | + incoming_vcard_raw=conflict.incoming_vcard_raw, |
| 49 | + incoming_hash=conflict.incoming_hash, |
| 50 | + local_hash=conflict.local_hash, |
| 51 | + resolved_at=conflict.resolved_at, |
| 52 | + resolution_type=conflict.resolution_type, |
| 53 | + created_at=conflict.created_at, |
| 54 | + local_vcard_raw=conflict.local_vcard_raw, |
| 55 | + ) |
| 56 | + result.append(conflict_public) |
| 57 | + |
| 58 | + count_stmt = ( |
| 59 | + select(VCardConflict) |
| 60 | + .join(Contact, VCardConflict.contact_id == Contact.id) |
| 61 | + .where( |
| 62 | + Contact.owner_id == current_user.id, |
| 63 | + VCardConflict.resolved_at.is_(None), |
| 64 | + ) |
| 65 | + ) |
| 66 | + count = len(session.exec(count_stmt).all()) |
| 67 | + |
| 68 | + return VCardConflictsPublic(data=result, count=count) |
| 69 | + |
| 70 | + |
| 71 | +@router.post("/{conflict_id}/resolve", response_model=VCardConflictPublic) |
| 72 | +def resolve_vcard_conflict( |
| 73 | + session: SessionDep, |
| 74 | + current_user: CurrentUser, |
| 75 | + conflict_id: uuid.UUID, |
| 76 | + resolution_type: str, |
| 77 | +) -> VCardConflictPublic: |
| 78 | + """Resolve a vCard conflict by accepting remote or keeping local. |
| 79 | +
|
| 80 | + resolution_type must be one of: 'keep_local', 'accept_remote' |
| 81 | + """ |
| 82 | + if resolution_type not in ("keep_local", "accept_remote"): |
| 83 | + raise HTTPException( |
| 84 | + status_code=400, |
| 85 | + detail="resolution_type must be 'keep_local' or 'accept_remote'", |
| 86 | + ) |
| 87 | + |
| 88 | + # Get the conflict and verify ownership |
| 89 | + stmt = ( |
| 90 | + select(VCardConflict) |
| 91 | + .join(Contact, VCardConflict.contact_id == Contact.id) |
| 92 | + .where( |
| 93 | + VCardConflict.id == conflict_id, |
| 94 | + Contact.owner_id == current_user.id, |
| 95 | + ) |
| 96 | + ) |
| 97 | + conflict = session.exec(stmt).first() |
| 98 | + if not conflict: |
| 99 | + raise HTTPException(status_code=404, detail="Conflict not found") |
| 100 | + |
| 101 | + if conflict.resolved_at is not None: |
| 102 | + raise HTTPException(status_code=400, detail="Conflict already resolved") |
| 103 | + |
| 104 | + # Get the contact |
| 105 | + contact = session.get(Contact, conflict.contact_id) |
| 106 | + if not contact: |
| 107 | + raise HTTPException(status_code=404, detail="Contact not found") |
| 108 | + |
| 109 | + if resolution_type == "accept_remote": |
| 110 | + # Update contact with incoming vCard data |
| 111 | + from app.vcard import vcard_to_contact_data |
| 112 | + |
| 113 | + parsed = vcard_to_contact_data(conflict.incoming_vcard_raw) |
| 114 | + contact_data = parsed["contact"] |
| 115 | + for key, value in contact_data.items(): |
| 116 | + if hasattr(contact, key): |
| 117 | + setattr(contact, key, value) |
| 118 | + contact.vcard_raw = conflict.incoming_vcard_raw |
| 119 | + contact.vcard_sha256 = conflict.incoming_hash |
| 120 | + session.add(contact) |
| 121 | + |
| 122 | + # Mark conflict as resolved |
| 123 | + conflict.resolved_at = datetime.now(timezone.utc) |
| 124 | + conflict.resolution_type = resolution_type |
| 125 | + session.add(conflict) |
| 126 | + session.commit() |
| 127 | + session.refresh(conflict) |
| 128 | + |
| 129 | + return VCardConflictPublic( |
| 130 | + id=conflict.id, |
| 131 | + contact_id=conflict.contact_id, |
| 132 | + incoming_vcard_raw=conflict.incoming_vcard_raw, |
| 133 | + incoming_hash=conflict.incoming_hash, |
| 134 | + local_hash=conflict.local_hash, |
| 135 | + resolved_at=conflict.resolved_at, |
| 136 | + resolution_type=conflict.resolution_type, |
| 137 | + created_at=conflict.created_at, |
| 138 | + local_vcard_raw=conflict.local_vcard_raw, |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +@router.delete("/{conflict_id}", status_code=204) |
| 143 | +def delete_vcard_conflict( |
| 144 | + session: SessionDep, |
| 145 | + current_user: CurrentUser, |
| 146 | + conflict_id: uuid.UUID, |
| 147 | +) -> None: |
| 148 | + """Delete a vCard conflict (dismiss without action).""" |
| 149 | + stmt = ( |
| 150 | + select(VCardConflict) |
| 151 | + .join(Contact, VCardConflict.contact_id == Contact.id) |
| 152 | + .where( |
| 153 | + VCardConflict.id == conflict_id, |
| 154 | + Contact.owner_id == current_user.id, |
| 155 | + ) |
| 156 | + ) |
| 157 | + conflict = session.exec(stmt).first() |
| 158 | + if not conflict: |
| 159 | + raise HTTPException(status_code=404, detail="Conflict not found") |
| 160 | + |
| 161 | + session.delete(conflict) |
| 162 | + session.commit() |
0 commit comments