-> start chat with an user tries first to resume existing room before creating a new one.

This commit is contained in:
ylecollen 2015-01-27 16:27:13 +01:00
parent 69dbc492a5
commit 81f7fae056
4 changed files with 86 additions and 83 deletions

View file

@ -75,8 +75,16 @@ typedef enum : NSUInteger {
// return a MatrixIDs list of 1:1 room members // return a MatrixIDs list of 1:1 room members
- (NSArray*)oneToOneRoomMemberMatrixIDs; - (NSArray*)oneToOneRoomMemberMatrixIDs;
// search if a private room has been started with this user
// returns the room ID
// nil if not found
- (NSString*) privateRoomIdWith:(NSString*)otherMatrixID;
// check first if there no room between the both users
// if there is one, open it
// else create a new one
// create a private one to one chat room // create a private one to one chat room
- (void)createPrivateOneToOneRoomWith:(NSString*)otherMatrixID; - (void)startPrivateOneToOneRoomWith:(NSString*)otherMatrixID;
// Return the suitable url to display the content thumbnail into the provided view size // Return the suitable url to display the content thumbnail into the provided view size
// Note: the provided view size is supposed in points, this method will convert this size in pixels by considering screen scale // Note: the provided view size is supposed in points, this method will convert this size in pixels by considering screen scale
@ -87,9 +95,6 @@ typedef enum : NSUInteger {
- (NSString*)senderAvatarUrlForEvent:(MXEvent*)event withRoomState:(MXRoomState*)roomState; - (NSString*)senderAvatarUrlForEvent:(MXEvent*)event withRoomState:(MXRoomState*)roomState;
- (NSString*)displayTextForEvent:(MXEvent*)event withRoomState:(MXRoomState*)roomState inSubtitleMode:(BOOL)isSubtitle; - (NSString*)displayTextForEvent:(MXEvent*)event withRoomState:(MXRoomState*)roomState inSubtitleMode:(BOOL)isSubtitle;
// search if a 1:1 conversation has been started with this member
- (NSString*)getRoomStartedWithMember:(MXRoomMember*)roomMember;
// user power level in a dedicated room // user power level in a dedicated room
- (CGFloat)getPowerLevel:(MXRoomMember *)roomMember inRoom:(MXRoom *)room; - (CGFloat)getPowerLevel:(MXRoomMember *)roomMember inRoom:(MXRoom *)room;

View file

@ -542,35 +542,77 @@ static MatrixSDKHandler *sharedHandler = nil;
return matrixIDs; return matrixIDs;
} }
// search if a private room has been started with this user
// returns the room ID
// nil if not found
- (NSString*) privateRoomIdWith:(NSString*)otherMatrixID {
//
if (self.mxSession) {
// list the last messages of each room to get the rooms list
NSArray *recentEvents = [NSMutableArray arrayWithArray:[self.mxSession recentsWithTypeIn:self.eventsFilterForMessages]];
// loops
for (MXEvent *mxEvent in recentEvents) {
// get the dedicated mxRooms
MXRoom *mxRoom = [self.mxSession roomWithRoomId:mxEvent.roomId];
// accept only room with 2 users
if (mxRoom.state.members.count == 2) {
NSArray* roomMembers = mxRoom.state.members;
MXRoomMember* member1 = [roomMembers objectAtIndex:0];
MXRoomMember* member2 = [roomMembers objectAtIndex:1];
// check if they are the dedicated users
if (
([member1.userId isEqualToString:self.mxSession.myUser.userId] || [member1.userId isEqualToString:otherMatrixID]) &&
([member2.userId isEqualToString:self.mxSession.myUser.userId] || [member2.userId isEqualToString:otherMatrixID])) {
return mxRoom.state.roomId;
}
}
}
}
return nil;
}
// create a private one to one chat room // create a private one to one chat room
- (void)createPrivateOneToOneRoomWith:(NSString*)otherMatrixID { - (void)startPrivateOneToOneRoomWith:(NSString*)otherMatrixID {
// sanity check
if (self.mxRestClient) { if (self.mxRestClient) {
[self.mxRestClient createRoom:nil NSString* roomId = [self privateRoomIdWith:otherMatrixID];
visibility:kMXRoomVisibilityPrivate
roomAlias:nil // if the room exists
topic:nil if (roomId) {
success:^(MXCreateRoomResponse *response) { // open it
[[AppDelegate theDelegate].masterTabBarController showRoom:roomId];
// invite the other user only if it is defined and not onself } else {
if (otherMatrixID && ![self.userId isEqualToString:otherMatrixID]) { // create a new room
// add the user [self.mxRestClient createRoom:nil
[self.mxRestClient inviteUser:otherMatrixID toRoom:response.roomId success:^{ visibility:kMXRoomVisibilityPrivate
} failure:^(NSError *error) { roomAlias:nil
NSLog(@"%@ invitation failed (roomId: %@): %@", otherMatrixID, response.roomId, error); topic:nil
//Alert user success:^(MXCreateRoomResponse *response) {
[[AppDelegate theDelegate] showErrorAsAlert:error];
}]; // invite the other user only if it is defined and not onself
} if (otherMatrixID && ![self.userId isEqualToString:otherMatrixID]) {
// add the user
// Open created room [self.mxRestClient inviteUser:otherMatrixID toRoom:response.roomId success:^{
[[AppDelegate theDelegate].masterTabBarController showRoom:response.roomId]; } failure:^(NSError *error) {
NSLog(@"%@ invitation failed (roomId: %@): %@", otherMatrixID, response.roomId, error);
} failure:^(NSError *error) { //Alert user
NSLog(@"Create room failed: %@", error); [[AppDelegate theDelegate] showErrorAsAlert:error];
//Alert user }];
[[AppDelegate theDelegate] showErrorAsAlert:error]; }
}];
// Open created room
[[AppDelegate theDelegate].masterTabBarController showRoom:response.roomId];
} failure:^(NSError *error) {
NSLog(@"Create room failed: %@", error);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
}
} }
} }
@ -845,39 +887,6 @@ static MatrixSDKHandler *sharedHandler = nil;
return displayText; return displayText;
} }
// search if a conversation has been started with this user
- (NSString*) getRoomStartedWithMember:(MXRoomMember*)roomMember {
//
if (self.mxSession) {
// list the last messages of each room to get the rooms list
NSArray *recentEvents = [NSMutableArray arrayWithArray:[self.mxSession recentsWithTypeIn:self.eventsFilterForMessages]];
// loops
for (MXEvent *mxEvent in recentEvents) {
// get the dedicated mxRooms
MXRoom *mxRoom = [self.mxSession roomWithRoomId:mxEvent.roomId];
// accept only room with 2 users
if (mxRoom.state.members.count == 2) {
NSArray* roomMembers = mxRoom.state.members;
MXRoomMember* member1 = [roomMembers objectAtIndex:0];
MXRoomMember* member2 = [roomMembers objectAtIndex:1];
// check if they are the dedicated users
if (
([member1.userId isEqualToString:self.mxSession.myUser.userId] || [member1.userId isEqualToString:roomMember.userId]) &&
([member2.userId isEqualToString:self.mxSession.myUser.userId] || [member2.userId isEqualToString:roomMember.userId])) {
return mxRoom.state.roomId;
}
}
}
}
return nil;
}
- (NSUInteger) MXCacheSize { - (NSUInteger) MXCacheSize {
if (self.mxFileStore) { if (self.mxFileStore) {

View file

@ -405,7 +405,7 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl
if (matrixIDs.count == 1) { if (matrixIDs.count == 1) {
NSString* matrixID = [matrixIDs objectAtIndex:0]; NSString* matrixID = [matrixIDs objectAtIndex:0];
self.startChatMenu = [[MXCAlert alloc] initWithTitle:[NSString stringWithFormat:@"Start chat with %@", matrixID] message:nil style:MXCAlertStyleAlert]; self.startChatMenu = [[MXCAlert alloc] initWithTitle:[NSString stringWithFormat:@"Chat with %@", matrixID] message:nil style:MXCAlertStyleAlert];
[self.startChatMenu addActionWithTitle:@"Cancel" style:MXCAlertActionStyleDefault handler:^(MXCAlert *alert) { [self.startChatMenu addActionWithTitle:@"Cancel" style:MXCAlertActionStyleDefault handler:^(MXCAlert *alert) {
weakSelf.startChatMenu = nil; weakSelf.startChatMenu = nil;
@ -414,16 +414,16 @@ NSString *const kInvitationMessage = @"I'd like to chat with you with matrix. Pl
[self.startChatMenu addActionWithTitle:@"OK" style:MXCAlertActionStyleDefault handler:^(MXCAlert *alert) { [self.startChatMenu addActionWithTitle:@"OK" style:MXCAlertActionStyleDefault handler:^(MXCAlert *alert) {
weakSelf.startChatMenu = nil; weakSelf.startChatMenu = nil;
[mxHandler createPrivateOneToOneRoomWith:matrixID]; [mxHandler startPrivateOneToOneRoomWith:matrixID];
}]; }];
} else { } else {
self.startChatMenu = [[MXCAlert alloc] initWithTitle:[NSString stringWithFormat:@"Start chat with "] message:nil style:MXCAlertStyleActionSheet]; self.startChatMenu = [[MXCAlert alloc] initWithTitle:[NSString stringWithFormat:@"Chat with "] message:nil style:MXCAlertStyleActionSheet];
for(NSString* matrixID in matrixIDs) { for(NSString* matrixID in matrixIDs) {
[self.startChatMenu addActionWithTitle:matrixID style:MXCAlertActionStyleDefault handler:^(MXCAlert *alert) { [self.startChatMenu addActionWithTitle:matrixID style:MXCAlertActionStyleDefault handler:^(MXCAlert *alert) {
weakSelf.startChatMenu = nil; weakSelf.startChatMenu = nil;
[mxHandler createPrivateOneToOneRoomWith:matrixID]; [mxHandler startPrivateOneToOneRoomWith:matrixID];
}]; }];
} }

View file

@ -301,9 +301,9 @@
// offer to start a new chat only if the room is not a 1:1 room with this user // offer to start a new chat only if the room is not a 1:1 room with this user
// it does not make sense : it would open the same room // it does not make sense : it would open the same room
NSString* roomId = [mxHandler getRoomStartedWithMember:_mxRoomMember]; NSString* roomId = [mxHandler privateRoomIdWith:_mxRoomMember.userId];
if (![roomId isEqualToString:mxRoom.state.roomId]) { if (![roomId isEqualToString:mxRoom.state.roomId]) {
[buttonsTitles addObject:@"Start chat"]; [buttonsTitles addObject:@"Chat"];
} }
} }
@ -497,20 +497,9 @@
[[AppDelegate theDelegate] showErrorAsAlert:error]; [[AppDelegate theDelegate] showErrorAsAlert:error];
}]; }];
} else if ([text isEqualToString:@"Start chat"]) { } else if ([text isEqualToString:@"Chat"]) {
[self addPendingActionMask]; [self addPendingActionMask];
[[MatrixSDKHandler sharedHandler] startPrivateOneToOneRoomWith:_mxRoomMember.userId];
MatrixSDKHandler *mxHandler = [MatrixSDKHandler sharedHandler];
NSString* roomId = [mxHandler getRoomStartedWithMember:_mxRoomMember];
// if the room has already been started
if (roomId) {
// open it
[[AppDelegate theDelegate].masterTabBarController showRoom:roomId];
}
else {
[mxHandler createPrivateOneToOneRoomWith:_mxRoomMember.userId];
}
} }
} }
} }