Brion VIBBER has submitted this change and it was merged.

Change subject: Fixed account creation. Now works with updated api.
......................................................................


Fixed account creation. Now works with updated api.

Previously captcha solution was required before the api
would return things like "user name already taken"
messages. Now it's much nicer! Updated to work with new
api behavior.

Change-Id: I63b1977623bb9d9061f53c19c223a7b0f296d7c8
---
M Wikipedia-iOS.xcodeproj/project.pbxproj
M Wikipedia-iOS/Data/Operations/AccountCreationOp.h
M Wikipedia-iOS/Data/Operations/AccountCreationOp.m
M Wikipedia-iOS/Data/Operations/AccountCreationTokenOp.h
M Wikipedia-iOS/Data/Operations/AccountCreationTokenOp.m
M Wikipedia-iOS/View Controllers/AccountCreation/AccountCreationViewController.m
6 files changed, 104 insertions(+), 44 deletions(-)

Approvals:
  Brion VIBBER: Verified; Looks good to me, approved



diff --git a/Wikipedia-iOS.xcodeproj/project.pbxproj 
b/Wikipedia-iOS.xcodeproj/project.pbxproj
index f75d100..61afb6c 100644
--- a/Wikipedia-iOS.xcodeproj/project.pbxproj
+++ b/Wikipedia-iOS.xcodeproj/project.pbxproj
@@ -263,6 +263,8 @@
                04C695CF18ED08D900D9F2DA /* UIView+SearchSubviews.m in Sources 
*/ = {isa = PBXBuildFile; fileRef = 04C695CD18ED08D900D9F2DA /* 
UIView+SearchSubviews.m */; };
                04C695D218ED213000D9F2DA /* 
UIScrollView+NoHorizontalScrolling.m in Sources */ = {isa = PBXBuildFile; 
fileRef = 04C695D118ED213000D9F2DA /* UIScrollView+NoHorizontalScrolling.m */; 
};
                04C695D318ED213000D9F2DA /* 
UIScrollView+NoHorizontalScrolling.m in Sources */ = {isa = PBXBuildFile; 
fileRef = 04C695D118ED213000D9F2DA /* UIScrollView+NoHorizontalScrolling.m */; 
};
+               04C8781018F4A42700FA3B99 /* AccountCreationTokenOp.m in Sources 
*/ = {isa = PBXBuildFile; fileRef = 04C8780F18F4A42700FA3B99 /* 
AccountCreationTokenOp.m */; };
+               04C8781118F4A42700FA3B99 /* AccountCreationTokenOp.m in Sources 
*/ = {isa = PBXBuildFile; fileRef = 04C8780F18F4A42700FA3B99 /* 
AccountCreationTokenOp.m */; };
                04CF1CB6187C8F4400E9516F /* Languages in Resources */ = {isa = 
PBXBuildFile; fileRef = 04CF1CB5187C8F4400E9516F /* Languages */; };
                04CF1CB7187C8F4400E9516F /* Languages in Resources */ = {isa = 
PBXBuildFile; fileRef = 04CF1CB5187C8F4400E9516F /* Languages */; };
                04D122321899B8AC006B9A30 /* AlertWebView.m in Sources */ = {isa 
= PBXBuildFile; fileRef = 04D122311899B8AC006B9A30 /* AlertWebView.m */; };
@@ -570,6 +572,8 @@
                04C695CD18ED08D900D9F2DA /* UIView+SearchSubviews.m */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path 
= "UIView+SearchSubviews.m"; sourceTree = "<group>"; };
                04C695D018ED213000D9F2DA /* 
UIScrollView+NoHorizontalScrolling.h */ = {isa = PBXFileReference; fileEncoding 
= 4; lastKnownFileType = sourcecode.c.h; path = 
"UIScrollView+NoHorizontalScrolling.h"; sourceTree = "<group>"; };
                04C695D118ED213000D9F2DA /* 
UIScrollView+NoHorizontalScrolling.m */ = {isa = PBXFileReference; fileEncoding 
= 4; lastKnownFileType = sourcecode.c.objc; path = 
"UIScrollView+NoHorizontalScrolling.m"; sourceTree = "<group>"; };
+               04C8780E18F4A42700FA3B99 /* AccountCreationTokenOp.h */ = {isa 
= PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path 
= AccountCreationTokenOp.h; sourceTree = "<group>"; };
+               04C8780F18F4A42700FA3B99 /* AccountCreationTokenOp.m */ = {isa 
= PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; 
path = AccountCreationTokenOp.m; sourceTree = "<group>"; };
                04CF1CB5187C8F4400E9516F /* Languages */ = {isa = 
PBXFileReference; lastKnownFileType = folder; path = Languages; sourceTree = 
"<group>"; };
                04D122301899B8AC006B9A30 /* AlertWebView.h */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = 
