Browse Source

getUserProfile add account

Marino Faggiana 6 years ago
parent
commit
14476f3796

+ 0 - 1
iOSClient/CCGlobal.h

@@ -218,7 +218,6 @@
 
 // Metadata.Net ACTION
 #define actionGetUserAndGroup                           @"getUserAndGroup"
-#define actionGetUserProfile                            @"getUserProfile"
 #define actionGetSharePermissionsFile                   @"getSharePermissionsFile"
 #define actionMiddlewarePing                            @"middlewarePing"
 #define actionSearch                                    @"search"

+ 62 - 75
iOSClient/Networking/NCService.swift

@@ -260,12 +260,68 @@ class NCService: NSObject, OCNetworkingDelegate {
             return
         }
         
-        guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
-            return
-        }
-        
-        metadataNet.action = actionGetUserProfile
-        appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
+        let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: nil, withUserID: nil, withPassword: nil, withUrl: nil)
+        ocNetworking?.getUserProfile(appDelegate.activeAccount, completion: { (account, userProfile, message, errorCode) in
+            
+            if (errorCode == 0 && account! == self.appDelegate.activeAccount) {
+                
+                // Update User (+ userProfile.id) & active account & account network
+                guard let tableAccount = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!) else {
+                    self.appDelegate.messageNotification("Accopunt", description: "Internal error : account not found on DB", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
+                    return
+                }
+                
+                let user = tableAccount.user
+                let url = tableAccount.url
+                
+                self.appDelegate.settingActiveAccount(tableAccount.account, activeUrl: tableAccount.url, activeUser: tableAccount.user, activeUserID: tableAccount.userID, activePassword: tableAccount.password)
+                
+                // Call func thath required the userdID
+                self.appDelegate.activeFavorites.listingFavorites()
+                self.appDelegate.activeMedia.searchPhotoVideo()
+                NCFunctionMain.sharedInstance.synchronizeOffline()
+                
+                DispatchQueue.global(qos: .default).async {
+                    
+                    let address = "\(self.appDelegate.activeUrl!)/index.php/avatar/\(self.appDelegate.activeUser!)/128".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
+                    let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(user, activeUrl: url) + "-avatar.png"
+                    
+                    guard let imageData = try? Data(contentsOf: URL(string: address)!) else {
+                        DispatchQueue.main.async {
+                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
+                        }
+                        return
+                    }
+                    
+                    DispatchQueue.main.async {
+                        
+                        guard let image = UIImage(data: imageData) else {
+                            try? FileManager.default.removeItem(atPath: fileNamePath)
+                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
+                            return
+                        }
+                        
+                        if let data = image.pngData() {
+                            try? data.write(to: URL(fileURLWithPath: fileNamePath))
+                        }
+                        
+                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
+                    }
+                }
+                
+            } else {
+                
+                var error = ""
+                if let message = message {
+                    error = "Get user profile failure error \(errorCode) \(message)"
+                } else {
+                    error = "Get user profile failure error \(errorCode)"
+                }
+                
+                NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get user profile Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: "")
+            }
+            
+        })
     }
     
     private func requestActivityServer() {
@@ -326,73 +382,4 @@ class NCService: NSObject, OCNetworkingDelegate {
             //
         })
     }
