lookupUser(credentials, callback) { this.db.findOne({username: credentials.username}, (err, user) => { if (err) { return callback(err); } return callback(null, user); }); }
getPreviouslyMatchedUrls() { return new Promise((resolve, reject) => { this.db.find({type: 'matchedTorrents'}, (err, docs) => { if (err) { reject(err); } resolve(docs.reduce((matchedUrls, doc) => matchedUrls.concat(doc.urls), [])); }); }); }
loadDatabase() { const db = new Datastore({ autoload: true, filename: path.join(config.dbPath, 'users.db'), }); db.ensureIndex({fieldName: 'username', unique: true}); this.ready = true; return db; }
addNotification(notifications) { notifications = _.castArray(notifications); this.count.total = this.count.total + notifications.length; this.count.unread = this.count.unread + notifications.length; const timestamp = Date.now(); const notificationsToInsert = notifications.map(notification => ({ ts: timestamp, data: notification.data, id: notification.id, read: false, })); this.db.insert(notificationsToInsert, () => this.emitUpdate()); }
initialUserGate(handlers) { this.db.find({}, (err, users) => { if (users && users.length > 0) { return handlers.handleSubsequentUser(); } return handlers.handleInitialUser(); }); }
addItem(type, item, callback) { if (!this.isDBReady) return; this.db.insert(Object.assign(item, {type}), (err, newDoc) => { if (err) { callback(null, err); return; } callback(newDoc); }); }
countNotifications() { this.db.find({}, (err, docs) => { if (err) { this.count = Object.assign({}, INITIAL_COUNT_VALUE); } else { docs.forEach(notification => { if (notification.read) { this.count.read++; } else { this.count.unread++; } this.count.total++; }); } this.emitUpdate(); }); }
removeUser(username, callback) { this.db.findOne({username}).exec((findError, user) => { if (findError) { return callback(null, findError); } // Username not found. if (user == null) { return callback(null, user); } this.db.remove({username}, {}, removeError => { if (removeError) { return callback(null, removeError); } fs.removeSync(path.join(config.dbPath, user._id)); return callback({username}); }); }); }
clearNotifications(options, callback) { this.db.remove({}, {multi: true}, err => { if (err) { callback(null, err); return; } this.count = Object.assign({}, INITIAL_COUNT_VALUE); callback(); }); this.emitUpdate(); }
getAll(query, callback) { query = query || {}; this.db.find({}, (err, docs) => { if (err) { callback(null, err); return; } callback( docs.reduce((memo, item) => { const type = `${item.type}s`; if (memo[type] == null) { memo[type] = []; } memo[type].push(item); return memo; }, {}), ); }); }
modifyItem(id, newItem, callback) { if (!this.isDBReady) { return; } this.db.update({_id: id}, {$set: newItem}, {}, err => { if (err) { callback(null, err); return; } callback(null); }); }
updateUser(username, userRecordPatch, callback) { this.db.update({username}, {$set: userRecordPatch}, (err, numUsersUpdated, updatedUser) => { if (err) return callback(null, err); // Username not found. if (numUsersUpdated === 0) { return callback(null, err); } return callback(updatedUser); }); }
queryItem(type, query, callback) { query = query || {}; this.db.find(Object.assign(query, {type}), (err, docs) => { if (err) { callback(null, err); return; } callback(docs); }); }
listUsers(callback) { this.db.find({}, (err, users) => { if (err) { return callback(null, err); } return callback(users); }); }