Skip to content

Commit

Permalink
element-hq/element-ios/issues/4090 - Add custom reply text to voice m…
Browse files Browse the repository at this point in the history
…essages, add duration and samples to sendVoiceMessage.
  • Loading branch information
stefanceriu committed Jul 13, 2021
1 parent 733f4b2 commit 3e8f798
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Changes to be released in next version
* MXTools: Default to 1080p when converting a video (vector-im/element-ios/issues/4478).
* MXEvent: add support for voice messages
* MXRoom: Add support for sending slow motion videos using AVAsset (vector-im/element-ios/issues/4483).
* MXSendReplyEventStringsLocalizable: Added senderSentAVoiceMessage property

🐛 Bugfix
* Fix QR self verification with QR code (#1147)
Expand All @@ -16,6 +17,7 @@ Changes to be released in next version

⚠️ API Changes
* MXSDKOptions: Add videoConversionPresetName to customise video conversion quality.
* MXRoom: Added duration and sample parameters on the sendVoiceMessage method (vector-im/element-ios/issues/4090)

🗣 Translations
*
Expand Down
7 changes: 5 additions & 2 deletions MatrixSDK/Contrib/Swift/Data/MXRoom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ public extension MXRoom {
- parameters:
- localURL: the local filesystem path of the file to send.
- mimeType: (optional) the mime type of the file. Defaults to `audio/ogg`.
- duration: the length of the voice message in milliseconds
- samples: an array of floating point values normalized to [0, 1]
- localEcho: a pointer to a MXEvent object.
This pointer is set to an actual MXEvent object
Expand All @@ -348,8 +350,9 @@ public extension MXRoom {
- returns: a `MXHTTPOperation` instance.
*/

@nonobjc @discardableResult func sendVoiceMessage(localURL: URL, mimeType: String?, localEcho: inout MXEvent?, completion: @escaping (_ response: MXResponse<String?>) -> Void) -> MXHTTPOperation {
return __sendVoiceMessage(localURL, mimeType: mimeType, localEcho: &localEcho, success: currySuccess(completion), failure: curryFailure(completion), keepActualFilename: false)
@nonobjc @discardableResult func sendVoiceMessage(localURL: URL, mimeType: String?, duration: TimeInterval, samples: [Float]?, localEcho: inout MXEvent?, completion: @escaping (_ response: MXResponse<String?>) -> Void) -> MXHTTPOperation {
let boxedSamples = samples?.compactMap { NSNumber(value: $0) }
return __sendVoiceMessage(localURL, mimeType: mimeType, duration: duration, samples: boxedSamples, localEcho: &localEcho, success: currySuccess(completion), failure: curryFailure(completion), keepActualFilename: false)
}

/**
Expand Down
5 changes: 2 additions & 3 deletions MatrixSDK/Contrib/Swift/JSONModels/MXEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public enum MXEventType: Equatable, Hashable {

/// Types of messages
public enum MXMessageType: Equatable, Hashable {
case text, emote, notice, image, audio, voiceMessage, video, location, file
case text, emote, notice, image, audio, video, location, file
case custom(String)

public var identifier: String {
Expand All @@ -151,7 +151,6 @@ public enum MXMessageType: Equatable, Hashable {
case .notice: return kMXMessageTypeNotice
case .image: return kMXMessageTypeImage
case .audio: return kMXMessageTypeAudio
case .voiceMessage: return kMXMessageTypeVoiceMessage
case .video: return kMXMessageTypeVideo
case .location: return kMXMessageTypeLocation
case .file: return kMXMessageTypeFile
Expand All @@ -160,7 +159,7 @@ public enum MXMessageType: Equatable, Hashable {
}

public init(identifier: String) {
let messages: [MXMessageType] = [.text, .emote, .notice, .image, .audio, .voiceMessage, .video, .location, .file]
let messages: [MXMessageType] = [.text, .emote, .notice, .image, .audio, .video, .location, .file]
self = messages.first(where: { $0.identifier == identifier }) ?? .custom(identifier)
}
}
Expand Down
4 changes: 4 additions & 0 deletions MatrixSDK/Data/MXRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,8 @@ FOUNDATION_EXPORT NSInteger const kMXRoomAlreadyJoinedErrorCode;
@param fileLocalURL the local filesystem path of the file to send.
@param mimeType (optional) the mime type of the file. Defaults to `audio/ogg`
@param duration the lenght of the voice message in milliseconds
@param samples an array of floating point values normalized to [0, 1], boxed within NSNumbers
@param localEcho a pointer to a MXEvent object (@see sendMessageWithContent: for details).
@param success A block object called when the operation succeeds. It returns
the event id of the event generated on the home server
Expand All @@ -544,6 +546,8 @@ FOUNDATION_EXPORT NSInteger const kMXRoomAlreadyJoinedErrorCode;
*/
- (MXHTTPOperation*)sendVoiceMessage:(NSURL*)fileLocalURL
mimeType:(NSString*)mimeType
duration:(NSTimeInterval)duration
samples:(NSArray<NSNumber *> *)samples
localEcho:(MXEvent**)localEcho
success:(void (^)(NSString *eventId))success
failure:(void (^)(NSError *error))failure
Expand Down
27 changes: 26 additions & 1 deletion MatrixSDK/Data/MXRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -1427,14 +1427,34 @@ - (MXHTTPOperation*)sendAudioFile:(NSURL*)fileLocalURL

- (MXHTTPOperation*)sendVoiceMessage:(NSURL*)fileLocalURL
mimeType:(NSString*)mimeType
duration:(NSTimeInterval)duration
samples:(NSArray<NSNumber *> *)samples
localEcho:(MXEvent**)localEcho
success:(void (^)(NSString *eventId))success
failure:(void (^)(NSError *error))failure
keepActualFilename:(BOOL)keepActualName
{
NSMutableDictionary *extensibleAudioContent = @{kMXMessageContentKeyExtensibleAudioDuration : @(duration)}.mutableCopy;

static NSUInteger scaledWaveformSampleCeiling = 1024;

NSMutableArray *scaledSamples = [NSMutableArray array];
for (NSNumber *sample in samples) {
if (sample.floatValue < 0.0 || sample.floatValue > 1.0) { // Samples should be linearly normalized to [0, 1]
continue;
}

[scaledSamples addObject:@((NSInteger)(scaledWaveformSampleCeiling * sample.floatValue))];
}

if (scaledSamples.count) {
[extensibleAudioContent setObject:scaledSamples forKey:kMXMessageContentKeyExtensibleAudioWaveform];
}

return [self _sendFile:fileLocalURL
msgType:kMXMessageTypeAudio
additionalTypes:@{kMXMessageTypeVoiceMessage : @{}}
additionalTypes:@{kMXMessageContentKeyVoiceMessageMSC3245 : @{},
kMXMessageContentKeyExtensibleAudio: extensibleAudioContent}
mimeType:(mimeType ?: @"audio/ogg")
localEcho:localEcho
success:success
Expand Down Expand Up @@ -1991,6 +2011,11 @@ - (void)getReplyContentBodiesWithEventToReply:(MXEvent*)eventToReply
senderMessageBody = stringLocalizations.senderSentAVideo;
senderMessageFormattedBody = senderMessageBody;
}
else if (eventToReply.isVoiceMessage)
{
senderMessageBody = stringLocalizations.senderSentAVoiceMessage;
senderMessageFormattedBody = senderMessageBody;
}
else if ([msgtype isEqualToString:kMXMessageTypeAudio])
{
senderMessageBody = stringLocalizations.senderSentAnAudioFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
@property (copy, readonly, nonnull) NSString *senderSentAnImage;
@property (copy, readonly, nonnull) NSString *senderSentAVideo;
@property (copy, readonly, nonnull) NSString *senderSentAnAudioFile;
@property (copy, readonly, nonnull) NSString *senderSentAVoiceMessage;
@property (copy, readonly, nonnull) NSString *senderSentAFile;
@property (copy, readonly, nonnull) NSString *messageToReplyToPrefix;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ - (instancetype)init
_senderSentAnImage = @"sent an image.";
_senderSentAVideo = @"sent a video.";
_senderSentAnAudioFile = @"sent an audio file.";
_senderSentAVoiceMessage = @"sent a voice message.";
_senderSentAFile = @"sent a file.";
_messageToReplyToPrefix = @"In reply to";
}
Expand Down
1 change: 1 addition & 0 deletions MatrixSDK/Data/MXSendReplyEventStringsLocalizable.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@property (copy, readonly, nonnull) NSString *senderSentAnImage;
@property (copy, readonly, nonnull) NSString *senderSentAVideo;
@property (copy, readonly, nonnull) NSString *senderSentAnAudioFile;
@property (copy, readonly, nonnull) NSString *senderSentAVoiceMessage;
@property (copy, readonly, nonnull) NSString *senderSentAFile;
@property (copy, readonly, nonnull) NSString *messageToReplyToPrefix;

Expand Down
17 changes: 13 additions & 4 deletions MatrixSDK/JSONModels/MXEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ FOUNDATION_EXPORT NSString *const kMXMessageTypeFile;
FOUNDATION_EXPORT NSString *const kMXMessageTypeServerNotice;
FOUNDATION_EXPORT NSString *const kMXMessageTypeKeyVerificationRequest;

FOUNDATION_EXPORT NSString *const kMXMessageTypeVoiceMessage;
FOUNDATION_EXPORT NSString *const kMXMessageTypeVoiceMessageMSC2516;
FOUNDATION_EXPORT NSString *const kMXMessageTypeVoiceMessageMSC3245;

/**
Event relations
*/
Expand All @@ -197,6 +193,14 @@ FOUNDATION_EXPORT NSString *const MXEventRelationTypeReplace; // Edition
*/
FOUNDATION_EXPORT NSString *const kMXEventLocalEventIdPrefix;


FOUNDATION_EXPORT NSString *const kMXMessageContentKeyVoiceMessage;
FOUNDATION_EXPORT NSString *const kMXMessageContentKeyVoiceMessageMSC2516;
FOUNDATION_EXPORT NSString *const kMXMessageContentKeyVoiceMessageMSC3245;
FOUNDATION_EXPORT NSString *const kMXMessageContentKeyExtensibleAudio;
FOUNDATION_EXPORT NSString *const kMXMessageContentKeyExtensibleAudioDuration;
FOUNDATION_EXPORT NSString *const kMXMessageContentKeyExtensibleAudioWaveform;

/**
The internal event state used to handle the different steps of the event sending.
*/
Expand Down Expand Up @@ -450,6 +454,11 @@ extern NSString *const kMXEventIdentifierKey;
*/
- (BOOL)isReplyEvent;

/**
Return YES if the event contains a voice message
*/
- (BOOL)isVoiceMessage;

/**
Return YES if the event content has been edited.
*/
Expand Down
51 changes: 31 additions & 20 deletions MatrixSDK/JSONModels/MXEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,34 +85,37 @@
// Use temporary event type until the MSC approval
NSString *const kMXEventTypeStringSpaceChild = @"org.matrix.msc1772.space.child";

NSString *const kMXMessageTypeText = @"m.text";
NSString *const kMXMessageTypeEmote = @"m.emote";
NSString *const kMXMessageTypeNotice = @"m.notice";
NSString *const kMXMessageTypeImage = @"m.image";
NSString *const kMXMessageTypeAudio = @"m.audio";
NSString *const kMXMessageTypeVideo = @"m.video";
NSString *const kMXMessageTypeLocation = @"m.location";
NSString *const kMXMessageTypeFile = @"m.file";
NSString *const kMXMessageTypeServerNotice = @"m.server_notice";
NSString *const kMXMessageTypeText = @"m.text";
NSString *const kMXMessageTypeEmote = @"m.emote";
NSString *const kMXMessageTypeNotice = @"m.notice";
NSString *const kMXMessageTypeImage = @"m.image";
NSString *const kMXMessageTypeAudio = @"m.audio";
NSString *const kMXMessageTypeVideo = @"m.video";
NSString *const kMXMessageTypeLocation = @"m.location";
NSString *const kMXMessageTypeFile = @"m.file";
NSString *const kMXMessageTypeServerNotice = @"m.server_notice";
NSString *const kMXMessageTypeKeyVerificationRequest = @"m.key.verification.request";

NSString *const kMXMessageTypeVoiceMessageMSC2516 = @"org.matrix.msc2516.voice";
NSString *const kMXMessageTypeVoiceMessageMSC3245 = @"org.matrix.msc3245.voice";
NSString *const kMXMessageTypeVoiceMessage = @"m.voice";
NSString *const MXEventRelationTypeAnnotation = @"m.annotation";
NSString *const MXEventRelationTypeReference = @"m.reference";
NSString *const MXEventRelationTypeReplace = @"m.replace";

NSString *const MXEventRelationTypeAnnotation = @"m.annotation";
NSString *const MXEventRelationTypeReference = @"m.reference";
NSString *const MXEventRelationTypeReplace = @"m.replace";

NSString *const kMXEventLocalEventIdPrefix = @"kMXEventLocalId_";
NSString *const kMXEventLocalEventIdPrefix = @"kMXEventLocalId_";

uint64_t const kMXUndefinedTimestamp = (uint64_t)-1;

NSString *const kMXEventDidChangeSentStateNotification = @"kMXEventDidChangeSentStateNotification";
NSString *const kMXEventDidChangeSentStateNotification = @"kMXEventDidChangeSentStateNotification";
NSString *const kMXEventDidChangeIdentifierNotification = @"kMXEventDidChangeIdentifierNotification";
NSString *const kMXEventDidDecryptNotification = @"kMXEventDidDecryptNotification";
NSString *const kMXEventDidDecryptNotification = @"kMXEventDidDecryptNotification";

NSString *const kMXEventIdentifierKey = @"kMXEventIdentifierKey";

NSString *const kMXEventIdentifierKey = @"kMXEventIdentifierKey";
NSString *const kMXMessageContentKeyVoiceMessageMSC2516 = @"org.matrix.msc2516.voice";
NSString *const kMXMessageContentKeyVoiceMessageMSC3245 = @"org.matrix.msc3245.voice";
NSString *const kMXMessageContentKeyVoiceMessage = @"m.voice";
NSString *const kMXMessageContentKeyExtensibleAudio = @"org.matrix.msc1767.audio";
NSString *const kMXMessageContentKeyExtensibleAudioDuration = @"duration";
NSString *const kMXMessageContentKeyExtensibleAudioWaveform = @"waveform";

#pragma mark - MXEvent
@interface MXEvent ()
Expand Down Expand Up @@ -433,6 +436,14 @@ - (BOOL)isReplyEvent
return self.eventType == MXEventTypeRoomMessage && self.content[@"m.relates_to"][@"m.in_reply_to"][@"event_id"] != nil;
}

- (BOOL)isVoiceMessage
{
NSString *msgtype = self.content[@"msgtype"];
return [msgtype isEqualToString:kMXMessageTypeAudio] && (self.content[kMXMessageContentKeyVoiceMessage] ||
self.content[kMXMessageContentKeyVoiceMessageMSC2516] ||
self.content[kMXMessageContentKeyVoiceMessageMSC3245]);
}

- (BOOL)contentHasBeenEdited
{
return self.unsignedData.relations.replace != nil;
Expand Down

0 comments on commit 3e8f798

Please sign in to comment.