From 883785db86459804243b85b0453175515285ae37 Mon Sep 17 00:00:00 2001 From: Karthikeyan S Date: Thu, 11 Apr 2024 13:05:49 +0530 Subject: [PATCH] feat: add sendAudioEvent and isVoiceMessage method to msc extensions --- .../extension_audio_events/audio_events.dart | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 lib/msc_extensions/extension_audio_events/audio_events.dart diff --git a/lib/msc_extensions/extension_audio_events/audio_events.dart b/lib/msc_extensions/extension_audio_events/audio_events.dart new file mode 100644 index 00000000..3d1a735f --- /dev/null +++ b/lib/msc_extensions/extension_audio_events/audio_events.dart @@ -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 || + content['org.matrix.msc3245.voice'] is Map; +} + +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 sendAudioEvent( + MatrixAudioFile audioFile, { + Event? replyTo, + bool isVoiceMessage = false, + int? durationInMs, + List? waveform, + Map? otherFileInfo, + }) { + final extraContent = >{}; + 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'] = {}; + } + extraContent['org.matrix.msc1767.audio'] = { + 'duration': durationInMs, + 'waveform': waveform, + }; + return sendFileEvent( + audioFile, + inReplyTo: replyTo, + extraContent: extraContent, + ); + } +}