Skip to content

Commit

Permalink
fix: able to do a human like
Browse files Browse the repository at this point in the history
  • Loading branch information
hfreire committed Jun 8, 2017
1 parent 8664873 commit a9e023f
Show file tree
Hide file tree
Showing 17 changed files with 361 additions and 143 deletions.
29 changes: 17 additions & 12 deletions src/database/recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const transformRowToObject = function (row) {
row.updated_date = new Date(row.updated_date)
}

if (row.liked_date) {
row.liked_date = new Date(row.liked_date)
if (row.decision_date) {
row.decision_date = new Date(row.decision_date)
}

if (row.matched_date) {
Expand All @@ -45,6 +45,11 @@ const transformRowToObject = function (row) {
row.data = JSON.parse(row.data)
}

row.like = !!row.like
row.is_pass = !!row.is_pass
row.is_human_decision = !!row.is_human_decision
row.train = !!row.train

return row
}

Expand All @@ -61,12 +66,12 @@ const transformObjectToRow = function (object) {
object.updated_date = object.updated_date.toISOString()
}

if (object.matched_date instanceof Date) {
object.matched_date = object.matched_date.toISOString()
if (object.decision_date instanceof Date) {
object.decision_date = object.decision_date.toISOString()
}

if (object.liked_date instanceof Date) {
object.liked_date = object.liked_date.toISOString()
if (object.matched_date instanceof Date) {
object.matched_date = object.matched_date.toISOString()
}

if (object.trained_date instanceof Date) {
Expand All @@ -90,8 +95,8 @@ const queryAll = function (...args) {
}

const buildWhereClause = (keys, values) => {
if (_.includes(keys, 'liked_date')) {
const index = _.indexOf(keys, 'liked_date')
if (_.includes(keys, 'decision_date')) {
const index = _.indexOf(keys, 'decision_date')
values[ index ] = values[ index ].split(' ')[ 0 ]
values.splice(index, 0, values[ index ])
}
Expand All @@ -109,7 +114,7 @@ const buildWhereClause = (keys, values) => {
}

return keys.map((key) => {
if (key === 'liked_date') {
if (key === 'decision_date') {
return `${key} > ? AND ${key} < strftime('%Y-%m-%dT%H:%M:%fZ', datetime(?, '+1 day'))`
}

Expand All @@ -135,9 +140,9 @@ const buildWhereClause = (keys, values) => {
}

const buildSelectClause = (unique, select) => {
if (unique && _.includes(select, 'liked_date')) {
const index = _.indexOf(select, 'liked_date')
select[ index ] = `strftime('%Y-%m-%d',${select[ index ]}) AS liked_date`
if (unique && _.includes(select, 'decision_date')) {
const index = _.indexOf(select, 'decision_date')
select[ index ] = `strftime('%Y-%m-%d',${select[ index ]}) AS decision_date`
}

if (unique && _.includes(select, 'matched_date')) {
Expand Down
13 changes: 9 additions & 4 deletions src/database/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ const createSchema = function () {
'name VARCHAR(32) DEFAULT NULL,' +
'thumbnail_url VARCHAR(512) DEFAULT NULL,' +
'like INTEGER NOT NULL DEFAULT 0,' +
'liked_date DATETIME DEFAULT NULL,' +
'is_pass INTEGER NOT NULL DEFAULT 0,' +
'decision_date DATETIME DEFAULT NULL,' +
'is_human_decision INTEGER NOT NULL DEFAULT 0,' +
'match INTEGER NOT NULL DEFAULT 0,' +
'match_id VARCHAR(64) DEFAULT NULL,' +
'matched_date DATETIME DEFAULT NULL,' +
Expand Down Expand Up @@ -86,10 +88,13 @@ const createSchema = function () {
'date DATE PRIMARY KEY,' +
'created_date DATETIME DEFAULT (strftime(\'%Y-%m-%dT%H:%M:%fZ\', datetime(\'now\'))), ' +
'updated_date DATETIME DEFAULT (strftime(\'%Y-%m-%dT%H:%M:%fZ\', datetime(\'now\'))), ' +
'likes INTEGER NOT NULL DEFAULT 0,' +
'passes INTEGER NOT NULL DEFAULT 0,' +
'machine_likes INTEGER NOT NULL DEFAULT 0,' +
'human_likes INTEGER NOT NULL DEFAULT 0,' +
'machine_passes INTEGER NOT NULL DEFAULT 0,' +
'human_passes INTEGER NOT NULL DEFAULT 0,' +
'trains INTEGER NOT NULL DEFAULT 0,' +
'matches INTEGER NOT NULL DEFAULT 0' +
'matches INTEGER NOT NULL DEFAULT 0,' +
'skips INTEGER NOT NULL DEFAULT 0' +
')'))
.then(() => this._database.runAsync(
'CREATE TABLE IF NOT EXISTS messages (' +
Expand Down
28 changes: 24 additions & 4 deletions src/dates/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ const findAccount = (channel) => {
})
}

const likeOrPass = (channel, recommendation, like, pass) => {
if (like) {
return Recommendation.like(channel, recommendation)
.then((recommendation) => {
recommendation.is_human_decision = false
recommendation.decision_date = new Date()

return recommendation
})
} else if (pass) {
return Recommendation.pass(channel, recommendation)
.then((recommendation) => {
recommendation.is_human_decision = false
recommendation.decision_date = new Date()

return recommendation
})
}

return Promise.resolve(recommendation)
}

class Dates {
constructor () {
this._channels = {
Expand Down Expand Up @@ -130,11 +152,9 @@ class Dates {
const channelRecommendationId = channelRecommendation._id

return Recommendation.checkOut(channel, channelRecommendationId, channelRecommendation)
.then((recommendation) => {
return Recommendation.likeOrPass(channel, recommendation)
.then(({ recommendation, like, pass }) => {
return likeOrPass(channel, recommendation, like, pass)
.catch(OutOfLikesError, () => {
recommendation.like = 0

skipped++

return recommendation
Expand Down
3 changes: 2 additions & 1 deletion src/dates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
module.exports = {
Dates: require('./dates'),
Taste: require('./taste'),
Recommendation: require('./recommendation').Recommendation
Recommendation: require('./recommendation').Recommendation,
Stats: require('./stats')
}
3 changes: 1 addition & 2 deletions src/dates/match/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ class Match {

return Message.readMessages(channel, accountUserId, recommendation.id, match.messages)
.then(() => {
// TODO: checkOut() is putting like = 0 if a new match comes in and is not similar enough
return Recommendation.checkOut(channel, channelRecommendationId, channelRecommendation)
.catch(AlreadyCheckedOutEarlierError, () => recommendation)
})
.then((recommendation) => Recommendation.fallInLove(recommendation))
.then(({ recommendation }) => Recommendation.fallInLove(recommendation))
.then((recommendation) => Recommendations.save(channelName, channelRecommendationId, recommendation))
.then((recommendation) => {
if (match.is_new_message) {
Expand Down
74 changes: 57 additions & 17 deletions src/dates/recommendation/recommendation.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Recommendation {
return Promise.reject(new Error('invalid arguments'))
}

let like
let pass

return this.findOrCreateNewRecommendation(channel, channelRecommendationId, channelRecommendation)
.then((recommendation) => {
if (recommendation.last_checked_out_date) {
Expand All @@ -40,8 +43,10 @@ class Recommendation {

recommendation.last_checked_out_date = new Date()
recommendation.data = channelRecommendation
recommendation.like = photos.like
recommendation.photos_similarity_mean = photos.faceSimilarityMean

like = photos.like
pass = photos.pass
})
})
.then(() => {
Expand All @@ -52,31 +57,58 @@ class Recommendation {
})
}
})
.then(() => recommendation)
.then(() => {
return { recommendation, like, pass }
})
})
}

likeOrPass (channel, recommendation) {
like (channel, recommendation) {
if (!channel || !recommendation) {
return Promise.reject(new Error('invalid arguments'))
}

return Promise.resolve()
.then(() => {
if (recommendation.like) {
return channel.like(recommendation.channel_id, recommendation.data.photos[ 0 ].id, recommendation.data.content_hash, recommendation.data.s_number)
.then((match) => {
recommendation.liked_date = new Date()
recommendation.match = !!match
if (match) {
recommendation.match_id = match._id
}
})
.then(() => recommendation)
}
if (recommendation.like) {
return Promise.resolve()
}

return recommendation
if (recommendation.is_pass) {
return Promise.reject(new Error('can not like'))
}

const channelRecommendationId = recommendation.channel_id

const { photos, content_hash, s_number } = recommendation.data
const photoId = photos[ 0 ].id

return channel.like(channelRecommendationId, photoId, content_hash, s_number)
.then((match) => {
recommendation.like = true

recommendation.match = !!match
if (match) {
recommendation.match_id = match._id
}
})
.then(() => recommendation)
}

pass (channel, recommendation) {
if (!channel || !recommendation) {
return Promise.reject(new Error('invalid arguments'))
}

if (recommendation.is_pass) {
return Promise.resolve()
}

if (recommendation.like) {
return Promise.reject(new Error('can not pass'))
}

recommendation.is_pass = true

return Promise.resolve(recommendation)
}

fallInLove (recommendation) {
Expand All @@ -97,6 +129,14 @@ class Recommendation {
}))
}

couldDoBetter (recommendation) {
if (!recommendation) {
return Promise.reject(new Error('invalid arguments'))
}

return Promise.resolve(recommendation)
}

findOrCreateNewRecommendation (channel, channelRecommendationId, channelRecommendation) {
if (!channel || !channelRecommendationId) {
return Promise.reject(new Error('invalid arguments'))
Expand Down
9 changes: 6 additions & 3 deletions src/dates/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ const Promise = require('bluebird')
const Database = require('../database')

const stats = [
{ name: 'likes', metric: 'liked_date', criteria: { like: 1 } },
{ name: 'passes', metric: 'last_checked_out_date', criteria: { like: 0 } },
{ name: 'machine_likes', metric: 'decision_date', criteria: { is_human_decision: 0, like: 1 } },
{ name: 'human_likes', metric: 'decision_date', criteria: { is_human_decision: 1, like: 1 } },
{ name: 'machine_passes', metric: 'decision_date', criteria: { is_human_decision: 0, is_pass: 1 } },
{ name: 'human_passes', metric: 'decision_date', criteria: { is_human_decision: 1, is_pass: 1 } },
{ name: 'trains', metric: 'trained_date', criteria: { train: 1 } },
{ name: 'matches', metric: 'matched_date', criteria: { match: 1 } }
{ name: 'matches', metric: 'matched_date', criteria: { match: 1 } },
{ name: 'skips', metric: 'last_checked_out_date', criteria: { like: 0, is_pass: 0 } }
]

class Stats {
Expand Down
81 changes: 81 additions & 0 deletions src/routes/like-recommendation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 { Route } = require('serverful')

const Promise = require('bluebird')

const Logger = require('modern-logger')

const Joi = require('joi')
const Boom = require('boom')

const { Tinder } = require('../channels')
const { Recommendations, Channels } = require('../database')
const { Recommendation, Stats } = require('../dates')

class LikeRecommendation extends Route {
constructor () {
super('POST', '/recommendations/{id}/like', 'Like', 'Like a recommendation')
}

handler (request, reply) {
const { id } = request.params

Recommendations.findById(id)
.then((recommendation) => {
if (!recommendation) {
return Promise.reject(new Error())
}

const channelName = recommendation.channel
const channelRecommendationId = recommendation.channel_id

return Channels.findByName(channelName)
.then((channel) => {
return Recommendation.like(Tinder, recommendation)
.then((recommendation) => {
recommendation.is_human_decision = true
recommendation.decision_date = new Date()

return recommendation
})
})
.then((recommendation) => {
return Recommendations.save(channelName, channelRecommendationId, recommendation)
.then((recommendation) => {
Recommendation.fallInLove(recommendation)
.then(() => Stats.updateByDate(new Date()))

return recommendation
})
})
})
.then((recommendation) => reply(recommendation))
.catch((error) => {
Logger.error(error)

reply(Boom.badImplementation(error.message, error))
})
}

auth () {
return false
}

validate () {
return {
params: {
id: Joi.string()
.required()
.description('recommendation id')
}
}
}
}

module.exports = new LikeRecommendation()
Loading

0 comments on commit a9e023f

Please sign in to comment.