Skip to content

Commit

Permalink
Merge pull request #1595 from matrix-org/yostyle/voice_message_with_a…
Browse files Browse the repository at this point in the history
…dditionnal_content

Support additional content in voice message
  • Loading branch information
yostyle authored Oct 4, 2022
2 parents f2d6895 + c8b92e7 commit 4d1c50a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
5 changes: 3 additions & 2 deletions MatrixSDK/Contrib/Swift/Data/MXRoom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ public extension MXRoom {

- parameters:
- localURL: the local filesystem path of the file to send.
- additionalContentParams: (optional) the additional parameters to the content.
- 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]
Expand All @@ -368,9 +369,9 @@ public extension MXRoom {
- returns: a `MXHTTPOperation` instance.
*/

@nonobjc @discardableResult func sendVoiceMessage(localURL: URL, mimeType: String?, duration: UInt, samples: [Float]?, threadId: String? = nil, localEcho: inout MXEvent?, completion: @escaping (_ response: MXResponse<String?>) -> Void) -> MXHTTPOperation {
@nonobjc @discardableResult func sendVoiceMessage(localURL: URL, additionalContentParams: [String : Any]?, mimeType: String?, duration: UInt, samples: [Float]?, threadId: String? = nil, 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, threadId: threadId, localEcho: &localEcho, success: currySuccess(completion), failure: curryFailure(completion), keepActualFilename: false)
return __sendVoiceMessage(localURL, additionalContentParams: additionalContentParams, mimeType: mimeType, duration: duration, samples: boxedSamples, threadId: threadId, localEcho: &localEcho, success: currySuccess(completion), failure: curryFailure(completion), keepActualFilename: false)
}

/**
Expand Down
28 changes: 28 additions & 0 deletions MatrixSDK/Data/MXRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,34 @@ FOUNDATION_EXPORT NSInteger const kMXRoomInvalidInviteSenderErrorCode;
failure:(void (^)(NSError *error))failure
keepActualFilename:(BOOL)keepActualName NS_REFINED_FOR_SWIFT;

/**
Send a voice message to the room.
@param fileLocalURL the local filesystem path of the file to send.
@param additionalContentParams (optional) the additional parameters to the content.
@param mimeType (optional) the mime type of the file. Defaults to `audio/ogg`
@param duration the length of the voice message in milliseconds
@param samples an array of floating point values normalized to [0, 1], boxed within NSNumbers
@param threadId the identifier of thread to send the event.
@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
@param failure A block object called when the operation fails.
@param keepActualName if YES, the filename in the local storage will be kept while sending.
@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)sendVoiceMessage:(NSURL*)fileLocalURL
additionalContentParams:(NSDictionary *)additionalContentParams
mimeType:(NSString*)mimeType
duration:(NSUInteger)duration
samples:(NSArray<NSNumber *> *)samples
threadId:(NSString*)threadId
localEcho:(MXEvent**)localEcho
success:(void (^)(NSString *eventId))success
failure:(void (^)(NSError *error))failure
keepActualFilename:(BOOL)keepActualName NS_REFINED_FOR_SWIFT;

/**
Cancel a sending operation.
Expand Down
32 changes: 30 additions & 2 deletions MatrixSDK/Data/MXRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,28 @@ - (MXHTTPOperation*)sendVoiceMessage:(NSURL*)fileLocalURL
success:(void (^)(NSString *eventId))success
failure:(void (^)(NSError *error))failure
keepActualFilename:(BOOL)keepActualName
{
return [self sendVoiceMessage:fileLocalURL
additionalContentParams:nil
mimeType:mimeType
duration:duration
samples:samples
threadId:threadId
localEcho:localEcho
success:success
failure:failure keepActualFilename:keepActualName];
}

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

Expand All @@ -1572,10 +1594,16 @@ - (MXHTTPOperation*)sendVoiceMessage:(NSURL*)fileLocalURL
[extensibleAudioContent setObject:scaledSamples forKey:kMXMessageContentKeyExtensibleAudioWaveform];
}

NSMutableDictionary *extensibleVoiceMessageContent = @{kMXMessageContentKeyVoiceMessageMSC3245 : @{},
kMXMessageContentKeyExtensibleAudioMSC1767: extensibleAudioContent}.mutableCopy;

if (additionalContentParams.count) {
[extensibleVoiceMessageContent addEntriesFromDictionary:additionalContentParams];
}

return [self _sendFile:fileLocalURL
msgType:kMXMessageTypeAudio
additionalTypes:@{kMXMessageContentKeyVoiceMessageMSC3245 : @{},
kMXMessageContentKeyExtensibleAudioMSC1767: extensibleAudioContent}
additionalTypes:extensibleVoiceMessageContent
mimeType:(mimeType ?: @"audio/ogg")
threadId:threadId
localEcho:localEcho
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1595.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support additional content in voice message.

0 comments on commit 4d1c50a

Please sign in to comment.