createComment method
Creates a comment inside a thread.
The method automatically injects the tenant and external user identifiers
required by the backend. Returns the created Comment as acknowledged by
the server or throws CommentsApiException if the call fails. Provide
parentId to create a reply, metadata for custom attributes, and
override authorName or authorAvatarUrl to bypass the defaults taken
from externalUser.
Implementation
Future<Comment> createComment({
required String threadId,
required String body,
String? parentId,
Map<String, dynamic>? metadata,
String? authorName,
String? authorAvatarUrl,
}) async {
final token = await _ensureToken();
final uri = _uri('/api/comments');
final response = await _http.post(
uri,
headers: {
'Authorization': token.token,
'Content-Type': 'application/json',
},
body: jsonEncode({
'tenant_id': token.tenantId,
'thread_id': threadId,
'external_user_id': externalUser.id,
'author_name': authorName ?? externalUser.name,
'author_avatar_url': authorAvatarUrl ?? externalUser.avatarUrl,
'body': body,
if (parentId != null) 'parent_id': parentId,
if (metadata != null) 'metadata': metadata,
}),
);
if (response.statusCode >= 400) {
throw _mapError(response);
}
final payload = jsonDecode(response.body);
if (payload is List &&
payload.isNotEmpty &&
payload.first is Map<String, dynamic>) {
return Comment.fromJson(payload.first as Map<String, dynamic>);
}
if (payload is Map<String, dynamic>) {
return Comment.fromJson(payload);
}
throw CommentsApiException(response.statusCode, 'unexpected_response',
details: payload);
}