api/watch_ticket.php performed the ticket_watchers INSERT IGNORE/DELETE
directly with no AuditLogModel call, unlike every other ticket-adjacent
mutation (comments, attachments, dependencies, status/field changes),
so watching/unwatching never showed up in a ticket's timeline.
Added AuditLogModel::log() calls to both the watch and unwatch paths,
gated on the DB statement's affected_rows so a no-op (already watching,
already not watching) doesn't produce a duplicate timeline entry. Added
'watch'/'unwatch' to AuditLogModel's VALID_ACTION_TYPES, and timeline
rendering in views/TicketView.php ("started watching this ticket" /
"stopped watching this ticket").
Verified against real MariaDB: watch -> unwatch -> watch again produces
exactly 2 timeline entries (not 4) since the two no-op repeats correctly
produced zero rows changed and were not logged; confirmed formatAction()/
getEventIcon() render both action types correctly.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0117oBw2jN4kALYeS8HPq4zV
147 lines
4.9 KiB
PHP
147 lines
4.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Watch / Unwatch Ticket API
|
|
*
|
|
* GET ?ticket_id=N → returns { watching: bool, watcher_count: int }
|
|
* POST { ticket_id, action: 'watch'|'unwatch' } → toggles watcher row
|
|
*/
|
|
|
|
require_once __DIR__ . '/bootstrap.php';
|
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
|
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true) ?? [];
|
|
|
|
$ticketIdRaw = isset($_GET['ticket_id']) ? $_GET['ticket_id'] : ($data['ticket_id'] ?? '');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$ticketIdRaw = $data['ticket_id'] ?? '';
|
|
$action = $data['action'] ?? '';
|
|
|
|
if ($ticketIdRaw === '' || !in_array($action, ['watch', 'unwatch'], true)) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'Invalid parameters']);
|
|
exit;
|
|
}
|
|
|
|
$ticketModel = new TicketModel($conn);
|
|
$ticket = $ticketModel->getTicketById((string)$ticketIdRaw);
|
|
if (!$ticket || !$ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'error' => 'Ticket not found']);
|
|
exit;
|
|
}
|
|
|
|
// Use the canonical ticket_id string from the fetched ticket row, not the
|
|
// raw request value, so ticket_watchers always stores exactly what's in
|
|
// tickets.ticket_id.
|
|
$ticketId = $ticket['ticket_id'];
|
|
|
|
if ($action === 'watch') {
|
|
$stmt = $conn->prepare(
|
|
"INSERT IGNORE INTO ticket_watchers (ticket_id, user_id) VALUES (?, ?)"
|
|
);
|
|
$stmt->bind_param("si", $ticketId, $userId);
|
|
$stmt->execute();
|
|
$rowsChanged = $stmt->affected_rows;
|
|
$stmt->close();
|
|
} else {
|
|
$stmt = $conn->prepare(
|
|
"DELETE FROM ticket_watchers WHERE ticket_id = ? AND user_id = ?"
|
|
);
|
|
$stmt->bind_param("si", $ticketId, $userId);
|
|
$stmt->execute();
|
|
$rowsChanged = $stmt->affected_rows;
|
|
$stmt->close();
|
|
}
|
|
|
|
// Only log an actual state change — INSERT IGNORE/DELETE are no-ops when
|
|
// the user was already watching/not watching, and that shouldn't show up
|
|
// in the ticket's timeline as a new event.
|
|
if ($rowsChanged > 0) {
|
|
(new AuditLogModel($conn))->log($userId, $action, 'ticket', $ticketId);
|
|
}
|
|
|
|
// Return updated state
|
|
$countStmt = $conn->prepare(
|
|
"SELECT COUNT(*) as cnt FROM ticket_watchers WHERE ticket_id = ?"
|
|
);
|
|
$countStmt->bind_param("s", $ticketId);
|
|
$countStmt->execute();
|
|
$count = (int)$countStmt->get_result()->fetch_assoc()['cnt'];
|
|
$countStmt->close();
|
|
|
|
apiRespond([
|
|
'success' => true,
|
|
'watching' => $action === 'watch',
|
|
'watcher_count' => $count,
|
|
]);
|
|
}
|
|
|
|
// GET — return current watch state for this user
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
|
|
exit;
|
|
}
|
|
|
|
if ($ticketIdRaw === '') {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'ticket_id required']);
|
|
exit;
|
|
}
|
|
|
|
// Enforce ticket visibility before returning watch state / watcher names, so a
|
|
// restricted ticket's watcher list and count aren't disclosed (the POST path
|
|
// already checks this).
|
|
$ticketModel = new TicketModel($conn);
|
|
$ticket = $ticketModel->getTicketById((string)$ticketIdRaw);
|
|
if (!$ticket || !$ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'error' => 'Ticket not found']);
|
|
exit;
|
|
}
|
|
|
|
$ticketId = $ticket['ticket_id'];
|
|
|
|
$watchingStmt = $conn->prepare(
|
|
"SELECT COUNT(*) as cnt FROM ticket_watchers WHERE ticket_id = ? AND user_id = ?"
|
|
);
|
|
$watchingStmt->bind_param("si", $ticketId, $userId);
|
|
$watchingStmt->execute();
|
|
$watching = (bool)$watchingStmt->get_result()->fetch_assoc()['cnt'];
|
|
$watchingStmt->close();
|
|
|
|
// Fetch watcher list (up to 6) with display names for avatar group
|
|
$watchersStmt = $conn->prepare(
|
|
"SELECT u.user_id, COALESCE(u.display_name, u.username) AS display_name
|
|
FROM ticket_watchers tw
|
|
JOIN users u ON tw.user_id = u.user_id
|
|
WHERE tw.ticket_id = ?
|
|
ORDER BY tw.created_at ASC
|
|
LIMIT 6"
|
|
);
|
|
$watchersStmt->bind_param("s", $ticketId);
|
|
$watchersStmt->execute();
|
|
$watchersResult = $watchersStmt->get_result();
|
|
$watchers = [];
|
|
while ($row = $watchersResult->fetch_assoc()) {
|
|
$watchers[] = ['user_id' => (int)$row['user_id'], 'display_name' => $row['display_name']];
|
|
}
|
|
$watchersStmt->close();
|
|
|
|
// True watcher count (the list above is capped at 6 for the avatar group)
|
|
$countStmt = $conn->prepare("SELECT COUNT(*) AS cnt FROM ticket_watchers WHERE ticket_id = ?");
|
|
$countStmt->bind_param("s", $ticketId);
|
|
$countStmt->execute();
|
|
$count = (int)$countStmt->get_result()->fetch_assoc()['cnt'];
|
|
$countStmt->close();
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'watching' => $watching,
|
|
'watcher_count' => $count,
|
|
'watchers' => $watchers,
|
|
]);
|