stalkee
Version:
a Telegram bot who can send voice messages via inline mode added by admin with sorting them by numbers of uses
52 lines (51 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.selectAudiosByWords = void 0;
const utils_1 = require("../utils");
const models_1 = require("../models");
const selectAudiosByWords = async (words) => {
try {
if (words.length == 0) {
return [];
}
// limitation: select all audios that include first word
const audios = await models_1.Audio.createQueryBuilder('a')
.innerJoin('a.words', 'w1')
.where('w1.word = :word', { word: words[0] })
.innerJoinAndSelect('a.words', 'w2')
.orderBy('a.n_uses', 'DESC')
.getMany();
// count same words of each selected audio, sync indexes
const wordsCounter = [];
for (const audio of audios) {
const counter = {};
for (const word of audio.words) {
if (!(word.word in counter)) {
counter[word.word] = 0;
}
counter[word.word]++;
}
wordsCounter.push(counter);
}
const res = [];
for (let i = 0; i < audios.length; i++) {
let excluded = false;
for (const word of words) {
if (!(word in wordsCounter[i]) || wordsCounter[i][word] <= 0) {
excluded = true;
break;
}
wordsCounter[i][word]--;
}
if (!excluded) {
res.push(audios[i]);
}
}
res.splice(utils_1.config.limits.max_result_size);
return res;
}
catch (err) {
utils_1.logger.error('' + err, 'controller.select_audios_by_words');
}
};
exports.selectAudiosByWords = selectAudiosByWords;