Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions src/controllers/discourse.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

fetchQueue.on('active', () => {
if (fetchQueue.pending>2){
logger.info(`Discourse api queue jobs pending: ${fetchQueue.pending}`)
}
});

exports.config = config;

const fetchDiscordApiQueue = async (path, method, params, body , ntries, expect_json) =>{
Expand All @@ -67,9 +61,7 @@ const fetchDiscordApiQueue = async (path, method, params, body , ntries, expect_
})
}

if ( (path !== 'search') || (res.status !==200 )){
logger.info(`Discourse api ${method} request to "${path}" ${res.status}:${res.statusText} Try number ${ntries+1}`)
}
logger.info(`Discourse api ${method} request to "${path}" ${res.status}:${res.statusText} Try number ${ntries+1}`)

if (expect_json){
let json = await res.json()
Expand All @@ -79,11 +71,6 @@ const fetchDiscordApiQueue = async (path, method, params, body , ntries, expect_
}

const fetchDiscordApi = async (path, method, params={}, body = {}, ntries = 0, expect_json = true) =>{

if ( (path !== 'search')){
logger.info(`Discourse api ${method} request to "${path}" added to queue`)
}

try{
return await fetchQueue.add( async() => fetchDiscordApiQueue(path, method, params, body, ntries, expect_json))
}
Expand Down
3 changes: 1 addition & 2 deletions src/controllers/gcProcessing.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ exports.getMediaInfo = async (md5, mimetype, dirpath = './Media') =>{
}
else{
logger.error(`Could not extrat text and tags from "${md5}.${mime.extension(mimetype)}"`)
return [null, null];
// return ['No text extracted: format not supported', {name:'unsuported_format', salience: 10, tagType:'OTHER'}];
return ['No text extracted: format not supported', {name:'unsuported_format', salience: 10, tagType:'OTHER'}];
}

logger.info(`Text and tags extracted from "${md5}.${mime.extension(mimetype)}"`)
Expand Down
6 changes: 1 addition & 5 deletions src/controllers/messageBuffers.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ const setAsProcessing = async (docs) =>{
}

const processPrivateGroup = async (docs, client) =>{
logger.info(`Started processing private buffer group from ${docs[0].senderId} at ${docs[0].chatId}`);

let msgIds = await messagesController.matchMessages(docs, true);
let [grpObj, isNew] = await messagesController.matchMessageGroup( msgIds, true);
Expand All @@ -124,12 +123,9 @@ const processPrivateGroup = async (docs, client) =>{
}

await grpObj.save()
logger.info(`Finished processing private buffer group from ${docs[0].senderId} at ${docs[0].chatId}`);
}

const processGroupGroup = async (docs, client) =>{
logger.info(`Started processing group buffer group from ${docs[0].senderId} at ${docs[0].chatId}`);

let msgIds = await messagesController.matchMessages(docs, false);
let grpObjs = await messagesController.matchAllMessageGroups(msgIds)

Expand All @@ -152,7 +148,7 @@ const processGroupGroup = async (docs, client) =>{
await discourseController.updateForwardingScoreTag(grpObj)

})
logger.info(`Finished processing group buffer group from ${docs[0].senderId} at ${docs[0].chatId}`);

}

const processGroup = async (group, client) =>{
Expand Down
26 changes: 1 addition & 25 deletions src/controllers/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ const discourseController = require('./discourse');
const messageBufferController = require('./messageBuffers');
const msgsTexts = require('../msgsTexts.json');

const logger = require('../helpers/logger')

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}


exports.sendMultiMessage = async (client, chatId, msgs, delay = 2000) => {
logger.info(`Started sending ${msgs.length} message(s) to ${chatId}`);
for (let msg of msgs){
await client.sendText(chatId, msg);
await sleep(delay)
}
logger.info(`Finished sending ${msgs.length} message(s) to ${chatId}`);
}

exports.genTopicInfo = (topic_id) =>{
Expand All @@ -42,8 +38,6 @@ exports.genPreGrpReplyMessage = (groupInfo, userObj) =>{
}

exports.replyGroupMessage = async (messageGroup, client, groupInfo) =>{
logger.info(`Started replying group message: ${messageGroup._id}`);

if (await messageGroup.getReplyMessage()){
let publishVeracity = ['noContex','false','trueWithReservations','partially'];
if (publishVeracity.indexOf(await messageGroup.getVeracity()) >= 0){
Expand All @@ -60,13 +54,10 @@ exports.replyGroupMessage = async (messageGroup, client, groupInfo) =>{
return true;
}
}
logger.info(`Finished replying group message: ${messageGroup._id}`);
return false;
}

exports.replyPrivateMessage = async (messageGroup, client, senderInfo, isNew) =>{
logger.info(`Started replying private message: ${messageGroup._id}`);

let msgs = []
if (isNew){
msgs.push(msgsTexts.user.NEW_MSG.join('\n'))
Expand All @@ -83,7 +74,6 @@ exports.replyPrivateMessage = async (messageGroup, client, senderInfo, isNew) =>

this.sendMultiMessage(client,senderInfo.senderId,msgs)

logger.info(`Finished replying private message: ${messageGroup._id}`);
return Boolean(await messageGroup.getReplyMessage())
}

Expand Down Expand Up @@ -177,8 +167,6 @@ exports.publishReply = async ( messageGroup, client ) =>{
}

exports.getForwardingScoreTag = async (messageGroup) =>{
logger.info(`Started getting forwarding score tag for ${messageGroup._id}`);

let docs = await Message.aggregate(
[
{ $match: { _id: { $in: messageGroup.messages } } },
Expand All @@ -203,12 +191,10 @@ exports.getForwardingScoreTag = async (messageGroup) =>{

let tag = tags[scores.findIndex(x=>x>=docs[0].group_forwarding_score)-1]

logger.info(`Finished getting forwarding score tag for ${messageGroup._id} => x${tag}`);
return `x${tag}`
}

exports.getMessagesTags = async (messageIds) => {
logger.info(`Started getting tags for ${messageIds}`);
let docs = await Message.find(
{
'_id': { $in:
Expand All @@ -230,21 +216,19 @@ exports.getMessagesTags = async (messageIds) => {
},
[]
)
logger.info(`Finished getting tags for ${messageIds}`);

return gcController.mergeTagLists(tagList);
}


exports.matchAllMessageGroups = async (messageIds) => {
logger.info(`Started matching all message groups for ${messageIds}`);
// TODO atualmente se um grupo está contido no outro o mais externo é sempre o selecionado,
// seria interessante se o mais externo que tenha alguma resposta fosse o selecionado.
let msgGroups = await MessageGroup.find({messages: {"$not": {"$elemMatch": {"$nin" : messageIds }}}})
let allIds = msgGroups.map( x => x._id);
msgGroups = msgGroups.filter( (msgGroup) =>{
return !msgGroup.isSubSetOf.some((el1) => allIds.some((el2) => (el1.equals(el2))))
})
logger.info(`Finished matching all message groups for ${messageIds}`);
return msgGroups
}

Expand All @@ -263,14 +247,11 @@ exports.setIsSubsetOf = async (msgGroup) =>{
}

exports.matchMessageGroup = async (messageIds, createIfNull = false ) => {
logger.info(`Started matching message groups for ${messageIds}`);

messageIds.sort()

let msgGroup = await MessageGroup.findOne({messages: messageIds});

if (msgGroup){
logger.info(`Finished matching message groups for ${messageIds}: group found`);
return [msgGroup, false];
}
else if (createIfNull){
Expand All @@ -279,16 +260,12 @@ exports.matchMessageGroup = async (messageIds, createIfNull = false ) => {
tags: await this.getMessagesTags(messageIds)
} );
await this.setIsSubsetOf(msgGroup);
logger.info(`Finished matching message groups for ${messageIds}: group created`);
return [msgGroup, true];
}
logger.info(`Finished matching message groups for ${messageIds}: no group found`);
return [null, null];
}

exports.matchMessages = async(messageDocs, createIfNull) => {
logger.info(`Started matching messages ${messageDocs.map(el =>el.messageId)}`);

let msgIds = [];

for (let doc of messageDocs){
Expand All @@ -313,7 +290,6 @@ exports.matchMessages = async(messageDocs, createIfNull) => {
msgIds.push(null);
}
}
logger.info(`Finished matching messages ${messageDocs.map(el =>el.messageId)} => ${msgIds}`);
return msgIds;
}

Expand Down