Skip to content

Commit

Permalink
v0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
komplexb committed Oct 22, 2020
1 parent 927b199 commit 1cd02fa
Show file tree
Hide file tree
Showing 6 changed files with 14,980 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ npm install
npm run dev

# build electron app for production
# requires node 6
npm run build

# lint all JS/Vue component files in `app/src`
Expand Down
4 changes: 2 additions & 2 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Notifyer",
"productName": "Notifyer",
"version": "0.0.7",
"version": "0.0.8",
"description": "Notifyer Desktop",
"main": "electron.js",
"dependencies": {
Expand All @@ -26,4 +26,4 @@
"email": "byronbuckley@live.com",
"url": "https://github.com/komplexb/notifyer-electron"
}
}
}
38 changes: 30 additions & 8 deletions app/src/components/Note.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</div>
</template>
<template v-else>
<em>{{body}}</em>
<div v-html="body"></div>
</template>
</div>
</div>
Expand All @@ -45,7 +45,7 @@
const { ipcRenderer: ipc, shell } = require('electron')
const path = require('path')
const storage = require('../main/store')
const { getRandomNote } = require('../main/onenote')
const { getRandomNote, getNoteContents } = require('../main/onenote')
const { URLS } = require('../../app.config')
const {app} = require('electron').remote
const storeSettings = require('node-persist')
Expand Down Expand Up @@ -126,12 +126,29 @@
shell.openExternal((storeSettings.getItemSync('openWith') === 'OneNote') ? oneNoteClientUrl.href : oneNoteWebUrl.href)
},
setNote (note) {
const { title, noteLinks, preview: {previewText, links} } = note
this.title = title
this.body = previewText
this.imgUrl = (links.previewImageUrl.href === null) ? URLS.THUMBNAIL : links.previewImageUrl.href
this.noteLinks = noteLinks
this.loading = false // hide loading overlay
// eslint-disable-line
const { title, noteLinks, url, preview: {links} } = note
getNoteContents(url)
.then((page) => {
// extract page body content from note
const parser = new DOMParser()
const wrapper = parser.parseFromString(page.content, 'text/html')
// remove images
const el = wrapper.querySelector('div')
for (let img of el.getElementsByTagName('img')) {
el.removeChild(img)
}
this.title = title
this.body = el.innerHTML
this.imgUrl = (links.previewImageUrl.href === null) ? URLS.THUMBNAIL : links.previewImageUrl.href
this.noteLinks = noteLinks
this.loading = false // hide loading overlay
})
.catch(function (err) {
console.error(err)
})
}
}
}
Expand Down Expand Up @@ -181,6 +198,11 @@
/*justify-content: center;*/
}
/* don't allow links to be clicked*/
#note .description a {
pointer-events: none;
}
.title-area {
display: flex;
}
Expand Down
87 changes: 65 additions & 22 deletions app/src/main/onenote.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* global alert */
const {app} = require('electron').remote
const { app } = require('electron').remote
const path = require('path')
const apiRequests = require('superagent')
const is = require('electron-is')
const {URLS, TIMEOUTS} = require('../../app.config')
const {refreshOneNoteToken} = require('./auth')
const { URLS, TIMEOUTS } = require('../../app.config')
const { refreshOneNoteToken } = require('./auth')
const storage = require('./store')

