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.
166 lines
3.6 KiB
166 lines
3.6 KiB
import DatabaseHandler from './databaseHandler.js'
|
|
import AppsStore from './apps.js'
|
|
|
|
/**
|
|
* 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 TagsDatabase extends DatabaseHandler {
|
|
|
|
constructor() {
|
|
super('tags')
|
|
|
|
// add index for apps
|
|
this.createIndex([
|
|
'name'
|
|
], 'tags')
|
|
}
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @param {object} data
|
|
*
|
|
*/
|
|
async 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) => {
|
|
console.log(documents)
|
|
return this.filterTags(documents, tags)
|
|
}).then(async (tags) => {
|
|
console.log(tags)
|
|
if (tags.length > 0) {
|
|
|
|
// adding tags to entries
|
|
for (let i = 0; i < tags.length; i++) {
|
|
await this.db.post({
|
|
'name': tags[i]
|
|
})
|
|
}
|
|
|
|
this.removeNotNeeded()
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
*
|
|
*
|
|
*/
|
|
removeNotNeeded() {
|
|
|
|
const query = {
|
|
'selector': {
|
|
'name': {
|
|
'$exists': true
|
|
}
|
|
}, 'fields': [
|
|
'_id',
|
|
'_rev',
|
|
'name'
|
|
]
|
|
}
|
|
|
|
this.db.find(query).then((tags) => {
|
|
console.log(tags.docs)
|
|
if (tags.docs.length > 0) {
|
|
|
|
const appsStore = new AppsStore()
|
|
|
|
for (let i = 0; i < tags.docs.length; i++) {
|
|
|
|
//
|
|
const parameters = {
|
|
tags: [],
|
|
sort: 'name'
|
|
}
|
|
|
|
parameters.tags.push(tags.docs[i].name)
|
|
console.log(parameters)
|
|
|
|
appsStore.find(parameters).then(async (documents) => {
|
|
if (!documents.docs) {
|
|
await this.db.remove(tags.docs[i])
|
|
}
|
|
})
|
|
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*
|
|
*/
|
|
find() {
|
|
const query = {
|
|
'fields': [
|
|
'name'
|
|
],
|
|
'selector': {
|
|
'name': {
|
|
'$exists': true
|
|
}
|
|
},
|
|
'sort': [
|
|
'name'
|
|
]
|
|
}
|
|
|
|
return this.db.find(query).then((documents) => {
|
|
if (documents.warning) {
|
|
console.log(documents.warning)
|
|
}
|
|
|
|
return documents.docs
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
export default TagsDatabase |