<commit_before>'use strict';

var services = angular.module('Reader.services', []);

services.factory('reader', function ($http, $q) {
  
  var hasCategory = function (raw, categoryMatcher) {
    return raw.categories.some(function matches(category) {
      return categoryMatcher.test(category);
    });
  }
  
  // provides a token to a function, retries once on fail
  var withToken = (function () {    
    var token;
    
    var onRefreshSuccess = function (response) {
      token = response.data;
      return token;
    };
    
    var refreshToken = function () {
      var tokenUrl = 'https://www.google.com/reader/api/0/token';
      return $http.get(tokenUrl).then(onRefreshSuccess);
    };
    
    var ensureToken = function () {
      if(token === undefined) {
        return refreshToken();
      } else {
        return $q.when(token);
      }
    };
    
    return function (http) {
      var onHttpSuccess = function (value) {
        return value;
      };
      
      var retry = function (error) {
        return refreshToken().then(http);
      };
      
      return ensureToken().then(http).then(onHttpSuccess, retry);
    };
  }());
  
  var editTag = function (params) {
    var doEditTag = function (token) {
      params = params || {};
      params.output = 'json';
      params.T = token;
      var editTagUrl = 'https://www.google.com/reader/api/0/edit-tag';
      return $http.post(editTagUrl, '', { params: params });
    };
    
    return withToken(doEditTag);    
  };
  
  var addTag = function (itemId, tag) {
    return editTag({
      i: itemId,
      a: tag
    });
  };
  
  var removeTag = function (itemId, tag) {
    return editTag({
      i: itemId,
      r: tag
    });
  };
  
  var Item = function (raw) {
  // extract the content
    this.content = '';
    if (raw.content) {
      this.content = raw.content.content;
    } else if (raw.summary) {
      this.content = raw.summary.content;
    }
    
    // create a snippet
    var tmp = document.createElement('div');
    tmp.innerHTML = this.content;
    this.snippet = tmp.textContent || tmp.innerText;
    
    // create a viewmodel
    this.title = raw.title;
    this.url = raw.alternate[0].href;
    this.origin = {
      title: raw.origin.title,
      url: raw.origin.htmlUrl
    };
    this.read = hasCategory(raw, /^user\/[-\d]+\/state\/com\.google\/read$/);
    this.starred = hasCategory(raw, /^user\/[-\d]+\/state\/com\.google\/starred$/);
    this.id = raw.id;
  };
  
  Item.prototype.markAsRead = function () {
    self = this;
    addTag(this.id, 'user/-/state/com.google/read').then(function () {
      self.read = true;
      chrome.extension.sendMessage({ method: "updateUnreadCount" });
    });
  };
  
  Item.prototype.star = function () {
    self = this;
    addTag(this.id, 'user/-/state/com.google/starred').then(function () {
      self.starred = true;
    });
  };
  
  Item.prototype.unStar = function () {
    self = this;
    removeTag(this.id, 'user/-/state/com.google/starred').then(function () {
      self.starred = false;
    });
  };
  
  var List = function (url, n, params) {
    this.url = url;
    this.params = params || {};    
    this.items = [];
    this.continuation;
    this.loading = false;
    this.loadItems(n);
  };
  
  List.prototype.loadItems = function (n) {
    var deferred = $q.defer();
    
    this.params.output = 'json';
    this.params.ck = Date.now();
    this.params.client = 'notifier';
    this.params.n = n || 1;
    this.params.c = this.continuation;
    
    if (!this.loading) {
      this.loading = true;
      var _this = this;
      $http.get(this.url, {
        params: this.params
      }).success(function onSuccess(data) {
        console.log(data);
        data.items.forEach(function addToList(raw) {
          _this.items.push(new Item(raw));
        });
        _this.continuation = data.continuation;
        _this.loading = false;
        deferred.resolve(_this);
      }).error(function onError(error) {
        _this.loading = false;
        deferred.reject(error);
      });
    } else {
      deferred.reject(new Error('loading'));
    }
    
    return deferred.promise;
  };
  
  List.prototype.markAllAsRead = function (n) {
    var markAllAsReadUrl = 'https://www.google.com/reader/api/0/mark-all-as-read';
    
    var doMarkAllAsRead = function (token) {
      var params = {
        output: 'json',
        T: token,
        s: 'user/-/state/com.google/reading-list'
      }
      return $http.post(markAllAsReadUrl, '', { params: params });
    }
    
    var self = this;
    var markAllAsReadLocal = function () {
      self.items.forEach(function (item) {
        item.read = true;
      });
    };
    
    withToken(doMarkAllAsRead).then(markAllAsReadLocal);
  };
  
  List.prototype.canLoadMore = function (n) {
    return this.continuation || this.loading;
  };
  
  List.prototype.getIterator = function (index) {
    return new ListIterator(this, index);
  };
  
  var ListIterator = function (list, index) {
    this.list = list;
    this.index;
    this.current;
    this.previousDescr;
    this.nextDescr;
    this.init(index || 0);
  };
  
  ListIterator.prototype.init = function (index) {
    this.index = index;
    this.current = this.list.items[this.index];
    
    if (this.hasNext()) {<commit_msg>Update unread count when markAllAsRead<commit_after>'use strict';

var services = angular.module('Reader.services', []);

services.factory('reader', function ($http, $q) {
  
  var hasCategory = function (raw, categoryMatcher) {
    return raw.categories.some(function matches(category) {
      return categoryMatcher.test(category);
    });
  }
  
  // provides a token to a function, retries once on fail
  var withToken = (function () {    
    var token;
    
    var onRefreshSuccess = function (response) {
      token = response.data;
      return token;
    };
    
    var refreshToken = function () {
      var tokenUrl = 'https://www.google.com/reader/api/0/token';
      return $http.get(tokenUrl).then(onRefreshSuccess);
    };
    
    var ensureToken = function () {
      if(token === undefined) {
        return refreshToken();
      } else {
        return $q.when(token);
      }
    };
    
    return function (http) {
      var onHttpSuccess = function (value) {
        return value;
      };
      
      var retry = function (error) {
        return refreshToken().then(http);
      };
      
      return ensureToken().then(http).then(onHttpSuccess, retry);
    };
  }());
  
  var editTag = function (params) {
    var doEditTag = function (token) {
      params = params || {};
      params.output = 'json';
      params.T = token;
      var editTagUrl = 'https://www.google.com/reader/api/0/edit-tag';
      return $http.post(editTagUrl, '', { params: params });
    };
    
    return withToken(doEditTag);    
  };
  
  var addTag = function (itemId, tag) {
    return editTag({
      i: itemId,
      a: tag
    });
  };
  
  var removeTag = function (itemId, tag) {
    return editTag({
      i: itemId,
      r: tag
    });
  };
  
  var Item = function (raw) {
  // extract the content
    this.content = '';
    if (raw.content) {
      this.content = raw.content.content;
    } else if (raw.summary) {
      this.content = raw.summary.content;
    }
    
    // create a snippet
    var tmp = document.createElement('div');
    tmp.innerHTML = this.content;
    this.snippet = tmp.textContent || tmp.innerText;
    
    // create a viewmodel
    this.title = raw.title;
    this.url = raw.alternate[0].href;
    this.origin = {
      title: raw.origin.title,
      url: raw.origin.htmlUrl
    };
    this.read = hasCategory(raw, /^user\/[-\d]+\/state\/com\.google\/read$/);
    this.starred = hasCategory(raw, /^user\/[-\d]+\/state\/com\.google\/starred$/);
    this.id = raw.id;
  };
  
  Item.prototype.markAsRead = function () {
    self = this;
    addTag(this.id, 'user/-/state/com.google/read').then(function () {
      self.read = true;
      chrome.extension.sendMessage({ method: "updateUnreadCount" });
    });
  };
  
  Item.prototype.star = function () {
    self = this;
    addTag(this.id, 'user/-/state/com.google/starred').then(function () {
      self.starred = true;
    });
  };
  
  Item.prototype.unStar = function () {
    self = this;
    removeTag(this.id, 'user/-/state/com.google/starred').then(function () {
      self.starred = false;
    });
  };
  
  var List = function (url, n, params) {
    this.url = url;
    this.params = params || {};    
    this.items = [];
    this.continuation;
    this.loading = false;
    this.loadItems(n);
  };
  
  List.prototype.loadItems = function (n) {
    var deferred = $q.defer();
    
    this.params.output = 'json';
    this.params.ck = Date.now();
    this.params.client = 'notifier';
    this.params.n = n || 1;
    this.params.c = this.continuation;
    
    if (!this.loading) {
      this.loading = true;
      var _this = this;
      $http.get(this.url, {
        params: this.params
      }).success(function onSuccess(data) {
        console.log(data);
        data.items.forEach(function addToList(raw) {
          _this.items.push(new Item(raw));
        });
        _this.continuation = data.continuation;
        _this.loading = false;
        deferred.resolve(_this);
      }).error(function onError(error) {
        _this.loading = false;
        deferred.reject(error);
      });
    } else {
      deferred.reject(new Error('loading'));
    }
    
    return deferred.promise;
  };
  
  List.prototype.markAllAsRead = function (n) {
    var markAllAsReadUrl = 'https://www.google.com/reader/api/0/mark-all-as-read';
    
    var doMarkAllAsRead = function (token) {
      var params = {
        output: 'json',
        T: token,
        s: 'user/-/state/com.google/reading-list'
      }
      return $http.post(markAllAsReadUrl, '', { params: params });
    }
    
    var self = this;
    var markAllAsReadLocal = function () {
      chrome.extension.sendMessage({ method: "updateUnreadCount" });
      self.items.forEach(function (item) {
        item.read = true;
      });
    };
    
    withToken(doMarkAllAsRead).then(markAllAsReadLocal);
  };
  
  List.prototype.canLoadMore = function (n) {
    return this.continuation || this.loading;
  };
  
  List.prototype.getIterator = function (index) {
    return new ListIterator(this, index);
  };
  
  var ListIterator = function (list, index) {
    this.list = list;
    this.index;
    this.current;
    this.previousDescr;
    this.nextDescr;
    this.init(index || 0);
  };
  
  ListIterator.prototype.init = function (index) {
    this.index = index;
    this.current = this.list.items[this.index];
    
    if (this.hasNext()) {
{'fraction_non_alphanumeric': 0.15014903129657228, 'fraction_numerical': 0.0011177347242921013, 'mean_word_length': 2.2272317403065824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 6, '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': '305042', 'n_tokens_mistral': 3492, 'n_tokens_neox': 3452, 'n_words': 667}