Răsfoiți Sursa

setNotificationServer add account

Marino Faggiana 6 ani în urmă
părinte
comite
d6495dc8c1

+ 0 - 1
iOSClient/CCGlobal.h

@@ -221,7 +221,6 @@
 #define actionGetSharePermissionsFile                   @"getSharePermissionsFile"
 #define actionMiddlewarePing                            @"middlewarePing"
 #define actionSearch                                    @"search"
-#define actionSetNotificationServer                     @"setNotificationServer"
 #define actionShare                                     @"share"
 #define actionShareWith                                 @"shareWith"
 #define actionUnShare                                   @"unShare"

+ 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)setNotificationServer:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, 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;
@@ -112,9 +114,6 @@
 - (void)getSharePermissionsFileSuccess:(CCMetadataNet *)metadataNet permissions:(NSString *)permissions;
 - (void)getSharePermissionsFileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode;
 
-// Notification
-- (void)setNotificationServerSuccessFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode;
-
 // Search
 - (void)searchSuccessFailure:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas message:(NSString *)message errorCode:(NSInteger)errorCode;
 

+ 16 - 27
iOSClient/Networking/OCNetworking.m

@@ -1645,52 +1645,41 @@
     }];
 }
 
-- (void)setNotificationServer
+- (void)setNotificationServer:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
 {
+    tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
+    if (tableAccount == nil) {
+        completion(account, 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]];
     
-    NSString *type = _metadataNet.optionAny;
-    
-    [communication setNotificationServer:_metadataNet.serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
-        
-        // Test active account
-        tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
-        if (![recordAccount.account isEqualToString:_metadataNet.account]) {
-            if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)])
-                [self.delegate setNotificationServerSuccessFailure:_metadataNet message:NSLocalizedString(@"_error_user_not_available_", nil) errorCode:k_CCErrorUserNotAvailble];
-            
-            [self complete];
-            return;
-        }
+    [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
         
-        if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)])
-            [self.delegate setNotificationServerSuccessFailure:_metadataNet message:nil errorCode:0];
-        
-        [self complete];
+        completion(account, 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(setNotificationServerSuccessFailure:message:errorCode:)]) {
-            
-            if (errorCode == 503)
-                [self.delegate setNotificationServerSuccessFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
-            else
-                [self.delegate setNotificationServerSuccessFailure:_metadataNet 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, message, errorCode);
     }];
 }
 

+ 48 - 45
iOSClient/Notification/CCNotification.swift

@@ -84,14 +84,30 @@ class CCNotification: UITableViewController, OCNetworkingDelegate {
 
                 tableView.setEditing(false, animated: true)
 
-                let metadataNet = CCMetadataNet.init(account: self.appDelegate.activeAccount)!
-                
-                metadataNet.action = actionSetNotificationServer
-                metadataNet.optionString = "\(notification.idNotification)"
-                metadataNet.optionAny = "DELETE"
-                metadataNet.serverUrl = "\(self.appDelegate.activeUrl!)/\(k_url_acces_remote_notification_api)/\(metadataNet.optionString!)"
-
-                self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
+                let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: nil, withUserID: nil, withPassword: nil, withUrl: nil)
+                ocNetworking?.setNotificationServer(self.appDelegate.activeAccount, serverUrl: "\(self.appDelegate.activeUrl!)/\(k_url_acces_remote_notification_api)/\(notification.idNotification)", type: "DELETE", completion: { (account, message, errorCode) in
+                    
+                    if (errorCode == 0 && account! == self.appDelegate.activeAccount) {
+                        
+                        let listOfNotifications = self.appDelegate.listOfNotifications as NSArray as! [OCNotifications]
+                        
+                        if let index = listOfNotifications.index(where: {$0.idNotification == notification.idNotification})  {
+                            self.appDelegate.listOfNotifications.removeObject(at: index)
+                        }
+                        
+                        self.reloadDatasource()
+                        
+                        if self.appDelegate.listOfNotifications.count == 0 {
+                            self.viewClose()
+                        }
+                        
+                    } else {
+                        
+                        if (errorCode != 0) {
+                            self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
+                        }
+                    }
+                })
             }
             
             remove.backgroundColor = .red
@@ -113,14 +129,30 @@ class CCNotification: UITableViewController, OCNetworkingDelegate {
                             
                             tableView.setEditing(false, animated: true)
 
-                            let metadataNet = CCMetadataNet.init(account: self.appDelegate.activeAccount)!
-                            
-                            metadataNet.action = actionSetNotificationServer
-                            metadataNet.optionString = "\(notification.idNotification)"
-                            metadataNet.serverUrl =  (actionNotification as! OCNotificationsAction).link
-                            metadataNet.optionAny = (actionNotification as! OCNotificationsAction).type
-                            
-                            self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
+                            let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: nil, withUserID: nil, withPassword: nil, withUrl: nil)
+                            ocNetworking?.setNotificationServer(self.appDelegate.activeAccount, serverUrl: (actionNotification as! OCNotificationsAction).link, type: (actionNotification as! OCNotificationsAction).type, completion: { (account, message, errorCode) in
+                                
+                                if (errorCode == 0 && account! == self.appDelegate.activeAccount) {
+                                    
+                                    let listOfNotifications = self.appDelegate.listOfNotifications as NSArray as! [OCNotifications]
+                                    
+                                    if let index = listOfNotifications.index(where: {$0.idNotification == notification.idNotification})  {
+                                        self.appDelegate.listOfNotifications.removeObject(at: index)
+                                    }
+                                    
+                                    self.reloadDatasource()
+                                    
+                                    if self.appDelegate.listOfNotifications.count == 0 {
+                                        self.viewClose()
+                                    }
+                                    
+                                } else {
+                                    
+                                    if (errorCode != 0) {
+                                        self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
+                                    }
+                                }
+                            })
                         }
                     }
                 }
@@ -190,35 +222,6 @@ class CCNotification: UITableViewController, OCNetworkingDelegate {
         tableView.deselectRow(at: indexPath, animated: true)
     }
     
-    // MARK: - Networking delegate
-
-    func setNotificationServerSuccessFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
-        
-        // Check Active Account
-        if (metadataNet.account != appDelegate.activeAccount) {
-            return
-        }
-        
-        if (errorCode == 0) {
-            
-            let listOfNotifications = appDelegate.listOfNotifications as NSArray as! [OCNotifications]
-            
-            if let index = listOfNotifications.index(where: {$0.idNotification == Int(metadataNet.optionString)})  {
-                appDelegate.listOfNotifications.removeObject(at: index)
-            }
-            
-            reloadDatasource()
-            
-            if appDelegate.listOfNotifications.count == 0 {
-                viewClose()
-            }
-            
-        } else {
-            
-            appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-        }
-    }
-    
     // MARK: - Get Image from url
     
     func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _  response: URLResponse?, _ error: Error?) -> Void) {