forked from eatvio/eatvio_app
69 lines
2.6 KiB
Dart
69 lines
2.6 KiB
Dart
import 'package:eatvio_app/features/feed/models/feed_blog_post_model.dart';
|
||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||
|
||
part 'chat_message_model.freezed.dart';
|
||
part 'chat_message_model.g.dart';
|
||
|
||
int _parseAuthorId(dynamic json) {
|
||
if (json == null) return 0;
|
||
if (json is num) return json.toInt();
|
||
return 0;
|
||
}
|
||
|
||
DateTime _parseUpdatedAt(dynamic json) {
|
||
if (json == null) return DateTime.now();
|
||
if (json is String) return parseDate(json);
|
||
return DateTime.now();
|
||
}
|
||
|
||
String _parseData(dynamic json) {
|
||
if (json == null) return '';
|
||
return json.toString();
|
||
}
|
||
|
||
@freezed
|
||
abstract class ChatMessageModel with _$ChatMessageModel {
|
||
const factory ChatMessageModel({
|
||
required int id,
|
||
@JsonKey(name: 'data', fromJson: _parseData) required String data,
|
||
@JsonKey(name: 'media_data') required List<dynamic>? mediaData,
|
||
@JsonKey(name: 'urls_media_info') List<Map<String, dynamic>>? urlsMediaInfo,
|
||
@JsonKey(name: 'author_id', fromJson: _parseAuthorId) required int authorId,
|
||
@JsonKey(name: 'updated_at', fromJson: _parseUpdatedAt) required DateTime updatedAt,
|
||
@JsonKey(name: 'read_by_to_user', defaultValue: false) required bool isRead,
|
||
// @deprecated Backend BC-поле. В broadcast (ChatMessageUpdated) оно
|
||
// recipient-relative и для второго устройства отправителя всегда false,
|
||
// поэтому решать «моё/не моё» по нему нельзя. Источник истины — authorId:
|
||
// сравнивайте его со своим user_id (wsManager.userId). Поле оставлено
|
||
// только для обратной совместимости парсинга.
|
||
@JsonKey(name: 'is_author_message_auth_user') required bool? isCurrentUserAuthor,
|
||
@JsonKey(name: 'room_id') required String? roomId,
|
||
@JsonKey(name: 'type', unknownEnumValue: ChatMessageType.unknown) ChatMessageType? type,
|
||
@Default(MessageState.sent) @JsonKey(includeToJson: false, includeFromJson: false) MessageState messageState,
|
||
@Default(null) @JsonKey(includeToJson: false, includeFromJson: false) String? uuid,
|
||
}) = _ChatMessageModel;
|
||
|
||
factory ChatMessageModel.fromJson(Map<String, Object?> json) => _$ChatMessageModelFromJson(json);
|
||
}
|
||
|
||
enum MessageState { loading, sent, error }
|
||
|
||
enum ChatMessageType {
|
||
@JsonValue('text')
|
||
text,
|
||
@JsonValue('system')
|
||
system,
|
||
@JsonValue('attachment')
|
||
attachment,
|
||
@JsonValue('image')
|
||
image,
|
||
@JsonValue('plan_order')
|
||
planOrder,
|
||
@JsonValue('video_call_started')
|
||
videoCallStarted,
|
||
@JsonValue('video_call_ended')
|
||
videoCallEnded,
|
||
@JsonValue('unknown')
|
||
unknown;
|
||
}
|