payfrit-api/api/hub/channels/get.php
Mike 629c7d2cef Add Hub Channels API — CRUD endpoints for channel management
New endpoints under /api/hub/channels/:
- create.php: Create channel with type (public/private/direct), auto-add creator as owner
- list.php: List channels with filters (type, agent membership, archived, pagination)
- get.php: Get channel by ID or Name, includes member list
- update.php: Update display name, purpose, archive status (admin/owner only)
- delete.php: Hard-delete channel (owner only), FK cascade removes members
- members.php: List channel members with agent info
- join.php: Join public channels (private requires invite)
- leave.php: Leave channel (owners blocked from leaving)

Database: Hub_Channels + Hub_ChannelMembers tables with FK cascade.
Task #59 (T51-Sub1)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 01:06:14 +00:00

74 lines
2.1 KiB
PHP

<?php
/**
* GET /api/hub/channels/get.php
*
* Get a single channel by ID or Name.
*
* Query params:
* ID int get by ID
* Name string get by name (if ID not provided)
*
* Response: { OK: true, Channel: { ... }, Members: [...] }
*/
require_once __DIR__ . '/../../helpers.php';
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
jsonResponse(['OK' => false, 'ERROR' => 'method_not_allowed'], 405);
}
$id = (int) ($_GET['ID'] ?? 0);
$name = trim($_GET['Name'] ?? '');
if ($id <= 0 && $name === '') {
jsonResponse(['OK' => false, 'ERROR' => 'id_or_name_required']);
}
if ($id > 0) {
$channel = queryOne("SELECT * FROM Hub_Channels WHERE ID = ?", [$id]);
} else {
$channel = queryOne("SELECT * FROM Hub_Channels WHERE Name = ?", [$name]);
}
if (!$channel) {
jsonResponse(['OK' => false, 'ERROR' => 'channel_not_found'], 404);
}
// Fetch members
$members = queryTimed(
"SELECT m.*, a.AgentName, a.AgentType, a.Role AS AgentRole
FROM Hub_ChannelMembers m
LEFT JOIN Sprinter_Agents a ON a.FullAddress = m.AgentAddress
WHERE m.ChannelID = ?
ORDER BY m.JoinedAt ASC",
[(int) $channel['ID']]
);
$memberList = [];
foreach ($members as $m) {
$memberList[] = [
'AgentAddress' => $m['AgentAddress'],
'AgentName' => $m['AgentName'] ?? '',
'AgentType' => $m['AgentType'] ?? '',
'Role' => $m['Role'],
'JoinedAt' => toISO8601($m['JoinedAt']),
'LastViewedAt' => $m['LastViewedAt'] ? toISO8601($m['LastViewedAt']) : null,
];
}
jsonResponse([
'OK' => true,
'Channel' => [
'ID' => (int) $channel['ID'],
'Name' => $channel['Name'],
'DisplayName' => $channel['DisplayName'],
'Purpose' => $channel['Purpose'],
'ChannelType' => $channel['ChannelType'],
'CreatedBy' => $channel['CreatedBy'],
'IsArchived' => (bool) $channel['IsArchived'],
'CreatedAt' => toISO8601($channel['CreatedAt']),
'UpdatedAt' => toISO8601($channel['UpdatedAt']),
'MemberCount' => count($memberList),
],
'Members' => $memberList,
]);