2018-02-01 05:45:03 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
2018-02-01 05:45:03 +00:00
|
|
|
const {
|
2018-06-22 05:15:04 +00:00
|
|
|
getSortedAvailMessageConferences,
|
|
|
|
getAvailableMessageAreasByConfTag,
|
|
|
|
getSortedAvailMessageAreasByConfTag,
|
2019-09-12 04:03:24 +00:00
|
|
|
hasMessageConfAndAreaRead,
|
|
|
|
filterMessageListByReadACS,
|
2018-06-23 03:26:46 +00:00
|
|
|
} = require('./message_area.js');
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const Message = require('./message.js');
|
2018-02-01 05:45:03 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
2018-02-01 05:45:03 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'Message Base Search',
|
|
|
|
desc : 'Module for quickly searching the message base',
|
|
|
|
author : 'NuSkooler',
|
2018-02-01 05:45:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const MciViewIds = {
|
2018-06-22 05:15:04 +00:00
|
|
|
search : {
|
2018-06-23 03:26:46 +00:00
|
|
|
searchTerms : 1,
|
|
|
|
search : 2,
|
|
|
|
conf : 3,
|
|
|
|
area : 4,
|
|
|
|
to : 5,
|
|
|
|
from : 6,
|
|
|
|
advSearch : 7,
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-02-01 05:45:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = class MessageBaseSearch extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
this.menuMethods = {
|
|
|
|
search : (formData, extraArgs, cb) => {
|
|
|
|
return this.searchNow(formData, cb);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
mciReady(mciData, cb) {
|
|
|
|
super.mciReady(mciData, err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prepViewController('search', 0, mciData.menu, (err, vc) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const confView = vc.getView(MciViewIds.search.conf);
|
|
|
|
const areaView = vc.getView(MciViewIds.search.area);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if(!confView || !areaView) {
|
|
|
|
return cb(Errors.DoesNotExist('Missing one or more required views'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const availConfs = [ { text : '-ALL-', data : '' } ].concat(
|
|
|
|
getSortedAvailMessageConferences(this.client).map(conf => Object.assign(conf, { text : conf.conf.name, data : conf.confTag } )) || []
|
|
|
|
);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
let availAreas = [ { text : '-ALL-', data : '' } ]; // note: will populate if conf changes from ALL
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
confView.setItems(availConfs);
|
|
|
|
areaView.setItems(availAreas);
|
|
|
|
|
|
|
|
confView.setFocusItemIndex(0);
|
|
|
|
areaView.setFocusItemIndex(0);
|
|
|
|
|
|
|
|
confView.on('index update', idx => {
|
|
|
|
availAreas = [ { text : '-ALL-', data : '' } ].concat(
|
|
|
|
getSortedAvailMessageAreasByConfTag(availConfs[idx].confTag, { client : this.client }).map(
|
|
|
|
area => Object.assign(area, { text : area.area.name, data : area.areaTag } )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
areaView.setItems(availAreas);
|
|
|
|
areaView.setFocusItemIndex(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
vc.switchFocus(MciViewIds.search.searchTerms);
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
searchNow(formData, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const isAdvanced = formData.submitId === MciViewIds.search.advSearch;
|
|
|
|
const value = formData.value;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const filter = {
|
2018-06-23 03:26:46 +00:00
|
|
|
resultType : 'messageList',
|
|
|
|
sort : 'modTimestamp',
|
|
|
|
terms : value.searchTerms,
|
|
|
|
//extraFields : [ 'area_tag', 'message_uuid', 'reply_to_message_id', 'to_user_name', 'from_user_name', 'subject', 'modified_timestamp' ],
|
|
|
|
limit : 2048, // :TODO: best way to handle this? we should probably let the user know if some results are returned
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2019-09-12 04:03:24 +00:00
|
|
|
const returnNoResults = () => {
|
|
|
|
return this.gotoMenu(
|
|
|
|
this.menuConfig.config.noResultsMenu || 'messageSearchNoResults',
|
|
|
|
{ menuFlags : [ 'popParent' ] },
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(isAdvanced) {
|
2018-06-23 03:26:46 +00:00
|
|
|
filter.toUserName = value.toUserName;
|
|
|
|
filter.fromUserName = value.fromUserName;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if(value.confTag && !value.areaTag) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// areaTag may be a string or array of strings
|
|
|
|
// getAvailableMessageAreasByConfTag() returns a obj - we only need tags
|
2018-06-22 05:15:04 +00:00
|
|
|
filter.areaTag = _.map(
|
|
|
|
getAvailableMessageAreasByConfTag(value.confTag, { client : this.client } ),
|
|
|
|
(area, areaTag) => areaTag
|
|
|
|
);
|
|
|
|
} else if(value.areaTag) {
|
2019-09-12 04:03:24 +00:00
|
|
|
if(hasMessageConfAndAreaRead(this.client, value.areaTag)) {
|
|
|
|
filter.areaTag = value.areaTag; // specific conf + area
|
|
|
|
} else {
|
|
|
|
return returnNoResults();
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Message.findMessages(filter, (err, messageList) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2019-09-12 04:03:24 +00:00
|
|
|
// don't include results without ACS -- if the user searched by
|
|
|
|
// explicit conf/area tag, we should have already filtered (above)
|
|
|
|
if(!value.confTag && !value.areaTag) {
|
|
|
|
messageList = filterMessageListByReadACS(this.client, messageList);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(0 === messageList.length) {
|
2019-09-12 04:03:24 +00:00
|
|
|
return returnNoResults();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const menuOpts = {
|
|
|
|
extraArgs : {
|
|
|
|
messageList,
|
2018-06-23 03:26:46 +00:00
|
|
|
noUpdateLastReadId : true
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2018-06-23 03:26:46 +00:00
|
|
|
menuFlags : [ 'popParent' ],
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return this.gotoMenu(
|
|
|
|
this.menuConfig.config.messageListMenu || 'messageAreaMessageList',
|
|
|
|
menuOpts,
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2018-02-01 05:45:03 +00:00
|
|
|
};
|