<commit_before>vitaApp.factory('SyncService', ['uuid', '$q', 'awsService', 
                                'PostService', 'FriendService', 'PlaceService',
function(uuid, $q, awsService,
         postService, friendService, placeService) {
  var syncService = {};
  
  syncService.AWSService = awsService;
  syncService.PostService = postService;
  
  syncService.SyncAll = function(lastSyncDate) {
    var deferred = $q.defer();
    
    syncService.SyncPosts(lastSyncDate)
    .then(function() {
      syncService.SyncPlaces(lastSyncDate);
    })
    .then(function() {
      syncService.SyncFriends(lastSyncDate);
    })
    .finally(function () {
      deferred.resolve();
    });
    
    return deferred.promise;
  };
  
  syncService.SyncPlaces = function(lastSyncDate) {
    var deferred = $q.defer();      
    
    syncService.AWSService.ListBucket('places/')
    .then(function (remotePlaces){
      //Get all local places
      placeService.GetPlaces()
      .then(function(localPlaces) {
        return syncService.ProcessArrays('places', localPlaces, remotePlaces);
      })
      .then(function() {
        return deferred.resolve();
      });           
    });
    
    return deferred.promise;
  };
  
  syncService.ProcessArrays = function(type, localsObjects, remoteObjects) {   
    var deferred = $q.defer(); 
    
    var localsToPush = [];
    var serverToGet = [];
    var i = 0;
    
    if (remoteObjects == null || remoteObjects.Contents.length == 0) {
      if (localsObjects != null && localsObjects.length > 0) {        
        //Push everything up
        for (i = 0; i < localsObjects.length; i++) {
          localsToPush.push(localsObjects[i]);
        }  
      }          
    }  else if(localsObjects == null || localsObjects.length == 0) {
      if (remoteObjects != null && remoteObjects.Contents.length > 0) {        
        //Bring everything down
        for (i = 0; i < remoteObjects.Contents.length; i++) {
          serverToGet.push(remoteObjects.Contents[i]);
        }  
      }              
    } else {
      //Loop through weed out what is already on server
      for (i = 0; i < remoteObjects.Contents.length ; i++) {
        var remoteObject = remoteObjects.Contents[i];
        
        var id = remoteObject.Key.slice(type.length + 1, -5);
        var lastModified = Date.parse(remoteObject.LastModified);
        
        var localObject = syncService.FindInList(id, localsObjects, true);
        if (localObject == null) {
          console.log("Remote Object to Get");
          serverToGet.push(remoteObject);          
        } else {
          //Check the date
          console.log("Found object checking date");
        }
      } 
      
      //remaining local objects are not on the server
      for (j = 0; j < localsObjects.length; j++) {
        var localObj = localsObjects[j];
        
        if (localObj != null) {
          localsToPush.push(localObj);
        }
      }
    } 
    
    syncService.SendAllToServer(type, localsToPush)
    .then(function() {
      deferred.resolve();
    })
    
    return deferred.promise;
  }
  
  syncService.SendAllToServer = function(type, items) {
    var deferred = $q.defer();
    var promises = [];
    
    for (i = 0; i < items.length; i++) {
      var item = items[i];
      
      var promise = syncService.AWSService.Save(type, item);
      promises.push(promise);
    }
    
    $q.all(promises)
    .then(function(values) {
      deferred.resolve();
    });
        
    return deferred.promise;
  }
  
  syncService.PullAllFromServer = function(type, items) {
    
  }
  
  syncService.FindInList = function(id, list, remove) {
    var output = null;
    var element = -1;
    
    for (var j = 0; j<list.length; j++) {
      var obj = list[j];
      
      if (obj._id == id) {
        output = obj;
        element = j;
        break;
      }
    }
    
    if (output != null && remove) {
      list.splice(element, 1);
    }
    
    return output;
  }
  
  syncService.SyncFriends = function(lastSyncDate) {
    var deferred = $q.defer();      
    
    syncService.AWSService.ListBucket('friends/')
    .then(function (data){
      
      return deferred.resolve();
    });
    
    return deferred.promise;
  };
  
  syncService.SyncPosts = function(lastSyncDate) {
    var deferred = $q.defer();      
    
    syncService.AWSService.ListBucket('posts/')
    .then(function (data){
      
      return deferred.resolve();
    });
    
    return deferred.promise;
  };
 
  return syncService;
}]);<commit_msg>syncing<commit_after>vitaApp.factory('SyncService', ['uuid', '$q', 'awsService', 
                                'PostService', 'FriendService', 'PlaceService',
function(uuid, $q, awsService,
         postService, friendService, placeService) {
  var syncService = {};
  
  syncService.AWSService = awsService;
  syncService.PostService = postService;
  
  syncService.SyncAll = function(lastSyncDate) {
    var deferred = $q.defer();
    
    syncService.SyncPosts(lastSyncDate)
    .then(function() {
      syncService.SyncPlaces(lastSyncDate);
    })
    .then(function() {
      syncService.SyncFriends(lastSyncDate);
    })
    .finally(function () {
      deferred.resolve();
    });
    
    return deferred.promise;
  };
  
  syncService.SyncPlaces = function(lastSyncDate) {
    var deferred = $q.defer();      
    
    syncService.AWSService.ListBucket('places/')
    .then(function (remotePlaces){
      //Get all local places
      placeService.GetPlaces()
      .then(function(localPlaces) {
        return syncService.ProcessArrays('places', localPlaces, remotePlaces);
      })
      .then(function() {
        return deferred.resolve();
      });           
    });
    
    return deferred.promise;
  };
  
  syncService.AddToArrayIfNotInternal = function(arrayObj, testObj) {
    if (testObj != null) {
      var id = testObj._id;
      if (id != null) {
        var idStart = id.substring(0, 7);
        if (idStart != "_design") {
          arrayObj.push(testObj);
        }
      }
    } 
  }
  
  syncService.ProcessArrays = function(type, localsObjects, remoteObjects) {   
    var deferred = $q.defer(); 
    
    var localsToPush = [];
    var serverToGet = [];
    var localObj = null;
    var i = 0;
    
    if (remoteObjects == null || remoteObjects.Contents.length == 0) {
      if (localsObjects != null && localsObjects.length > 0) {        
        //Push everything up
        for (i = 0; i < localsObjects.length; i++) {
          localObj = localsObjects[i];
          
          syncService.AddToArrayIfNotInternal(localsToPush, localObj);
        }  
      }          
    } else if (localsObjects == null || localsObjects.length == 0) {
      if (remoteObjects != null && remoteObjects.Contents.length > 0) {        
        //Bring everything down
        for (i = 0; i < remoteObjects.Contents.length; i++) {
          serverToGet.push(remoteObjects.Contents[i]);
        }  
      }              
    } else {
      //Loop through weed out what is already on server
      for (i = 0; i < remoteObjects.Contents.length ; i++) {
        var remoteObject = remoteObjects.Contents[i];
        
        var id = remoteObject.Key.slice(type.length + 1, -5);
        var lastModified = Date.parse(remoteObject.LastModified);
        
        var localObject = syncService.FindInList(id, localsObjects, true);
        if (localObject == null) {
          console.log("Remote Object to Get");
          serverToGet.push(remoteObject);          
        } else {
          //Check the date
          console.log("Found object checking date");
        }
      } 
      
      //remaining local objects are not on the server
      for (j = 0; j < localsObjects.length; j++) {
        localObj = localsObjects[j];
        
        syncService.AddToArrayIfNotInternal(localsToPush, localObj);
      }
    } 
    
    syncService.SendAllToServer(type, localsToPush)
    .then(function() {
      deferred.resolve();
    })
    
    return deferred.promise;
  }
  
  syncService.SendAllToServer = function(type, items) {
    var deferred = $q.defer();
    var promises = [];
    
    for (i = 0; i < items.length; i++) {
      var item = items[i];
            
      var promise = syncService.AWSService.Save(type, item);
      promises.push(promise);
    }
    
    $q.all(promises)
    .then(function(values) {
      deferred.resolve();
    });
        
    return deferred.promise;
  }
  
  syncService.PullAllFromServer = function(type, items) {
    
  }
  
  syncService.FindInList = function(id, list, remove) {
    var output = null;
    var element = -1;
    
    for (var j = 0; j<list.length; j++) {
      var obj = list[j];
      
      if (obj._id == id) {
        output = obj;
        element = j;
        break;
      }
    }
    
    if (output != null && remove) {
      list.splice(element, 1);
    }
    
    return output;
  }
  
  syncService.SyncFriends = function(lastSyncDate) {
    var deferred = $q.defer();      
    
    syncService.AWSService.ListBucket('friends/')
    .then(function (data){
      
      return deferred.resolve();
    });
    
    return deferred.promise;
  };
  
  syncService.SyncPosts = function(lastSyncDate) {
    var deferred = $q.defer();      
    
    syncService.AWSService.ListBucket('posts/')
    .then(function (data){
      
      return deferred.resolve();
    });
    
    return deferred.promise;
  };
 
  return syncService;
}]);
{'fraction_non_alphanumeric': 0.1263202365863963, 'fraction_numerical': 0.0033798056611744824, 'mean_word_length': 2.0407835581245988, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6779562', 'n_tokens_mistral': 2921, 'n_tokens_neox': 2846, 'n_words': 576}