forked from byteball/ocore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_storage.js
24 lines (20 loc) · 850 Bytes
/
chat_storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*jslint node: true */
"use strict";
var db = require('./db.js');
function store(correspondent_address, message, is_incoming, type) {
var type = type || 'text';
db.query("INSERT INTO chat_messages ('correspondent_address', 'message', 'is_incoming', 'type') VALUES (?, ?, ?, ?)", [correspondent_address, message, is_incoming, type]);
}
function load(correspondent_address, up_to_id, limit, cb) {
db.query("SELECT id, message, creation_date, is_incoming, type FROM chat_messages \n\
WHERE correspondent_address=? AND id < "+up_to_id+" ORDER BY id DESC LIMIT ?", [correspondent_address, limit], function(rows){
cb(rows);
});
}
function purge(correspondent_address) {
db.query("DELETE FROM chat_messages \n\
WHERE correspondent_address=?", [correspondent_address]);
}
exports.store = store;
exports.load = load;
exports.purge = purge;