forked from adamjodlowski/tweet-perf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.js
89 lines (57 loc) · 1.93 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Engine module responsible for almost all application logic.
*/
function Engine() {
this.tweetId = 0;
};
exports.Engine = Engine;
var databaseModule = require('./database-couchdb');
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) {
var results = [];
tweets.sort(dateSorter);
for (var i=0; i < tweets.slice(0,19).length; i++){
results[i] = {
"created_at": tweets[i].created_at,
"text": tweets[i].text,
"id": tweets[i].id,
"user": {
"name": tweets[i].name,
"id": tweets[i].id,
"screen_name": tweets[i].screen_name
}
};
};
callback(results);
} else callback([]);
});
};
//------------------------------------------------------------------------------
// date sorting, needed for reducing tweets returned by home timeline
// we're passing it to sort() method
var dateSorter = function (a, b) {
//console.log(a.created_at);
//console.log(Date.parse(a.created_at));
//console.log(b.created_at);
//console.log(Date.parse(b.created_at));
//console.log(Date.parse(a.created_at) > Date.parse(b.created_at));
return Date.parse(a.created_at) > Date.parse(b.created_at) ? true : false;
};