-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
happn.js
238 lines (205 loc) · 7.74 KB
/
happn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright (c) 2017, Hugo Freire <hugo@exec.sh>.
*
* This source code is licensed under the license found in the
* LICENSE.md file in the root directory of this source tree.
*/
/* eslint-disable camelcase */
const HAPPN_FACEBOOK_LOGIN_APP_CLIENT_ID = '247294518656661'
const HAPPN_FACEBOOK_LOGIN_APP_REDIRECT_URI = 'https://www.happn.fr'
const HAPPN_FACEBOOK_LOGIN_APP_OPTIONAL_PARAMS = { scope: 'basic_info', response_type: 'token' }
const Channel = require('./channel')
const _ = require('lodash')
const Promise = require('bluebird')
const Health = require('health-checkup')
const { NotAuthorizedError } = require('./errors')
const { HappnWrapper, HappnNotAuthorizedError } = require('happn-wrapper')
const Database = require('../database')
const defaultOptions = {
oauth: {
facebook: {
clientId: HAPPN_FACEBOOK_LOGIN_APP_CLIENT_ID,
redirectUri: HAPPN_FACEBOOK_LOGIN_APP_REDIRECT_URI,
optionalParams: HAPPN_FACEBOOK_LOGIN_APP_OPTIONAL_PARAMS
}
},
channel: { is_enabled: false }
}
class Happn extends Channel {
constructor (options = {}) {
super('happn')
this._options = _.defaults(options, defaultOptions)
this._happn = new HappnWrapper()
Health.addCheck(this.name, () => new Promise((resolve, reject) => {
if (this._happn.circuitBreaker.isOpen()) {
return reject(new Error(`circuit breaker is open`))
} else {
return resolve()
}
}))
}
authorize () {
const authorize = function ({ facebookAccessToken }) {
return this._happn.authorize(facebookAccessToken)
.then(() => {
return { userId: this._happn.userId, accessToken: this._happn.accessToken }
})
}
return this.findOrAuthorizeIfNeeded(authorize.bind(this))
.then((channel) => {
this._happn.userId = channel.userId
this._happn.accessToken = channel.accessToken
})
}
getRecommendations () {
const _getRecommendations = () => {
return this._happn.getRecommendations(16)
.then(({ data }) => {
const _data = _.concat([], data)
return this._happn.getRecommendations(16, 16)
.then(({ data }) => _.concat(_data, data))
})
.mapSeries((data) => {
return {
channelName: 'happn',
channelRecommendationId: data.notifier.id,
name: data.notifier.first_name,
photos: _.map(data.notifier.profiles, (photo) => _.pick(photo, [ 'url', 'id' ])),
isTheirLike: data.notifier.is_accepted ? true : undefined,
isLike: data.notifier.my_relation === 1 ? true : undefined,
isMatch: data.notifier.my_relation === 4 ? true : undefined,
data
}
})
.catch(HappnNotAuthorizedError, () => { throw new NotAuthorizedError() })
}
return _getRecommendations()
.catch(NotAuthorizedError, () => {
return this.onNotAuthorizedError.bind(this)()
.then(() => _getRecommendations())
})
}
getUpdates () {
const _getUpdates = () => {
return Database.channels.find({ where: { name: this._name } })
.then(({ lastActivityDate, userId }) => {
const _lastActivityDate = !lastActivityDate ? undefined : lastActivityDate
return this._happn.getUpdates(_lastActivityDate)
.then(({ conversations }) => {
return Database.channels.update({ lastActivityDate: new Date() }, { where: { name: this._name } })
.then(() => {
return Promise.mapSeries(conversations, (conversation) => {
const channelRecommendationId = _(conversation.participants)
.map('user.id')
.filter((id) => id !== userId)
.value()[ 0 ]
const _isNewMatch = (channelRecommendationId) => {
return Database.recommendations.findOne({
where: {
channelName: 'happn',
channelRecommendationId
}
})
.then((recommendation) => {
if (recommendation === null) {
return true
}
return !recommendation.isMatch
})
}
return Promise.props({
isNewMatch: _isNewMatch(channelRecommendationId),
recommendation: this.getUser(channelRecommendationId)
})
.then(({ isNewMatch, recommendation }) => {
if (isNewMatch) {
recommendation.channelMatchId = channelRecommendationId
recommendation.matchedDate = new Date(conversation.creation_date)
}
const messages = _.map(conversation.messages, (message) => {
return {
channelName: 'happn',
channelMessageId: message.id,
recommendationId: channelRecommendationId,
isFromRecommendation: message.sender.id !== userId,
sentDate: new Date(message.creation_date.replace(/T/, ' ').replace(/\..+/, '')),
text: message.message
}
})
return { isNewMatch, recommendation, messages }
})
})
})
})
})
.catch(HappnNotAuthorizedError, () => { throw new NotAuthorizedError() })
}
return _getUpdates()
.catch(NotAuthorizedError, () => {
return this.onNotAuthorizedError.bind(this)()
.then(() => _getUpdates())
})
}
like (userId) {
const _like = (userId) => {
return Promise.try(() => {
if (!userId) {
throw new Error('invalid arguments')
}
})
.then(() => this._happn.like(userId))
.then(() => this._happn.getUser(userId))
.then(({ data }) => {
if (data.my_relation === 4) {
return {
channelName: 'happn',
channelRecommendationId: data.id,
name: data.first_name,
photos: _.map(data.profiles, (photo) => _.pick(photo, [ 'url', 'id' ])),
channelMatchId: data.id,
matchedDate: new Date(),
data
}
}
})
.catch(HappnNotAuthorizedError, () => { throw new NotAuthorizedError() })
}
return _like(userId)
.catch(NotAuthorizedError, () => {
return this.onNotAuthorizedError.bind(this)()
.then(() => _like(userId))
})
}
getUser (userId) {
const _getUser = (userId) => {
return Promise.try(() => {
if (!userId) {
throw new Error('invalid arguments')
}
})
.then(() => this._happn.getUser(userId))
.then(({ data }) => {
return {
channelName: 'happn',
channelRecommendationId: data.id,
name: data.first_name,
photos: _.map(data.profiles, (photo) => _.pick(photo, [ 'url', 'id' ])),
data
}
})
.catch(HappnNotAuthorizedError, () => { throw new NotAuthorizedError() })
}
return _getUser(userId)
.catch(NotAuthorizedError, () => {
return this.onNotAuthorizedError.bind(this)()
.then(() => _getUser(userId))
})
}
onNotAuthorizedError () {
this._happn.accessToken = undefined
this._happn.refreshToken = undefined
this._happn.userId = undefined
return super.onNotAuthorizedError()
}
}
module.exports = new Happn()