Added support for database operations. Now when requesting a item, it is saved to a database and sends a email from pi.midboe account.
This commit is contained in:
		| @@ -7,6 +7,8 @@ const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); | |||||||
| var Promise = require('bluebird'); | var Promise = require('bluebird'); | ||||||
| var rp = require('request-promise'); | var rp = require('request-promise'); | ||||||
|  |  | ||||||
|  | const establishedDatabase = require('src/database/database'); | ||||||
|  |  | ||||||
| const MailTemplate = require('src/plex/mailTemplate') | const MailTemplate = require('src/plex/mailTemplate') | ||||||
|  |  | ||||||
| var pythonShell = require('python-shell'); | var pythonShell = require('python-shell'); | ||||||
| @@ -15,6 +17,13 @@ const nodemailer = require('nodemailer'); | |||||||
|  |  | ||||||
| class RequestRepository { | class RequestRepository { | ||||||
|  |  | ||||||
|  | 	constructor(database) { | ||||||
|  | 		this.database = database || establishedDatabase; | ||||||
|  | 		this.queries = { | ||||||
|  | 			'insertRequest': "INSERT INTO requests VALUES (?, ?, ?, ?, ?, ?, CURRENT_DATE)" | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	searchRequest(query, page, type) { | 	searchRequest(query, page, type) { | ||||||
| 		// TODO get from cache | 		// TODO get from cache | ||||||
| 		// STRIP METADATA THAT IS NOT ALLOWED | 		// STRIP METADATA THAT IS NOT ALLOWED | ||||||
| @@ -99,23 +108,31 @@ class RequestRepository { | |||||||
| 	* @param {identifier, type} the id of the media object and type of media must be defined | 	* @param {identifier, type} the id of the media object and type of media must be defined | ||||||
| 	* @returns {Promise} If nothing has gone wrong. | 	* @returns {Promise} If nothing has gone wrong. | ||||||
| 	*/  | 	*/  | ||||||
| 	sendRequest(identifier, type) { | 	sendRequest(identifier, type, ip) { | ||||||
| 		// TODO add to DB so can have a admin page | 		// TODO add to DB so can have a admin page | ||||||
| 		// TODO try a cache hit on the movie item | 		// TODO try a cache hit on the movie item | ||||||
|  |  | ||||||
| 		tmdb.lookup(identifier, type).then(movie => { | 		tmdb.lookup(identifier, type).then(movie => { | ||||||
|  |  | ||||||
|  | 			// Add request to database | ||||||
|  | 			this.database.run(this.queries.insertRequest, [movie.id, movie.title, movie.year, movie.poster, 'NULL', ip]) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 			//  | ||||||
|  |  | ||||||
|  |  | ||||||
| 			// create reusable transporter object using the default SMTP transport | 			// create reusable transporter object using the default SMTP transport | ||||||
| 			let transporter = nodemailer.createTransport({ | 			let transporter = nodemailer.createTransport({ | ||||||
| 			    host: configuration.get('mail', 'host'), | 				service: 'gmail', | ||||||
| 			    port: 26, |  | ||||||
| 			    ignoreTLS: true, |  | ||||||
| 			    tls :{rejectUnauthorized: false}, |  | ||||||
| 			    secure: false, // secure:true for port 465, secure:false for port 587 |  | ||||||
| 			    auth: { | 			    auth: { | ||||||
| 			        user: configuration.get('mail', 'user'), | 			        user: configuration.get('mail', 'user_pi'), | ||||||
| 			        pass: configuration.get('mail', 'password') | 			        pass: configuration.get('mail', 'password_pi') | ||||||
| 			    } | 			    } | ||||||
|  | 			    // host: configuration.get('mail', 'host'), | ||||||
|  | 			    // port: 26, | ||||||
|  | 			    // ignoreTLS: true, | ||||||
|  | 			    // tls :{rejectUnauthorized: false}, | ||||||
|  | 			    // secure: false, // secure:true for port 465, secure:false for port 587 | ||||||
| 			}); | 			}); | ||||||
|  |  | ||||||
| 			const mailTemplate = new MailTemplate(movie) | 			const mailTemplate = new MailTemplate(movie) | ||||||
| @@ -123,7 +140,7 @@ class RequestRepository { | |||||||
| 			// setup email data with unicode symbols | 			// setup email data with unicode symbols | ||||||
| 			let mailOptions = { | 			let mailOptions = { | ||||||
| 				// TODO get the mail adr from global location (easy to add) | 				// TODO get the mail adr from global location (easy to add) | ||||||
| 			    from: 'MovieRequester <support@kevinmidboe.com>', // sender address | 			    from: 'MovieRequester <pi.midboe@gmail.com>', // sender address | ||||||
| 			    to: 'kevin.midboe@gmail.com', // list of receivers | 			    to: 'kevin.midboe@gmail.com', // list of receivers | ||||||
| 			    subject: 'Download request', // Subject line | 			    subject: 'Download request', // Subject line | ||||||
| 			    text: mailTemplate.toText(), | 			    text: mailTemplate.toText(), | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user