diff --git a/db.js b/db.js index 42c286eb..a50fd4bd 100644 --- a/db.js +++ b/db.js @@ -1494,33 +1494,91 @@ module.exports.CreateDB = function (parent, func) { } }); }; - obj.GetEvents = function (ids, domain, func) { + obj.GetEvents = function (ids, domain, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC', [domain], func); + query = query + "WHERE (domain = $1"; + if (filter != null) { + query = query + " AND action = $2"; + dataarray.push(filter); + } + query = query + ") ORDER BY time DESC"; } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC', [domain], func); + query = query + 'JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (' + dbMergeSqlArray(ids) + '))'; + if (filter != null) { + query = query + " AND action = $2"; + dataarray.push(filter); + } + query = query + ") GROUP BY id ORDER BY time DESC "; } + sqlDbQuery(query, dataarray, func); }; - obj.GetEventsWithLimit = function (ids, domain, limit, func) { + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC LIMIT $2', [domain, limit], func); + query = query + "WHERE (domain = $1"; + if (filter != null) { + query = query + " AND action = $2) ORDER BY time DESC LIMIT $3"; + dataarray.push(filter); + } else { + query = query + ") ORDER BY time DESC LIMIT $2"; + } } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC LIMIT $2', [domain, limit], func); + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND (target IN (" + dbMergeSqlArray(ids) + "))"; + if (filter != null) { + query = query + " AND action = $2) GROUP BY id ORDER BY time DESC LIMIT $3"; + dataarray.push(filter); + } else { + query = query + ") GROUP BY id ORDER BY time DESC LIMIT $2"; + } } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); }; - obj.GetUserEvents = function (ids, domain, userid, func) { + obj.GetUserEvents = function (ids, domain, userid, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain, userid]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC', [domain, userid], func); + query = query + "WHERE (domain = $1 AND userid = $2"; + if (filter != null) { + query = query + " AND action = $3"; + dataarray.push(filter); + } + query = query + ") ORDER BY time DESC"; } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC', [domain, userid], func); + query = query + 'JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (' + dbMergeSqlArray(ids) + '))'; + if (filter != null) { + query = query + " AND action = $3"; + dataarray.push(filter); + } + query = query + ") GROUP BY id ORDER BY time DESC"; } + sqlDbQuery(query, dataarray, func); }; - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) { + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain, userid]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC LIMIT $3', [domain, userid, limit], func); + query = query + "WHERE (domain = $1 AND userid = $2"; + if (filter != null) { + query = query + " AND action = $3) ORDER BY time DESC LIMIT $4"; + dataarray.push(filter); + } else { + query = query + ") ORDER BY time DESC LIMIT $3"; + } } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (' + dbMergeSqlArray(ids) + '))) GROUP BY id ORDER BY time DESC LIMIT $3', [domain, userid, limit], func); + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target IN (" + dbMergeSqlArray(ids) + "))"; + if (filter != null) { + query = query + " AND action = $3) GROUP BY id ORDER BY time DESC LIMIT $4"; + dataarray.push(filter); + } else { + query = query + ") GROUP BY id ORDER BY time DESC LIMIT $3"; + } } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); }; obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) { if (ids.indexOf('*') >= 0) { @@ -1530,8 +1588,30 @@ module.exports.CreateDB = function (parent, func) { } }; //obj.GetUserLoginEvents = function (domain, userid, func) { } // TODO - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) ORDER BY time DESC LIMIT $3', [nodeid, domain, limit], func); }; - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) AND ((userid = $3) OR (userid IS NULL)) ORDER BY time DESC LIMIT $4', [nodeid, domain, userid, limit], func); }; + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) { + var query = "SELECT doc FROM events WHERE (nodeid = $1 AND domain = $2"; + var dataarray = [nodeid, domain]; + if (filter != null) { + query = query + "AND action = $3) ORDER BY time DESC LIMIT $4"; + dataarray.push(filter); + } else { + query = query + ") ORDER BY time DESC LIMIT $3"; + } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); + }; + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) { + var query = "SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) AND ((userid = $3) OR (userid IS NULL)) "; + var dataarray = [nodeid, domain, userid]; + if (filter != null) { + query = query + "AND (action = $4) ORDER BY time DESC LIMIT $5"; + dataarray.push(filter); + } else { + query = query + "ORDER BY time DESC LIMIT $4"; + } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); + }; obj.RemoveAllEvents = function (domain) { sqlDbQuery('DELETE FROM events', null, function (err, docs) { }); }; obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND nodeid = $2', [domain, nodeid], function (err, docs) { }); }; obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND userid = $2', [domain, userid], function (err, docs) { }); }; @@ -1669,42 +1749,78 @@ module.exports.CreateDB = function (parent, func) { obj.dbCounters.eventsSet++; obj.file.ref('events').push(event).then(function (userRef) { if (func) { func(); } }); }; - obj.GetEvents = function (ids, domain, func) { + obj.GetEvents = function (ids, domain, filter, func) { // This request is slow since we have not found a .filter() that will take two arrays and match a single item. - obj.file.query('events').filter('domain', '==', domain).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) { - const docs = []; - for (var i in snapshots) { - const doc = snapshots[i].val(); - if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue; - var found = false; - for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches - if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); } - } - func(null, docs); - }); + if (filter != null) { + obj.file.query('events').filter('domain', '==', domain).filter('action', '==', filter).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) { + const docs = []; + for (var i in snapshots) { + const doc = snapshots[i].val(); + if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue; + var found = false; + for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches + if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); } + } + func(null, docs); + }); + } else { + obj.file.query('events').filter('domain', '==', domain).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) { + const docs = []; + for (var i in snapshots) { + const doc = snapshots[i].val(); + if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue; + var found = false; + for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches + if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); } + } + func(null, docs); + }); + } }; - obj.GetEventsWithLimit = function (ids, domain, limit, func) { + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) { // This request is slow since we have not found a .filter() that will take two arrays and match a single item. // TODO: Request a new AceBase feature for a 'array:contains-one-of' filter: // obj.file.indexes.create('events', 'ids', { type: 'array' }); // db.query('events').filter('ids', 'array:contains-one-of', ids) - obj.file.query('events').filter('domain', '==', domain).take(limit).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) { - const docs = []; - for (var i in snapshots) { - const doc = snapshots[i].val(); - if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue; - var found = false; - for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches - if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); } - } - func(null, docs); - }); + if (filter != null) { + obj.file.query('events').filter('domain', '==', domain).filter('action', '==', filter).take(limit).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) { + const docs = []; + for (var i in snapshots) { + const doc = snapshots[i].val(); + if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue; + var found = false; + for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches + if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); } + } + func(null, docs); + }); + } else { + obj.file.query('events').filter('domain', '==', domain).take(limit).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type'] }, function (snapshots) { + const docs = []; + for (var i in snapshots) { + const doc = snapshots[i].val(); + if ((doc.ids == null) || (!Array.isArray(doc.ids))) continue; + var found = false; + for (var j in doc.ids) { if (ids.indexOf(doc.ids[j]) >= 0) { found = true; } } // Check if one of the items in both arrays matches + if (found) { delete doc.ids; if (typeof doc.account == 'object') { doc.account = common.aceUnEscapeFieldNames(doc.account); } docs.push(doc); } + } + func(null, docs); + }); + } }; - obj.GetUserEvents = function (ids, domain, userid, func) { - obj.file.query('events').filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); + obj.GetUserEvents = function (ids, domain, userid, filter, func) { + if (filter != null) { + obj.file.query('events').filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).filter('action', '==', filter).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); + } else { + obj.file.query('events').filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); + } }; - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) { - obj.file.query('events').take(limit).filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) { + if (filter != null) { + obj.file.query('events').take(limit).filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).filter('action', '==', filter).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); + } else { + obj.file.query('events').take(limit).filter('domain', '==', domain).filter('userid', 'in', userid).filter('ids', 'in', ids).sort('time', false).get({ exclude: ['_id', 'domain', 'node', 'type', 'ids'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); + } }; obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) { obj.file.query('events').filter('domain', '==', domain).filter('ids', 'in', ids).filter('msgid', 'in', msgids).filter('time', 'between', [start, end]).sort('time', false).get({ exclude: ['type', '_id', 'domain', 'node'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); @@ -1712,10 +1828,19 @@ module.exports.CreateDB = function (parent, func) { obj.GetUserLoginEvents = function (domain, userid, func) { obj.file.query('events').filter('domain', '==', domain).filter('action', 'in', ['authfail', 'login']).filter('userid', '==', userid).filter('msgArgs', 'exists').sort('time', false).get({ include: ['action', 'time', 'msgid', 'msgArgs', 'tokenName'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); }; - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { - obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) { + if (filter != null) { + obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('action', '==', filter).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); + } else { + obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); + } }; - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) { + if (filter != null) { + obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('userid', '==', userid).filter('action', '==', filter).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); + } else { + obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('userid', '==', userid).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); + } obj.file.query('events').take(limit).filter('domain', '==', domain).filter('nodeid', '==', nodeid).filter('userid', '==', userid).sort('time', false).get({ exclude: ['type', 'etype', '_id', 'domain', 'ids', 'node', 'nodeid'] }, function (snapshots) { const docs = []; for (var i in snapshots) { docs.push(snapshots[i].val()); } func(null, docs); }); }; obj.RemoveAllEvents = function (domain) { @@ -1913,33 +2038,98 @@ module.exports.CreateDB = function (parent, func) { } }); }; - obj.GetEvents = function (ids, domain, func) { + obj.GetEvents = function (ids, domain, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC', [domain], func); + query = query + "WHERE (domain = $1"; + if (filter != null) { + query = query + " AND action = $2"; + dataarray.push(filter); + } + query = query + ") ORDER BY time DESC"; } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))) GROUP BY id ORDER BY time DESC', [domain, ids], func); + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))"; + dataarray.push(ids); + if (filter != null) { + query = query + " AND action = $3"; + dataarray.push(filter); + } + query = query + ") GROUP BY id ORDER BY time DESC"; } + sqlDbQuery(query, dataarray, func); }; - obj.GetEventsWithLimit = function (ids, domain, limit, func) { + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1) ORDER BY time DESC LIMIT $2', [domain, limit], func); + query = query + "WHERE (domain = $1"; + if (filter != null) { + query = query + " AND action = $2) ORDER BY time DESC LIMIT $3"; + dataarray.push(filter); + } else { + query = query + ") ORDER BY time DESC LIMIT $2"; + } } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))) GROUP BY id ORDER BY time DESC LIMIT $3', [domain, ids, limit], func); + if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead. + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND (target = ANY ($2))"; + dataarray.push(ids); + if (filter != null) { + query = query + " AND action = $3) ORDER BY time DESC LIMIT $4"; + dataarray.push(filter); + } else { + query = query + ") ORDER BY time DESC LIMIT $3"; + } } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); }; - obj.GetUserEvents = function (ids, domain, userid, func) { + obj.GetUserEvents = function (ids, domain, userid, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain, userid]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC', [domain, userid], func); + query = query + "WHERE (domain = $1 AND userid = $2"; + if (filter != null) { + query = query + " AND action = $3"; + dataarray.push(filter); + } + query = query + ") ORDER BY time DESC"; } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))) GROUP BY id ORDER BY time DESC', [domain, userid, ids], func); + if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead. + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))"; + dataarray.push(ids); + if (filter != null) { + query = query + " AND action = $4"; + dataarray.push(filter); + } + query = query + ") GROUP BY id ORDER BY time DESC"; } + sqlDbQuery(query, dataarray, func); }; - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) { + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain, userid]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = $1 AND userid = $2) ORDER BY time DESC LIMIT $3', [domain, userid, limit], func); + query = query + "WHERE (domain = $1 AND userid = $2"; + if (filter != null) { + query = query + " AND action = $3) ORDER BY time DESC LIMIT $4 "; + dataarray.push(filter); + } else { + query = query + ") ORDER BY time DESC LIMIT $3"; + } } else { - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))) GROUP BY id ORDER BY time DESC LIMIT $4', [domain, userid, ids, limit], func); + if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead. + query = query + "JOIN eventids ON id = fkid WHERE (domain = $1 AND userid = $2 AND (target = ANY ($3))"; + dataarray.push(ids); + if (filter != null) { + query = query + " AND action = $4) GROUP BY id ORDER BY time DESC LIMIT $5"; + dataarray.push(filter); + } else { + query = query + ") GROUP BY id ORDER BY time DESC LIMIT $4"; + } } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); }; obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) { if (ids.indexOf('*') >= 0) { @@ -1949,8 +2139,30 @@ module.exports.CreateDB = function (parent, func) { } }; //obj.GetUserLoginEvents = function (domain, userid, func) { } // TODO - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) ORDER BY time DESC LIMIT $3', [nodeid, domain, limit], func); }; - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = $1) AND (domain = $2) AND ((userid = $3) OR (userid IS NULL)) ORDER BY time DESC LIMIT $4', [nodeid, domain, userid, limit], func); }; + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) { + var query = "SELECT doc FROM events WHERE (nodeid = $1 AND domain = $2"; + var dataarray = [nodeid, domain]; + if (filter != null) { + query = query + " AND action = $3) ORDER BY time DESC LIMIT $4"; + dataarray.push(filter); + } else { + query = query + ") ORDER BY time DESC LIMIT $3"; + } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); + }; + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) { + var query = "SELECT doc FROM events WHERE (nodeid = $1 AND domain = $2 AND ((userid = $3) OR (userid IS NULL))"; + var dataarray = [nodeid, domain, userid]; + if (filter != null) { + query = query + " AND action = $4) ORDER BY time DESC LIMIT $5"; + dataarray.push(filter); + } else { + query = query + ") ORDER BY time DESC LIMIT $4"; + } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); + }; obj.RemoveAllEvents = function (domain) { sqlDbQuery('DELETE FROM events', null, function (err, docs) { }); }; obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND nodeid = $2', [domain, nodeid], function (err, docs) { }); }; obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = $1 AND userid = $2', [domain, userid], function (err, docs) { }); }; @@ -2078,37 +2290,95 @@ module.exports.CreateDB = function (parent, func) { for (var i in event.ids) { if (event.ids[i] != '*') { batchQuery.push(['INSERT INTO eventids VALUE (LAST_INSERT_ID(), ?)', [event.ids[i]]]); } } sqlDbBatchExec(batchQuery, function (err, docs) { if (func != null) { func(err, docs); } }); }; - obj.GetEvents = function (ids, domain, func) { + obj.GetEvents = function (ids, domain, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = ?) ORDER BY time DESC', [domain], func); + query = query + "WHERE (domain = ?"; + if (filter != null) { + query = query + " AND action = ?"; + dataarray.push(filter); + } + query = query + ") ORDER BY time DESC"; } else { if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead. - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = ? AND target IN (?)) GROUP BY id ORDER BY time DESC', [domain, ids], func); + query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND target IN (?)"; + dataarray.push(ids); + if (filter != null) { + query = query + " AND action = ?"; + dataarray.push(filter); + } + query = query + ") GROUP BY id ORDER BY time DESC"; } + sqlDbQuery(query, dataarray, func); }; - obj.GetEventsWithLimit = function (ids, domain, limit, func) { + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = ?) ORDER BY time DESC LIMIT ?', [domain, limit], func); + query = query + "WHERE (domain = ?"; + if (filter != null) { + query = query + " AND action = ? "; + dataarray.push(filter); + } + query = query + ") ORDER BY time DESC LIMIT ?"; } else { if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead. - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = ? AND target IN (?)) GROUP BY id ORDER BY time DESC LIMIT ?', [domain, ids, limit], func); + query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND target IN (?)"; + dataarray.push(ids); + if (filter != null) { + query = query + " AND action = ?"; + dataarray.push(filter); + } + query = query + ") GROUP BY id ORDER BY time DESC LIMIT ?"; } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); }; - obj.GetUserEvents = function (ids, domain, userid, func) { + obj.GetUserEvents = function (ids, domain, userid, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain, userid]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = ? AND userid = ?) ORDER BY time DESC', [domain, userid], func); + query = query + "WHERE (domain = ? AND userid = ?"; + if (filter != null) { + query = query + " AND action = ?"; + dataarray.push(filter); + } + query = query + ") ORDER BY time DESC"; } else { if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead. - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = ? AND userid = ? AND target IN (?)) GROUP BY id ORDER BY time DESC', [domain, userid, ids], func); + query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND userid = ? AND target IN (?)"; + dataarray.push(ids); + if (filter != null) { + query = query + " AND action = ?"; + dataarray.push(filter); + } + query = query + ") GROUP BY id ORDER BY time DESC"; } + sqlDbQuery(query, dataarray, func); }; - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) { + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) { + var query = "SELECT doc FROM events "; + var dataarray = [domain, userid]; if (ids.indexOf('*') >= 0) { - sqlDbQuery('SELECT doc FROM events WHERE (domain = ? AND userid = ?) ORDER BY time DESC LIMIT ?', [domain, userid, limit], func); + query = query + "WHERE (domain = ? AND userid = ?"; + if (filter != null) { + query = query + " AND action = ?"; + dataarray.push(filter); + } + query = query + ") ORDER BY time DESC LIMIT ?"; } else { if (ids.length == 0) { ids = ''; } // MySQL can't handle a query with IN() on an empty array, we have to use an empty string instead. - sqlDbQuery('SELECT doc FROM events JOIN eventids ON id = fkid WHERE (domain = ? AND userid = ? AND target IN (?)) GROUP BY id ORDER BY time DESC LIMIT ?', [domain, userid, ids, limit], func); + query = query + "JOIN eventids ON id = fkid WHERE (domain = ? AND userid = ? AND target IN (?)"; + dataarray.push(ids); + if (filter != null) { + query = query + " AND action = ?"; + dataarray.push(filter); + } + query = query + ") GROUP BY id ORDER BY time DESC LIMIT ?"; } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); }; obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) { if (ids.indexOf('*') >= 0) { @@ -2119,8 +2389,30 @@ module.exports.CreateDB = function (parent, func) { } }; //obj.GetUserLoginEvents = function (domain, userid, func) { } // TODO - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = ?) AND (domain = ?) ORDER BY time DESC LIMIT ?', [nodeid, domain, limit], func); }; - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { sqlDbQuery('SELECT doc FROM events WHERE (nodeid = ?) AND (domain = ?) AND ((userid = ?) OR (userid IS NULL)) ORDER BY time DESC LIMIT ?', [nodeid, domain, userid, limit], func); }; + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) { + var query = "SELECT doc FROM events WHERE (nodeid = ? AND domain = ?"; + var dataarray = [nodeid, domain]; + if (filter != null) { + query = query + " AND action = ?) ORDER BY time DESC LIMIT ?"; + dataarray.push(filter); + } else { + query = query + ") ORDER BY time DESC LIMIT ?"; + } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); + }; + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) { + var query = "SELECT doc FROM events WHERE (nodeid = ? AND domain = ? AND ((userid = ?) OR (userid IS NULL))"; + var dataarray = [nodeid, domain, userid]; + if (filter != null) { + query = query + " AND action = ?) ORDER BY time DESC LIMIT ?"; + dataarray.push(filter); + } else { + query = query + ") ORDER BY time DESC LIMIT ?"; + } + dataarray.push(limit); + sqlDbQuery(query, dataarray, func); + }; obj.RemoveAllEvents = function (domain) { sqlDbQuery('DELETE FROM events', null, function (err, docs) { }); }; obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = ? AND nodeid = ?', [domain, nodeid], function (err, docs) { }); }; obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; sqlDbQuery('DELETE FROM events WHERE domain = ? AND userid = ?', [domain, userid], function (err, docs) { }); }; @@ -2355,14 +2647,38 @@ module.exports.CreateDB = function (parent, func) { obj.StoreEvent = function (event, func) { obj.dbCounters.eventsSet++; obj.eventsfile.insertOne(event, func); }; } - obj.GetEvents = function (ids, domain, func) { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).toArray(func); }; - obj.GetEventsWithLimit = function (ids, domain, limit, func) { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).toArray(func); }; - obj.GetUserEvents = function (ids, domain, userid, func) { obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).toArray(func); }; - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) { obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).toArray(func); }; + obj.GetEvents = function (ids, domain, filter, func) { + var finddata = { domain: domain, ids: { $in: ids } }; + if (filter != null) finddata.action = filter; + obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).toArray(func); + }; + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) { + var finddata = { domain: domain, ids: { $in: ids } }; + if (filter != null) finddata.action = filter; + obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).toArray(func); + }; + obj.GetUserEvents = function (ids, domain, userid, filter, func) { + var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }; + if (filter != null) finddata.action = filter; + obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).toArray(func); + }; + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) { + var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }; + if (filter != null) finddata.action = filter; + obj.eventsfile.find(finddata).project({ type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).toArray(func); + }; obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) { obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }], msgid: { $in: msgids }, time: { $gte: start, $lte: end } }).project({ type: 0, _id: 0, domain: 0, node: 0 }).sort({ time: 1 }).toArray(func); }; obj.GetUserLoginEvents = function (domain, userid, func) { obj.eventsfile.find({ domain: domain, action: { $in: ['authfail', 'login'] }, userid: userid, msgArgs: { $exists: true } }).project({ action: 1, time: 1, msgid: 1, msgArgs: 1, tokenName: 1 }).sort({ time: -1 }).toArray(func); }; - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { obj.eventsfile.find({ domain: domain, nodeid: nodeid }).project({ type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).toArray(func); }; - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { obj.eventsfile.find({ domain: domain, nodeid: nodeid, userid: { $in: [userid, null] } }).project({ type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).toArray(func); }; + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) { + var finddata = { domain: domain, nodeid: nodeid }; + if (filter != null) finddata.action = filter; + obj.eventsfile.find(finddata).project({ type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).toArray(func); + }; + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) { + var finddata = { domain: domain, nodeid: nodeid, userid: { $in: [userid, null] } }; + if (filter != null) finddata.action = filter; + obj.eventsfile.find(finddata).project({ type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).toArray(func); + }; obj.RemoveAllEvents = function (domain) { obj.eventsfile.deleteMany({ domain: domain }, { multi: true }); }; obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; obj.eventsfile.deleteMany({ domain: domain, nodeid: nodeid }, { multi: true }); }; obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; obj.eventsfile.deleteMany({ domain: domain, userid: userid }, { multi: true }); }; @@ -2527,20 +2843,40 @@ module.exports.CreateDB = function (parent, func) { // Database actions on the events collection obj.GetAllEvents = function (func) { obj.eventsfile.find({}, func); }; obj.StoreEvent = function (event, func) { obj.eventsfile.insert(event, func); }; - obj.GetEvents = function (ids, domain, func) { if (obj.databaseType == 1) { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).exec(func); } else { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }, func); } }; - obj.GetEventsWithLimit = function (ids, domain, limit, func) { if (obj.databaseType == 1) { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).exec(func); } else { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit, func); } }; - obj.GetUserEvents = function (ids, domain, userid, func) { + obj.GetEvents = function (ids, domain, filter, func) { + var finddata = { domain: domain, ids: { $in: ids } }; + if (filter != null) finddata.action = filter; if (obj.databaseType == 1) { - obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).exec(func); + obj.eventsfile.find(finddata, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).exec(func); } else { - obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }, func); + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }, func); } }; - obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, func) { + obj.GetEventsWithLimit = function (ids, domain, limit, filter, func) { + var finddata = { domain: domain, ids: { $in: ids } }; + if (filter != null) finddata.action = filter; if (obj.databaseType == 1) { - obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).exec(func); + obj.eventsfile.find(finddata, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).exec(func); } else { - obj.eventsfile.find({ domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit, func); + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit, func); + } + }; + obj.GetUserEvents = function (ids, domain, userid, filter, func) { + var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }; + if (filter != null) finddata.action = filter; + if (obj.databaseType == 1) { + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).exec(func); + } else { + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }, func); + } + }; + obj.GetUserEventsWithLimit = function (ids, domain, userid, limit, filter, func) { + var finddata = { domain: domain, $or: [{ ids: { $in: ids } }, { userid: userid }] }; + if (filter != null) finddata.action = filter; + if (obj.databaseType == 1) { + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).exec(func); + } else { + obj.eventsfile.find(finddata, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit, func); } }; obj.GetEventsTimeRange = function (ids, domain, msgids, start, end, func) { @@ -2557,8 +2893,24 @@ module.exports.CreateDB = function (parent, func) { obj.eventsfile.find({ domain: domain, action: { $in: ['authfail', 'login'] }, userid: userid, msgArgs: { $exists: true } }, { action: 1, time: 1, msgid: 1, msgArgs: 1, tokenName: 1 }).sort({ time: -1 }, func); } }; - obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, func) { if (obj.databaseType == 1) { obj.eventsfile.find({ domain: domain, nodeid: nodeid }, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).exec(func); } else { obj.eventsfile.find({ domain: domain, nodeid: nodeid }, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit, func); } }; - obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, func) { if (obj.databaseType == 1) { obj.eventsfile.find({ domain: domain, nodeid: nodeid, userid: { $in: [userid, null] } }, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).exec(func); } else { obj.eventsfile.find({ domain: domain, nodeid: nodeid }, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit, func); } }; + obj.GetNodeEventsWithLimit = function (nodeid, domain, limit, filter, func) { + var finddata = { domain: domain, nodeid: nodeid }; + if (filter != null) finddata.action = filter; + if (obj.databaseType == 1) { + obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).exec(func); + } else { + obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit, func); + } + }; + obj.GetNodeEventsSelfWithLimit = function (nodeid, domain, userid, limit, filter, func) { + var finddata = { domain: domain, nodeid: nodeid, userid: { $in: [userid, null] } }; + if (filter != null) finddata.action = filter; + if (obj.databaseType == 1) { + obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit).exec(func); + } else { + obj.eventsfile.find(finddata, { type: 0, etype: 0, _id: 0, domain: 0, ids: 0, node: 0, nodeid: 0 }).sort({ time: -1 }).limit(limit, func); + } + }; obj.RemoveAllEvents = function (domain) { obj.eventsfile.remove({ domain: domain }, { multi: true }); }; obj.RemoveAllNodeEvents = function (domain, nodeid) { if ((domain == null) || (nodeid == null)) return; obj.eventsfile.remove({ domain: domain, nodeid: nodeid }, { multi: true }); }; obj.RemoveAllUserEvents = function (domain, userid) { if ((domain == null) || (userid == null)) return; obj.eventsfile.remove({ domain: domain, userid: userid }, { multi: true }); }; diff --git a/meshuser.js b/meshuser.js index 07e46fba..8f6c1078 100644 --- a/meshuser.js +++ b/meshuser.js @@ -1022,15 +1022,20 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use // TODO: Add the meshes command.userid has access to (???) var filter = [command.userid]; + var actionfilter = null; + if (command.filter != null) { + if (['agentlog','batchupload','changenode','manual','relaylog','removenode','runcommands'].includes(command.filter)) actionfilter = command.filter; + } + if ((command.limit == null) || (typeof command.limit != 'number')) { // Send the list of all events for this session - db.GetUserEvents(filter, domain.id, command.userid, function (err, docs) { + db.GetUserEvents(filter, domain.id, command.userid, actionfilter, function (err, docs) { if (err != null) return; try { ws.send(JSON.stringify({ action: 'events', events: docs, userid: command.userid, tag: command.tag })); } catch (ex) { } }); } else { // Send the list of most recent events for this session, up to 'limit' count - db.GetUserEventsWithLimit(filter, domain.id, command.userid, command.limit, function (err, docs) { + db.GetUserEventsWithLimit(filter, domain.id, command.userid, command.limit, actionfilter, function (err, docs) { if (err != null) return; try { ws.send(JSON.stringify({ action: 'events', events: docs, userid: command.userid, tag: command.tag })); } catch (ex) { } }); @@ -1048,15 +1053,20 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use var limit = 10000; if (common.validateInt(command.limit, 1, 1000000) == true) { limit = command.limit; } + var filter = null; + if (command.filter != null) { + if (['agentlog','batchupload','changenode','manual','relaylog','removenode','runcommands'].includes(command.filter)) filter = command.filter; + } + if (((rights & MESHRIGHT_LIMITEVENTS) != 0) && (rights != MESHRIGHT_ADMIN)) { // Send the list of most recent events for this nodeid that only apply to us, up to 'limit' count - db.GetNodeEventsSelfWithLimit(node._id, domain.id, user._id, limit, function (err, docs) { + db.GetNodeEventsSelfWithLimit(node._id, domain.id, user._id, limit, filter, function (err, docs) { if (err != null) return; try { ws.send(JSON.stringify({ action: 'events', events: docs, nodeid: node._id, tag: command.tag })); } catch (ex) { } }); } else { // Send the list of most recent events for this nodeid, up to 'limit' count - db.GetNodeEventsWithLimit(node._id, domain.id, limit, function (err, docs) { + db.GetNodeEventsWithLimit(node._id, domain.id, limit, filter, function (err, docs) { if (err != null) return; try { ws.send(JSON.stringify({ action: 'events', events: docs, nodeid: node._id, tag: command.tag })); } catch (ex) { } }); @@ -1076,15 +1086,20 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use for (var link in obj.user.links) { if (((obj.user.links[link].rights & MESHRIGHT_LIMITEVENTS) != 0) && ((obj.user.links[link].rights != MESHRIGHT_ADMIN))) { exGroupFilter2.push(link); } } for (var i in filter2) { if (exGroupFilter2.indexOf(filter2[i]) == -1) { filter.push(filter2[i]); } } + var actionfilter = null; + if (command.filter != null) { + if (['agentlog','batchupload','changenode','manual','relaylog','removenode','runcommands'].includes(command.filter)) actionfilter = command.filter; + } + if ((command.limit == null) || (typeof command.limit != 'number')) { // Send the list of all events for this session - db.GetEvents(filter, domain.id, function (err, docs) { + db.GetEvents(filter, domain.id, actionfilter, function (err, docs) { if (err != null) return; try { ws.send(JSON.stringify({ action: 'events', events: docs, user: command.user, tag: command.tag })); } catch (ex) { } }); } else { // Send the list of most recent events for this session, up to 'limit' count - db.GetEventsWithLimit(filter, domain.id, command.limit, function (err, docs) { + db.GetEventsWithLimit(filter, domain.id, command.limit, actionfilter, function (err, docs) { if (err != null) return; try { ws.send(JSON.stringify({ action: 'events', events: docs, user: command.user, tag: command.tag })); } catch (ex) { } }); @@ -1101,7 +1116,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use if (err != null) { try { ws.send(JSON.stringify({ action: 'recordings', error: 1, tag: command.tag })); } catch (ex) { } return; } if ((command.limit == null) || (typeof command.limit != 'number')) { // Send the list of all recordings - db.GetEvents(['recording'], domain.id, function (err, docs) { + db.GetEvents(['recording'], domain.id, null, function (err, docs) { if (err != null) { try { ws.send(JSON.stringify({ action: 'recordings', error: 2, tag: command.tag })); } catch (ex) { } return; } for (var i in docs) { delete docs[i].action; delete docs[i].etype; delete docs[i].msg; // TODO: We could make a more specific query in the DB and never have these. @@ -1111,7 +1126,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use }); } else { // Send the list of most recent recordings, up to 'limit' count - db.GetEventsWithLimit(['recording'], domain.id, command.limit, function (err, docs) { + db.GetEventsWithLimit(['recording'], domain.id, command.limit, null, function (err, docs) { if (err != null) { try { ws.send(JSON.stringify({ action: 'recordings', error: 2, tag: command.tag })); } catch (ex) { } return; } for (var i in docs) { delete docs[i].action; delete docs[i].etype; delete docs[i].msg; // TODO: We could make a more specific query in the DB and never have these. @@ -4779,7 +4794,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use }); } else { // Old way - db.GetUserEvents([user._id], domain.id, user._id, function (err, docs) { + db.GetUserEvents([user._id], domain.id, user._id, null, function (err, docs) { if (err != null) return; var e = []; for (var i in docs) { @@ -4805,7 +4820,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use }); } else { // Old way - db.GetUserEvents([command.userid], domain.id, user._id, function (err, docs) { + db.GetUserEvents([command.userid], domain.id, user._id, null, function (err, docs) { if (err != null) return; var e = []; for (var i in docs) { if ((docs[i].msgArgs) && (docs[i].userid == command.userid) && ((docs[i].action == 'authfail') || (docs[i].action == 'login'))) { e.push({ t: docs[i].time, m: docs[i].msgid, a: docs[i].msgArgs }); } } diff --git a/views/default.handlebars b/views/default.handlebars index 7ce89f17..3ac35050 100644 --- a/views/default.handlebars +++ b/views/default.handlebars @@ -470,6 +470,17 @@