49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
<?php
|
|
// /messages/api/delete_conversation.php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../_bootstrap.php';
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
if (!hash_equals($_SESSION['_csrf'] ?? '', $_POST['_csrf'] ?? '')) {
|
|
json_response(['error' => 'csrf'], 419);
|
|
}
|
|
|
|
$role = $GLOBALS['MESSAGES_CTX']['role']; // 'praticien' | 'client'
|
|
$meId = $GLOBALS['MESSAGES_CTX']['me_id'];
|
|
$convId = (int)($_POST['conversation_id'] ?? 0);
|
|
|
|
if ($convId <= 0) {
|
|
json_response(['error' => 'bad_params'], 400);
|
|
}
|
|
|
|
// Vérifie l'accès (participant)
|
|
$conv = assert_conversation_access($pdo, $convId, $role, $meId);
|
|
|
|
// Colonnes dépendant du rôle
|
|
$colDeleted = ($role === 'praticien') ? 'deleted_for_praticien' : 'deleted_for_client';
|
|
$colUnread = ($role === 'praticien') ? 'p_unread' : 'c_unread';
|
|
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
// 1) Marquer la conversation supprimée "pour moi"
|
|
$pdo->prepare("UPDATE conversations SET $colDeleted = 1 WHERE id = ?")
|
|
->execute([$convId]);
|
|
|
|
// 2) Masquer tous ses messages "pour moi"
|
|
$pdo->prepare("UPDATE messages SET $colDeleted = 1 WHERE conversation_id = ?")
|
|
->execute([$convId]);
|
|
|
|
// 3) Réinitialiser mes non-lus sur cette conversation
|
|
$pdo->prepare("UPDATE conversation_counters SET $colUnread = 0 WHERE conversation_id = ?")
|
|
->execute([$convId]);
|
|
|
|
$pdo->commit();
|
|
} catch (Throwable $e) {
|
|
if ($pdo->inTransaction()) { $pdo->rollBack(); }
|
|
json_response(['error' => 'server_error'], 500);
|
|
}
|
|
|
|
json_response(['ok' => true]);
|