You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
6.6 KiB
274 lines
6.6 KiB
import DatabaseHandler from './databaseHandler.js'
|
|
import FileHandler from './fileHandler.js'
|
|
import TagsDatabase from './tags.js'
|
|
|
|
const { ipcRenderer } = require('electron')
|
|
|
|
/**
|
|
* apps
|
|
*
|
|
* @author Björn Hase
|
|
* @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3
|
|
* @link https://gitea.node001.net/HerrHase/tellme-bot.git
|
|
*
|
|
*/
|
|
class AppsDatabase extends DatabaseHandler {
|
|
|
|
constructor() {
|
|
super('apps')
|
|
|
|
// add index for apps to sort by name
|
|
this.createIndex([
|
|
'name'
|
|
], 'name-sort')
|
|
|
|
// add index for apps to sort by date_started
|
|
this.createIndex([
|
|
'date_started'
|
|
], 'date-started-sort')
|
|
}
|
|
|
|
/**
|
|
* create app
|
|
*
|
|
* @param {object} data
|
|
* @return {object}
|
|
*
|
|
*/
|
|
async create(data) {
|
|
|
|
// default for date_started
|
|
data.date_started = 0
|
|
|
|
if (data.thumbnail) {
|
|
data.thumbnail = await this.createOrUpdateFile(data.thumbnail)
|
|
}
|
|
|
|
return this.db.post(data)
|
|
.then((response) => {
|
|
|
|
// if tags are in data update tags also in database
|
|
if (data.tags) {
|
|
const tagsDatabase = new TagsDatabase()
|
|
tagsDatabase.update(data.tags)
|
|
}
|
|
|
|
return this.findOneById(response._id)
|
|
}).catch((error) => {
|
|
|
|
// @TODO handle errors from pouchdb
|
|
console.error(error)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* create or update file with fileHandler
|
|
*
|
|
*
|
|
* @param {[type]} file
|
|
* @param {[type]} entry
|
|
* @param {[type]} key
|
|
* @return {mixed}
|
|
*
|
|
*/
|
|
async createOrUpdateFile(file, entry, key) {
|
|
|
|
// fileHandler
|
|
const fileHandler = new FileHandler(ipcRenderer.sendSync('getPathUserData') + '/attachments')
|
|
|
|
const reader = new FileReader()
|
|
|
|
// if file is blob use readFileFromBlob to get data
|
|
if (file instanceof Blob) {
|
|
|
|
file.data = await fileHandler.readFileFromBlob(file)
|
|
|
|
if (entry && entry[key]) {
|
|
fileHandler.remove(entry[key])
|
|
}
|
|
|
|
file = fileHandler.put(file.name, file.type, file.data)
|
|
}
|
|
|
|
return file
|
|
}
|
|
|
|
/**
|
|
* update app
|
|
*
|
|
* @param {object} data
|
|
* @return {object}
|
|
*
|
|
*/
|
|
async update(data) {
|
|
|
|
const entry = await this.findOneById(data._id)
|
|
|
|
if (data.thumbnail) {
|
|
data.thumbnail = await this.createOrUpdateFile(data.thumbnail, entry, 'thumbnail')
|
|
}
|
|
|
|
return this.db.put(data)
|
|
.then(async (response) => {
|
|
|
|
const tagsDatabase = new TagsDatabase()
|
|
|
|
// if tags are in data update tags also in database
|
|
if (data.tags && data.tags.length > 0) {
|
|
tagsDatabase.update(data.tags)
|
|
} else {
|
|
await tagsDatabase.removeNotNeeded()
|
|
}
|
|
|
|
return this.findOneById(response._id)
|
|
}).catch((error) => {
|
|
|
|
// @TODO handle errors from pouchdb
|
|
console.error(error)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* remove app
|
|
*
|
|
* @param {object} document
|
|
* @return {object}
|
|
*
|
|
*/
|
|
async remove(document) {
|
|
|
|
const entry = await this.findOneById(document._id)
|
|
|
|
if (entry.thumbnail) {
|
|
const fileHandler = new FileHandler(ipcRenderer.sendSync('getPathUserData') + '/attachments')
|
|
fileHandler.remove(entry.thumbnail)
|
|
}
|
|
|
|
return this.db.remove(document)
|
|
.then(async (response) => {
|
|
|
|
// successfull remove document, check for tags that
|
|
// not longer used
|
|
if (response.ok === true) {
|
|
const tagsDatabase = new TagsDatabase()
|
|
await tagsDatabase.removeNotNeeded()
|
|
}
|
|
|
|
return response
|
|
}).catch((error) => {
|
|
|
|
// @TODO handle errors from pouchdb
|
|
console.error(error)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* find one app by id
|
|
*
|
|
* @param {string} id
|
|
* @return {mixed}
|
|
*
|
|
*/
|
|
findOneById(id) {
|
|
const query = {
|
|
'fields': [
|
|
'_id',
|
|
'_rev',
|
|
'name',
|
|
'command',
|
|
'description',
|
|
'thumbnail',
|
|
'tags',
|
|
'date_started'
|
|
],
|
|
'selector': {
|
|
'_id' : id
|
|
}
|
|
}
|
|
|
|
return this.db.find(query).then((documents) => {
|
|
if (documents.docs.length === 0) {
|
|
return null
|
|
} else {
|
|
return documents.docs[0]
|
|
}
|
|
}).catch((error) => {
|
|
|
|
// @TODO handle errors from pouchdb
|
|
console.error(error)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* find apps
|
|
*
|
|
* @return {mixed}
|
|
*
|
|
*/
|
|
async find(parameters) {
|
|
|
|
const query = {
|
|
'selector': {
|
|
|
|
},
|
|
'fields': [
|
|
'_id',
|
|
'_rev',
|
|
'name',
|
|
'command',
|
|
'description',
|
|
'thumbnail',
|
|
'tags',
|
|
'date_started'
|
|
]
|
|
}
|
|
|
|
// adding sort from parameters
|
|
if (parameters.sort) {
|
|
query.sort = []
|
|
query.sort.push(parameters.sort)
|
|
|
|
// if sort is date_stared change 'date-started-sort'
|
|
if (parameters.sort === 'date_started') {
|
|
query.use_index = '_design/date-started-sort'
|
|
query.selector = {
|
|
'date_started': {
|
|
'$exists': true
|
|
}
|
|
}
|
|
|
|
// @TODO refactor this, change logic in filter
|
|
query.sort = [{
|
|
'date_started': 'desc'
|
|
}]
|
|
|
|
// if sort is name change 'name-sort'
|
|
} else {
|
|
query.use_index = '_design/name-sort'
|
|
query.selector = {
|
|
'name': {
|
|
'$regex': ''
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// adding tags from parameters
|
|
if (parameters.tags.length > 0) {
|
|
query.selector.tags = {
|
|
$in: parameters.tags
|
|
}
|
|
}
|
|
|
|
return this.db.find(query).then(async (documents) => {
|
|
return documents.docs
|
|
}).catch((error) => {
|
|
|
|
// @TODO handle errors from pouchdb
|
|
console.error(error)
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
export default AppsDatabase |