Skip to content

Commit

Permalink
fix(tinder): save last_activity_date after getting updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hfreire committed Jun 4, 2017
1 parent 45931e5 commit 64adf60
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/channels/tinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

/* eslint-disable camelcase */

const FACEBOOK_USER_EMAIL = process.env.FACEBOOK_USER_EMAIL
const FACEBOOK_USER_PASSWORD = process.env.FACEBOOK_USER_PASSWORD
const FACEBOOK_TINDER_APP_AUTHZ_URL = 'https://www.facebook.com/v2.6/dialog/oauth?redirect_uri=fb464891386855067%3A%2F%2Fauthorize%2F&state=%7B%22challenge%22%3A%22q1WMwhvSfbWHvd8xz5PT6lk6eoA%253D%22%2C%220_auth_logger_id%22%3A%2254783C22-558A-4E54-A1EE-BB9E357CC11F%22%2C%22com.facebook.sdk_client_state%22%3Atrue%2C%223_method%22%3A%22sfvc_auth%22%7D&scope=user_birthday%2Cuser_photos%2Cuser_education_history%2Cemail%2Cuser_relationship_details%2Cuser_friends%2Cuser_work_history%2Cuser_likes&response_type=token%2Csigned_request&default_audience=friends&return_scopes=true&auth_type=rerequest&client_id=464891386855067&ret=login&sdk=ios&logger_id=54783C22-558A-4E54-A1EE-BB9E357CC11F#_=_'
Expand Down Expand Up @@ -106,7 +108,7 @@ class Tinder extends Channel {

this._tinder.authorizeCircuitBreaker = this._breaker.slaveCircuit((...params) => retry(() => this._tinder.authorizeAsync(...params), this._options.retry))
this._tinder.getRecommendationsCircuitBreaker = this._breaker.slaveCircuit((params) => retry(() => this._tinder.getRecommendationsAsync(params), this._options.retry))
this._tinder.getUpdatesCircuitBreaker = this._breaker.slaveCircuit(() => retry(() => this._tinder.getUpdatesAsync(), this._options.retry))
this._tinder.getUpdatesCircuitBreaker = this._breaker.slaveCircuit((...params) => retry(() => this._tinder.getUpdatesAsync(...params), this._options.retry))
this._tinder.getHistoryCircuitBreaker = this._breaker.slaveCircuit(() => retry(() => this._tinder.getHistoryAsync(), this._options.retry))
this._tinder.likeCircuitBreaker = this._breaker.slaveCircuit((...params) => retry(() => this._tinder.likeAsync(...params), this._options.retry))

Expand Down Expand Up @@ -150,7 +152,14 @@ class Tinder extends Channel {
throw new NotAuthorizedError()
}
})
.then(() => this._tinder.getUpdatesCircuitBreaker.exec())
.then(() => Channels.findByName(this.name))
.then(({ last_activity_date }) => this._tinder.getUpdatesCircuitBreaker.exec(last_activity_date))
.then((data) => {
const last_activity_date = new Date()

return Channels.save(this.name, { last_activity_date })
.then(() => data)
})
.catch((error) => handleError.bind(this)(error))
}

Expand All @@ -161,9 +170,6 @@ class Tinder extends Channel {
}
})
.then(() => this._tinder.getHistoryCircuitBreaker.exec())
.then((data) => {
console.log(data)
})
.catch((error) => handleError.bind(this)(error))
}

Expand All @@ -187,7 +193,6 @@ class Tinder extends Channel {
})
.then(() => this._tinder.likeCircuitBreaker.exec(userId, photoId, contentHash, sNumber))
.then(({ match, likes_remaining }) => {
// eslint-disable-next-line camelcase
if (!likes_remaining) {
throw new Error('Out of likes')
}
Expand Down
5 changes: 5 additions & 0 deletions src/database/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const transformRowToObject = function (row) {

row.created_date = new Date(row.created_date)
row.updated_date = new Date(row.updated_date)
row.last_activity_date = new Date(row.last_activity_date)

return row
}
Expand All @@ -38,6 +39,10 @@ const transformObjectToRow = function (object) {
object.updated_date = object.updated_date.toISOString().replace(/T/, ' ').replace(/\..+/, '')
}

if (object.last_activity_date instanceof Date) {
object.last_activity_date = object.last_activity_date.toISOString().replace(/T/, ' ').replace(/\..+/, '')
}

return object
}

Expand Down
1 change: 1 addition & 0 deletions src/database/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const createSchema = function () {
'updated_date DATETIME DEFAULT CURRENT_TIMESTAMP, ' +
'is_enabled INTEGER NOT NULL DEFAULT 0,' +
'auth_id INTEGER NULL,' +
'last_activity_date DATETIME DEFAULT CURRENT_TIMESTAMP,' +
'PRIMARY KEY (name)' +
')'))
.then(() => this._database.runAsync(
Expand Down
20 changes: 20 additions & 0 deletions test/channels/tinder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2017, Hugo Freire <hugo@exec.sh>.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

const { SQLite } = require('../../src/database')

describe('Tinder', () => {
let subject

afterEach(() => td.reset())

describe('when getting updates', () => {
beforeEach(() => {
subject = require('../../src/channels/tinder')
})
})
})

0 comments on commit 64adf60

Please sign in to comment.