70 lines
1.9 KiB
Dart
70 lines
1.9 KiB
Dart
import 'support_ticket.dart';
|
||
|
||
const maxReplyLength = 4000;
|
||
|
||
class SupportMessage {
|
||
const SupportMessage({
|
||
required this.id,
|
||
required this.ticketId,
|
||
required this.authorKind,
|
||
required this.authorName,
|
||
required this.body,
|
||
required this.createdAt,
|
||
});
|
||
|
||
final String id;
|
||
final String ticketId;
|
||
final String authorKind;
|
||
final String authorName;
|
||
final String body;
|
||
final DateTime createdAt;
|
||
|
||
bool get isStaff => authorKind == 'staff';
|
||
|
||
factory SupportMessage.fromJson(Map<String, dynamic> json) {
|
||
return SupportMessage(
|
||
id: json['id'] as String,
|
||
ticketId: json['ticketId'] as String,
|
||
authorKind: json['authorKind'] as String? ?? 'player',
|
||
authorName: json['authorName'] as String? ?? '',
|
||
body: json['body'] as String? ?? '',
|
||
createdAt: DateTime.parse(json['createdAt'] as String),
|
||
);
|
||
}
|
||
}
|
||
|
||
class SupportThread {
|
||
const SupportThread({required this.ticket, required this.messages});
|
||
|
||
final SupportTicket ticket;
|
||
final List<SupportMessage> messages;
|
||
|
||
factory SupportThread.fromJson(Map<String, dynamic> json) {
|
||
final ticketJson = json['ticket'];
|
||
final messagesJson = json['messages'];
|
||
if (ticketJson is! Map || messagesJson is! List) {
|
||
throw const FormatException('Unexpected thread payload');
|
||
}
|
||
return SupportThread(
|
||
ticket: SupportTicket.fromJson(Map<String, dynamic>.from(ticketJson)),
|
||
messages: [
|
||
for (final item in messagesJson)
|
||
if (item is Map)
|
||
SupportMessage.fromJson(Map<String, dynamic>.from(item)),
|
||
],
|
||
);
|
||
}
|
||
}
|
||
|
||
/// Returns an error message when [raw] is not 1–4000 characters after trim.
|
||
String? validateReplyBody(String raw) {
|
||
final body = raw.trim();
|
||
if (body.isEmpty) {
|
||
return 'Enter a message.';
|
||
}
|
||
if (body.length > maxReplyLength) {
|
||
return 'Message must be $maxReplyLength characters or fewer.';
|
||
}
|
||
return null;
|
||
}
|