mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 13:01:31 +08:00 
			
		
		
		
	refactored "date note" functions out of reddit plugin
This commit is contained in:
		
							parent
							
								
									af34b378e2
								
							
						
					
					
						commit
						aad5ea577f
					
				| @ -9,11 +9,7 @@ const unescape = require('unescape'); | |||||||
| const attributes = require('../services/attributes'); | const attributes = require('../services/attributes'); | ||||||
| const sync_mutex = require('../services/sync_mutex'); | const sync_mutex = require('../services/sync_mutex'); | ||||||
| const config = require('../services/config'); | const config = require('../services/config'); | ||||||
| 
 | const date_notes = require('../services/date_notes'); | ||||||
| const CALENDAR_ROOT_ATTRIBUTE = 'calendar_root'; |  | ||||||
| const YEAR_ATTRIBUTE = 'year_note'; |  | ||||||
| const MONTH_ATTRIBUTE = 'month_note'; |  | ||||||
| const DATE_ATTRIBUTE = 'date_note'; |  | ||||||
| 
 | 
 | ||||||
| // "reddit" date note is subnote of date note which contains all reddit comments from that date
 | // "reddit" date note is subnote of date note which contains all reddit comments from that date
 | ||||||
| const REDDIT_DATE_ATTRIBUTE = 'reddit_date_note'; | const REDDIT_DATE_ATTRIBUTE = 'reddit_date_note'; | ||||||
| @ -31,97 +27,13 @@ function redditId(kind, id) { | |||||||
|     return kind + "_" + id; |     return kind + "_" + id; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function getNoteStartingWith(parentNoteId, startsWith) { |  | ||||||
|     return await sql.getFirstValue(`SELECT note_id FROM notes JOIN notes_tree USING(note_id) 
 |  | ||||||
|                                     WHERE parent_note_id = ? AND note_title LIKE '${startsWith}%' |  | ||||||
|                                     AND notes.is_deleted = 0 AND is_protected = 0  |  | ||||||
|                                     AND notes_tree.is_deleted = 0`, [parentNoteId]);
 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| async function getRootNoteId() { |  | ||||||
|     let rootNoteId = await sql.getFirstValue(`SELECT notes.note_id FROM notes JOIN attributes USING(note_id) 
 |  | ||||||
|               WHERE attributes.name = '${CALENDAR_ROOT_ATTRIBUTE}' AND notes.is_deleted = 0`);
 |  | ||||||
| 
 |  | ||||||
|     if (!rootNoteId) { |  | ||||||
|         rootNoteId = (await notes.createNewNote('root', { |  | ||||||
|             note_title: 'Calendar', |  | ||||||
|             target: 'into', |  | ||||||
|             is_protected: false |  | ||||||
|         })).noteId; |  | ||||||
| 
 |  | ||||||
|         await attributes.createAttribute(rootNoteId, CALENDAR_ROOT_ATTRIBUTE); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     return rootNoteId; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| async function getYearNoteId(dateTimeStr, rootNoteId) { |  | ||||||
|     const yearStr = dateTimeStr.substr(0, 4); |  | ||||||
| 
 |  | ||||||
|     let yearNoteId = await attributes.getNoteIdWithAttribute(YEAR_ATTRIBUTE, yearStr); |  | ||||||
| 
 |  | ||||||
|     if (!yearNoteId) { |  | ||||||
|         yearNoteId = await getNoteStartingWith(rootNoteId, yearStr); |  | ||||||
| 
 |  | ||||||
|         if (!yearNoteId) { |  | ||||||
|             yearNoteId = await createNote(rootNoteId, yearStr); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         await attributes.createAttribute(yearNoteId, YEAR_ATTRIBUTE, yearStr); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     return yearNoteId; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| async function getMonthNoteId(dateTimeStr, rootNoteId) { |  | ||||||
|     const monthStr = dateTimeStr.substr(0, 7); |  | ||||||
|     const monthNumber = dateTimeStr.substr(5, 2); |  | ||||||
| 
 |  | ||||||
|     let monthNoteId = await attributes.getNoteIdWithAttribute(MONTH_ATTRIBUTE, monthStr); |  | ||||||
| 
 |  | ||||||
|     if (!monthNoteId) { |  | ||||||
|         const yearNoteId = await getYearNoteId(dateTimeStr, rootNoteId); |  | ||||||
| 
 |  | ||||||
|         monthNoteId = await getNoteStartingWith(yearNoteId, monthNumber); |  | ||||||
| 
 |  | ||||||
|         if (!monthNoteId) { |  | ||||||
|             monthNoteId = await createNote(yearNoteId, monthNumber); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         await attributes.createAttribute(monthNoteId, MONTH_ATTRIBUTE, monthStr); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     return monthNoteId; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| async function getDateNoteId(dateTimeStr, rootNoteId) { |  | ||||||
|     const dateStr = dateTimeStr.substr(0, 10); |  | ||||||
|     const dayNumber = dateTimeStr.substr(8, 2); |  | ||||||
| 
 |  | ||||||
|     let dateNoteId = await attributes.getNoteIdWithAttribute(DATE_ATTRIBUTE, dateStr); |  | ||||||
| 
 |  | ||||||
|     if (!dateNoteId) { |  | ||||||
|         const monthNoteId = await getMonthNoteId(dateTimeStr, rootNoteId); |  | ||||||
| 
 |  | ||||||
|         dateNoteId = await getNoteStartingWith(monthNoteId, dayNumber); |  | ||||||
| 
 |  | ||||||
|         if (!dateNoteId) { |  | ||||||
|             dateNoteId = await createNote(monthNoteId, dayNumber); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         await attributes.createAttribute(dateNoteId, DATE_ATTRIBUTE, dateStr); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     return dateNoteId; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| async function getDateNoteIdForReddit(dateTimeStr, rootNoteId) { | async function getDateNoteIdForReddit(dateTimeStr, rootNoteId) { | ||||||
|     const dateStr = dateTimeStr.substr(0, 10); |     const dateStr = dateTimeStr.substr(0, 10); | ||||||
| 
 | 
 | ||||||
|     let redditDateNoteId = await attributes.getNoteIdWithAttribute(REDDIT_DATE_ATTRIBUTE, dateStr); |     let redditDateNoteId = await attributes.getNoteIdWithAttribute(REDDIT_DATE_ATTRIBUTE, dateStr); | ||||||
| 
 | 
 | ||||||
|     if (!redditDateNoteId) { |     if (!redditDateNoteId) { | ||||||
|         const dateNoteId = await getDateNoteId(dateTimeStr, rootNoteId); |         const dateNoteId = await date_notes.getDateNoteId(dateTimeStr, rootNoteId); | ||||||
| 
 | 
 | ||||||
|         redditDateNoteId = await createNote(dateNoteId, "Reddit"); |         redditDateNoteId = await createNote(dateNoteId, "Reddit"); | ||||||
| 
 | 
 | ||||||
| @ -196,7 +108,7 @@ karma: ${comment.score}, created at ${dateTimeStr}</p><p></p>` | |||||||
| let redditAccounts = []; | let redditAccounts = []; | ||||||
| 
 | 
 | ||||||
| async function runImport() { | async function runImport() { | ||||||
|     const rootNoteId = await getRootNoteId(); |     const rootNoteId = await date_notes.getRootNoteId(); | ||||||
| 
 | 
 | ||||||
|     // technically mutex shouldn't be necessary but we want to avoid doing potentially expensive import
 |     // technically mutex shouldn't be necessary but we want to avoid doing potentially expensive import
 | ||||||
|     // concurrently with sync
 |     // concurrently with sync
 | ||||||
|  | |||||||
							
								
								
									
										110
									
								
								services/date_notes.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										110
									
								
								services/date_notes.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,110 @@ | |||||||
|  | "use strict"; | ||||||
|  | 
 | ||||||
|  | const sql = require('./sql'); | ||||||
|  | const notes = require('./notes'); | ||||||
|  | const attributes = require('./attributes'); | ||||||
|  | 
 | ||||||
|  | const CALENDAR_ROOT_ATTRIBUTE = 'calendar_root'; | ||||||
|  | const YEAR_ATTRIBUTE = 'year_note'; | ||||||
|  | const MONTH_ATTRIBUTE = 'month_note'; | ||||||
|  | const DATE_ATTRIBUTE = 'date_note'; | ||||||
|  | 
 | ||||||
|  | async function createNote(parentNoteId, noteTitle, noteText) { | ||||||
|  |     return (await notes.createNewNote(parentNoteId, { | ||||||
|  |         note_title: noteTitle, | ||||||
|  |         note_text: noteText, | ||||||
|  |         target: 'into', | ||||||
|  |         is_protected: false | ||||||
|  |     })).noteId; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function getNoteStartingWith(parentNoteId, startsWith) { | ||||||
|  |     return await sql.getFirstValue(`SELECT note_id FROM notes JOIN notes_tree USING(note_id) 
 | ||||||
|  |                                     WHERE parent_note_id = ? AND note_title LIKE '${startsWith}%' | ||||||
|  |                                     AND notes.is_deleted = 0 AND is_protected = 0  | ||||||
|  |                                     AND notes_tree.is_deleted = 0`, [parentNoteId]);
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function getRootNoteId() { | ||||||
|  |     let rootNoteId = await sql.getFirstValue(`SELECT notes.note_id FROM notes JOIN attributes USING(note_id) 
 | ||||||
|  |               WHERE attributes.name = '${CALENDAR_ROOT_ATTRIBUTE}' AND notes.is_deleted = 0`);
 | ||||||
|  | 
 | ||||||
|  |     if (!rootNoteId) { | ||||||
|  |         rootNoteId = (await notes.createNewNote('root', { | ||||||
|  |             note_title: 'Calendar', | ||||||
|  |             target: 'into', | ||||||
|  |             is_protected: false | ||||||
|  |         })).noteId; | ||||||
|  | 
 | ||||||
|  |         await attributes.createAttribute(rootNoteId, CALENDAR_ROOT_ATTRIBUTE); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return rootNoteId; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function getYearNoteId(dateTimeStr, rootNoteId) { | ||||||
|  |     const yearStr = dateTimeStr.substr(0, 4); | ||||||
|  | 
 | ||||||
|  |     let yearNoteId = await attributes.getNoteIdWithAttribute(YEAR_ATTRIBUTE, yearStr); | ||||||
|  | 
 | ||||||
|  |     if (!yearNoteId) { | ||||||
|  |         yearNoteId = await getNoteStartingWith(rootNoteId, yearStr); | ||||||
|  | 
 | ||||||
|  |         if (!yearNoteId) { | ||||||
|  |             yearNoteId = await createNote(rootNoteId, yearStr); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         await attributes.createAttribute(yearNoteId, YEAR_ATTRIBUTE, yearStr); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return yearNoteId; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function getMonthNoteId(dateTimeStr, rootNoteId) { | ||||||
|  |     const monthStr = dateTimeStr.substr(0, 7); | ||||||
|  |     const monthNumber = dateTimeStr.substr(5, 2); | ||||||
|  | 
 | ||||||
|  |     let monthNoteId = await attributes.getNoteIdWithAttribute(MONTH_ATTRIBUTE, monthStr); | ||||||
|  | 
 | ||||||
|  |     if (!monthNoteId) { | ||||||
|  |         const yearNoteId = await getYearNoteId(dateTimeStr, rootNoteId); | ||||||
|  | 
 | ||||||
|  |         monthNoteId = await getNoteStartingWith(yearNoteId, monthNumber); | ||||||
|  | 
 | ||||||
|  |         if (!monthNoteId) { | ||||||
|  |             monthNoteId = await createNote(yearNoteId, monthNumber); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         await attributes.createAttribute(monthNoteId, MONTH_ATTRIBUTE, monthStr); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return monthNoteId; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function getDateNoteId(dateTimeStr, rootNoteId) { | ||||||
|  |     const dateStr = dateTimeStr.substr(0, 10); | ||||||
|  |     const dayNumber = dateTimeStr.substr(8, 2); | ||||||
|  | 
 | ||||||
|  |     let dateNoteId = await attributes.getNoteIdWithAttribute(DATE_ATTRIBUTE, dateStr); | ||||||
|  | 
 | ||||||
|  |     if (!dateNoteId) { | ||||||
|  |         const monthNoteId = await getMonthNoteId(dateTimeStr, rootNoteId); | ||||||
|  | 
 | ||||||
|  |         dateNoteId = await getNoteStartingWith(monthNoteId, dayNumber); | ||||||
|  | 
 | ||||||
|  |         if (!dateNoteId) { | ||||||
|  |             dateNoteId = await createNote(monthNoteId, dayNumber); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         await attributes.createAttribute(dateNoteId, DATE_ATTRIBUTE, dateStr); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return dateNoteId; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module.exports = { | ||||||
|  |     getRootNoteId, | ||||||
|  |     getYearNoteId, | ||||||
|  |     getMonthNoteId, | ||||||
|  |     getDateNoteId | ||||||
|  | }; | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 azivner
						azivner