Опросы (новый тип сообщения):
- модель Poll/PollAnswer и PollAttachment (_type: POLL)
- PollsModule: загрузка через opcode 306 (GET_POLL_UPDATES) с кэшем
- рендер опроса в баблах (вопрос, варианты, прогресс-бары, голоса)
Видео:
- getVideoUrl через opcode 83 (VIDEO_PLAY): выбор MP4_*/HLS из ответа
- полноэкранный плеер (video_player) с play/pause и перемоткой
- тап по видео в чате запускает воспроизведение
Файлы:
- getFileUrl через opcode 88 (FILE_DOWNLOAD) с корректным форматом
{messageId, chatId, fileId} → url (раньше слался неверный {url, token})
- скачивание во временную папку и открытие системным приложением
(path_provider, open_filex)
Фото:
- полноэкранный просмотрщик с зумом (был TODO-заглушкой)
- фикс «сжатости»: декодирование с учётом devicePixelRatio вместо ×2
597 lines
15 KiB
Dart
597 lines
15 KiB
Dart
enum AttachmentType {
|
|
photo,
|
|
video,
|
|
audio,
|
|
file,
|
|
contact,
|
|
location,
|
|
sticker,
|
|
control,
|
|
poll,
|
|
}
|
|
|
|
abstract class MessageAttachment {
|
|
final AttachmentType type;
|
|
final String? previewData;
|
|
final String? baseUrl;
|
|
final String? fileUrl;
|
|
|
|
const MessageAttachment({
|
|
required this.type,
|
|
this.previewData,
|
|
this.baseUrl,
|
|
this.fileUrl,
|
|
});
|
|
|
|
factory MessageAttachment.fromMap(Map<String, dynamic> map) {
|
|
final type = (map['_type'] as String? ?? '').toUpperCase();
|
|
switch (type) {
|
|
case 'PHOTO':
|
|
return PhotoAttachment.fromMap(map);
|
|
case 'VIDEO':
|
|
return VideoAttachment.fromMap(map);
|
|
case 'AUDIO':
|
|
return AudioAttachment.fromMap(map);
|
|
case 'FILE':
|
|
return FileAttachment.fromMap(map);
|
|
case 'STICKER':
|
|
return StickerAttachment.fromMap(map);
|
|
case 'CONTACT':
|
|
return ContactAttachment.fromMap(map);
|
|
case 'LOCATION':
|
|
return LocationAttachment.fromMap(map);
|
|
case 'CONTROL':
|
|
return ControlAttachment.fromMap(map);
|
|
case 'POLL':
|
|
return PollAttachment.fromMap(map);
|
|
case 'SHARE':
|
|
return FileAttachment.fromMap(map);
|
|
case 'INLINE_KEYBOARD':
|
|
return UnknownAttachment(map);
|
|
default:
|
|
return UnknownAttachment(map);
|
|
}
|
|
}
|
|
|
|
Map<String, dynamic> toMap();
|
|
}
|
|
|
|
class PhotoAttachment extends MessageAttachment {
|
|
final int? photoId;
|
|
final String? photoToken;
|
|
final int? width;
|
|
final int? height;
|
|
final int? size;
|
|
|
|
const PhotoAttachment({
|
|
super.previewData,
|
|
super.baseUrl,
|
|
super.fileUrl,
|
|
this.photoId,
|
|
this.photoToken,
|
|
this.width,
|
|
this.height,
|
|
this.size,
|
|
}) : super(type: AttachmentType.photo);
|
|
|
|
factory PhotoAttachment.fromMap(Map<String, dynamic> map) {
|
|
String? previewStr;
|
|
final previewRaw = map['previewData'];
|
|
if (previewRaw is String) {
|
|
previewStr = previewRaw;
|
|
} else if (previewRaw is List) {
|
|
try {
|
|
final bytes = List<int>.from(previewRaw);
|
|
final base64 = String.fromCharCodes(bytes);
|
|
previewStr = 'data:image/webp;base64,$base64';
|
|
} catch (_) {}
|
|
}
|
|
|
|
return PhotoAttachment(
|
|
previewData: previewStr,
|
|
baseUrl: map['baseUrl'] as String?,
|
|
photoId: map['photoId'] as int?,
|
|
photoToken: map['photoToken'] as String?,
|
|
width: map['width'] as int?,
|
|
height: map['height'] as int?,
|
|
size: map['size'] as int?,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() => {
|
|
'_type': 'PHOTO',
|
|
'previewData': previewData,
|
|
'baseUrl': baseUrl,
|
|
'photoId': photoId,
|
|
'photoToken': photoToken,
|
|
'width': width,
|
|
'height': height,
|
|
'size': size,
|
|
};
|
|
}
|
|
|
|
class VideoAttachment extends MessageAttachment {
|
|
final int? videoId;
|
|
final String? videoToken;
|
|
final int? width;
|
|
final int? height;
|
|
final int? duration;
|
|
final int? size;
|
|
|
|
const VideoAttachment({
|
|
super.previewData,
|
|
super.baseUrl,
|
|
super.fileUrl,
|
|
this.videoId,
|
|
this.videoToken,
|
|
this.width,
|
|
this.height,
|
|
this.duration,
|
|
this.size,
|
|
}) : super(type: AttachmentType.video);
|
|
|
|
factory VideoAttachment.fromMap(Map<String, dynamic> map) {
|
|
String? previewStr;
|
|
final previewRaw = map['previewData'];
|
|
if (previewRaw is String) {
|
|
previewStr = previewRaw;
|
|
} else if (previewRaw is List) {
|
|
try {
|
|
final bytes = List<int>.from(previewRaw);
|
|
final base64 = String.fromCharCodes(bytes);
|
|
previewStr = 'data:image/webp;base64,$base64';
|
|
} catch (_) {}
|
|
}
|
|
|
|
return VideoAttachment(
|
|
previewData: previewStr,
|
|
baseUrl: map['baseUrl'] as String?,
|
|
videoId: map['videoId'] as int?,
|
|
videoToken: map['videoToken'] as String?,
|
|
width: map['width'] as int?,
|
|
height: map['height'] as int?,
|
|
duration: map['duration'] as int?,
|
|
size: map['size'] as int?,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() => {
|
|
'_type': 'VIDEO',
|
|
'previewData': previewData,
|
|
'baseUrl': baseUrl,
|
|
'videoId': videoId,
|
|
'videoToken': videoToken,
|
|
'width': width,
|
|
'height': height,
|
|
'duration': duration,
|
|
'size': size,
|
|
};
|
|
}
|
|
|
|
class AudioAttachment extends MessageAttachment {
|
|
final int? audioId;
|
|
final String? audioToken;
|
|
final int? duration;
|
|
final int? size;
|
|
final String? waveform;
|
|
|
|
const AudioAttachment({
|
|
super.previewData,
|
|
super.baseUrl,
|
|
super.fileUrl,
|
|
this.audioId,
|
|
this.audioToken,
|
|
this.duration,
|
|
this.size,
|
|
this.waveform,
|
|
}) : super(type: AttachmentType.audio);
|
|
|
|
factory AudioAttachment.fromMap(Map<String, dynamic> map) {
|
|
String? previewStr;
|
|
final previewRaw = map['previewData'];
|
|
if (previewRaw is String) {
|
|
previewStr = previewRaw;
|
|
} else if (previewRaw is List) {
|
|
try {
|
|
final bytes = List<int>.from(previewRaw);
|
|
final base64 = String.fromCharCodes(bytes);
|
|
previewStr = 'data:image/webp;base64,$base64';
|
|
} catch (_) {}
|
|
}
|
|
|
|
String? waveStr;
|
|
final waveRaw = map['wave'];
|
|
if (waveRaw is String) {
|
|
waveStr = waveRaw;
|
|
} else if (waveRaw is List) {
|
|
try {
|
|
final bytes = List<int>.from(waveRaw);
|
|
final base64 = String.fromCharCodes(bytes);
|
|
waveStr = 'data:image/webp;base64,$base64';
|
|
} catch (_) {}
|
|
}
|
|
|
|
return AudioAttachment(
|
|
previewData: previewStr,
|
|
baseUrl: map['baseUrl']?.toString(),
|
|
fileUrl: map['url']?.toString(),
|
|
audioId: map['audioId'] as int?,
|
|
audioToken: map['token']?.toString(),
|
|
duration: map['duration'] as int?,
|
|
size: map['size'] as int?,
|
|
waveform: waveStr,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() => {
|
|
'_type': 'AUDIO',
|
|
'previewData': previewData,
|
|
'baseUrl': baseUrl,
|
|
'audioId': audioId,
|
|
'audioToken': audioToken,
|
|
'duration': duration,
|
|
'size': size,
|
|
'waveform': waveform,
|
|
};
|
|
}
|
|
|
|
class FileAttachment extends MessageAttachment {
|
|
final int? fileId;
|
|
final String? fileToken;
|
|
final String? name;
|
|
final int? size;
|
|
|
|
const FileAttachment({
|
|
super.previewData,
|
|
super.baseUrl,
|
|
super.fileUrl,
|
|
this.fileId,
|
|
this.fileToken,
|
|
this.name,
|
|
this.size,
|
|
}) : super(type: AttachmentType.file);
|
|
|
|
factory FileAttachment.fromMap(Map<String, dynamic> map) {
|
|
String? previewStr;
|
|
final previewRaw = map['previewData'];
|
|
if (previewRaw is String) {
|
|
previewStr = previewRaw;
|
|
} else if (previewRaw is List) {
|
|
try {
|
|
final bytes = List<int>.from(previewRaw);
|
|
final base64 = String.fromCharCodes(bytes);
|
|
previewStr = 'data:image/webp;base64,$base64';
|
|
} catch (_) {}
|
|
}
|
|
|
|
return FileAttachment(
|
|
previewData: previewStr,
|
|
baseUrl: map['baseUrl'] as String?,
|
|
fileId: map['fileId'] as int?,
|
|
fileToken: map['fileToken'] as String?,
|
|
name: map['name'] as String?,
|
|
size: map['size'] as int?,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() => {
|
|
'_type': 'FILE',
|
|
'previewData': previewData,
|
|
'baseUrl': baseUrl,
|
|
'fileId': fileId,
|
|
'fileToken': fileToken,
|
|
'name': name,
|
|
'size': size,
|
|
};
|
|
}
|
|
|
|
class StickerAttachment extends MessageAttachment {
|
|
final String? stickerId;
|
|
final String? stickerPackId;
|
|
final int? width;
|
|
final int? height;
|
|
|
|
const StickerAttachment({
|
|
super.previewData,
|
|
super.baseUrl,
|
|
super.fileUrl,
|
|
this.stickerId,
|
|
this.stickerPackId,
|
|
this.width,
|
|
this.height,
|
|
}) : super(type: AttachmentType.sticker);
|
|
|
|
factory StickerAttachment.fromMap(Map<String, dynamic> map) {
|
|
String? previewStr;
|
|
final previewRaw = map['previewData'];
|
|
if (previewRaw is String) {
|
|
previewStr = previewRaw;
|
|
} else if (previewRaw is List) {
|
|
try {
|
|
final bytes = List<int>.from(previewRaw);
|
|
final base64 = String.fromCharCodes(bytes);
|
|
previewStr = 'data:image/webp;base64,$base64';
|
|
} catch (_) {}
|
|
}
|
|
|
|
return StickerAttachment(
|
|
previewData: previewStr,
|
|
baseUrl: (map['url'] ?? map['baseUrl'])?.toString(),
|
|
stickerId: map['stickerId']?.toString(),
|
|
stickerPackId: map['setId']?.toString() ?? map['stickerPackId']?.toString(),
|
|
width: map['width'] as int?,
|
|
height: map['height'] as int?,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() => {
|
|
'_type': 'STICKER',
|
|
'previewData': previewData,
|
|
'baseUrl': baseUrl,
|
|
'stickerId': stickerId,
|
|
'stickerPackId': stickerPackId,
|
|
'width': width,
|
|
'height': height,
|
|
};
|
|
}
|
|
|
|
class ContactAttachment extends MessageAttachment {
|
|
final String? userId;
|
|
final String? firstName;
|
|
final String? lastName;
|
|
final String? phoneNumber;
|
|
final String? photoUrl;
|
|
final int? contactId;
|
|
final String? name;
|
|
|
|
const ContactAttachment({
|
|
super.previewData,
|
|
super.baseUrl,
|
|
super.fileUrl,
|
|
this.userId,
|
|
this.firstName,
|
|
this.lastName,
|
|
this.phoneNumber,
|
|
this.photoUrl,
|
|
this.contactId,
|
|
this.name,
|
|
}) : super(type: AttachmentType.contact);
|
|
|
|
factory ContactAttachment.fromMap(Map<String, dynamic> map) {
|
|
return ContactAttachment(
|
|
previewData: map['previewData']?.toString(),
|
|
baseUrl: map['baseUrl']?.toString(),
|
|
userId: map['userId']?.toString(),
|
|
firstName: map['firstName']?.toString(),
|
|
lastName: map['lastName']?.toString(),
|
|
phoneNumber: map['phoneNumber']?.toString(),
|
|
photoUrl: map['photoUrl']?.toString(),
|
|
contactId: map['contactId'] is int ? map['contactId'] as int : int.tryParse(map['contactId']?.toString() ?? ''),
|
|
name: map['name']?.toString(),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() => {
|
|
'_type': 'CONTACT',
|
|
'previewData': previewData,
|
|
'baseUrl': baseUrl,
|
|
'userId': userId,
|
|
'firstName': firstName,
|
|
'lastName': lastName,
|
|
'phoneNumber': phoneNumber,
|
|
'photoUrl': photoUrl,
|
|
'contactId': contactId,
|
|
'name': name,
|
|
};
|
|
}
|
|
|
|
class LocationAttachment extends MessageAttachment {
|
|
final double? latitude;
|
|
final double? longitude;
|
|
final String? title;
|
|
final String? address;
|
|
|
|
const LocationAttachment({
|
|
super.previewData,
|
|
super.baseUrl,
|
|
super.fileUrl,
|
|
this.latitude,
|
|
this.longitude,
|
|
this.title,
|
|
this.address,
|
|
}) : super(type: AttachmentType.location);
|
|
|
|
factory LocationAttachment.fromMap(Map<String, dynamic> map) {
|
|
return LocationAttachment(
|
|
previewData: map['previewData'] as String?,
|
|
baseUrl: map['baseUrl'] as String?,
|
|
latitude: (map['latitude'] as num?)?.toDouble(),
|
|
longitude: (map['longitude'] as num?)?.toDouble(),
|
|
title: map['title'] as String?,
|
|
address: map['address'] as String?,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() => {
|
|
'_type': 'LOCATION',
|
|
'previewData': previewData,
|
|
'baseUrl': baseUrl,
|
|
'latitude': latitude,
|
|
'longitude': longitude,
|
|
'title': title,
|
|
'address': address,
|
|
};
|
|
}
|
|
|
|
class ControlAttachment extends MessageAttachment {
|
|
final String? event;
|
|
final String? title;
|
|
final List<int>? userIds;
|
|
final int? userId;
|
|
|
|
const ControlAttachment({
|
|
super.previewData,
|
|
super.baseUrl,
|
|
super.fileUrl,
|
|
this.event,
|
|
this.title,
|
|
this.userIds,
|
|
this.userId,
|
|
}) : super(type: AttachmentType.control);
|
|
|
|
factory ControlAttachment.fromMap(Map<String, dynamic> map) {
|
|
String? title = map['title']?.toString();
|
|
if ((title == null || title.isEmpty) && map['shortMessage'] != null) {
|
|
title = map['shortMessage'].toString();
|
|
}
|
|
|
|
return ControlAttachment(
|
|
previewData: map['previewData']?.toString(),
|
|
baseUrl: map['baseUrl']?.toString(),
|
|
event: map['event']?.toString(),
|
|
title: title,
|
|
userIds: (map['userIds'] as List?)?.map((e) => e is int ? e : int.tryParse(e?.toString() ?? '') ?? 0).toList(),
|
|
userId: map['userId'] is int ? map['userId'] as int : int.tryParse(map['userId']?.toString() ?? ''),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() => {
|
|
'_type': 'CONTROL',
|
|
'previewData': previewData,
|
|
'baseUrl': baseUrl,
|
|
'event': event,
|
|
'title': title,
|
|
'userIds': userIds,
|
|
'userId': userId,
|
|
};
|
|
}
|
|
|
|
class PollAttachment extends MessageAttachment {
|
|
final int pollId;
|
|
final String? title;
|
|
|
|
const PollAttachment({
|
|
required this.pollId,
|
|
this.title,
|
|
}) : super(type: AttachmentType.poll);
|
|
|
|
factory PollAttachment.fromMap(Map<String, dynamic> map) {
|
|
final id = map['pollId'] ?? map['id'];
|
|
return PollAttachment(
|
|
pollId: id is int ? id : int.tryParse(id?.toString() ?? '') ?? 0,
|
|
title: (map['title'] ?? map['question'])?.toString(),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() => {
|
|
'_type': 'POLL',
|
|
'pollId': pollId,
|
|
'title': title,
|
|
};
|
|
}
|
|
|
|
class ForwardedMessageAttachment extends MessageAttachment {
|
|
final int originalSenderId;
|
|
final String? originalSenderName;
|
|
final String? originalSenderAvatar;
|
|
final String? originalMessageId;
|
|
final int? originalTime;
|
|
final String? originalText;
|
|
final int? originalChatId;
|
|
final List<MessageAttachment>? originalAttachments;
|
|
final ContactAttachment? originalContact;
|
|
|
|
const ForwardedMessageAttachment({
|
|
required this.originalSenderId,
|
|
this.originalSenderName,
|
|
this.originalSenderAvatar,
|
|
this.originalMessageId,
|
|
this.originalTime,
|
|
this.originalText,
|
|
this.originalChatId,
|
|
this.originalAttachments,
|
|
this.originalContact,
|
|
}) : super(type: AttachmentType.photo);
|
|
|
|
factory ForwardedMessageAttachment.fromMap(Map<String, dynamic> map) {
|
|
final linkRaw = map['link'];
|
|
Map<String, dynamic>? link;
|
|
if (linkRaw is Map) {
|
|
link = Map<String, dynamic>.from(linkRaw);
|
|
}
|
|
|
|
Map<String, dynamic>? message;
|
|
if (link != null) {
|
|
final msgRaw = link['message'];
|
|
if (msgRaw is Map) {
|
|
message = Map<String, dynamic>.from(msgRaw);
|
|
}
|
|
}
|
|
|
|
List<MessageAttachment>? originalAttaches;
|
|
ContactAttachment? originalContact;
|
|
if (message != null) {
|
|
final attaches = message['attaches'] as List?;
|
|
if (attaches != null) {
|
|
final contactAttaches = attaches
|
|
.whereType<Map>()
|
|
.where((a) => (a['_type'] as String?)?.toUpperCase() == 'CONTACT')
|
|
.toList();
|
|
if (contactAttaches.isNotEmpty) {
|
|
originalContact = ContactAttachment.fromMap(
|
|
Map<String, dynamic>.from(contactAttaches.first),
|
|
);
|
|
}
|
|
originalAttaches = attaches
|
|
.whereType<Map>()
|
|
.where((a) {
|
|
final type = (a['_type'] as String?)?.toUpperCase();
|
|
return type != 'CONTACT';
|
|
})
|
|
.map((a) => MessageAttachment.fromMap(Map<String, dynamic>.from(a)))
|
|
.toList();
|
|
}
|
|
}
|
|
|
|
return ForwardedMessageAttachment(
|
|
originalSenderId: (message?['sender'] as int?) ?? 0,
|
|
originalMessageId: message?['id']?.toString(),
|
|
originalTime: message?['time'] as int?,
|
|
originalText: message?['text'] as String?,
|
|
originalChatId: link?['chatId'] as int?,
|
|
originalAttachments: originalAttaches,
|
|
originalContact: originalContact,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() => {
|
|
'_type': 'FORWARD',
|
|
'originalSenderId': originalSenderId,
|
|
'originalSenderName': originalSenderName,
|
|
'originalSenderAvatar': originalSenderAvatar,
|
|
'originalMessageId': originalMessageId,
|
|
'originalTime': originalTime,
|
|
'originalText': originalText,
|
|
'originalChatId': originalChatId,
|
|
};
|
|
}
|
|
|
|
class UnknownAttachment extends MessageAttachment {
|
|
final Map<String, dynamic> rawData;
|
|
|
|
const UnknownAttachment(this.rawData) : super(type: AttachmentType.photo);
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() => rawData;
|
|
}
|