Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 38 additions & 52 deletions lib/chat_log.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ module.exports.set_db = function(current_db){
};

module.exports.add = function(data,callback){
db.collection(table_name, function(err, collection) {
collection.save( data, function(){
if (callback != undefined ){
callback();
}
});
db.collection(table_name).insertOne( data, function(){
if (callback != undefined ){
callback();
}
});
};

module.exports.remove = function(id){
db.collection(table_name, function(err, collection) {
collection.remove( {_id: new mongo.ObjectID(id)} ,{safe:true}, function(){});
});
db.collection(table_name)
.remove( {_id: new mongo.ObjectID(id)} ,{safe:true}, function(){});
};

function getConditionsByKeyword(keyword){
Expand Down Expand Up @@ -80,11 +77,10 @@ module.exports.get = function(data, callback){
}
}

db.collection(table_name, function(err, collection) {
collection.find({ $and: conditions }, {limit: CHAT_LIMIT, sort:{date: -1}}).toArray(function(err, results) {
db.collection(table_name)
.find({ $and: conditions }, {limit: CHAT_LIMIT, sort:{date: -1}}).toArray(function(err, results) {
callback(results);
});
});
};

module.exports.get_older = function(data, callback){
Expand Down Expand Up @@ -117,111 +113,101 @@ module.exports.get_older = function(data, callback){
}
}

db.collection(table_name, function(err, collection) {
collection.find({_id: new mongo.ObjectID(id)}).toArray(function(err, last_msg){
let collection = db.collection(table_name)
collection
.find({_id: new mongo.ObjectID(id)}).toArray(function(err, last_msg){
conditions.push({ date: {$lte: last_msg[0].date}});
collection.find({ $and: conditions }, {limit: CHAT_LIMIT, sort:{date: -1}}).toArray(function(err, results) {
callback(results);
});
});
});
};

module.exports.size = function(room_id,callback){
if (room_id == 1){
db.collection(table_name, function(err, collection) {
collection.countDocuments({ $or: [{room_id: room_id},{room_id: undefined}]},function(err, count){
db.collection(table_name)
.countDocuments({ $or: [{room_id: room_id},{room_id: undefined}]},function(err, count){
callback(count);
});
});
}else{
db.collection(table_name, function(err, collection) {
collection.countDocuments({room_id: room_id},function(err, count){
db.collection(table_name)
.countDocuments({room_id: room_id},function(err, count){
callback(count);
});
});
}
};

module.exports.get_active_number = function(){
return new Promise(function(callback){
db.collection(table_active_number, function(err, collection) {
collection.findOne({},function(err, number) {
if (number == null){
number = {num: DEFAULT_ACTIVE_NUMBER};
}
db.collection(table_active_number).findOne({},function(err, number) {
if (number == null){
number = {num: DEFAULT_ACTIVE_NUMBER};
}

callback(number);
});
callback(number);
});
});
}

module.exports.update_active_number = function(number){
db.collection(table_active_number, function(err, collection) {
collection.findOne({},function(err, active_number) {
db.collection(table_active_number)
.findOne({},function(err, active_number) {
if (active_number != null){
collection.update( {_id: active_number._id}, {'$set': number}, {safe: true}, function(){});
collection.updateOne( {_id: active_number._id}, {'$set': number}, {safe: true}, function(){});
}else{
collection.save( number, function(){} );
collection.insertOne( number, function(){} );
}
});
});
}

module.exports.set_room_name = function(room_id, name){
db.collection(table_room_name, function(err, collection) {
collection.findOne({no: room_id},function(err, room) {
db.collection(table_room_name)
.findOne({no: room_id},function(err, room) {
if (room != null){
collection.update( {_id: room._id}, {'$set': {name: name}}, {safe: true}, function(){});
collection.updateOne( {_id: room._id}, {'$set': {name: name}}, {safe: true}, function(){});
}else{
collection.save( {no: room_id, name: name}, function(){} );
collection.insertOne( {no: room_id, name: name}, function(){} );
}
});
});
}

module.exports.get_room_name = function(room_id, callback){
db.collection(table_room_name, function(err, collection) {
collection.findOne({no: room_id},function(err, room) {
db.collection(table_room_name)
.findOne({no: room_id},function(err, room) {
if (room != null){
callback(room.name);
}
});
});
}

module.exports.get_room_names = function(){
return new Promise(function(callback){
db.collection(table_room_name, function(err, collection) {
collection.find({}).toArray(function(err, rooms) {
db.collection(table_room_name)
.find({}).toArray(function(err, rooms) {
if (rooms != null){
callback(rooms);
}
});
});
});
}


module.exports.get_tab_numbers = function(){
return new Promise(function(callback){
db.collection(table_tab_numbers, function(err, collection) {
collection.findOne({},function(err, data) {
db.collection(table_tab_numbers)
.findOne({},function(err, data) {
callback(data);
});
});
});
}

module.exports.update_tab_numbers = function(numbers){
db.collection(table_tab_numbers, function(err, collection) {
collection.findOne({},function(err, tab_numbers) {
db.collection(table_tab_numbers)
.findOne({},function(err, tab_numbers) {
if (tab_numbers != null){
collection.update( {_id: tab_numbers._id}, {'$set': numbers}, {safe: true}, function(){});
collection.updateOne( {_id: tab_numbers._id}, {'$set': numbers}, {safe: true}, function(){});
}else{
collection.save( numbers, function(){} );
collection.insertOne( numbers, function(){} );
}
});
});
}
Loading