-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
207 lines (175 loc) · 7.2 KB
/
server.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var Twitter = require('twitter');
var ObjectId = require('mongodb').ObjectID;
var fs = require('fs');
var util = require('util');
var log_file;
var log_stdout;
var log_id = 0;
var file_exists = true;
var client = new Twitter({
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
/**
* Debug file set-up | Easier fixing when issue has a log....
*/
var mkdirSync = function (path) {
try {
fs.mkdirSync(path);
} catch (e) {
if (e.code != 'EEXIST') throw e;
}
};
mkdirSync(__dirname + "/logs");
while (file_exists) {
if (!fs.existsSync(__dirname + '/logs/debug_' + log_id + '.log')) {
log_file = fs.createWriteStream(__dirname + '/logs/debug_' + log_id + '.log', {flags: 'w'});
log_stdout = process.stdout;
file_exists = !file_exists;
}
else
log_id++;
}
app.use(express.static(__dirname + '/public'));
app.use("/bootstrap_scripts", express.static(path.join(__dirname, 'node_modules/bootstrap/dist/')));
app.all('/', function(req, res){
res.sendFile(__dirname + '/public/index.html');
});
app.all('/topic/:topic', function(req, res) {
res.sendFile(__dirname + '/public/index.html', {topic: req.topic});
});
//app.all('/*', function(req, res, next) {
// res.sendFile('index.html', { root: __dirname+'/public/' });
//});
//app.all('/', function (req, res) {
// res.sendFile(__dirname + '/public/index.html');
//});
console.log(__dirname);
var maxUsername = 1000000;
var minUsername = 9999999;
var trendsList = [];
(function(){
client.get('trends/place', {id: 1}, function (error, tweets, response) {
console.log(error);
if (error) throw error;
var trends = tweets[0].trends;
trendsList = [];
trends.forEach(function (trend) {
trendsList.push(trend.name);
});
});
setTimeout(arguments.callee, 1000 * 60 * 30);// Update the trending topics every 30 mins...
})();
var MongoClient = require('mongodb').MongoClient, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/starForums';
// Use connect method to connect to the Server
var dbCon;
MongoClient.connect(url, function (err, dbConnection) {
assert.equal(null, err);
dbCon = dbConnection;
console.log("Connected correctly to server. Server can now send queries.");
});
io.on('connection', function (socket) {
console.log("User connected from " + socket.request.connection._peername.address.address);
socket.emit('trendingTopics', JSON.stringify(trendsList));
var topicsCollection = dbCon.collection('topic_comments');
topicsCollection.find().sort({'_id': -1}).limit(5).toArray(function (err, items) {
socket.emit('recentTopics', JSON.stringify(items));
});
socket.on('disconnect', function () {
console.log('User Disconnected.');
});
socket.on('fetchUserInfo', function (user_sessionJSON) {
var user_session = JSON.parse(user_sessionJSON);
var usersCollection = dbCon.collection('users');
usersCollection.find({session: user_session.session}).toArray(function (err, results) {
if (results.length > 0) {
socket.emit('returnUserInfo', JSON.stringify(results[0]));
}
else {
var userInfo = {
username: Math.floor(Math.random() * (maxUsername - minUsername)) + minUsername,
user_ip: user_session.ip,
session: (Math.random().toString(36) + '00000000000000000').slice(2, 40 + 2)//crypto.createHash('md5').update((Math.random().toString(36)+'00000000000000000').slice(2, 40+2)).digest('hex')
};
usersCollection.find({"username": userInfo.username}).toArray(function (err, results) {
if (results.length > 0) {
// Deny registration - Username already exists.
//socket.emit("failedUserRegistration")
}
else {
// Process the registration...
usersCollection.insertOne(userInfo, {w: 1}, function (err2, result2) {
socket.emit("createdUser", JSON.stringify({session: userInfo.session}));
});
}
});
}
});
});
socket.on('deleteComment', function (comment_details) {
var comment = JSON.parse(comment_details);
if (comment != undefined) {
var usersCollection = dbCon.collection('users');
usersCollection.find({session: comment.session}).toArray(function (err, results) {
var username = results[0].username;
var topicsCollection = dbCon.collection('topic_comments');
topicsCollection.find({
username: username,
_id: ObjectId(comment._id)
}).toArray(function (err2, results2) {
if (results2.length > 0) {
console.log(username + " deleted a comment with '" + results2[0].comment + "'");
topicsCollection.removeOne({
username: username,
_id: ObjectId(comment._id)
}, function (err3, results3) {
io.emit("deletedComment", JSON.stringify(results2[0]));
var topicsCollection = dbCon.collection('topic_comments');
topicsCollection.find().sort({'_id': -1}).limit(5).toArray(function (err, items) {
socket.emit('recentTopics', JSON.stringify(items));
});
});
}
});
});
}
});
socket.on('addComment', function (comment_details) {
var commentDetails = JSON.parse(comment_details);
var usersCollection = dbCon.collection('users');
usersCollection.find({session: commentDetails.session}).toArray(function (err, results) {
var commentsCollection = dbCon.collection('topic_comments');
commentsCollection.insertOne({
username: results[0].username,
topic: commentDetails.topic,
topic_lower: commentDetails.topic.toLowerCase(),
comment: commentDetails.comment,
post_time: new Date().getTime()
}, function (err2, results2) {
io.emit('new_comment', JSON.stringify(results2.ops[0]));
});
})
});
socket.on('requestTopicDetails', function (topicName) {
var topicsCollection = dbCon.collection('topic_comments');
topicsCollection.find({topic_lower: topicName.toLowerCase()}).toArray(function (err, results) {
socket.emit('topicDetails', JSON.stringify(results));
})
});
});
http.listen(3000, function () {
console.log('Listening on *:3000');
});
console.log = function (d) { //
log_file.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};