Browse Source

getNotificationServer add account

Marino Faggiana 6 years ago
parent
commit
021581a24a

+ 0 - 1
iOSClient/CCGlobal.h

@@ -219,7 +219,6 @@
 // Metadata.Net ACTION
 #define actionGetUserAndGroup                           @"getUserAndGroup"
 #define actionGetUserProfile                            @"getUserProfile"
-#define actionGetNotificationServer                     @"getNotificationServer"
 #define actionGetSharePermissionsFile                   @"getSharePermissionsFile"
 #define actionGetExternalSitesServer                    @"getExternalSitesServer"
 #define actionMiddlewarePing                            @"middlewarePing"

+ 57 - 64
iOSClient/Networking/NCService.swift

@@ -118,8 +118,63 @@ class NCService: NSObject, OCNetworkingDelegate {
                 // Read Notification
                 if (capabilities!.isNotificationServerEnabled) {
                     
-                    //metadataNet.action = actionGetNotificationServer
-                    //appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
+                    ocNetworking?.getNotificationServer(account!, completion: { (account, listOfNotifications, message, errorCode) in
+                        
+                        if (errorCode == 0 && account! == self.appDelegate.activeAccount) {
+                            
+                            DispatchQueue.global(qos: .default).async {
+                                
+                                let sortedListOfNotifications = (listOfNotifications! as NSArray).sortedArray(using: [
+                                    NSSortDescriptor(key: "date", ascending: false)
+                                    ])
+                                
+                                var old = ""
+                                var new = ""
+                                
+                                for notification in listOfNotifications! {
+                                    let id = (notification as! OCNotifications).idNotification
+                                    if let icon = (notification as! OCNotifications).icon {
+                                        NCUtility.sharedInstance.convertSVGtoPNGWriteToUserData(svgUrlString: icon, fileName: nil, width: 25, rewrite: false)
+                                    }
+                                    new = new + String(describing: id)
+                                }
+                                for notification in self.appDelegate.listOfNotifications! {
+                                    let id = (notification as! OCNotifications).idNotification
+                                    old = old + String(describing: id)
+                                }
+                                
+                                DispatchQueue.main.async {
+                                    
+                                    if (new != old) {
+                                        
+                                        self.appDelegate.listOfNotifications = NSMutableArray.init(array: sortedListOfNotifications)
+                                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationReloadData"), object: nil)
+                                        
+                                        // Update Main NavigationBar
+                                        if (self.appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
+                                            self.appDelegate.activeMain.setUINavigationBarDefault()
+                                        }
+                                    }
+                                }
+                            }
+                            
+                        } else {
+                            
+                            var error = ""
+                            if let message = message {
+                                error = "Get Notification Server failure error \(errorCode) \(message)"
+                            } else {
+                                error = "Get Notification Server failure error \(errorCode)"
+                            }
+                            
+                            NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Notification Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: self.appDelegate.activeUrl)
+                            
+                            // Update Main NavigationBar
+                            if (self.appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
+                                self.appDelegate.activeMain.setUINavigationBarDefault()
+                            }
+                        }
+                    })
                     
                 } else {
                     
@@ -337,67 +392,5 @@ class NCService: NSObject, OCNetworkingDelegate {
         }
     }
     
-    func getNotificationServerSuccessFailure(_ metadataNet: CCMetadataNet!, listOfNotifications: [Any]?, message: String?, errorCode: Int) {
     
-        // Check Active Account
-        if (metadataNet.account != appDelegate.activeAccount) {
-            return
-        }
-        
-        if (errorCode == 0) {
-            
-            DispatchQueue.global(qos: .default).async {
-
-                let sortedListOfNotifications = (listOfNotifications! as NSArray).sortedArray(using: [
-                    NSSortDescriptor(key: "date", ascending: false)
-                ])
-                
-                var old = ""
-                var new = ""
-                
-                for notification in listOfNotifications! {
-                    let id = (notification as! OCNotifications).idNotification
-                    if let icon = (notification as! OCNotifications).icon {
-                        NCUtility.sharedInstance.convertSVGtoPNGWriteToUserData(svgUrlString: icon, fileName: nil, width: 25, rewrite: false)
-                    }
-                    new = new + String(describing: id)
-                }
-                for notification in self.appDelegate.listOfNotifications! {
-                    let id = (notification as! OCNotifications).idNotification
-                    old = old + String(describing: id)
-                }
-                
-                
-                DispatchQueue.main.async {
-                
-                    if (new != old) {
-                    
-                        self.appDelegate.listOfNotifications = NSMutableArray.init(array: sortedListOfNotifications)
-                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationReloadData"), object: nil)
-                
-                        // Update Main NavigationBar
-                        if (self.appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
-                            self.appDelegate.activeMain.setUINavigationBarDefault()
-                        }
-                    }
-                }
-            }
-            
-        } else {
-            
-            var error = ""
-            if let message = message {
-                error = "Get Notification Server failure error \(errorCode) \(message)"
-            } else {
-                error = "Get Notification Server failure error \(errorCode)"
-            }
-            
-            NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Notification Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
-            
-            // Update Main NavigationBar
-            if (appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
-                appDelegate.activeMain.setUINavigationBarDefault()
-            }
-        }
-    }
 }

+ 2 - 1
iOSClient/Networking/OCNetworking.h

@@ -73,6 +73,8 @@
 
 - (void)getCapabilitiesOfServer:(NSString *)account completion:(void (^)(NSString *account, OCCapabilities *capabilities, NSString *message, NSInteger errorCode))completion;
 
+- (void)getNotificationServer:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, 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;
@@ -109,7 +111,6 @@
 - (void)getExternalSitesServerSuccessFailure:(CCMetadataNet *)metadataNet listOfExternalSites:(NSArray *)listOfExternalSites message:(NSString *)message errorCode:(NSInteger)errorCode;
 
 // Notification
-- (void)getNotificationServerSuccessFailure:(CCMetadataNet *)metadataNet listOfNotifications:(NSArray *)listOfNotifications message:(NSString *)message errorCode:(NSInteger)errorCode;
 - (void)setNotificationServerSuccessFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode;
 
 // User Profile

+ 17 - 26
iOSClient/Networking/OCNetworking.m

@@ -1573,53 +1573,44 @@
 #pragma mark ===== Notification =====
 #pragma --------------------------------------------------------------------------------------------
 
-- (void)getNotificationServer
+- (void)getNotificationServer:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, 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 getNotificationServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
-        
-        // Test active account
-        tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
-        if (![recordAccount.account isEqualToString:_metadataNet.account]) {
-            if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)])
-                [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:NSLocalizedString(@"_error_user_not_available_",  nil) errorCode:k_CCErrorUserNotAvailble];
-            
-            [self complete];
-            return;
-        }
+    [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
         
-        if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)])
-            [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:listOfNotifications message:nil errorCode:0];
-        
-        [self complete];
+        completion(account, listOfNotifications, 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(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)]) {
-            
-            if (errorCode == 503)
-                [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
-            else
-                [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications: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];
         
         // Activity
-        [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionGetNotification selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
+        [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionGetNotification selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:@""];
         
-        [self complete];
+        completion(account, nil, message, errorCode);
     }];
 }