-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdelete_chat.php
More file actions
92 lines (82 loc) · 2.6 KB
/
Copy pathdelete_chat.php
File metadata and controls
92 lines (82 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
// Include the database connection file
require_once 'bootstrap.php';
require_once 'db.php';
require_once __DIR__ . '/inc/rag_cleanup.php';
if (empty($_SESSION['user_data']['userid']) || empty($_SESSION['authorized'])) {
http_response_code(401);
exit('User not authenticated');
}
$user = $_SESSION['user_data']['userid'];
// Check if the 'chat_id' is set in the POST data
if(isset($_POST['chat_id'])) {
// Assign the 'chat_id' from the POST data to the $chat_id variable
$chat_id = filter_input(INPUT_POST, 'chat_id', FILTER_SANITIZE_STRING);
if (!$chat_id) {
die("Invalid input");
}
$pdo->beginTransaction();
$docIds = [];
try {
$docStmt = $pdo->prepare("
SELECT d.id
FROM document d
INNER JOIN chat c ON c.id = d.chat_id
WHERE d.chat_id = :chat_id
AND d.deleted = 0
AND c.user = :user
AND c.deleted = 0
");
$docStmt->execute([
'chat_id' => $chat_id,
'user' => $user,
]);
$docIds = array_map('intval', $docStmt->fetchAll(PDO::FETCH_COLUMN));
$stmtDocs = $pdo->prepare("
UPDATE document d
INNER JOIN chat c ON c.id = d.chat_id
SET d.`deleted` = 1
WHERE d.chat_id = :chat_id
AND c.user = :user
");
$stmtDocs->execute([
'chat_id' => $chat_id,
'user' => $user,
]);
$stmt1 = $pdo->prepare("UPDATE chat SET `deleted` = 1 WHERE id = :id AND user = :user");
$stmt1->execute([
'id' => $chat_id,
'user' => $user,
]);
if ($stmt1->rowCount() < 1) {
$pdo->rollBack();
http_response_code(403);
exit('Not authorized to delete this chat');
}
$stmt2 = $pdo->prepare("
UPDATE exchange e
INNER JOIN chat c ON c.id = e.chat_id
SET e.`deleted` = 1
WHERE e.chat_id = :chat_id
AND c.user = :user
");
$stmt2->execute([
'chat_id' => $chat_id,
'user' => $user,
]);
$pdo->commit();
} catch (Exception $e) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
throw $e;
}
if (!empty($docIds)) {
try {
$qdrantCfg = $config['qdrant'] ?? [];
ragCleanupProcessDocuments($pdo, $docIds, $qdrantCfg);
} catch (Throwable $cleanupError) {
error_log('delete_chat rag cleanup warning: ' . $cleanupError->getMessage());
}
}
}