mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 13:01:31 +08:00 
			
		
		
		
	creating / updating notes_image rows
This commit is contained in:
		
							parent
							
								
									c0e45a73a8
								
							
						
					
					
						commit
						edc9a1a2bf
					
				| @ -135,25 +135,7 @@ async function protectNoteHistory(noteId, dataKey, protect, sourceId) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function updateNote(noteId, newNote, dataKey, sourceId) { | async function saveNoteHistory(noteId, dataKey, sourceId, nowStr) { | ||||||
|     if (newNote.detail.is_protected) { |  | ||||||
|         await encryptNote(newNote, dataKey); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     const now = new Date(); |  | ||||||
|     const nowStr = utils.nowDate(); |  | ||||||
| 
 |  | ||||||
|     const historySnapshotTimeInterval = parseInt(await options.getOption('history_snapshot_time_interval')); |  | ||||||
| 
 |  | ||||||
|     const historyCutoff = utils.dateStr(new Date(now.getTime() - historySnapshotTimeInterval * 1000)); |  | ||||||
| 
 |  | ||||||
|     const existingNoteHistoryId = await sql.getFirstValue( |  | ||||||
|         "SELECT note_history_id FROM notes_history WHERE note_id = ? AND date_modified_to >= ?", [noteId, historyCutoff]); |  | ||||||
| 
 |  | ||||||
|     await sql.doInTransaction(async () => { |  | ||||||
|         const msSinceDateCreated = now.getTime() - utils.parseDate(newNote.detail.date_created).getTime(); |  | ||||||
| 
 |  | ||||||
|         if (!existingNoteHistoryId && msSinceDateCreated >= historySnapshotTimeInterval * 1000) { |  | ||||||
|     const oldNote = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId]); |     const oldNote = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId]); | ||||||
| 
 | 
 | ||||||
|     if (oldNote.is_protected) { |     if (oldNote.is_protected) { | ||||||
| @ -174,7 +156,80 @@ async function updateNote(noteId, newNote, dataKey, sourceId) { | |||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     await sync_table.addNoteHistorySync(newNoteHistoryId, sourceId); |     await sync_table.addNoteHistorySync(newNoteHistoryId, sourceId); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function saveNoteImages(noteId, noteText, sourceId) { | ||||||
|  |     const existingNoteImages = await sql.getAll("SELECT * FROM notes_image WHERE note_id = ?", [noteId]); | ||||||
|  |     const foundImageIds = []; | ||||||
|  |     const now = utils.nowDate(); | ||||||
|  |     const re = /src="\/api\/images\/([a-zA-Z0-9]+)\//g; | ||||||
|  |     let match; | ||||||
|  | 
 | ||||||
|  |     while (match = re.exec(noteText)) { | ||||||
|  |         console.log(match); | ||||||
|  | 
 | ||||||
|  |         const imageId = match[1]; | ||||||
|  |         const existingNoteImage = existingNoteImages.find(ni => ni.image_id === imageId); | ||||||
|  | 
 | ||||||
|  |         if (!existingNoteImage) { | ||||||
|  |             const noteImageId = utils.newNoteImageId(); | ||||||
|  | 
 | ||||||
|  |             await sql.insert("notes_image", { | ||||||
|  |                 note_image_id: noteImageId, | ||||||
|  |                 note_id: noteId, | ||||||
|  |                 image_id: imageId, | ||||||
|  |                 is_deleted: 0, | ||||||
|  |                 date_modified: now, | ||||||
|  |                 date_created: now | ||||||
|  |             }); | ||||||
|  | 
 | ||||||
|  |             await sync_table.addNoteImageSync(noteImageId, sourceId); | ||||||
|         } |         } | ||||||
|  |         else if (existingNoteImage.is_deleted) { | ||||||
|  |             await sql.execute("UPDATE notes_image SET is_deleted = 0, date_modified = ? WHERE note_image_id = ?", | ||||||
|  |                 [now, existingNoteImage.note_image_id]); | ||||||
|  | 
 | ||||||
|  |             await sync_table.addNoteImageSync(existingNoteImage.note_image_id, sourceId); | ||||||
|  |         } | ||||||
|  |         // else we don't need to do anything
 | ||||||
|  | 
 | ||||||
|  |         foundImageIds.push(imageId); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // marking note images as deleted if they are not present on the page anymore
 | ||||||
|  |     const unusedNoteImages = existingNoteImages.filter(ni => !foundImageIds.includes(ni.image_id)); | ||||||
|  | 
 | ||||||
|  |     for (const unusedNoteImage of unusedNoteImages) { | ||||||
|  |         await sql.execute("UPDATE notes_image SET is_deleted = 1, date_modified = ? WHERE note_image_id = ?", | ||||||
|  |             [now, unusedNoteImage.note_image_id]); | ||||||
|  | 
 | ||||||
|  |         await sync_table.addNoteImageSync(unusedNoteImage.note_image_id, sourceId); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function updateNote(noteId, newNote, dataKey, sourceId) { | ||||||
|  |     if (newNote.detail.is_protected) { | ||||||
|  |         await encryptNote(newNote, dataKey); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const now = new Date(); | ||||||
|  |     const nowStr = utils.nowDate(); | ||||||
|  | 
 | ||||||
|  |     const historySnapshotTimeInterval = parseInt(await options.getOption('history_snapshot_time_interval')); | ||||||
|  | 
 | ||||||
|  |     const historyCutoff = utils.dateStr(new Date(now.getTime() - historySnapshotTimeInterval * 1000)); | ||||||
|  | 
 | ||||||
|  |     const existingNoteHistoryId = await sql.getFirstValue( | ||||||
|  |         "SELECT note_history_id FROM notes_history WHERE note_id = ? AND date_modified_to >= ?", [noteId, historyCutoff]); | ||||||
|  | 
 | ||||||
|  |     await sql.doInTransaction(async () => { | ||||||
|  |         const msSinceDateCreated = now.getTime() - utils.parseDate(newNote.detail.date_created).getTime(); | ||||||
|  | 
 | ||||||
|  |         if (!existingNoteHistoryId && msSinceDateCreated >= historySnapshotTimeInterval * 1000) { | ||||||
|  |             await saveNoteHistory(noteId, dataKey, sourceId, nowStr); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         await saveNoteImages(noteId, newNote.detail.note_text, sourceId); | ||||||
| 
 | 
 | ||||||
|         await protectNoteHistory(noteId, dataKey, newNote.detail.is_protected); |         await protectNoteHistory(noteId, dataKey, newNote.detail.is_protected); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -32,8 +32,8 @@ async function addImageSync(imageId, sourceId) { | |||||||
|     await addEntitySync("images", imageId, sourceId); |     await addEntitySync("images", imageId, sourceId); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function addNoteImageSync(imageId, sourceId) { | async function addNoteImageSync(noteImageId, sourceId) { | ||||||
|     await addEntitySync("notes_image", imageId, sourceId); |     await addEntitySync("notes_image", noteImageId, sourceId); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function addEntitySync(entityName, entityId, sourceId) { | async function addEntitySync(entityName, entityId, sourceId) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 azivner
						azivner