feat: add sendAudioEvent and isVoiceMessage method to msc extensions

This commit is contained in:
Karthikeyan S 2024-04-11 13:05:49 +05:30
parent 4bc4b14124
commit 883785db86
No known key found for this signature in database
GPG Key ID: 28BA6AEE539ECE2E
1 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,41 @@
import 'package:matrix/matrix.dart';
extension AudioEventExtension on Event {
/// returns if the event is a voice message
bool get isVoiceMessage =>
content['org.matrix.msc2516.voice'] is Map<String, Object?> ||
content['org.matrix.msc3245.voice'] is Map<String, Object?>;
}
extension AudioEventRoomExtension on Room {
/// Sends an audio file with appropriate info to this room. Returns the event
/// ID generated by the server for this event.
Future<String?> sendAudioEvent(
MatrixAudioFile audioFile, {
Event? replyTo,
bool isVoiceMessage = false,
int? durationInMs,
List<int>? waveform,
Map<String, dynamic>? otherFileInfo,
}) {
final extraContent = <String, Map<String, Object?>>{};
if (durationInMs != null) {
otherFileInfo ??= {};
otherFileInfo['duration'] = durationInMs;
}
if (otherFileInfo != null) extraContent['info'] = otherFileInfo;
if (isVoiceMessage) {
// No content, this is only used to identify if the event is a voice message.
extraContent['org.matrix.msc3245.voice'] = <String, Object?>{};
}
extraContent['org.matrix.msc1767.audio'] = <String, Object?>{
'duration': durationInMs,
'waveform': waveform,
};
return sendFileEvent(
audioFile,
inReplyTo: replyTo,
extraContent: extraContent,
);
}
}