From 772b7639e74c3c313c2d5dc03dc185ddef3ceafc Mon Sep 17 00:00:00 2001 From: rafaelgssa Date: Sat, 20 Jun 2020 13:26:28 -0300 Subject: [PATCH] Detect synced items when adding with release date --- src/class/history-sync/ActivityActionCreators.js | 7 +++++++ src/class/history-sync/ActivityStore.js | 9 +++++++++ src/class/history-sync/TraktWebAPIUtils.js | 9 ++++++++- src/class/history-sync/ViewingActivityApp.js | 11 +++++++---- src/modules/history-sync/activity-constants.js | 3 ++- src/modules/history-sync/index.js | 2 -- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/class/history-sync/ActivityActionCreators.js b/src/class/history-sync/ActivityActionCreators.js index 6fcc7a6..b792d8b 100644 --- a/src/class/history-sync/ActivityActionCreators.js +++ b/src/class/history-sync/ActivityActionCreators.js @@ -79,4 +79,11 @@ export default class ActivityActionCreators { response }); } + + static addWithReleaseDate(value) { + ViewingActivityAppDispatcher.dispatch({ + type: ActionTypes.ADD_WITH_RELEASE_DATE, + value, + }); + } } diff --git a/src/class/history-sync/ActivityStore.js b/src/class/history-sync/ActivityStore.js index 853b4f2..d42d217 100644 --- a/src/class/history-sync/ActivityStore.js +++ b/src/class/history-sync/ActivityStore.js @@ -9,6 +9,7 @@ let _activities = []; let _isLoading = false; let _isLoadingTraktData = true; let _message = undefined; +let _addWithReleaseDate = false; class ActivityStore extends EventEmitter { emitChange() { @@ -53,6 +54,10 @@ class ActivityStore extends EventEmitter { activityToUpdate.trakt = activity.trakt; activityToUpdate.add = activity.add; } + + addWithReleaseDate() { + return _addWithReleaseDate; + } } const activityStore = new ActivityStore(); @@ -117,6 +122,10 @@ activityStore.dispatchToken = ViewingActivityAppDispatcher.register((action) => _message = browser.i18n.getMessage(`syncFailed`); activityStore.emitChange(); break; + case ActionTypes.ADD_WITH_RELEASE_DATE: + _addWithReleaseDate = action.value; + activityStore.emitChange(); + break; } }); export default activityStore; diff --git a/src/class/history-sync/TraktWebAPIUtils.js b/src/class/history-sync/TraktWebAPIUtils.js index ad9f597..c78f7bd 100644 --- a/src/class/history-sync/TraktWebAPIUtils.js +++ b/src/class/history-sync/TraktWebAPIUtils.js @@ -4,6 +4,7 @@ import BrowserStorage from '../BrowserStorage'; import Request from '../Request'; import Search from '../Search'; import ActivityActionCreators from './ActivityActionCreators'; +import ActivityStore from './ActivityStore'; const URL = `${Settings.apiUri}/sync/history`; @@ -16,8 +17,9 @@ export default class TraktWebAPIUtils { } } - static addActivities(activities, addWithReleaseDate) { + static addActivities(activities) { // noinspection JSIgnoredPromiseFromCall + const addWithReleaseDate = ActivityStore.addWithReleaseDate(); Request.send({ method: `POST`, url: URL, @@ -112,10 +114,15 @@ export default class TraktWebAPIUtils { let alreadyOnTrakt = false; let date; + const addWithReleaseDate = ActivityStore.addWithReleaseDate(); const historyEntries = JSON.parse(response).reverse(); for (let history of historyEntries) { if (history && history.watched_at) { date = moment(history.watched_at); + if (addWithReleaseDate && history.watched_at.endsWith('00.000Z')) { + alreadyOnTrakt = true; + break; + } if (date.diff(options.netflix.date, `days`) === 0) { alreadyOnTrakt = true; break; diff --git a/src/class/history-sync/ViewingActivityApp.js b/src/class/history-sync/ViewingActivityApp.js index 9477c47..50725c5 100644 --- a/src/class/history-sync/ViewingActivityApp.js +++ b/src/class/history-sync/ViewingActivityApp.js @@ -7,13 +7,13 @@ import TmdbImageContainer from '../tmdb/TmdbImageContainer'; import ActivityList from './ActivityList'; import ActivityStore from './ActivityStore'; import TraktWebAPIUtils from './TraktWebAPIUtils'; +import ActivityActionCreators from './ActivityActionCreators'; /* global componentHandler */ export default class ViewingActivityApp extends React.Component { getInitialState() { return Object.assign(this.getStateFromStores(), { - addWithReleaseDate: this.props.addWithReleaseDate, hideSynced: this.props.hideSynced, use24Clock: this.props.use24Clock, pagesToLoad: this.props.pagesToLoad @@ -26,7 +26,8 @@ export default class ViewingActivityApp extends React.Component { isLoadingTraktData: ActivityStore.isLoadingTraktData(), loading: ActivityStore.isLoading(), message: ActivityStore.getMessage(), - page: ActivityStore.getPage() + page: ActivityStore.getPage(), + addWithReleaseDate: ActivityStore.addWithReleaseDate(), }; } @@ -36,7 +37,9 @@ export default class ViewingActivityApp extends React.Component { } componentDidMount() { + ActivityActionCreators.addWithReleaseDate(this.props.addWithReleaseDate); ActivityStore.addChangeListener(this._onChange.bind(this)); + NetflixApiUtils.getActivities(); } componentWillUnmount() { @@ -58,7 +61,7 @@ export default class ViewingActivityApp extends React.Component { const confirmationMessage = browser.i18n.getMessage(`confirmSync`); if (confirm(confirmationMessage)) { this.setState({loading: true}); - TraktWebAPIUtils.addActivities(this.state.activities, this.state.addWithReleaseDate); + TraktWebAPIUtils.addActivities(this.state.activities); } } @@ -96,7 +99,7 @@ export default class ViewingActivityApp extends React.Component { storage.prefs.addWithReleaseDate = addWithReleaseDate; BrowserStorage.set({prefs: storage.prefs}, true); }); - this.setState({addWithReleaseDate}); + ActivityActionCreators.addWithReleaseDate(addWithReleaseDate); } _onToggleSynced(event) { diff --git a/src/modules/history-sync/activity-constants.js b/src/modules/history-sync/activity-constants.js index 6aa01c2..c546435 100644 --- a/src/modules/history-sync/activity-constants.js +++ b/src/modules/history-sync/activity-constants.js @@ -9,7 +9,8 @@ const ActionTypes = { TOGGLE_ACTIVITY: `TOGGLE_ACTIVITY`, SYNC_SUCCESS: `SYNC_SUCCESS`, SYNC_FAILED: `SYNC_FAILED`, - UPDATE_ACTIVITY: `UPDATE_ACTIVITY` + UPDATE_ACTIVITY: `UPDATE_ACTIVITY`, + ADD_WITH_RELEASE_DATE: 'ADD_WITH_RELEASE_DATE', }; export default ActionTypes; diff --git a/src/modules/history-sync/index.js b/src/modules/history-sync/index.js index 7ebbb5b..2dd6a8f 100644 --- a/src/modules/history-sync/index.js +++ b/src/modules/history-sync/index.js @@ -2,7 +2,6 @@ import '../../assets'; import React from 'react'; import ReactDOM from 'react-dom'; import BrowserStorage from '../../class/BrowserStorage'; -import NetflixApiUtils from '../../class/NetflixApiUtils'; import Rollbar from '../../class/Rollbar'; import ViewingActivityApp from '../../class/history-sync/ViewingActivityApp'; import shared from '../../class/Shared'; @@ -12,7 +11,6 @@ shared.setBackgroundPage(true); // noinspection JSIgnoredPromiseFromCall Rollbar.init(); BrowserStorage.get(`prefs`).then(storage => { - NetflixApiUtils.getActivities(); ReactDOM.render(