AlertWebView.h; sourceTree = "<group>"; };
                04D122311899B8AC006B9A30 /* AlertWebView.m */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path 
= AlertWebView.m; sourceTree = "<group>"; };
@@ -1208,6 +1212,8 @@
                                043C668918BE9A8E00580E9B /* PreviewWikiTextOp.m 
*/,
                                0476967A18BBFC9400071963 /* AccountCreationOp.h 
*/,
                                0476967B18BBFC9400071963 /* AccountCreationOp.m 
*/,
+                               04C8780E18F4A42700FA3B99 /* 
AccountCreationTokenOp.h */,
+                               04C8780F18F4A42700FA3B99 /* 
AccountCreationTokenOp.m */,
                                04DB0BE418BC2E1E00B4BCF3 /* CaptchaResetOp.h */,
                                04DB0BE518BC2E1E00B4BCF3 /* CaptchaResetOp.m */,
                                04992BBE18B687AF00A6C22B /* SearchOp.h */,
@@ -1790,6 +1796,7 @@
                                04992BC018B687AF00A6C22B /* SearchOp.m in 
Sources */,
                                041A3B5E18E11ED90079FF1C /* LanguagesCell.m in 
Sources */,
                                04D34DD91863F70A00610A87 /* Section.m in 
Sources */,
+                               04C8781018F4A42700FA3B99 /* 
AccountCreationTokenOp.m in Sources */,
                                D42E75EB18D11237002EA7E5 /* MWLanguageInfo.m in 
Sources */,
                                04A81E8618871EB000EEBADB /* 
Section+ImageRecords.m in Sources */,
                                047FF5471889078C009DB293 /* Image+Convenience.m 
in Sources */,
@@ -1888,6 +1895,7 @@
                                04A97E8818B81D5D0046B166 /* 
AccountCreationViewController.m in Sources */,
                                047ED63A18C13E4900442BE3 /* PreviewWebView.m in 
Sources */,
                                04D149ED18888A0D006B4104 /* 
DownloadNonLeadSectionsOp.m in Sources */,
+                               04C8781118F4A42700FA3B99 /* 
AccountCreationTokenOp.m in Sources */,
                                04090A3C187FB7D000577EDF /* UIView+Debugging.m 
in Sources */,
                                04C43ABF183442FC006C643B /* 
NSRunLoop+TimeOutAndFlag.m in Sources */,
                                D4EE00BD1824459D0090790F /* PageTitleTests.m in 
Sources */,
diff --git a/Wikipedia-iOS/Data/Operations/AccountCreationOp.h 
b/Wikipedia-iOS/Data/Operations/AccountCreationOp.h
index 6748298..e7e95ca 100644
--- a/Wikipedia-iOS/Data/Operations/AccountCreationOp.h
+++ b/Wikipedia-iOS/Data/Operations/AccountCreationOp.h
@@ -4,7 +4,7 @@
 
 typedef enum {
     ACCOUNT_CREATION_ERROR_UNKNOWN = 0,
-    ACCOUNT_CREATION_ERROR_NEEDS_TOKEN = 1
+    ACCOUNT_CREATION_ERROR_NEEDS_CAPTCHA = 1
 } AccountCreationOpErrors;
 
 @interface AccountCreationOp : MWNetworkOp
@@ -16,7 +16,6 @@
             password: (NSString *) password
             realName: (NSString *) realName
                email: (NSString *) email
-               token: (NSString *) token
            captchaId: (NSString *) captchaId
          captchaWord: (NSString *) captchaWord
      completionBlock: (void (^)(NSString *))completionBlock
