| 
									
										
										
										
											2018-01-13 18:02:41 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  | const sql = require('../services/sql'); | 
					
						
							|  |  |  | const notes = require('../services/notes'); | 
					
						
							|  |  |  | const axios = require('axios'); | 
					
						
							|  |  |  | const log = require('../services/log'); | 
					
						
							|  |  |  | const utils = require('../services/utils'); | 
					
						
							|  |  |  | const unescape = require('unescape'); | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  | const attributes = require('../services/attributes'); | 
					
						
							| 
									
										
										
										
											2018-01-13 22:51:39 -05:00
										 |  |  | const sync_mutex = require('../services/sync_mutex'); | 
					
						
							|  |  |  | const config = require('../services/config'); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | const date_notes = require('../services/date_notes'); | 
					
						
							| 
									
										
										
										
											2018-01-16 20:11:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | // "reddit" date note is subnote of date note which contains all reddit comments from that date
 | 
					
						
							|  |  |  | const REDDIT_DATE_ATTRIBUTE = 'reddit_date_note'; | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function createNote(parentNoteId, noteTitle, noteText) { | 
					
						
							|  |  |  |     return (await notes.createNewNote(parentNoteId, { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         title: noteTitle, | 
					
						
							|  |  |  |         content: noteText, | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |         target: 'into', | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         isProtected: false | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |     })).noteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function redditId(kind, id) { | 
					
						
							|  |  |  |     return kind + "_" + id; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-12 22:44:55 -05:00
										 |  |  | async function getDateNoteIdForReddit(dateTimeStr, rootNoteId) { | 
					
						
							|  |  |  |     const dateStr = dateTimeStr.substr(0, 10); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-16 20:11:50 -05:00
										 |  |  |     let redditDateNoteId = await attributes.getNoteIdWithAttribute(REDDIT_DATE_ATTRIBUTE, dateStr); | 
					
						
							| 
									
										
										
										
											2018-01-12 22:44:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!redditDateNoteId) { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |         const dateNoteId = await date_notes.getDateNoteId(dateTimeStr, rootNoteId); | 
					
						
							| 
									
										
										
										
											2018-01-12 22:44:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |         redditDateNoteId = await createNote(dateNoteId, "Reddit"); | 
					
						
							| 
									
										
										
										
											2018-01-12 22:44:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-16 20:11:50 -05:00
										 |  |  |         await attributes.createAttribute(redditDateNoteId, REDDIT_DATE_ATTRIBUTE, dateStr); | 
					
						
							| 
									
										
										
										
											2018-02-13 22:34:33 -05:00
										 |  |  |         await attributes.createAttribute(redditDateNoteId, "hide_in_autocomplete"); | 
					
						
							| 
									
										
										
										
											2018-01-12 22:44:55 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return redditDateNoteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-16 20:11:50 -05:00
										 |  |  | async function importComments(rootNoteId, accountName, afterId = null) { | 
					
						
							| 
									
										
										
										
											2018-01-12 22:44:55 -05:00
										 |  |  |     let url = `https://www.reddit.com/user/${accountName}.json`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (afterId) { | 
					
						
							|  |  |  |         url += "?after=" + afterId; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const response = await axios.get(url); | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  |     const listing = response.data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (listing.kind !== 'Listing') { | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |         log.info(`Reddit: Unknown object kind ${listing.kind}`); | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const children = listing.data.children; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |     let importedComments = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  |     for (const child of children) { | 
					
						
							|  |  |  |         const comment = child.data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |         let commentNoteId = await attributes.getNoteIdWithAttribute('reddit_id', redditId(child.kind, comment.id)); | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (commentNoteId) { | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const dateTimeStr = utils.dateStr(new Date(comment.created_utc * 1000)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 23:24:40 -05:00
										 |  |  |         const permaLink = 'https://reddit.com' + comment.permalink; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |         const noteText = | 
					
						
							| 
									
										
										
										
											2018-01-13 23:24:40 -05:00
										 |  |  | `<p><a href="${permaLink}">${permaLink}</a></p>
 | 
					
						
							|  |  |  | <p>author: <a href="https://reddit.com/u/${comment.author}">${comment.author}</a>,  | 
					
						
							|  |  |  | subreddit: <a href="https://reddit.com/r/${comment.subreddit}">${comment.subreddit}</a>,  | 
					
						
							|  |  |  | karma: ${comment.score}, created at ${dateTimeStr}</p><p></p>` | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  |             + unescape(comment.body_html); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |         let parentNoteId = await getDateNoteIdForReddit(dateTimeStr, rootNoteId); | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             commentNoteId = await createNote(parentNoteId, comment.link_title, noteText); | 
					
						
							| 
									
										
										
										
											2018-01-12 22:44:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |             log.info("Reddit: Imported comment to note " + commentNoteId); | 
					
						
							|  |  |  |             importedComments++; | 
					
						
							| 
									
										
										
										
											2018-01-12 22:44:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |             await attributes.createAttribute(commentNoteId, "reddit_kind", child.kind); | 
					
						
							|  |  |  |             await attributes.createAttribute(commentNoteId, "reddit_id", redditId(child.kind, comment.id)); | 
					
						
							|  |  |  |             await attributes.createAttribute(commentNoteId, "reddit_created_utc", comment.created_utc); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-01-12 22:44:55 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |     // if there have been no imported comments on this page, there shouldn't be any to import
 | 
					
						
							|  |  |  |     // on the next page since those are older
 | 
					
						
							|  |  |  |     if (listing.data.after && importedComments > 0) { | 
					
						
							| 
									
										
										
										
											2018-01-16 20:11:50 -05:00
										 |  |  |         importedComments += await importComments(rootNoteId, accountName, listing.data.after); | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |     return importedComments; | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 22:51:39 -05:00
										 |  |  | let redditAccounts = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function runImport() { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     const rootNoteId = await date_notes.getRootNoteId(); | 
					
						
							| 
									
										
										
										
											2018-01-16 20:11:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 22:51:39 -05:00
										 |  |  |     // technically mutex shouldn't be necessary but we want to avoid doing potentially expensive import
 | 
					
						
							|  |  |  |     // concurrently with sync
 | 
					
						
							|  |  |  |     await sync_mutex.doExclusively(async () => { | 
					
						
							|  |  |  |         let importedComments = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const account of redditAccounts) { | 
					
						
							| 
									
										
										
										
											2018-01-16 20:11:50 -05:00
										 |  |  |             importedComments += await importComments(rootNoteId, account); | 
					
						
							| 
									
										
										
										
											2018-01-13 22:51:39 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         log.info(`Reddit: Imported ${importedComments} comments.`); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  | sql.dbReady.then(async () => { | 
					
						
							| 
									
										
										
										
											2018-01-13 23:03:17 -05:00
										 |  |  |     if (!config['Reddit'] || config['Reddit']['enabled'] !== true) { | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 22:51:39 -05:00
										 |  |  |     const redditAccountsStr = config['Reddit']['accounts']; | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 22:51:39 -05:00
										 |  |  |     if (!redditAccountsStr) { | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |         log.info("Reddit: No reddit accounts defined in option 'reddit_accounts'"); | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 22:51:39 -05:00
										 |  |  |     redditAccounts = redditAccountsStr.split(",").map(s => s.trim()); | 
					
						
							| 
									
										
										
										
											2018-01-12 22:44:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-16 20:11:50 -05:00
										 |  |  |     const pollingIntervalInSeconds = config['Reddit']['pollingIntervalInSeconds'] || (4 * 3600); | 
					
						
							| 
									
										
										
										
											2018-01-12 22:44:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 22:51:39 -05:00
										 |  |  |     setInterval(runImport, pollingIntervalInSeconds * 1000); | 
					
						
							| 
									
										
										
										
											2018-01-16 20:11:50 -05:00
										 |  |  |     setTimeout(runImport, 10000); // 10 seconds after startup - intentionally after initial sync
 | 
					
						
							| 
									
										
										
										
											2018-01-11 23:54:17 -05:00
										 |  |  | }); |