games_app/prediqo_mobile/lib/data/models/support_models.dart
2026-09-24 15:23:48 +02:00

145 lines
4.4 KiB
Dart

class SupportConversationModel {
const SupportConversationModel({
required this.id,
required this.status,
this.unreadUser = 0,
});
final String id;
final String status;
final int unreadUser;
factory SupportConversationModel.fromJson(Map<String, dynamic> json) {
return SupportConversationModel(
id: json['id'] as String,
status: (json['status'] ?? 'open').toString(),
unreadUser: (json['unreadUser'] as num?)?.toInt() ?? 0,
);
}
}
class SupportMessageModel {
const SupportMessageModel({
required this.id,
required this.conversationId,
required this.senderType,
required this.createdAt,
this.senderDisplayName,
this.body,
this.attachmentUrl,
this.attachmentName,
this.attachmentMime,
this.readAt,
this.editedAt,
this.deletedAt,
this.canEdit = false,
this.canDelete = false,
this.editAvailableUntil,
this.deleteAvailableUntil,
});
final String id;
final String conversationId;
final String senderType;
final String createdAt;
final String? senderDisplayName;
final String? body;
final String? attachmentUrl;
final String? attachmentName;
final String? attachmentMime;
final String? readAt;
final String? editedAt;
final String? deletedAt;
final bool canEdit;
final bool canDelete;
final String? editAvailableUntil;
final String? deleteAvailableUntil;
bool get isDeleted => deletedAt != null;
bool actionOpen(String? until, bool allowed) {
if (!allowed || until == null || until.isEmpty) return false;
final dt = DateTime.tryParse(until);
if (dt == null) return false;
return dt.isAfter(DateTime.now());
}
bool get editOpen => actionOpen(editAvailableUntil, canEdit);
bool get deleteOpen => actionOpen(deleteAvailableUntil, canDelete);
bool get isMine => senderType == 'user' || senderType == 'visitor';
bool get isImage {
if (attachmentMime?.startsWith('image/') == true) return true;
final name = (attachmentName ?? attachmentUrl ?? '').toLowerCase();
return name.endsWith('.jpg') ||
name.endsWith('.jpeg') ||
name.endsWith('.png') ||
name.endsWith('.gif') ||
name.endsWith('.webp') ||
name.endsWith('.heic') ||
name.endsWith('.bmp');
}
SupportMessageModel copyWith({
String? body,
String? readAt,
String? editedAt,
String? deletedAt,
bool? canEdit,
bool? canDelete,
String? editAvailableUntil,
String? deleteAvailableUntil,
bool clearAttachment = false,
}) {
return SupportMessageModel(
id: id,
conversationId: conversationId,
senderType: senderType,
createdAt: createdAt,
senderDisplayName: senderDisplayName,
body: body ?? this.body,
attachmentUrl: clearAttachment ? null : attachmentUrl,
attachmentName: clearAttachment ? null : attachmentName,
attachmentMime: clearAttachment ? null : attachmentMime,
readAt: readAt ?? this.readAt,
editedAt: editedAt ?? this.editedAt,
deletedAt: deletedAt ?? this.deletedAt,
canEdit: canEdit ?? this.canEdit,
canDelete: canDelete ?? this.canDelete,
editAvailableUntil: editAvailableUntil ?? this.editAvailableUntil,
deleteAvailableUntil: deleteAvailableUntil ?? this.deleteAvailableUntil,
);
}
factory SupportMessageModel.fromJson(Map<String, dynamic> json) {
return SupportMessageModel(
id: json['id'] as String,
conversationId: (json['conversationId'] ?? '').toString(),
senderType: (json['senderType'] ?? 'staff').toString(),
createdAt: (json['createdAt'] ?? '').toString(),
senderDisplayName: json['senderDisplayName'] as String?,
body: json['body'] as String?,
attachmentUrl: json['attachmentUrl'] as String?,
attachmentName: json['attachmentName'] as String?,
attachmentMime: json['attachmentMime'] as String?,
readAt: json['readAt'] as String?,
editedAt: json['editedAt'] as String?,
deletedAt: json['deletedAt'] as String?,
canEdit: json['canEdit'] == true,
canDelete: json['canDelete'] == true,
editAvailableUntil: json['editAvailableUntil'] as String?,
deleteAvailableUntil: json['deleteAvailableUntil'] as String?,
);
}
}
class SupportMessagesPage {
const SupportMessagesPage({
required this.messages,
required this.staffTyping,
});
final List<SupportMessageModel> messages;
final bool staffTyping;
}