Add support for sending messages
FossilOrigin-Name: cce4e8d73aefdb9a721a42b68b1c769c886d4fdb9a0a45d5bea30f41fe52d237
This commit is contained in:
parent
191a6412ae
commit
491d648e3a
6 changed files with 200 additions and 14 deletions
|
@ -36,11 +36,11 @@ typedef void (^mtx_client_login_block_t)(MTXClient *_Nullable client,
|
||||||
id _Nullable exception);
|
id _Nullable exception);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A block called when the device was logged out.
|
* @brief A block called when the response for an operation was received.
|
||||||
*
|
*
|
||||||
* @param exception `nil` on success, otherwise an exception
|
* @param exception `nil` on success, otherwise an exception
|
||||||
*/
|
*/
|
||||||
typedef void (^mtx_client_logout_block_t)(id _Nullable exception);
|
typedef void (^mtx_client_response_block_t)(id _Nullable exception);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A block called when the room list was fetched.
|
* @brief A block called when the room list was fetched.
|
||||||
|
@ -61,13 +61,6 @@ typedef void (^mtx_client_room_list_block_t)(
|
||||||
typedef void (^mtx_client_room_join_block_t)(OFString *_Nullable roomID,
|
typedef void (^mtx_client_room_join_block_t)(OFString *_Nullable roomID,
|
||||||
id _Nullable exception);
|
id _Nullable exception);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief A block called when a room was left.
|
|
||||||
*
|
|
||||||
* @param exception An exception if leaving the room failed
|
|
||||||
*/
|
|
||||||
typedef void (^mtx_client_room_leave_block_t)(id _Nullable exception);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A class that represents a client.
|
* @brief A class that represents a client.
|
||||||
*/
|
*/
|
||||||
|
@ -133,7 +126,7 @@ typedef void (^mtx_client_room_leave_block_t)(id _Nullable exception);
|
||||||
*
|
*
|
||||||
* @param block A block to call when logging out succeeded or failed
|
* @param block A block to call when logging out succeeded or failed
|
||||||
*/
|
*/
|
||||||
- (void)logOutWithBlock: (mtx_client_logout_block_t)block;
|
- (void)logOutWithBlock: (mtx_client_response_block_t)block;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Fetches the list of joined rooms.
|
* @brief Fetches the list of joined rooms.
|
||||||
|
@ -158,7 +151,18 @@ typedef void (^mtx_client_room_leave_block_t)(id _Nullable exception);
|
||||||
* @param block A block to call when the room was left
|
* @param block A block to call when the room was left
|
||||||
*/
|
*/
|
||||||
- (void)leaveRoom: (OFString *)roomID
|
- (void)leaveRoom: (OFString *)roomID
|
||||||
block: (mtx_client_room_leave_block_t)block;
|
block: (mtx_client_response_block_t)block;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sends the specified message to the specified room ID.
|
||||||
|
*
|
||||||
|
* @param message The message to send
|
||||||
|
* @param roomID The room ID to which to send the message
|
||||||
|
* @param block A block to call when the message was sent
|
||||||
|
*/
|
||||||
|
- (void)sendMessage: (OFString *)message
|
||||||
|
roomID: (OFString *)roomID
|
||||||
|
block: (mtx_client_response_block_t)block;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
OF_ASSUME_NONNULL_END
|
OF_ASSUME_NONNULL_END
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
#import "MTXLeaveRoomFailedException.h"
|
#import "MTXLeaveRoomFailedException.h"
|
||||||
#import "MTXLoginFailedException.h"
|
#import "MTXLoginFailedException.h"
|
||||||
#import "MTXLogoutFailedException.h"
|
#import "MTXLogoutFailedException.h"
|
||||||
|
#import "MTXSendMessageFailedException.h"
|
||||||
|
|
||||||
static void
|
static void
|
||||||
validateHomeserver(OFURL *homeserver)
|
validateHomeserver(OFURL *homeserver)
|
||||||
|
@ -188,7 +189,7 @@ validateHomeserver(OFURL *homeserver)
|
||||||
homeserver: _homeserver];
|
homeserver: _homeserver];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)logOutWithBlock: (mtx_client_logout_block_t)block
|
- (void)logOutWithBlock: (mtx_client_response_block_t)block
|
||||||
{
|
{
|
||||||
void *pool = objc_autoreleasePoolPush();
|
void *pool = objc_autoreleasePoolPush();
|
||||||
MTXRequest *request =
|
MTXRequest *request =
|
||||||
|
@ -290,7 +291,7 @@ validateHomeserver(OFURL *homeserver)
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)leaveRoom: (OFString *)roomID
|
- (void)leaveRoom: (OFString *)roomID
|
||||||
block: (mtx_client_room_leave_block_t)block
|
block: (mtx_client_response_block_t)block
|
||||||
{
|
{
|
||||||
void *pool = objc_autoreleasePoolPush();
|
void *pool = objc_autoreleasePoolPush();
|
||||||
MTXRequest *request = [self requestWithPath: [OFString
|
MTXRequest *request = [self requestWithPath: [OFString
|
||||||
|
@ -317,4 +318,40 @@ validateHomeserver(OFURL *homeserver)
|
||||||
|
|
||||||
objc_autoreleasePoolPop(pool);
|
objc_autoreleasePoolPop(pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)sendMessage: (OFString *)message
|
||||||
|
roomID: (OFString *)roomID
|
||||||
|
block: (mtx_client_response_block_t)block;
|
||||||
|
{
|
||||||
|
void *pool = objc_autoreleasePoolPush();
|
||||||
|
OFString *path = [OFString stringWithFormat:
|
||||||
|
@"/_matrix/client/r0/rooms/%@/send/m.room.message", roomID];
|
||||||
|
MTXRequest *request = [self requestWithPath: path];
|
||||||
|
request.method = OF_HTTP_REQUEST_METHOD_POST;
|
||||||
|
request.body = @{
|
||||||
|
@"msgtype": @"m.text",
|
||||||
|
@"body": message
|
||||||
|
};
|
||||||
|
[request performWithBlock: ^ (mtx_response_t response, int statusCode,
|
||||||
|
id exception) {
|
||||||
|
if (exception != nil) {
|
||||||
|
block(exception);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (statusCode != 200) {
|
||||||
|
block([MTXSendMessageFailedException
|
||||||
|
exceptionWithMessage: message
|
||||||
|
roomID: roomID
|
||||||
|
statusCode: statusCode
|
||||||
|
response: response
|
||||||
|
client: self]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
block(nil);
|
||||||
|
}];
|
||||||
|
|
||||||
|
objc_autoreleasePoolPop(pool);
|
||||||
|
}
|
||||||
@end
|
@end
|
||||||
|
|
51
src/exceptions/MTXSendMessageFailedException.h
Normal file
51
src/exceptions/MTXSendMessageFailedException.h
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Jonathan Schleifer <js@nil.im>
|
||||||
|
*
|
||||||
|
* https://fossil.nil.im/objmatrix
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice is present in all copies.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#import <ObjFW/ObjFW.h>
|
||||||
|
|
||||||
|
#import "MTXClientException.h"
|
||||||
|
|
||||||
|
OF_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@interface MTXSendMessageFailedException: MTXClientException
|
||||||
|
@property (readonly, nonatomic) OFString *message;
|
||||||
|
@property (readonly, nonatomic) OFString *roomID;
|
||||||
|
|
||||||
|
+ (instancetype)exceptionWithStatusCode: (int)statusCode
|
||||||
|
response: (mtx_response_t)response
|
||||||
|
client: (MTXClient *)client OF_UNAVAILABLE;
|
||||||
|
+ (instancetype)exceptionWithMessage: (OFString *)message
|
||||||
|
roomID: (OFString *)roomID
|
||||||
|
statusCode: (int)statusCode
|
||||||
|
response: (mtx_response_t)response
|
||||||
|
client: (MTXClient *)client;
|
||||||
|
- (instancetype)initWithStatusCode: (int)statusCode
|
||||||
|
response: (mtx_response_t)response
|
||||||
|
client: (MTXClient *)client OF_UNAVAILABLE;
|
||||||
|
- (instancetype)initWithMessage: (OFString *)message
|
||||||
|
roomID: (OFString *)roomID
|
||||||
|
statusCode: (int)statusCode
|
||||||
|
response: (mtx_response_t)response
|
||||||
|
client: (MTXClient *)client OF_DESIGNATED_INITIALIZER;
|
||||||
|
@end
|
||||||
|
|
||||||
|
OF_ASSUME_NONNULL_END
|
76
src/exceptions/MTXSendMessageFailedException.m
Normal file
76
src/exceptions/MTXSendMessageFailedException.m
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Jonathan Schleifer <js@nil.im>
|
||||||
|
*
|
||||||
|
* https://fossil.nil.im/objmatrix
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice is present in all copies.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#import "MTXSendMessageFailedException.h"
|
||||||
|
|
||||||
|
#import "MTXClient.h"
|
||||||
|
|
||||||
|
@implementation MTXSendMessageFailedException
|
||||||
|
+ (instancetype)exceptionWithMessage: (OFString *)message
|
||||||
|
roomID: (OFString *)roomID
|
||||||
|
statusCode: (int)statusCode
|
||||||
|
response: (mtx_response_t)response
|
||||||
|
client: (MTXClient *)client
|
||||||
|
{
|
||||||
|
return [[[self alloc] initWithMessage: message
|
||||||
|
roomID: roomID
|
||||||
|
statusCode: statusCode
|
||||||
|
response: response
|
||||||
|
client: client] autorelease];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (instancetype)initWithMessage: (OFString *)message
|
||||||
|
roomID: (OFString *)roomID
|
||||||
|
statusCode: (int)statusCode
|
||||||
|
response: (mtx_response_t)response
|
||||||
|
client: (MTXClient *)client
|
||||||
|
{
|
||||||
|
self = [super initWithStatusCode: statusCode
|
||||||
|
response: response
|
||||||
|
client: client];
|
||||||
|
|
||||||
|
@try {
|
||||||
|
_message = [message copy];
|
||||||
|
_roomID = [roomID copy];
|
||||||
|
} @catch (id e) {
|
||||||
|
[self release];
|
||||||
|
@throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)dealloc
|
||||||
|
{
|
||||||
|
[_message release];
|
||||||
|
[_roomID release];
|
||||||
|
|
||||||
|
[super dealloc];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (OFString *)description
|
||||||
|
{
|
||||||
|
return [OFString stringWithFormat:
|
||||||
|
@"Failed to send message to room %@ for %@: %@",
|
||||||
|
_roomID, self.client.userID, self.response];
|
||||||
|
}
|
||||||
|
@end
|
|
@ -8,7 +8,8 @@ SRCS = MTXClientException.m \
|
||||||
MTXJoinRoomFailedException.m \
|
MTXJoinRoomFailedException.m \
|
||||||
MTXLeaveRoomFailedException.m \
|
MTXLeaveRoomFailedException.m \
|
||||||
MTXLoginFailedException.m \
|
MTXLoginFailedException.m \
|
||||||
MTXLogoutFailedException.m
|
MTXLogoutFailedException.m \
|
||||||
|
MTXSendMessageFailedException.m
|
||||||
INCLUDES = ${SRCS:.m=.h}
|
INCLUDES = ${SRCS:.m=.h}
|
||||||
|
|
||||||
include ../../buildsys.mk
|
include ../../buildsys.mk
|
||||||
|
|
|
@ -90,6 +90,23 @@ OF_APPLICATION_DELEGATE(Tests)
|
||||||
|
|
||||||
of_log(@"Joined room %@", roomID);
|
of_log(@"Joined room %@", roomID);
|
||||||
|
|
||||||
|
[self sendMessage: roomID];
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)sendMessage: (OFString *)roomID
|
||||||
|
{
|
||||||
|
[_client sendMessage: @"ObjMatrix test successful!"
|
||||||
|
roomID: roomID
|
||||||
|
block: ^ (id exception) {
|
||||||
|
if (exception != nil) {
|
||||||
|
of_log(@"Failed to send message to room %@: %@",
|
||||||
|
roomID, exception);
|
||||||
|
[OFApplication terminateWithStatus: 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
of_log(@"Message sent to %@", roomID);
|
||||||
|
|
||||||
[self leaveRoom: roomID];
|
[self leaveRoom: roomID];
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue