Close #934. Remove feed item read state syncing

This commit is contained in:
Adomas Venčkauskas 2016-03-31 12:37:01 +03:00
parent fabc2ba6a2
commit 2b41f7af1d
7 changed files with 22 additions and 125 deletions

View file

@ -331,33 +331,14 @@ Zotero.Feed.prototype.erase = Zotero.Promise.coroutine(function* (options = {})
yield Zotero.Feed._super.prototype.erase.call(this, options); yield Zotero.Feed._super.prototype.erase.call(this, options);
}); });
Zotero.Feed.prototype.getSyncedSettings = function () { Zotero.Feed.prototype.storeSyncedSettings = Zotero.Promise.coroutine(function* () {
if (!this._syncedSettings) {
let syncedFeeds = Zotero.SyncedSettings.get(Zotero.Libraries.userLibraryID, 'feeds') || {}; let syncedFeeds = Zotero.SyncedSettings.get(Zotero.Libraries.userLibraryID, 'feeds') || {};
this._syncedSettings = syncedFeeds[this.url]; syncedFeeds[this.url] = {
}
if (!this._syncedSettings) {
this._syncedSettings = {
url: this.url, url: this.url,
name: this.name, name: this.name,
cleanupAfter: this.cleanupAfter, cleanupAfter: this.cleanupAfter,
refreshInterval: this.refreshInterval, refreshInterval: this.refreshInterval
markedAsRead: {}
}; };
}
return this._syncedSettings;
};
Zotero.Feed.prototype.setSyncedSettings = Zotero.Promise.coroutine(function* (syncedSettings, store=false) {
this._syncedSettings = syncedSettings;
if (store) {
return this.storeSyncedSettings();
}
});
Zotero.Feed.prototype.storeSyncedSettings = Zotero.Promise.coroutine(function* () {
let syncedFeeds = Zotero.SyncedSettings.get(Zotero.Libraries.userLibraryID, 'feeds') || {};
syncedFeeds[this.url] = this.getSyncedSettings();
return Zotero.SyncedSettings.set(Zotero.Libraries.userLibraryID, 'feeds', syncedFeeds); return Zotero.SyncedSettings.set(Zotero.Libraries.userLibraryID, 'feeds', syncedFeeds);
}); });
@ -405,7 +386,6 @@ Zotero.Feed.prototype.clearExpiredItems = Zotero.Promise.coroutine(function* (it
Zotero.debug("Error clearing expired feed items"); Zotero.debug("Error clearing expired feed items");
Zotero.debug(e); Zotero.debug(e);
} }
return this.storeSyncedSettings();
}); });
Zotero.Feed.prototype._updateFeed = Zotero.Promise.coroutine(function* () { Zotero.Feed.prototype._updateFeed = Zotero.Promise.coroutine(function* () {
@ -534,9 +514,3 @@ Zotero.Feed.prototype.updateUnreadCount = Zotero.Promise.coroutine(function* ()
Zotero.Notifier.trigger('unreadCountUpdated', 'feed', this.id); Zotero.Notifier.trigger('unreadCountUpdated', 'feed', this.id);
} }
}); });
Zotero.Feed.prototype.updateFromJSON = Zotero.Promise.coroutine(function* (json) {
yield this.updateFeed();
yield Zotero.FeedItems.markAsReadByGUID(Object.keys(json.markedAsRead));
yield this.updateUnreadCount();
});

View file