const storeSettings = require('node-persist')
Expand All @@ -29,34 +29,45 @@ function getRandomNote () {
return refreshOneNoteToken()
.then(hasNoteSection)
.then(p)
.catch((err) => {
.catch(err => {
console.log(err)
})

function hasNoteSection () {
return Promise.resolve()
.then(() => {
if (storage.getItem('onenote_section_id') === null) {
return setNoteSection(storeSettings.getItemSync('sectionName'))
}
})
return Promise.resolve().then(() => {
if (storage.getItem('onenote_section_id') === null) {
return setNoteSection(storeSettings.getItemSync('sectionName'))
}
})
}

function p () {
return new Promise((resolve, reject) => {
const onenote_section_id = storage.getItem('onenote_section_id')
const {access_token} = storage.getItem('onenote')
const { access_token } = storage.getItem('onenote')
// instead of showing the first/last 100 records,
// randomly select a starting point and get the next 100 results
const skip = chance.natural({ min: 0, max: storage.getItem('onenote_section_count') || 1 })

// https://docs.microsoft.com/en-us/graph/onenote-get-content#example-get-requests
apiRequests
.get(`${URLS.SECTION}${onenote_section_id}/pages`)
.query({select: 'title,links,self'})
.query({
select: 'title,links,self', // fields to return
count: true, // show the amount of pages in section
top: 100, // maximum pages query can return
skip // The number of entries to skip in the result set.
})
.timeout(TIMEOUTS)
.set('Authorization', `Bearer ${access_token}`)
.then(function (response) {
if (response && response.ok) {
const notes = response.body.value
console.log('notes', notes.length)
const note = notes[chance.natural({min: 0, max: (notes.length - 1)})]
storage.setItem('onenote_section_count', response.body['@odata.count'])
// TODO
// if notes is zero REJECT or RETRY
const noteIndex = chance.natural({ min: 0, max: notes.length - 1 })
const note = notes[noteIndex]
resolve(getNotePreview(note))
} else {
console.log(response)
Expand All @@ -80,26 +91,28 @@ function getRandomNote () {
function setNoteSection (sectionName) {
return refreshOneNoteToken()
.then(p)
.catch((err) => {
.catch(err => {
console.log(err)
})

function p () {
return new Promise((resolve, reject) => {
const {access_token} = storage.getItem('onenote')
const { access_token } = storage.getItem('onenote')

apiRequests
.get(`${URLS.SECTION}`)
.query({filter: `name eq '${sectionName}'`})
.query({ filter: `name eq '${sectionName}'` })
.timeout(TIMEOUTS)
.set({
'Authorization': `Bearer ${access_token}`,
'FavorDataRecency': is.dev() ? 'false' : 'true'
Authorization: `Bearer ${access_token}`,
FavorDataRecency: is.dev() ? 'false' : 'true'
})
.then(function (response) {
if (response && response.ok) {
if (response.body.value.length === 0) {
alert(`Please create a section with the name '${sectionName}' and restart the app.`)
alert(
`Please create a section with the name '${sectionName}' and restart the app.`
)
reject()
} else {
storage.setItem('onenote_section_id', response.body.value[0].id)
Expand Down Expand Up @@ -135,15 +148,44 @@ function getNotePreview (note) {
const { links, self: url, title } = note

return new Promise((resolve, reject) => {
const {access_token} = storage.getItem('onenote')
const { access_token } = storage.getItem('onenote')

apiRequests
.get(`${url}/preview`)
.timeout(TIMEOUTS)
.set('Authorization', `Bearer ${access_token}`)
.then(function (response) {
if (response && response.ok) {
resolve({title, preview: response.body, noteLinks: links})
resolve({ title, preview: response.body, noteLinks: links, url })
} else {
console.log(response)
reject(response)
}
})
.catch(function (err) {
console.log(err)
reject(err)
})
})
}

/**
* https://dev.onenote.com/docs#/reference/get-pages/v10menotespagesidpreview/get
*
* @param {Object} note
* @returns {Promise} Description
*/
function getNoteContents (url) {
return new Promise((resolve, reject) => {
const { access_token } = storage.getItem('onenote')

apiRequests
.get(`${url}/content`)
.timeout(TIMEOUTS)
.set('Authorization', `Bearer ${access_token}`)
.then(function (response) {
if (response && response.ok) {
resolve({ content: response.text })
} else {
console.log(response)
reject(response)
Expand All @@ -158,5 +200,6 @@ function getNotePreview (note) {

module.exports = {
getRandomNote: getRandomNote,
getNoteContents: getNoteContents,
setNoteSection: setNoteSection
}
Loading

0 comments on commit 1cd02fa

Please sign in to comment.