2018-02-01 05:45:03 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
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,
|
2022-06-05 20:04:25 +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
|
2022-06-05 20:04:25 +00:00
|
|
|
const _ = require('lodash');
|
2018-02-01 05:45:03 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2022-06-05 20:04:25 +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 = {
|
2022-06-05 20:04:25 +00:00
|
|
|
search: {
|
|
|
|
searchTerms: 1,
|
|
|
|
search: 2,
|
|
|
|
conf: 3,
|
|
|
|
area: 4,
|
|
|
|
to: 5,
|
|
|
|
from: 6,
|
|
|
|
advSearch: 7,
|
|
|
|
},
|
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 = {
|
2022-06-05 20:04:25 +00:00
|
|
|
search: (formData, extraArgs, cb) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.searchNow(formData, cb);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
mciReady(mciData, cb) {
|
|
|
|
super.mciReady(mciData, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prepViewController('search', 0, mciData.menu, (err, vc) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const confView = vc.getView(MciViewIds.search.conf);
|
|
|
|
const areaView = vc.getView(MciViewIds.search.area);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!confView || !areaView) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(Errors.DoesNotExist('Missing one or more required views'));
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const availConfs = [{ text: '-ALL-', data: '' }].concat(
|
|
|
|
getSortedAvailMessageConferences(this.client).map(conf =>
|
|
|
|
Object.assign(conf, { text: conf.conf.name, data: conf.confTag })
|
|
|
|
) || []
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
|
2022-06-05 20:04:25 +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 => {
|
2022-06-05 20:04:25 +00:00
|
|
|
availAreas = [{ text: '-ALL-', data: '' }].concat(
|
|
|
|
getSortedAvailMessageAreasByConfTag(availConfs[idx].confTag, {
|
|
|
|
client: this.client,
|
|
|
|
}).map(area =>
|
|
|
|
Object.assign(area, {
|
|
|
|
text: area.area.name,
|
|
|
|
data: area.areaTag,
|
|
|
|
})
|
2018-06-22 05:15:04 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
areaView.setItems(availAreas);
|
|
|
|
areaView.setFocusItemIndex(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
vc.switchFocus(MciViewIds.search.searchTerms);
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
searchNow(formData, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const isAdvanced = formData.submitId === MciViewIds.search.advSearch;
|
|
|
|
const value = formData.value;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const filter = {
|
2022-06-05 20:04:25 +00:00
|
|
|
resultType: 'messageList',
|
|
|
|
sort: 'modTimestamp',
|
|
|
|
terms: value.searchTerms,
|
2018-06-23 03:26:46 +00:00
|
|
|
//extraFields : [ 'area_tag', 'message_uuid', 'reply_to_message_id', 'to_user_name', 'from_user_name', 'subject', 'modified_timestamp' ],
|
2022-06-05 20:04:25 +00:00
|
|
|
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',
|
2023-08-24 02:11:26 +00:00
|
|
|
{ menuFlags: ['popParent'] },
|
2019-09-12 04:03:24 +00:00
|
|
|
cb
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (isAdvanced) {
|
|
|
|
filter.toUserName = value.toUserName;
|
2018-06-23 03:26:46 +00:00
|
|
|
filter.fromUserName = value.fromUserName;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +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(
|
2022-06-05 20:04:25 +00:00
|
|
|
getAvailableMessageAreasByConfTag(value.confTag, {
|
|
|
|
client: this.client,
|
|
|
|
}),
|
2018-06-22 05:15:04 +00:00
|
|
|
(area, areaTag) => areaTag
|
|
|
|
);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (value.areaTag) {
|
|
|
|
if (hasMessageConfAndAreaRead(this.client, value.areaTag)) {
|
2019-09-12 04:03:24 +00:00
|
|
|
filter.areaTag = value.areaTag; // specific conf + area
|
|
|
|
} else {
|
|
|
|
return returnNoResults();
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Message.findMessages(filter, (err, messageList) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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)
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!value.confTag && !value.areaTag) {
|
2019-09-12 04:03:24 +00:00
|
|
|
messageList = filterMessageListByReadACS(this.client, messageList);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +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 = {
|
2022-06-05 20:04:25 +00:00
|
|
|
extraArgs: {
|
2018-06-22 05:15:04 +00:00
|
|
|
messageList,
|
2022-06-05 20:04:25 +00:00
|
|
|
noUpdateLastReadId: true,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2023-08-24 02:11:26 +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
|
|
|
};
|