]>
git.r.bdr.sh - rbdr/dead-drop/blob - lib/controllers/recordings.js
591dbd5d05d024aa860bc052cfd5c750e9ea803f
3 const Joi
= require('joi');
4 const Pify
= require('pify');
5 const Redis
= require('redis');
6 const Twilio
= require('twilio');
10 internals
.kContentType
= 'application/xml'; // The content type used to respond
11 internals
.kLanguage
= 'es-mx'; // the language to use
12 internals
.kMaxMessageLength
= 30; // max message length in seconds
13 internals
.kIdDateFormat
= 'YYMMDDHHmmssSSS'; // derive ids from current date. 15 digits.
14 internals
.kRecordingsSet
= 'recordings';
15 internals
.kRecordMessage
= 'Graba tu mensaje despues del bip. ' +
16 'Presiona cualquier tecla para finalizar tu mensaje. '; // the recording message
17 internals
.kConfirmationMessage
= 'Gracias. Tu mensaje es el número: ';
18 internals
.kNotFoundMessage
= 'Mensaje no encontrado. Adiós!';
20 internals
.kRecordingSchema
= Joi
.object().keys({
21 url: Joi
.string().required()
25 * Handles the HTTP requests for the recording menu
27 * @class RecordingsController
28 * @param {DeadDrop.tConfiguration} config The configuration to
31 module
.exports
= internals
.RecordingsController
= class RecordingsController
{
34 this._redis
= Redis
.createClient(config
.redis
);
36 // Log an error if it happens.
37 this._redis
.on('error', (err
) => {
44 * Start recording process
46 * @function startRecording
47 * @memberof RecordingsController
49 * @return {generator} a koa compatible handler generator function
53 return function * () {
55 const response
= new Twilio
.TwimlResponse();
57 // the action will default to post in the same URL, so no change
59 response
.say(internals
.kRecordMessage
, { language: internals
.kLanguage
})
61 maxLength: internals
.kMaxMessageLength
64 this.type
= internals
.kContentType
;
65 this.body
= response
.toString();
70 * Saves the recording for later use
72 * @function saveRecording
73 * @memberof RecordingsController
75 * @return {generator} a koa compatible handler generator function
81 return function * () {
83 const zadd
= Pify(self
._redis
.zadd
.bind(self
._redis
));
85 const response
= new Twilio
.TwimlResponse();
87 const id
= Date
.now().toString().substr(2,10);
88 const url
= this.request
.body
.RecordingUrl
;
89 const separatedId
= id
.split('').join('. ');
94 yield self
._validate(recording
).catch((err
) => {
96 this.throw(err
.message
, 422);
99 // Add to ordered set for quick fetches, and set for random fetches
100 yield zadd(internals
.kRecordingsSet
, parseInt(id
), url
);
102 response
.say(`${internals.kConfirmationMessage}${separatedId}`, { language: internals
.kLanguage
});
104 this.type
= internals
.kContentType
;
105 this.body
= response
.toString();
112 * @function getRecording
113 * @memberof RecordingsController
115 * @return {generator} a koa compatible handler generator function
121 return function * (id
) {
125 const zcard
= Pify(self
._redis
.zcard
.bind(self
._redis
));
126 const zrange
= Pify(self
._redis
.zrange
.bind(self
._redis
));
127 const zscore
= Pify(self
._redis
.zscore
.bind(self
._redis
));
128 const zrangebyscore
= Pify(self
._redis
.zrangebyscore
.bind(self
._redis
));
130 const response
= new Twilio
.TwimlResponse();
134 const maxNumber
= yield zcard(internals
.kRecordingsSet
);
135 const index
= Math
.floor(Math
.random() * maxNumber
); // get random between 0 and cardinality
136 location
= yield zrange(internals
.kRecordingsSet
, index
, index
);
139 location
= yield zrangebyscore(internals
.kRecordingsSet
, id
, id
);
142 if (location
&& location
.length
> 0) {
144 id
= yield zscore(internals
.kRecordingsSet
, location
[0]);
147 const separatedId
= id
.toString().split('').join('. ');
148 response
.play(location
[0]).say(separatedId
, { language: internals
.kLanguage
});
151 response
.say(internals
.kNotFoundMessage
, { language: internals
.kLanguage
});
154 this.type
= internals
.kContentType
;
155 this.body
= response
.toString();
159 // Validates the post schema
163 const validate
= Pify(Joi
.validate
.bind(Joi
));
164 return validate(post
, internals
.kRecordingSchema
);