-> 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
- (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
- (void)createPrivateOneToOneRoomWith:(NSString*)otherMatrixID;
- (void)startPrivateOneToOneRoomWith:(NSString*)otherMatrixID;
// 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
@ -87,9 +95,6 @@ typedef enum : NSUInteger {
- (NSString*)senderAvatarUrlForEvent:(MXEvent*)event withRoomState:(MXRoomState*)roomState;
- (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
- (CGFloat)getPowerLevel:(MXRoomMember *)roomMember inRoom:(MXRoom *)room;

View file

@ -542,10 +542,51 @@ static MatrixSDKHandler *sharedHandler = nil;
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
- (void)createPrivateOneToOneRoomWith:(NSString*)otherMatrixID {
// sanity check
- (void)startPrivateOneToOneRoomWith:(NSString*)otherMatrixID {
if (self.mxRestClient) {
NSString* roomId = [self privateRoomIdWith:otherMatrixID];
// if the room exists
if (roomId) {
// open it
[[AppDelegate theDelegate].masterTabBarController showRoom:roomId];
} else {
// create a new room
[self.mxRestClient createRoom:nil
visibility:kMXRoomVisibilityPrivate
roomAlias:nil
@ -573,6 +614,7 @@ static MatrixSDKHandler *sharedHandler = nil;
}];
}
}
}
- (NSString*)thumbnailURLForContent:(NSString*)contentURI inViewSize:(CGSize)viewSize withMethod:(MXThumbnailingMethod)thumbnailingMethod {
// Suppose this url is a matrix content uri, we use SDK to get the well adapted thumbnail from server
@ -845,39 +887,6 @@ static MatrixSDKHandler *sharedHandler = nil;
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 {
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) {
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) {
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) {
weakSelf.startChatMenu = nil;
[mxHandler createPrivateOneToOneRoomWith:matrixID];
[mxHandler startPrivateOneToOneRoomWith:matrixID];
}];
} 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) {
[self.startChatMenu addActionWithTitle:matrixID style:MXCAlertActionStyleDefault handler:^(MXCAlert *alert) {
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
// 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]) {
[buttonsTitles addObject:@"Start chat"];
[buttonsTitles addObject:@"Chat"];
}
}
@ -497,20 +497,9 @@
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
} else if ([text isEqualToString:@"Start chat"]) {
} else if ([text isEqualToString:@"Chat"]) {
[self addPendingActionMask];
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];
}
[[MatrixSDKHandler sharedHandler] startPrivateOneToOneRoomWith:_mxRoomMember.userId];
}
}
}