-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine.js
72 lines (39 loc) · 1.36 KB
/
engine.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Engine module responsible for almost all application logic.
*/
function Engine() {
this.tweetId = 0;
};
exports.Engine = Engine;
var databaseModule = require('./database-mysql');
var database = new databaseModule.Database();
Engine.prototype.getTweets = function (username, callback) {
//console.log('getting tweets for user ' + username);
database.selectTweets(username, function(tweets) {
if (tweets) {
tweets.sort(dateSorter);
callback(tweets.slice(0, 19));
} else callback([]);
});
};
Engine.prototype.postTweet = function (username, status, callback) {
//console.log('posting tweet for user ' + username + ' with status: ' + status);
database.insertTweet(username, status, function(tweet) {
callback(tweet);
});
};
Engine.prototype.getTimeline = function (username, callback) {
//console.log('getting timeline for user ' + username);
database.selectTimeline(username, function(tweets) {
if (tweets) {
tweets.sort(dateSorter);
callback(tweets.slice(0, 19));
} else callback([]);
});
};
//------------------------------------------------------------------------------
// date sorting, needed for reducing tweets returned by home timeline
// we're passing it to sort() method
var dateSorter = function (a, b) {
return a.created_at.getTime() < b.created_at.getTime() ? true : false;
};