@ -182,9 +182,6 @@ Zotero.FeedItem.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
Zotero.FeedItem.prototype._finalizeErase = Zotero.Promise.coroutine(function* () { Zotero.FeedItem.prototype._finalizeErase = Zotero.Promise.coroutine(function* () {
// Set for syncing // Set for syncing
let feed = Zotero.Feeds.get(this.libraryID); let feed = Zotero.Feeds.get(this.libraryID);
let syncedSettings = feed.getSyncedSettings();
delete syncedSettings.markedAsRead[this.guid];
yield feed.setSyncedSettings(syncedSettings);
return Zotero.FeedItem._super.prototype._finalizeErase.apply(this, arguments); return Zotero.FeedItem._super.prototype._finalizeErase.apply(this, arguments);
}); });
@ -195,18 +192,9 @@ Zotero.FeedItem.prototype.toggleRead = Zotero.Promise.coroutine(function* (state
if (changed) { if (changed) {
this.isRead = state; this.isRead = state;
// Set for syncing
let feed = Zotero.Feeds.get(this.libraryID);
let syncedSettings = feed.getSyncedSettings();
if (state) {
syncedSettings.markedAsRead[this.guid] = true;
} else {
delete syncedSettings.markedAsRead[this.guid];
}
yield feed.setSyncedSettings(syncedSettings, true);
yield this.saveTx(); yield this.saveTx();
let feed = Zotero.Feeds.get(this.libraryID);
yield feed.updateUnreadCount(); yield feed.updateUnreadCount();
} }
}); });

View file

@ -109,7 +109,7 @@ Zotero.FeedItems = new Proxy(function() {
}); });
/** /**
* Used on restore from sync * Currently not used
*/ */
this.markAsReadByGUID = Zotero.Promise.coroutine(function* (guids) { this.markAsReadByGUID = Zotero.Promise.coroutine(function* (guids) {
if (! Array.isArray(guids)) { if (! Array.isArray(guids)) {
@ -152,17 +152,8 @@ Zotero.FeedItems = new Proxy(function() {
for (let i=0; i<items.length; i++) { for (let i=0; i<items.length; i++) {
items[i].isRead = state; items[i].isRead = state;
// Set for syncing yield items[i].save();
let feed = Zotero.Feeds.get(items[i].libraryID); let feed = Zotero.Feeds.get(items[i].libraryID);
let syncedSettings = feed.getSyncedSettings();
if (state) {
syncedSettings.markedAsRead[items[i].guid] = true;
} else {
delete syncedSettings.markedAsRead[items[i].guid];
}
yield feed.setSyncedSettings(syncedSettings);
yield items[i].save({skipEditCheck: true});
feedsToUpdate.add(feed); feedsToUpdate.add(feed);
} }
}); });

View file

@ -93,12 +93,10 @@ Zotero.Feeds = new function() {
json = syncedFeeds; json = syncedFeeds;
} }
yield Zotero.SyncedSettings.set(Zotero.Libraries.userLibraryID, 'feeds', json); yield Zotero.SyncedSettings.set(Zotero.Libraries.userLibraryID, 'feeds', json);
//
let feeds = Zotero.Feeds.getAll(); let feeds = Zotero.Feeds.getAll();
for (let feed of feeds) { for (let feed of feeds) {
if (json[feed.url]) { if (json[feed.url]) {
Zotero.debug("Feed " + feed.url + " is being updated from remote JSON"); Zotero.debug("Feed " + feed.url + " exists remotely and locally");
yield feed.updateFromJSON(json[feed.url]);
delete json[feed.url]; delete json[feed.url];
} else { } else {
Zotero.debug("Feed " + feed.url + " does not exist in remote JSON. Deleting"); Zotero.debug("Feed " + feed.url + " does not exist in remote JSON. Deleting");
@ -107,10 +105,10 @@ Zotero.Feeds = new function() {
} }
// Because existing json[feed.url] got deleted, `json` now only contains new feeds // Because existing json[feed.url] got deleted, `json` now only contains new feeds
for (let url in json) { for (let url in json) {
Zotero.debug("Feed " + url + " is being created from remote JSON"); Zotero.debug("Feed " + url + " exists remotely but not locally. Creating");
let feed = new Zotero.Feed(json[url]); let feed = new Zotero.Feed(json[url]);
yield feed.saveTx(); yield feed.saveTx();
yield feed.updateFromJSON(json[url]); yield feed.updateFeed();
} }
}); });

View file

@ -180,18 +180,6 @@ describe("Zotero.FeedItem", function () {
//yield assert.isRejected(feedItem.EraseTx(), "does not allow erasing twice"); //yield assert.isRejected(feedItem.EraseTx(), "does not allow erasing twice");
}); });
it("should remove synced setting if exists", function* () {
let item = yield createDataObject('feedItem', { libraryID });
yield item.toggleRead();
let syncedSettings = feed.getSyncedSettings();
assert.ok(syncedSettings.markedAsRead[item.guid]);
yield item.eraseTx();
syncedSettings = feed.getSyncedSettings();
assert.notOk(syncedSettings.markedAsRead[item.guid]);
});
}); });
describe("#toggleRead()", function() { describe("#toggleRead()", function() {
@ -217,17 +205,6 @@ describe("Zotero.FeedItem", function () {
yield item.toggleRead(true); yield item.toggleRead(true);
assert.isFalse(item.save.called, "item was not saved on toggle read to same state"); assert.isFalse(item.save.called, "item was not saved on toggle read to same state");
}); });
it('should set relevant synced settings', function* () {
let item = yield createDataObject('feedItem', { libraryID });
item.isRead = false;
yield item.saveTx();
yield item.toggleRead();
let feed = Zotero.Feeds.get(item.libraryID);
let syncedSettings = feed.getSyncedSettings();
assert.ok(syncedSettings.markedAsRead[item.guid], "item marked as read stored in synced settings");
});
}); });
describe('#translate()', function() { describe('#translate()', function() {

View file

@ -138,15 +138,5 @@ describe("Zotero.FeedItems", function () {
assert.isFalse(save.thisValues[i].isRead, "#toggleRead called with true"); assert.isFalse(save.thisValues[i].isRead, "#toggleRead called with true");
} }
}); });
it('should set relevant sync settings', function* () {
items[0].isRead = false;
yield items[0].saveTx();
yield Zotero.FeedItems.toggleReadByID(ids);
let syncedFeeds = Zotero.SyncedSettings.get(Zotero.Libraries.userLibraryID, 'feeds');
let markedAsRead = Object.keys(syncedFeeds[feed.url].markedAsRead);
assert.deepEqual(markedAsRead, Object.keys(items).map((k) => items[k].guid));
});
}); });
}); });