-    
-    //MARK: -
-    //MARK: Delegate Service API NC
-    
-    func getUserProfileSuccessFailure(_ metadataNet: CCMetadataNet!, userProfile: OCUserProfile?, message: String?, errorCode: Int) {
-        
-        // Check Active Account
-        if (metadataNet.account != appDelegate.activeAccount) {
-            return
-        }
-        
-        if (errorCode == 0) {
-            
-            // Update User (+ userProfile.id) & active account & account network
-            guard let tableAccount = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!) else {
-                appDelegate.messageNotification("Accopunt", description: "Internal error : account not found on DB", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
-                return
-            }
-            
-            let user = tableAccount.user
-            let url = tableAccount.url
-            
-            appDelegate.settingActiveAccount(tableAccount.account, activeUrl: tableAccount.url, activeUser: tableAccount.user, activeUserID: tableAccount.userID, activePassword: tableAccount.password)
-            
-            // Call func thath required the userdID
-            appDelegate.activeFavorites.listingFavorites()
-            appDelegate.activeMedia.searchPhotoVideo()
-            NCFunctionMain.sharedInstance.synchronizeOffline()
-            
-            DispatchQueue.global(qos: .default).async {
-                
-                let address = "\(self.appDelegate.activeUrl!)/index.php/avatar/\(self.appDelegate.activeUser!)/128".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
-                let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(user, activeUrl: url) + "-avatar.png"
-                
-                guard let imageData = try? Data(contentsOf: URL(string: address)!) else {
-                    DispatchQueue.main.async {
-                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
-                    }
-                    return
-                }
-                
-                DispatchQueue.main.async {
-                    
-                    guard let image = UIImage(data: imageData) else {
-                        try? FileManager.default.removeItem(atPath: fileNamePath)
-                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
-                        return
-                    }
-                
-                    if let data = image.pngData() {
-                        try? data.write(to: URL(fileURLWithPath: fileNamePath))
-                    }
-                
-                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
-                }
-            }
-            
-        } else {
-            
-            var error = ""
-            if let message = message {
-                error = "Get user profile failure error \(errorCode) \(message)"
-            } else {
-                error = "Get user profile failure error \(errorCode)"
-            }
-            
-            NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get user profile Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
-        }
-    }
 }

+ 2 - 3
iOSClient/Networking/OCNetworking.h

@@ -79,6 +79,8 @@
 
 - (void)getNotificationServer:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion;
 
+- (void)getUserProfile:(NSString *)account completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion;
+
 - (void)subscribingPushNotificationServer:(NSString *)url pushToken:(NSString *)pushToken Hash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey success:(void(^)(NSString *deviceIdentifier, NSString *deviceIdentifierSignature, NSString *publicKey))success failure:(void (^)(NSString *message, NSInteger errorCode))failure;
 
 - (void)unsubscribingPushNotificationServer:(NSString *)url deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure;
@@ -113,9 +115,6 @@
 // Notification
 - (void)setNotificationServerSuccessFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode;
 
-// User Profile
-- (void)getUserProfileSuccessFailure:(CCMetadataNet *)metadataNet userProfile:(OCUserProfile *)userProfile message:(NSString *)message errorCode:(NSInteger)errorCode;
-
 // Search
 - (void)searchSuccessFailure:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas message:(NSString *)message errorCode:(NSInteger)errorCode;
 

+ 16 - 25
iOSClient/Networking/OCNetworking.m

@@ -1819,50 +1819,41 @@
 #pragma mark =====  User Profile =====
 #pragma --------------------------------------------------------------------------------------------
 
-- (void)getUserProfile
+- (void)getUserProfile:(NSString *)account completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
 {
+    tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
+    if (tableAccount == nil) {
+        completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
+    }
+    
     OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
     
-    [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
+    [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
     [communication setUserAgent:[CCUtility getUserAgent]];
     
-    [communication getUserProfileServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
-        
-        // Test active account
-        tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
-        if (![recordAccount.account isEqualToString:_metadataNet.account]) {
-            if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)])
-                [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:NSLocalizedString(@"_error_user_not_available_", nil) errorCode:k_CCErrorUserNotAvailble];
-            
-            [self complete];
-            return;
-        }
+    [communication getUserProfileServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
         
-        if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)])
-            [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:userProfile message:nil errorCode:0];
-        
-        [self complete];
+        completion(account, userProfile, nil, 0);
         
     } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
         
+        NSString *message = @"";
         NSInteger errorCode = response.statusCode;
         if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
             errorCode = error.code;
         
         // Error
-        if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)]) {
-            
-            if (errorCode == 503)
-                [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
-            else
-                [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
+        if (errorCode == 503) {
+            message = NSLocalizedString(@"_server_error_retry_", nil);
+        } else {
+            message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
         }
-
+        
         // Request trusted certificated
         if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
             [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
         
-        [self complete];
+        completion(account, nil, message, errorCode);
     }];
 }