diff --git a/Wikipedia-iOS/Data/Operations/AccountCreationOp.m 
b/Wikipedia-iOS/Data/Operations/AccountCreationOp.m
index 6a3d5db..188e8ee 100644
--- a/Wikipedia-iOS/Data/Operations/AccountCreationOp.m
+++ b/Wikipedia-iOS/Data/Operations/AccountCreationOp.m
@@ -57,7 +57,6 @@
             password: (NSString *) password
             realName: (NSString *) realName
                email: (NSString *) email
-               token: (NSString *) token
            captchaId: (NSString *) captchaId
          captchaWord: (NSString *) captchaWord
      completionBlock: (void (^)(NSString *))completionBlock
@@ -72,7 +71,6 @@
         self.password = password ? password : @"";
         self.realName = realName ? realName : @"";
         self.email = email ? email : @"";
-        self.token = token ? token : @"";
         self.captchaId = captchaId ? captchaId : @"";
         self.captchaWord = captchaWord ? captchaWord : @"";
 
@@ -105,19 +103,20 @@
 
             if(weakSelf.jsonRetrieved[@"createaccount"]){
                 NSString *createAccountResult = 
weakSelf.jsonRetrieved[@"createaccount"][@"result"];
-                if ([createAccountResult isEqualToString:@"NeedToken"]) {
+                if ([createAccountResult isEqualToString:@"NeedCaptcha"]) {
                     NSMutableDictionary *errorDict = @{}.mutableCopy;
-                    
-                    errorDict[NSLocalizedDescriptionKey] = 
NSLocalizedString(@"account-creation-captcha-required", nil);
-                    
-                    // Make the capcha id and url available from the error.
-                    errorDict[@"captchaId"] = 
weakSelf.jsonRetrieved[@"createaccount"][@"captcha"][@"id"];
-                    errorDict[@"captchaUrl"] = 
weakSelf.jsonRetrieved[@"createaccount"][@"captcha"][@"url"];
-                    errorDict[@"token"] = 
weakSelf.jsonRetrieved[@"createaccount"][@"token"];
+
+                    if (weakSelf.jsonRetrieved[@"createaccount"][@"captcha"]) {
+                        errorDict[NSLocalizedDescriptionKey] = 
NSLocalizedString(@"account-creation-captcha-required", nil);
+                        
+                        // Make the capcha id and url available from the error.
+                        errorDict[@"captchaId"] = 
weakSelf.jsonRetrieved[@"createaccount"][@"captcha"][@"id"];
+                        errorDict[@"captchaUrl"] = 
weakSelf.jsonRetrieved[@"createaccount"][@"captcha"][@"url"]; 
+                    }
                     
                     // Set error condition so dependent ops don't even start 
and so the errorBlock below will fire.
                     weakSelf.error = [NSError errorWithDomain: @"Account 
Creation Op"
-                                                         code: 
ACCOUNT_CREATION_ERROR_NEEDS_TOKEN
+                                                         code: 
ACCOUNT_CREATION_ERROR_NEEDS_CAPTCHA
                                                      userInfo: errorDict];
                 }
             }
diff --git a/Wikipedia-iOS/Data/Operations/AccountCreationTokenOp.h 
b/Wikipedia-iOS/Data/Operations/AccountCreationTokenOp.h
index 5c38439..c281697 100644
--- a/Wikipedia-iOS/Data/Operations/AccountCreationTokenOp.h
+++ b/Wikipedia-iOS/Data/Operations/AccountCreationTokenOp.h
@@ -2,10 +2,12 @@
 
 #import "MWNetworkOp.h"
 
-@interface EditTokenOp : MWNetworkOp
+@interface AccountCreationTokenOp : MWNetworkOp
 
-- (id)initWithDomain: (NSString *)domain
-     completionBlock: (void (^)(NSDictionary *))completionBlock
+- (id)initWithDomain: (NSString *) domain
+            userName: (NSString *) userName
+            password: (NSString *) password
+     completionBlock: (void (^)(NSString *))completionBlock
       cancelledBlock: (void (^)(NSError *))cancelledBlock
           errorBlock: (void (^)(NSError *))errorBlock
 ;
diff --git a/Wikipedia-iOS/Data/Operations/AccountCreationTokenOp.m 
b/Wikipedia-iOS/Data/Operations/AccountCreationTokenOp.m
index f2ee07c..82da757 100644
--- a/Wikipedia-iOS/Data/Operations/AccountCreationTokenOp.m
+++ b/Wikipedia-iOS/Data/Operations/AccountCreationTokenOp.m
@@ -1,33 +1,58 @@
 //  Created by Monte Hurd on 1/16/14.
 
-#import "EditTokenOp.h"
+#import "AccountCreationTokenOp.h"
 #import "MWNetworkActivityIndicatorManager.h"
 #import "SessionSingleton.h"
 #import "NSURLRequest+DictionaryRequest.h"
 
-@implementation EditTokenOp
+@interface AccountCreationTokenOp()
 
-- (id)initWithDomain: (NSString *)domain
-     completionBlock: (void (^)(NSDictionary *))completionBlock
+@property (strong, nonatomic) NSString *domain;
+@property (strong, nonatomic) NSString *userName;
+@property (strong, nonatomic) NSString *password;
+
+@end
+
+@implementation AccountCreationTokenOp
+
+-(NSURLRequest *)getRequest
+{
+    NSMutableDictionary *parameters =
+        @{
+          @"action":     @"createaccount",
+          @"name":       self.userName,
+          @"password":   self.password,
+          @"language":
+              ([self.domain isEqualToString:@"test"]) ?
+                @"en"
+                :
+                self.domain,
+          @"format":     @"json"
+          }.mutableCopy;
+    
+    return [NSURLRequest postRequestWithURL: [[SessionSingleton 
sharedInstance] urlForDomain:self.domain]
+                                 parameters: parameters];
+}
+
+- (id)initWithDomain: (NSString *) domain
+            userName: (NSString *) userName
+            password: (NSString *) password
+     completionBlock: (void (^)(NSString *))completionBlock
       cancelledBlock: (void (^)(NSError *))cancelledBlock
           errorBlock: (void (^)(NSError *))errorBlock
+
 {
     self = [super init];
     if (self) {
 
-        NSMutableDictionary *parameters = [@{
-                                             @"action": @"tokens",
-                                             @"type": @"edit",
-                                             @"format": @"json"
-                                             }mutableCopy];
-        
-        self.request = [NSURLRequest postRequestWithURL: [[SessionSingleton 
sharedInstance] urlForDomain:domain]
-                                             parameters: parameters
-                        ];
-        
-        __weak EditTokenOp *weakSelf = self;
+        self.domain = domain ? domain : @"";
+        self.userName = userName ? userName : @"";
+        self.password = password ? password : @"";
+
+        __weak AccountCreationTokenOp *weakSelf = self;
         self.aboutToStart = ^{
             [[MWNetworkActivityIndicatorManager sharedManager] push];
+            weakSelf.request = [weakSelf getRequest];
         };
         self.completionBlock = ^(){
             [[MWNetworkActivityIndicatorManager sharedManager] pop];
@@ -37,14 +62,13 @@
                 return;
             }
             
-            // Check for error retrieving section zero data.
             if(weakSelf.jsonRetrieved[@"error"]){
                 NSMutableDictionary *errorDict = 
[weakSelf.jsonRetrieved[@"error"] mutableCopy];
                 
                 errorDict[NSLocalizedDescriptionKey] = errorDict[@"info"];
                 
                 // Set error condition so dependent ops don't even start and 
so the errorBlock below will fire.
-                weakSelf.error = [NSError errorWithDomain:@"Edit Token Op" 
code:001 userInfo:errorDict];
+                weakSelf.error = [NSError errorWithDomain:@"Account Creation 
Token Op" code:001 userInfo:errorDict];
             }
             
             if (weakSelf.error) {
@@ -52,7 +76,7 @@
                 return;
             }
             
-            NSDictionary *result = weakSelf.jsonRetrieved;
+            NSString *result = 
weakSelf.jsonRetrieved[@"createaccount"][@"token"];
             
             completionBlock(result);
         };
diff --git a/Wikipedia-iOS/View 
Controllers/AccountCreation/AccountCreationViewController.m 
b/Wikipedia-iOS/View Controllers/AccountCreation/AccountCreationViewController.m
index 0d35834..6cdaa97 100644
--- a/Wikipedia-iOS/View 
Controllers/AccountCreation/AccountCreationViewController.m
+++ b/Wikipedia-iOS/View 
Controllers/AccountCreation/AccountCreationViewController.m
@@ -6,6 +6,7 @@
 #import "SessionSingleton.h"
 #import "UIViewController+Alert.h"
 #import "AccountCreationOp.h"
+#import "AccountCreationTokenOp.h"
 #import "CaptchaResetOp.h"
 #import "UIScrollView+ScrollSubviewToLocation.h"
 #import "UIButton+ColorMask.h"
@@ -302,7 +303,6 @@
                                      password: self.passwordField.text
                                      realName: self.realnameField.text
                                         email: self.emailField.text
-                                        token: self.token
                                     captchaId: self.captchaId
                                   captchaWord: 
self.captchaViewController.captchaTextBox.text
      
@@ -324,23 +324,51 @@
                                   
                               } errorBlock: ^(NSError *error){
                                   [self showAlert:error.localizedDescription];
-                                  
-                                  if (error.code == 
ACCOUNT_CREATION_ERROR_NEEDS_TOKEN) {
-                                      self.captchaId = 
error.userInfo[@"captchaId"];
-                                      self.token = error.userInfo[@"token"];
-                                      
-                                      
dispatch_async(dispatch_get_main_queue(), ^(){
-                                          self.captchaUrl = 
error.userInfo[@"captchaUrl"];
-                                          self.showCaptchaContainer = YES;
-                                      });
+
+                                  switch (error.code) {
+                                      case 
ACCOUNT_CREATION_ERROR_NEEDS_CAPTCHA:{
+                                          self.captchaId = 
error.userInfo[@"captchaId"];
+                                          
dispatch_async(dispatch_get_main_queue(), ^(){
+                                              self.captchaUrl = 
error.userInfo[@"captchaUrl"];
+                                              self.showCaptchaContainer = YES;
+                                          });
+                                      }
+                                          break;
+                                      default:
+                                          break;
                                   }
+                                  
                                   isAleadySaving = NO;
                               }];
 
+    AccountCreationTokenOp *accountCreationTokenOp =
+    [[AccountCreationTokenOp alloc] initWithDomain: [SessionSingleton 
sharedInstance].domain
+                                          userName: self.usernameField.text
+                                          password: self.passwordField.text
+                                   completionBlock: ^(NSString *token){
+                                       accountCreationOp.token = token;
+                                   }
+                                    cancelledBlock: ^(NSError *error){
+                                        [self showAlert:@""];
+                                        isAleadySaving = NO;
+                                    }
+                                        errorBlock: ^(NSError *error){
+                                            [self 
showAlert:error.localizedDescription];
+                                            isAleadySaving = NO;
+                                        }];
+
     accountCreationOp.delegate = self;
+    accountCreationTokenOp.delegate = self;
+
+    // The accountCreationTokenOp needs to succeed before the 
accountCreationOp can begin.
+    [accountCreationOp addDependency:accountCreationTokenOp];
 
     [[QueuesSingleton sharedInstance].accountCreationQ cancelAllOperations];
+    
+    [QueuesSingleton sharedInstance].loginQ.suspended = YES;
+    [[QueuesSingleton sharedInstance].accountCreationQ 
addOperation:accountCreationTokenOp];
     [[QueuesSingleton sharedInstance].accountCreationQ 
addOperation:accountCreationOp];
+    [QueuesSingleton sharedInstance].loginQ.suspended = NO;
 }
 
 -(void)hide

-- 
To view, visit https://gerrit.wikimedia.org/r/124760
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I63b1977623bb9d9061f53c19c223a7b0f296d7c8
Gerrit-PatchSet: 1
Gerrit-Project: apps/ios/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Mhurd <mh...@wikimedia.org>
Gerrit-Reviewer: Brion VIBBER <br...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to