View file

@ -199,39 +199,18 @@ describe("Zotero.Feed", function() {
}); });
}); });
describe("#getSyncedSettings", function() {
it("should return correct synced settings for the feed", function* () {
let url = 'http://' + Zotero.Utilities.randomString(10, 'abcde') + '.com/feed.rss';
let syncedFeeds = Zotero.SyncedSettings.get(Zotero.Libraries.userLibraryID, 'feeds');
assert.notOk(syncedFeeds[url]);
let feed = yield createFeed({url});
syncedFeeds = Zotero.SyncedSettings.get(Zotero.Libraries.userLibraryID, 'feeds');
assert.ok(syncedFeeds[url]);
let syncedData = feed.getSyncedSettings();
assert.deepEqual(syncedData, syncedFeeds[url]);
});
});
describe("#storeSyncedSettings", function() { describe("#storeSyncedSettings", function() {
it("should store updated settings for the feed", function* () { it("should store updated settings for the feed", function* () {
let guid = Zotero.Utilities.randomString(); let settings = {
let feed = yield createFeed(); name: Zotero.Utilities.randomString(),
url: 'http://' + Zotero.Utilities.randomString().toLowerCase() + '.com/feed.rss',
refreshInterval: 1,
cleanupAfter: 1
};
let feed = yield createFeed(settings);
let syncedFeeds = Zotero.SyncedSettings.get(Zotero.Libraries.userLibraryID, 'feeds'); let syncedFeeds = Zotero.SyncedSettings.get(Zotero.Libraries.userLibraryID, 'feeds');
assert.notOk(syncedFeeds[feed.url].markedAsRead[guid]); assert.deepEqual(syncedFeeds[feed.url], settings);
let syncedData = feed.getSyncedSettings();
syncedData.markedAsRead[guid] = true;
yield feed.setSyncedSettings(syncedData);
yield feed.storeSyncedSettings();
syncedFeeds = Zotero.SyncedSettings.get(Zotero.Libraries.userLibraryID, 'feeds');
assert.isTrue(syncedFeeds[feed.url].markedAsRead[guid]);
}); });
}); });