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.
potato-launcher/src/js/database/tags.js

169 lines
3.9 KiB

import DatabaseHandler from './databaseHandler.js'
import AppsStore from './apps.js'
import tagsStore from './../stores/tags.js'
/**
* tags
*
* @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 TagsDatabase extends DatabaseHandler {
constructor() {
super('tags')
// add index for apps
this.createIndex([
'name'
], 'tags')
}
/**
* adding new tags, if tags are already
* exists skip tag
*
* @param {object} data
*
*/
update(tags) {
const query = {
'selector': {
'name': {
'$in': tags
}
}, 'fields': [
'name'
]
}
// check for existings tags and remove
// adding tags that are not removed
this.db.find(query).then((documents) => {
return this.filterTags(documents, tags)
}).then((tags) => {
if (tags.length > 0) {
// adding tags to entries
for (let i = 0; i < tags.length; i++) {
this.db.post({
'name': tags[i]
}).then(() => {
// last tag, then check for remove
if (i === (tags.length - 1)) {
this.removeNotNeeded()
}
})
}
}
})
}
/**
* search for tags in apps and remove
* tags that not longer exists in apps
*
*
*/
removeNotNeeded() {
const query = {
'selector': {
'name': {
'$exists': true
}
}, 'fields': [
'_id',
'_rev',
'name'
]
}
this.db.find(query).then((tags) => {
if (tags.docs.length > 0) {
const appsStore = new AppsStore()
for (let i = 0; i < tags.docs.length; i++) {
// create parameters
const parameters = {
tags: [],
sort: 'name'
}
// add tag name to parameters
parameters.tags.push(tags.docs[i].name)
// find app by tag, if no app is found, remove tag and update filter
appsStore.find(parameters).then((apps) => {
if (apps.length === 0) {
this.db.remove(tags.docs[i]).then(() => {
tagsStore.get()
})
}
})
}
}
})
}
/**
* filter tags, if they are already exists
*
* @param {array} documents
* @param {array} tags
* @return {array}
*
*/
filterTags(documents, tags) {
if (documents.docs.length > 0) {
for (let i = 0; i < documents.docs.length; i++) {
// check for name in tags
const index = tags.indexOf(documents.docs[i].name)
// if found remove from tags
if (index >= 0) {
tags.splice(index, 1)
}
}
}
return tags
}
/**
* find apps
*
* @return {array}
*
*/
find() {
const query = {
'fields': [
'name'
],
'selector': {
'name': {
'$exists': true
}
},
'sort': [
'name'
]
}
return this.db.find(query).then((documents) => {
return documents.docs
})
}
}
export default TagsDatabase