-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtoggle_library.php
More file actions
49 lines (41 loc) · 1.47 KB
/
Copy pathtoggle_library.php
File metadata and controls
49 lines (41 loc) · 1.47 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
<?php
declare(strict_types=1);
require_once __DIR__ . '/bootstrap.php';
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit;
}
if (empty($_SESSION['user_data']['userid']) || empty($_SESSION['authorized'])) {
http_response_code(401);
echo json_encode(['error' => 'Authentication required']);
exit;
}
$payload = json_decode(file_get_contents('php://input') ?: '[]', true);
$chatId = isset($payload['chat_id']) ? trim((string)$payload['chat_id']) : '';
$libraryId = isset($payload['library_id']) ? (int)$payload['library_id'] : 0;
$requestedState = $payload['enabled'] ?? null;
if ($chatId === '' || $libraryId <= 0) {
http_response_code(400);
echo json_encode(['error' => 'chat_id and library_id are required']);
exit;
}
try {
$newState = toggle_library_for_chat($_SESSION['user_data']['userid'], $chatId, $libraryId, $requestedState);
if ($newState === null) {
http_response_code(404);
echo json_encode(['error' => 'Library selection not found for this chat']);
exit;
}
echo json_encode([
'ok' => true,
'chat_id' => $chatId,
'library_id' => $libraryId,
'enabled' => (bool)$newState,
]);
} catch (Throwable $e) {
error_log('toggle_library error: ' . $e->getMessage());
http_response_code(500);
echo json_encode(['error' => 'Unable to toggle library']);
}