summaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-03-27 12:26:04 +0100
committerBernhard Posselt <nukeawhale@gmail.com>2013-03-27 12:26:04 +0100
commit02ae36eba33a5e0957defd4619d337bfdd0c178f (patch)
treed80f58cdf9eb774d00fc5fc322bf0750b644dab2 /js
parent89a1713f062cc78b727c6240a91408d91611dbab (diff)
fixed mark all unread serverside (was missing highestitemid, dont use lastmodified to compare for new versions but use the highest item id. if items are updated and the guidHash and feedId are the same then it will be deleted and newly inserted to make the lastmodified feasable
Diffstat (limited to 'js')
-rw-r--r--js/app/controllers/feedcontroller.coffee4
-rw-r--r--js/app/services/models/itemmodel.coffee38
-rw-r--r--js/public/app.js39
-rw-r--r--js/tests/controllers/feedcontrollerSpec.coffee8
-rw-r--r--js/tests/services/models/itemmodelSpec.coffee29
5 files changed, 97 insertions, 21 deletions
diff --git a/js/app/controllers/feedcontroller.coffee b/js/app/controllers/feedcontroller.coffee
index 1c04cd92e..14e1aea5b 100644
--- a/js/app/controllers/feedcontroller.coffee
+++ b/js/app/controllers/feedcontroller.coffee
@@ -179,7 +179,7 @@ angular.module('News').factory '_FeedController', ->
@_persistence.getItems(type, id, 0)
@_active.handle({id: id, type: type})
else
- lastModified = @_itemModel.getLastModified()
+ lastModified = @_itemModel.getHighestId()
@_persistence.getItems(type, id, 0, null, lastModified)
@@ -208,6 +208,8 @@ angular.module('News').factory '_FeedController', ->
feed = @_feedModel.getById(id)
if angular.isDefined(feed)
feed.unreadCount = 0
+ # TODO: also update items in the right field if id is the
+ # the same
highestItemId = @_itemModel.getHighestId()
@_persistence.setFeedRead(id, highestItemId)
when @_feedType.Folder
diff --git a/js/app/services/models/itemmodel.coffee b/js/app/services/models/itemmodel.coffee
index 18af373d9..16250ecf7 100644
--- a/js/app/services/models/itemmodel.coffee
+++ b/js/app/services/models/itemmodel.coffee
@@ -27,14 +27,40 @@ angular.module('News').factory '_ItemModel',
class ItemModel extends _Model
- getLastModified: ->
- query = new _MaximumQuery('lastModified')
- lastModified = @get(query)
+ constructor: ->
+ @_guidFeedIdHash = {}
+ super()
+
+
+ clear: ->
+ @_guidFeedIdHash = {}
+ super()
+
+
+ # items have two unique fields: feed_id and guidhash
+ # in case we get updated items with the same two fields we
+ # also need to update the field
+ add: (data, clearCache=true) ->
+ hash = data.feedId + '_' + data.guidHash
+ entry = @_guidFeedIdHash[hash]
+
+ # update entry if exists with same feedid and guidhash
+ if angular.isDefined(entry)
+
+ # first update id that could have changed
+ delete @_dataMap[entry.id]
+ @_dataMap[data.id] = entry
+
+ # now copy over the elements data attrs
+ for key, value of data
+ if key == 'feedId' or key == 'guidHash'
+ continue
+ else
+ entry[key] = value
- if angular.isDefined(lastModified)
- return lastModified.lastModified
else
- return null
+ @_guidFeedIdHash[hash] = data
+ super(data, clearCache)
getHighestId: ->
diff --git a/js/public/app.js b/js/public/app.js
index 9efc9aece..d2ff687ae 100644
--- a/js/public/app.js
+++ b/js/public/app.js
@@ -372,7 +372,7 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
type: type
});
} else {
- lastModified = this._itemModel.getLastModified();
+ lastModified = this._itemModel.getHighestId();
return this._persistence.getItems(type, id, 0, null, lastModified);
}
};
@@ -824,17 +824,38 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
__extends(ItemModel, _super);
function ItemModel() {
- return ItemModel.__super__.constructor.apply(this, arguments);
+ this._guidFeedIdHash = {};
+ ItemModel.__super__.constructor.call(this);
}
- ItemModel.prototype.getLastModified = function() {
- var lastModified, query;
- query = new _MaximumQuery('lastModified');
- lastModified = this.get(query);
- if (angular.isDefined(lastModified)) {
- return lastModified.lastModified;
+ ItemModel.prototype.clear = function() {
+ this._guidFeedIdHash = {};
+ return ItemModel.__super__.clear.call(this);
+ };
+
+ ItemModel.prototype.add = function(data, clearCache) {
+ var entry, hash, key, value, _results;
+ if (clearCache == null) {
+ clearCache = true;
+ }
+ hash = data.feedId + '_' + data.guidHash;
+ entry = this._guidFeedIdHash[hash];
+ if (angular.isDefined(entry)) {
+ delete this._dataMap[entry.id];
+ this._dataMap[data.id] = entry;
+ _results = [];
+ for (key in data) {
+ value = data[key];
+ if (key === 'feedId' || key === 'guidHash') {
+ continue;
+ } else {
+ _results.push(entry[key] = value);
+ }
+ }
+ return _results;
} else {
- return null;
+ this._guidFeedIdHash[hash] = data;
+ return ItemModel.__super__.add.call(this, data, clearCache);
}
};
diff --git a/js/tests/controllers/feedcontrollerSpec.coffee b/js/tests/controllers/feedcontrollerSpec.coffee
index 8ef621030..6ec75e920 100644
--- a/js/tests/controllers/feedcontrollerSpec.coffee
+++ b/js/tests/controllers/feedcontrollerSpec.coffee
@@ -152,7 +152,7 @@ describe '_FeedController', ->
@ActiveFeed.handle({id: 3, type: 3})
@scope.loadFeed(3, 3)
- expect(@persistence.getItems).toHaveBeenCalledWith(3, 3, 0, null, 323)
+ expect(@persistence.getItems).toHaveBeenCalledWith(3, 3, 0, null, 6)
it 'should send a get all items query when feed changed', =>
@@ -232,9 +232,9 @@ describe '_FeedController', ->
it 'should mark feed as read', =>
@persistence.setFeedRead = jasmine.createSpy('setFeedRead')
@FeedModel.add({id: 5, unreadCount:2, folderId: 2})
- @ItemModel.add({id: 6})
- @ItemModel.add({id: 3})
- @ItemModel.add({id: 2})
+ @ItemModel.add({id: 6, feedId: 5, guidHash: 'a'})
+ @ItemModel.add({id: 3, feedId: 5, guidHash: 'a1'})
+ @ItemModel.add({id: 2, feedId: 5, guidHash: 'a2'})
@scope.markAllRead(@FeedType.Feed, 5)
expect(@persistence.setFeedRead).toHaveBeenCalledWith(5, 6)
diff --git a/js/tests/services/models/itemmodelSpec.coffee b/js/tests/services/models/itemmodelSpec.coffee
index 061f84f01..7d929b223 100644
--- a/js/tests/services/models/itemmodelSpec.coffee
+++ b/js/tests/services/models/itemmodelSpec.coffee
@@ -29,4 +29,31 @@ describe '_ItemModel', ->
it 'should extend model', =>
- expect(new @_ItemModel instanceof @_Model).toBeTruthy() \ No newline at end of file
+ expect(new @_ItemModel instanceof @_Model).toBeTruthy()
+
+
+ it 'should also update items with the same feed id and guidhash', =>
+ model = new @_ItemModel()
+ item1 = {id: 4, guidHash: 'abc', feedId: 3}
+ model.add(item1)
+
+ expect(model.getById(4)).toBe(item1)
+
+ # normal id update
+ item2 = {id: 4, guidHash: 'abc', feedId: 4}
+ model.add(item2)
+ expect(model.size()).toBe(1)
+
+ # new feeds should be added normally if different
+ item3 = {id: 5, guidHash: 'abc', feedId: 6}
+ model.add(item3)
+ expect(model.size()).toBe(2)
+
+ # feed should be updated when guidhash and feedid the same
+ item4 = {id: 3, guidHash: 'abc', feedId: 6}
+ model.add(item4)
+ expect(model.getById(3).guidHash).toBe(item4.guidHash)
+ expect(model.getById(3).feedId).toBe(item4.feedId)
+ expect(model.getById(3).id).toBe(item4.id)
+ expect(model.getById(5)).toBe(undefined)
+ expect(model.size()).toBe(2) \ No newline at end of file