Skip to content

Commit

Permalink
MXServiceTerms: Implement agreeToTerms
Browse files Browse the repository at this point in the history
  • Loading branch information
manuroe committed Aug 13, 2019
1 parent 18bc082 commit 2b23921
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
2 changes: 2 additions & 0 deletions MatrixSDK/MXRestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ FOUNDATION_EXPORT NSString *const kMXAccountDataTypeDirect;
FOUNDATION_EXPORT NSString *const kMXAccountDataTypePushRules;
FOUNDATION_EXPORT NSString *const kMXAccountDataTypeIgnoredUserList;
FOUNDATION_EXPORT NSString *const kMXAccountDataTypeUserWidgets;
FOUNDATION_EXPORT NSString *const kMXAccountDataTypeAcceptedTerms;
FOUNDATION_EXPORT NSString *const kMXAccountDataTypeAcceptedTermsKey;

/**
Account data keys
Expand Down
2 changes: 2 additions & 0 deletions MatrixSDK/MXRestClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
NSString *const kMXAccountDataTypePushRules = @"m.push_rules";
NSString *const kMXAccountDataTypeDirect = @"m.direct";
NSString *const kMXAccountDataTypeUserWidgets = @"m.widgets";
NSString *const kMXAccountDataTypeAcceptedTerms = @"m.accepted_terms";
NSString *const kMXAccountDataTypeAcceptedTermsKey = @"accepted";

/**
Account data keys
Expand Down
3 changes: 1 addition & 2 deletions MatrixSDK/NotificationCenter/ServiceTerms/MXServiceTerms.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ NS_ASSUME_NONNULL_BEGIN
@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)terms:(void (^)(MXLoginTerms * _Nullable terms))success
- (MXHTTPOperation*)terms:(void (^)(MXLoginTerms * _Nullable terms, NSArray<NSString*> * _Nullable alreadyAcceptedTermsUrls))success
failure:(nullable void (^)(NSError * _Nonnull))failure;

/**
Accept terms by their urls.
@param termsUrls urls of the terms documents.
@param mxSession the Matrix session of the user.
@param success A block object called when the operation succeeds.
@param failure A block object called when the operation fails.
Expand Down
60 changes: 53 additions & 7 deletions MatrixSDK/NotificationCenter/ServiceTerms/MXServiceTerms.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import "MXServiceTermsRestClient.h"

#import "MXRestClient.h"
#import "MXTools.h"


NSString *const kMXIntegrationManagerAPIPrefixPathV1 = @"_matrix/integrations/v1";
Expand All @@ -42,26 +43,57 @@ - (instancetype)initWithBaseUrl:(NSString*)baseUrl serviceType:(MXServiceType)se
_mxSession = mxSession;
_accessToken = [accessToken copy];

_restClient = [[MXServiceTermsRestClient alloc] initWithBaseUrl:self.termsBaseUrl accessToken:accessToken];
_restClient = [[MXServiceTermsRestClient alloc] initWithBaseUrl:self.termsBaseUrl accessToken:@"bobo"];//accessToken];
}
return self;
}

- (MXHTTPOperation*)terms:(void (^)(MXLoginTerms * _Nullable terms))success
- (MXHTTPOperation*)terms:(void (^)(MXLoginTerms * _Nullable terms, NSArray<NSString*> * _Nullable alreadyAcceptedTermsUrls))success
failure:(nullable void (^)(NSError * _Nonnull))failure
{
return [_restClient terms:success failure:failure];
return [_restClient terms:^(MXLoginTerms * _Nullable terms) {
success(terms, self.acceptedTerms);
} failure:failure];
}

- (MXHTTPOperation *)agreeToTerms:(NSArray<NSString *> *)termsUrls
success:(void (^)(void))success
failure:(void (^)(NSError * _Nonnull))failure
{
NSParameterAssert(_mxSession && _accessToken);
if (!_mxSession || !_accessToken)
{
if (failure)
{
MXError *error = [[MXError alloc] initWithErrorCode:kMXSDKErrCodeStringMissingParameters error:@"No Matrix session or no access token"];
failure([error createNSError]);
}
return nil;
}

// First, send consents to the 3rd party server
MXHTTPOperation *operation;
MXWeakify(self);
operation = [_restClient agreeToTerms:termsUrls success:^{
MXStrongifyAndReturnIfNil(self);

// Then, store consents to the user account data
// Merge newly and previously accepted terms to store in account data
NSSet *acceptedTermsUrls = [NSSet setWithArray:termsUrls];
if (self.acceptedTerms)
{
acceptedTermsUrls = [acceptedTermsUrls setByAddingObjectsFromArray:self.acceptedTerms];
}

// TODO
failure([[NSError alloc] initWithDomain:@"toto" code:0 userInfo:nil]);
return nil;
NSDictionary *accountDataAcceptedTerms = @{
kMXAccountDataTypeAcceptedTermsKey: acceptedTermsUrls.allObjects
};

MXHTTPOperation *operation;
operation = [self.mxSession setAccountData:accountDataAcceptedTerms forType:kMXAccountDataTypeAcceptedTerms success:success failure:failure];

} failure:failure];

return operation;
}

#pragma mark - Private methods
Expand All @@ -86,4 +118,18 @@ - (NSString*)termsBaseUrl
return termsBaseUrl;
}


#pragma mark - Private methods

// Accepted terms in account data
- (nullable NSArray<NSString*> *)acceptedTerms
{
NSArray<NSString*> *acceptedTerms;

MXJSONModelSetArray(acceptedTerms,
[_mxSession.accountData accountDataForEventType:kMXAccountDataTypeAcceptedTerms][kMXAccountDataTypeAcceptedTermsKey]);

return acceptedTerms;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ - (MXHTTPOperation*)terms:(void (^)(MXLoginTerms * _Nullable terms))success
MXLoginTerms *terms;
MXJSONModelSetMXJSONModel(terms, MXLoginTerms.class, JSONResponse);

NSLog(@"#### %@", JSONResponse);

success(terms);
}
failure:^(NSError *error) {
Expand Down

0 comments on commit 2b23921

Please sign in to comment.