feat(trackers): merge trackers info

This commit is contained in:
Alexey Kasyanchuk
2018-08-15 10:24:42 +03:00
parent 317cfdd3fb
commit 17371bd8ff
3 changed files with 36 additions and 4 deletions

View File

@ -118,7 +118,11 @@ const expand = (sphinx) => {
})
})
sphinx.replaceValues = (table, values, particial = true, key = 'id', callback = () => {}) => new Promise((resolve) => {
sphinx.replaceValues = (table, values, options = {}, callback = () => {}) => new Promise((resolve) => {
const {particial, key, merge, mergeCallback} = Object.assign({
particial: true,
key: 'id'
}, options)
values = Object.assign({}, values) // copy
let names = '';
@ -161,6 +165,16 @@ const expand = (sphinx) => {
return
}
if(merge)
{
for(const m of merge)
{
values[m] = Object.assign(JSON.parse(row[0][m] || '{}'), values[m])
if(mergeCallback)
mergeCallback(m, values[m])
}
}
data = `(${parseValues(Object.assign(row[0], values))})`
finalQuery()
})

View File

@ -116,7 +116,15 @@ module.exports = function (send, recive, dataDirectory, version, env)
return
logT('tracker', 'found', name, 'on', tracker)
this.sphinx.replaceValues('torrents', {hash, info: data}, true, 'hash')
this.sphinx.replaceValues('torrents', {hash, info: data}, {particial: true, key: 'hash', merge: ['info'], mergeCallback: (n, obj) => {
if(n != 'info')
return
if(!obj.trackers)
obj.trackers = []
obj.trackers.push(name)
obj.trackers = [...new Set(obj.trackers)]
} })
})
}
}