mirror of
https://github.com/vector-im/element-ios.git
synced 2024-09-30 16:22:39 +00:00
-> start chat with an user tries first to resume existing room before creating a new one.
This commit is contained in:
parent
69dbc492a5
commit
81f7fae056
4 changed files with 86 additions and 83 deletions
|
@ -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;
|
||||
|
||||
|
|
|
@ -542,35 +542,77 @@ 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) {
|
||||
[self.mxRestClient createRoom:nil
|
||||
visibility:kMXRoomVisibilityPrivate
|
||||
roomAlias:nil
|
||||
topic:nil
|
||||
success:^(MXCreateRoomResponse *response) {
|
||||
|
||||
// invite the other user only if it is defined and not onself
|
||||
if (otherMatrixID && ![self.userId isEqualToString:otherMatrixID]) {
|
||||
// add the user
|
||||
[self.mxRestClient inviteUser:otherMatrixID toRoom:response.roomId success:^{
|
||||
} failure:^(NSError *error) {
|
||||
NSLog(@"%@ invitation failed (roomId: %@): %@", otherMatrixID, response.roomId, 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];
|
||||
}];
|
||||
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
|
||||
topic:nil
|
||||
success:^(MXCreateRoomResponse *response) {
|
||||
|
||||
// invite the other user only if it is defined and not onself
|
||||
if (otherMatrixID && ![self.userId isEqualToString:otherMatrixID]) {
|
||||
// add the user
|
||||
[self.mxRestClient inviteUser:otherMatrixID toRoom:response.roomId success:^{
|
||||
} failure:^(NSError *error) {
|
||||
NSLog(@"%@ invitation failed (roomId: %@): %@", otherMatrixID, response.roomId, 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;
|
||||
}
|
||||
|
||||
|
||||
// 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) {
|
||||
|
|
|
@ -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];
|
||||
}];
|
||||
}
|
||||
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue