decrypt notes

This commit is contained in:
2023-05-22 20:48:53 +02:00
parent d45ceb9b41
commit b7e5da2354
11 changed files with 152 additions and 24 deletions

View File

@@ -1,10 +1,12 @@
import { defaultNotes } from '@/utils/defaultNotes'
import { viewModes, activeViewMode } from '@/composables/useViewMode'
import shortid from 'shortid'
import { useTitle } from '@vueuse/core'
import { doc, getDoc } from 'firebase/firestore'
import { viewModes, activeViewMode } from '@/composables/useViewMode'
import { initialized, user, db } from '@/composables/useFirebase'
import { decryptNotes, getEncryptionKey } from '@/composables/useEncryption'
import { defaultNotes } from '@/utils/defaultNotes'
import { mdToHtml } from '@/utils/markdown'
import { getAllMatches } from '@/utils/helpers'
import { initialized, user } from '@/composables/useFirebase'
import shortid from 'shortid'
const notesSources = computed(() => ({
local: true,
@@ -188,21 +190,45 @@ export function getNoteReferences(note: Note) {
: []
}
const parseBaseNotes = (notes: BaseNotes): BaseNotes => {
return Object.fromEntries(
Object.entries(notes).map(([noteId, note]) => {
return [
noteId,
{
id: noteId,
title: note.title,
content: note.content,
created: note.created,
modified: note.modified,
isRoot: note.isRoot
}
]
})
)
}
watch(
activeNotesSource,
() => {
async () => {
if (!activeNotesSource.value) return
baseNotes.value = {}
let notes: BaseNotes = {}
if (activeNotesSource.value === 'local') {
try {
const localNotes = JSON.parse(localStorage.getItem('notes') || '{}')
baseNotes.value = localNotes
notes = JSON.parse(localStorage.getItem('notes') || '{}')
} catch (error) {
console.log(error)
}
} else if (activeNotesSource.value === 'firebase') {
console.log('get notes from firebase')
} else if (activeNotesSource.value === 'firebase' && db.value) {
const firebaseNotes = (
await getDoc(doc(db.value, 'pages', user.value?.uid || ''))
).data() as { [noteId: string]: any }
const encryptionKey = await getEncryptionKey()
notes = encryptionKey ? decryptNotes(firebaseNotes, encryptionKey) : firebaseNotes
console.log('get notes from firebase', notes)
}
baseNotes.value = parseBaseNotes(notes)
if (!rootNote.value) insertDefaultNotes(defaultNotes)
setActiveNote(rootNote.value?.id)