This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from tegon/viewing-activity
Sync previous Viewing activity
- Loading branch information
Showing
39 changed files
with
1,435 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,8 @@ package | |
build | ||
*.log | ||
.DS_Store | ||
app/scripts/src/settings.js | ||
*.crx | ||
*.pem | ||
coverage | ||
app.zip | ||
app.zip | ||
config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
function ChromeStorage() {}; | ||
|
||
// Returns if window is not content_script, which sometimes fails to call chrome.storage | ||
// If it is, send a message so that background script handle chrome.storage calls | ||
ChromeStorage.isAvailable = function() { | ||
return !!chrome.tabs; | ||
}; | ||
|
||
ChromeStorage.get = function(key, callback) { | ||
if (ChromeStorage.isAvailable()) { | ||
chrome.storage.local.get(key, callback); | ||
} else { | ||
chrome.runtime.sendMessage({ type: 'getStorageValue', key: key }, callback); | ||
} | ||
}; | ||
|
||
ChromeStorage.set = function(value, callback) { | ||
if (ChromeStorage.isAvailable()) { | ||
chrome.storage.local.set(value, callback); | ||
} else { | ||
chrome.runtime.sendMessage({ type: 'setStorageValue', value: value }, callback); | ||
} | ||
}; | ||
|
||
ChromeStorage.clear = function(callback) { | ||
if (ChromeStorage.isAvailable()) { | ||
chrome.storage.local.clear(callback); | ||
} else { | ||
chrome.runtime.sendMessage({ type: 'clearStorage' }, callback); | ||
} | ||
}; | ||
|
||
module.exports = ChromeStorage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
var ViewingActivity = require('./viewing-activity.js'); | ||
var ViewingActivityParser = require('./viewing-activity-parser.js'); | ||
var WatchedHistory = require('./watched-history.js'); | ||
var ChromeStorage = require('../chrome-storage.js'); | ||
var Oauth = require('../oauth.js'); | ||
var async = require('async'); | ||
|
||
function Sync() { | ||
this.history = new WatchedHistory(); | ||
}; | ||
|
||
Sync.prototype = { | ||
needToSync: function(callback) { | ||
Oauth.getUserInfo(function() { | ||
ChromeStorage.get(null, function(data) { | ||
if (data.auto_sync) { | ||
var today = new Date(); | ||
today.setHours(0, 0, 0, 0); | ||
var lastSync = new Date(data.synced_at); | ||
lastSync.setHours(0, 0, 0, 0); | ||
var needToSync = !data.synced_at || today > lastSync; | ||
callback.call(this, needToSync); | ||
} else { | ||
callback.call(this, false); | ||
} | ||
}); | ||
}, function() { | ||
callback.call(this, false); | ||
}); | ||
}, | ||
|
||
start: function(callback) { | ||
this.callback = callback; | ||
this.onSyncStart(); | ||
}, | ||
|
||
onSyncStart: function() { | ||
ChromeStorage.get('synced_at', function(data) { | ||
ViewingActivity.list({ | ||
success: function(response) { | ||
ViewingActivityParser.start({ | ||
data: response, | ||
callback: this.syncActivities.bind(this), | ||
syncedAt: data.synced_at | ||
}); | ||
}.bind(this), | ||
error: this.onError.bind(this) | ||
}); | ||
}.bind(this)); | ||
}, | ||
|
||
syncActivities: function(activities) { | ||
if (activities.length > 0) { | ||
async.filter(activities, this.syncActivity.bind(this), function(activities) { | ||
if (activities.length > 0) { | ||
this.history.send(activities, this.onCompleted.bind(this)); | ||
} else { | ||
this.onCompleted(true); | ||
} | ||
}.bind(this)); | ||
} else { | ||
this.onCompleted(true); | ||
} | ||
}, | ||
|
||
onCompleted: function(success) { | ||
if (success) { | ||
var now = new Date(); | ||
ChromeStorage.set({ 'synced_at': now.toISOString() }, function() {}); | ||
} | ||
|
||
this.callback.call(this, success); | ||
}, | ||
|
||
syncActivity: function(activity, callback) { | ||
this.history.include({ | ||
activity: activity, | ||
success: function(include) { | ||
callback(!include); | ||
}, | ||
error: function(status, response) { | ||
callback(true); | ||
} | ||
}); | ||
}, | ||
|
||
onError: function(status, response) { | ||
console.error('traktflix: Sync error', status, response); | ||
} | ||
}; | ||
|
||
module.exports = Sync; |
Oops, something went wrong.