Skip to content

Commit

Permalink
Groups: self-management of membership of groups
Browse files Browse the repository at this point in the history
- MXRestClient: add methods to accept group invite and leave it.
- MXFileStore: bug fix: left groups were not removed correctly.

element-hq/riot-meta#114
  • Loading branch information
giomfo committed Dec 14, 2017
1 parent e430c80 commit cb4ac6d
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 7 deletions.
8 changes: 7 additions & 1 deletion MatrixSDK/Data/Store/MXFileStore/MXFileStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,13 @@ - (NSString*)groupFileForGroup:(NSString*)groupId forBackup:(BOOL)backup
{
if (backupEventStreamToken)
{
return [[[storeBackupPath stringByAppendingPathComponent:backupEventStreamToken] stringByAppendingPathComponent:kMXFileStoreGroupsFolder] stringByAppendingPathComponent:groupId];
NSString *groupBackupFolder = [[storeBackupPath stringByAppendingPathComponent:backupEventStreamToken] stringByAppendingPathComponent:kMXFileStoreGroupsFolder];
if (![NSFileManager.defaultManager fileExistsAtPath:groupBackupFolder])
{
[[NSFileManager defaultManager] createDirectoryAtPath:groupBackupFolder withIntermediateDirectories:YES attributes:nil error:nil];
}

return [groupBackupFolder stringByAppendingPathComponent:groupId];
}
else
{
Expand Down
27 changes: 27 additions & 0 deletions MatrixSDK/MXRestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -1959,4 +1959,31 @@ typedef enum : NSUInteger
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;

#pragma mark - Groups

/**
Accept the invitation to join a group.
@param groupId the id of the group to join.
@param success A block object called when the operation succeeds.
@param failure A block object called when the operation fails.
@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)acceptGroupInvite:(NSString*)groupId
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;
/**
Leave a group.
@param groupId the id of the group to leave.
@param success A block object called when the operation succeeds.
@param failure A block object called when the operation fails.
@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)leaveGroup:(NSString*)groupId
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;

@end
74 changes: 74 additions & 0 deletions MatrixSDK/MXRestClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -5393,5 +5393,79 @@ - (MXHTTPOperation*)deleteDeviceByDeviceId:(NSString *)deviceId

}];
}

#pragma mark - Groups
- (MXHTTPOperation*)acceptGroupInvite:(NSString*)groupId
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
{
return [self doGroupMembershipRequest:groupId
membership:@"accept_invite"
parameters:nil
success:success failure:failure];
}

- (MXHTTPOperation*)leaveGroup:(NSString*)groupId
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
{
return [self doGroupMembershipRequest:groupId
membership:@"leave"
parameters:nil
success:success failure:failure];
}

// Generic methods to change group membership
- (MXHTTPOperation*)doGroupMembershipRequest:(NSString*)grouId
membership:(NSString*)membership
parameters:(NSDictionary*)parameters
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
{
NSString *path = [NSString stringWithFormat:@"%@/groups/%@/self/%@", apiPathPrefix, [grouId stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], membership];

// A body is required even if empty
if (nil == parameters)
{
parameters = @{};
}

return [httpClient requestWithMethod:@"PUT"
path:path
parameters:parameters
success:^(NSDictionary *JSONResponse) {
if (success && processingQueue)
{
// Use here the processing queue in order to keep the server response order
dispatch_async(processingQueue, ^{

if (completionQueue)
{
dispatch_async(completionQueue, ^{

success();

});
}

});
}
}
failure:^(NSError *error) {
if (failure && processingQueue)
{
dispatch_async(processingQueue, ^{

if (completionQueue)
{
dispatch_async(completionQueue, ^{
failure(error);
});
}

});
}
}];
}

@end
13 changes: 13 additions & 0 deletions MatrixSDK/MXSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,19 @@ typedef void (^MXOnBackgroundSyncFail)(NSError *error);
*/
- (NSArray<MXGroup*>*)groups;

/**
Accept the invitation to join a group.
@param groupId the group id
@param success A block object called when the operation succeeds.
@param failure A block object called when the operation fails.
@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)acceptGroupInvite:(NSString*)groupId
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;

/**
Leave a group.
Expand Down
49 changes: 43 additions & 6 deletions MatrixSDK/MXSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -1913,16 +1913,53 @@ - (MXGroup *)groupWithGroupId:(NSString*)groupId
return _store.groups;
}

- (MXHTTPOperation*)acceptGroupInvite:(NSString*)groupId
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
{
return [matrixRestClient acceptGroupInvite:groupId success:^{

[self didJoinGroupWithId:groupId notify:YES];
if (success)
{
success();
}

} failure:failure];
}

- (MXHTTPOperation*)leaveGroup:(NSString*)groupId
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
{
// Not supported yet
if (failure)
{
failure ([NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedDescriptionKey:@"not_supported_yet"}]);
}
return nil;
return [matrixRestClient leaveGroup:groupId success:^{

// Check the group has been removed before calling the success callback
// This is automatically done when the homeserver sends the information.
if ([self groupWithGroupId:groupId])
{
// The group is still here, wait for the server sync
__block __weak id observer = [[NSNotificationCenter defaultCenter] addObserverForName:kMXSessionDidLeaveGroupNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {

if ([groupId isEqualToString:note.userInfo[kMXSessionNotificationGroupIdKey]])
{
[[NSNotificationCenter defaultCenter] removeObserver:observer];
if (success)
{
success();
}
}
}];
}
else
{
if (success)
{
success();
}
}

} failure:failure];
}

- (MXGroup *)didJoinGroupWithId:(NSString *)groupId notify:(BOOL)notify
Expand Down

0 comments on commit cb4ac6d

Please sign in to comment.