3 const assert = require('assert');
4 var persistence = require('gitter-web-persistence');
5 var processText = require('gitter-web-text-processor');
6 var ObjectID = require('mongodb').ObjectID;
7 var Promise = require('bluebird');
8 var StatusError = require('statuserror');
9 const mongoUtils = require('gitter-web-persistence-utils/lib/mongo-utils');
10 const mongoReadPrefs = require('gitter-web-persistence-utils/lib/mongo-read-prefs');
13 * For exporting things
15 function getCursorByRoomId(roomId) {
16 assert(mongoUtils.isLikeObjectId(roomId));
18 const cursor = persistence.Event.find({
22 .read(mongoReadPrefs.secondaryPreferred)
29 function newEventToTroupe(troupe, user, text, meta, payload, callback) {
30 text = text ? '' + text : '';
32 return Promise.try(function() {
33 if (!troupe) throw new StatusError(500, 'Invalid troupe');
34 if (!text) throw new StatusError(400, 'Text required');
36 return processText(text);
38 .then(function(parsed) {
39 var event = new persistence.Event();
41 event.fromUserId = user ? user.id : null;
42 event.toTroupeId = troupe.id;
43 event.sent = new Date();
45 event.html = parsed.html;
47 event.payload = payload;
54 function findEventsForTroupe(troupeId, options, callback) {
55 var q = persistence.Event.where('toTroupeId', troupeId);
57 if (options.startId) {
58 var startId = new ObjectID(options.startId);
59 q = q.where('_id').gte(startId);
62 if (options.beforeId) {
63 var beforeId = new ObjectID(options.beforeId);
64 q = q.where('_id').lt(beforeId);
68 .sort(options.sort || { sent: 'desc' })
69 .limit(options.limit || 20)
70 .skip(options.skip || 0)
72 .then(function(results) {