Skip to content

Commit

Permalink
Send voice messages (#1119)
Browse files Browse the repository at this point in the history
element-hq/element-ios/issues/4090 - Added additional message types support and sendVoiceMessage method.
  • Loading branch information
stefanceriu committed Jun 9, 2021
1 parent 32614df commit 6cf7a91
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Changes to be released in next version
* MXRoomSummary: `lastMessageEvent` property removed for performance reasons (vector-im/element-ios/issues/4360).
* MXRoomSummary: All properties about lastMessage are moved into `lastMessage` property.
* MXSession: Does not compute anymore last events for every room summaries by default. Use -[MXSession eventWithEventId:inRoom:success:failure:] method to load the last event for a room summary.
* MXRoom: Added method for seding voice messages (vector-im/element-ios/issues/4090).

🗣 Translations
*
Expand Down
28 changes: 28 additions & 0 deletions MatrixSDK/Contrib/Swift/Data/MXRoom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,34 @@ public extension MXRoom {
return __sendAudioFile(localURL, mimeType: mimeType, localEcho: &localEcho, success: currySuccess(completion), failure: curryFailure(completion), keepActualFilename: false)
}

/**
Send a voice message to the room.
- parameters:
- localURL: the local filesystem path of the file to send.
- mimeType: (optional) the mime type of the file. Defaults to `audio/ogg`.
- localEcho: a pointer to a MXEvent object.
This pointer is set to an actual MXEvent object
containing the local created event which should be used to echo the message in
the messages list until the resulting event come through the server sync.
For information, the identifier of the created local event has the prefix
`kMXEventLocalEventIdPrefix`.
You may specify nil for this parameter if you do not want this information.
You may provide your own MXEvent object, in this case only its send state is updated.
- completion: A block object called when the operation completes.
- response: Provides the event id of the event generated on the home server on success.
- 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)
}

/**
Set the topic of the room.
Expand Down
5 changes: 3 additions & 2 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, video, location, file
case text, emote, notice, image, audio, voiceMessage, video, location, file
case custom(String)

public var identifier: String {
Expand All @@ -151,6 +151,7 @@ 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 @@ -159,7 +160,7 @@ public enum MXMessageType: Equatable, Hashable {
}

public init(identifier: String) {
let messages: [MXMessageType] = [.text, .emote, .notice, .image, .audio, .video, .location, .file]
let messages: [MXMessageType] = [.text, .emote, .notice, .image, .audio, .voiceMessage, .video, .location, .file]
self = messages.first(where: { $0.identifier == identifier }) ?? .custom(identifier)
}
}
Expand Down
20 changes: 20 additions & 0 deletions MatrixSDK/Data/MXRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,26 @@ FOUNDATION_EXPORT NSString *const kMXRoomDidFlushDataNotification;
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 mimeType (optional) the mime type of the file. Defaults to `audio/ogg`
@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
mimeType:(NSString*)mimeType
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
51 changes: 46 additions & 5 deletions MatrixSDK/Data/MXRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -1372,22 +1372,58 @@ - (MXHTTPOperation*)sendFile:(NSURL*)fileLocalURL
}

- (MXHTTPOperation*)sendAudioFile:(NSURL*)fileLocalURL
mimeType:(NSString*)mimeType
localEcho:(MXEvent**)localEcho
success:(void (^)(NSString *eventId))success
failure:(void (^)(NSError *error))failure
keepActualFilename:(BOOL)keepActualName
mimeType:(NSString*)mimeType
localEcho:(MXEvent**)localEcho
success:(void (^)(NSString *eventId))success
failure:(void (^)(NSError *error))failure
keepActualFilename:(BOOL)keepActualName
{
return [self sendFile:fileLocalURL msgType:kMXMessageTypeAudio mimeType:mimeType localEcho:localEcho success:success failure:failure keepActualFilename:keepActualName];
}

- (MXHTTPOperation*)sendVoiceMessage:(NSURL*)fileLocalURL
mimeType:(NSString*)mimeType
localEcho:(MXEvent**)localEcho
success:(void (^)(NSString *eventId))success
failure:(void (^)(NSError *error))failure
keepActualFilename:(BOOL)keepActualName
{
return [self _sendFile:fileLocalURL
msgType:kMXMessageTypeAudio
additionalTypes:@{kMXMessageTypeVoiceMessage : @{}}
mimeType:(mimeType ?: @"audio/ogg")
localEcho:localEcho
success:success
failure:failure
keepActualFilename:keepActualName];
}

- (MXHTTPOperation*)sendFile:(NSURL*)fileLocalURL
msgType:(NSString*)msgType
mimeType:(NSString*)mimeType
localEcho:(MXEvent**)localEcho
success:(void (^)(NSString *eventId))success
failure:(void (^)(NSError *error))failure
keepActualFilename:(BOOL)keepActualName
{
return [self _sendFile:fileLocalURL
msgType:msgType
additionalTypes:nil
mimeType:mimeType
localEcho:localEcho
success:success
failure:failure
keepActualFilename:keepActualName];
}

- (MXHTTPOperation*)_sendFile:(NSURL*)fileLocalURL
msgType:(NSString*)msgType
additionalTypes:(NSDictionary *)additionalTypes
mimeType:(NSString*)mimeType
localEcho:(MXEvent**)localEcho
success:(void (^)(NSString *eventId))success
failure:(void (^)(NSError *error))failure
keepActualFilename:(BOOL)keepActualName
{
__block MXRoomOperation *roomOperation;

Expand Down Expand Up @@ -1433,6 +1469,11 @@ - (MXHTTPOperation*)sendFile:(NSURL*)fileLocalURL
}
} mutableCopy];

if(additionalTypes.count)
{
[msgContent addEntriesFromDictionary:additionalTypes];
}

__block MXEvent *event;
__block id uploaderObserver;

Expand Down
1 change: 1 addition & 0 deletions MatrixSDK/JSONModels/MXEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ FOUNDATION_EXPORT NSString *const kMXMessageTypeEmote;
FOUNDATION_EXPORT NSString *const kMXMessageTypeNotice;
FOUNDATION_EXPORT NSString *const kMXMessageTypeImage;
FOUNDATION_EXPORT NSString *const kMXMessageTypeAudio;
FOUNDATION_EXPORT NSString *const kMXMessageTypeVoiceMessage;
FOUNDATION_EXPORT NSString *const kMXMessageTypeVideo;
FOUNDATION_EXPORT NSString *const kMXMessageTypeLocation;
FOUNDATION_EXPORT NSString *const kMXMessageTypeFile;
Expand Down
1 change: 1 addition & 0 deletions MatrixSDK/JSONModels/MXEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
NSString *const kMXMessageTypeNotice = @"m.notice";
NSString *const kMXMessageTypeImage = @"m.image";
NSString *const kMXMessageTypeAudio = @"m.audio";
NSString *const kMXMessageTypeVoiceMessage = @"org.matrix.msc2516.voice";
NSString *const kMXMessageTypeVideo = @"m.video";
NSString *const kMXMessageTypeLocation = @"m.location";
NSString *const kMXMessageTypeFile = @"m.file";
Expand Down

0 comments on commit 6cf7a91

Please sign in to comment.