-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
406 lines (359 loc) · 10.9 KB
/
app.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const CircularJSON = require("circular-json");
const jaccard = require("jaccard");
const User = require("./models/user.js");
const Topic = require("./models/topic.js");
const Conversation = require("./models/conversation.js");
const Message = require("./models/message.js");
// from resource import resource
// Make sure to change this in client as well
const port = process.env.PORT | 3000;
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Connect to Mongoose
const mongourl = "mongodb://127.0.0.1:27017";
const dbName = "hukumaa";
const userCollectionName = "users";
const messageCollectionName = "messages";
const conversationCollectionName = "conversations";
const topicCollectionName = "topics";
mongoose.Promise = global.Promise;
mongoose.connect(mongourl + "/" + dbName, { useMongoClient: true });
var db = mongoose.connection;
//Bind connection to error event (to get notification of connection errors)
db.on("error", console.error.bind(console, "MongoDB connection error:"));
// Allow CORS
app.all("/*", function(request, response, next) {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "X-Requested-With");
response.header("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS");
response.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
// Info only
app.get("/info", (request, response) => {
console.log(new Date(), "+++--- /info");
response.send("Info");
});
// basic landing page
app.get("/", (request, response) => {
console.log(new Date(), "+++--- /");
response.send("This is indeed the basic landing page");
});
// create new user
app.post("/newtopic", (request, response) => {
// console.log(request.body);
var topic = request.body;
// assign _id, time,
if (!topic._id) {
topic._id = mongoose.Types.ObjectId().toString();
}
if (!topic.title) {
response.send("Cannot create empty topic");
}
db.collection(topicCollectionName).findOne({ title: topic.title }, function(
err,
results
) {
if (err) {
response.send("Error in creating topic", err);
}
if (results == null) {
var insertTopic = Topic(topic);
db.collection(topicCollectionName).insert(insertTopic);
response.status(201).send("Created topic " + topic.title);
} else {
response.send("Topic already exists");
}
});
});
// Get all topics / topics that match certain string
app.get("/topics", (request, response) => {
if (!request.query.search) request.query.search = "";
var query = { title: { $regex: request.query.search } };
db.collection(topicCollectionName)
.find(query)
.sort({ created: -1 })
.toArray(function(err, results) {
if (results) {
// if request.query.search
response.send(CircularJSON.stringify(results));
} else {
response.send("No results found");
}
if (err) console.log("Error retrieving topics", err);
});
});
app.post("/newuser", (request, response) => {
var user = request.body;
if (!user.name) {
response.send("No username provided!");
}
if (user.topics) {
for (index in user.topics) {
var topictitle = user.topics[index];
db.collection(topicCollectionName).findOne(
{ title: topictitle },
function(err, results) {
if (results == null) {
var insertTopic = {
_id: mongoose.Types.ObjectId().toString(),
title: topictitle
};
db.collection(topicCollectionName).insert(insertTopic);
console.log("Created topic " + topictitle);
}
}
);
}
}
var insertUser = User(user);
db.collection(userCollectionName).insert(insertUser);
response.status(201).send(CircularJSON.stringify(user));
});
app.get("/search", (request, response) => {
var topiclist = decodeURIComponent(request.query.topics).split(",");
console.log(topiclist);
var searchParams = {
type: "Senior"
};
db.collection(userCollectionName)
.find(searchParams)
.sort({ created: -1 })
.toArray(function(err, results) {
if (err) {
console.log("Error retrieving wise folks", err);
}
var scores = [];
var matches = [];
var minScore = 1,
minIndex = 0;
var keyvalues = [];
results.forEach(result => {
var jscore = jaccard.index(topiclist, result.topics);
keyvalues.push([result, jscore]);
});
// sort by score
keyvalues.sort(function compare(kv1, kv2) {
return kv2[1] - kv1[1];
});
var matches = [];
for (var i = 0; i < keyvalues.length; i++) matches.push(keyvalues[i][0]);
response.send(matches);
});
});
app.get("/person", (request, response) => {
if (!request.query.id) {
response.send("Need to send id !");
}
db.collection(userCollectionName).findOne({ _id: request.query.id }, function(
err,
result
) {
if (err) {
console.log("Cannot fetch resource of id!", err);
response.send({});
}
response.send(result);
});
});
app.post("/newconversation", (request, response) => {
var conversation = request.body;
// required id1 and id2
if (!conversation.id1 || !conversation.id2) {
response.send("Need both ids !");
}
db.collection(userCollectionName).findOne({ _id: conversation.id1 }, function(
err,
result
) {
if (err) {
response.send("Cannot find id1 ! ");
}
});
db.collection(userCollectionName).findOne({ _id: conversation.id2 }, function(
err,
result
) {
if (err) {
response.send("Cannot find id2 ! ");
}
});
var insertConversation = Conversation(conversation);
db.collection(conversationCollectionName).insertOne(
insertConversation,
function(err, result) {
if (err) {
response.send("Error in creating conversation " + err);
}
response.status(201).send(result.ops[0]);
}
);
});
app.put("/approveconversation", (request, response) => {
var status = request.body.status;
var conversationId = request.body.conversationid;
if (!status) {
response.send("Send status!");
}
var query = { _id: conversationId };
var update = { $set: { isApproved: status, toApprove: false } };
var options = { returnNewDocument: true };
db.collection(conversationCollectionName).findOneAndUpdate(
query,
update,
options,
function(err, result) {
if (err) {
response.send("Could not update status");
}
if (result) {
response.send("Updated status " + CircularJSON.stringify(result));
}
}
);
});
app.get("/inbox", (request, response) => {
var id = request.query.id;
db.collection(userCollectionName).findOne({ _id: id }, function(err, result) {
if (err) {
response.send("Error in searching id", err);
}
if (result == null) {
response.send("No user found");
}
// check for all conversations where this id is either in id1 or id2
var options = {};
var query = { $or: [{ id1: id }, { id2: id }] };
db.collection(conversationCollectionName)
.find(query, options)
.sort({ updated: -1 })
.toArray(function(err, results) {
if (err) {
response.send("Cannot fetch conversations", err);
}
response.send(results);
});
});
});
app.get("/conversation", (request, response) => {
var cid = request.query.id;
if (cid) {
db.collection(conversationCollectionName).findOne({ _id: cid }, function(
err,
result
) {
if (err || result == null) {
response.send("Cannot fetch conversation" + err);
}
var query = { conversation: cid };
var options = {};
db.collection(messageCollectionName)
.find(query, options)
.sort({ created: -1 })
.toArray(function(err, results) {
if (err) {
response.send("Cannot fetch messages" + err);
}
if (result == null) {
response.send("No conversation found");
}
result.messages = results;
response.send(result);
});
});
} else {
var id1 = request.query.id1;
var id2 = request.query.id2;
if (!id1 || !id2) {
response.send("Need to send conv id or id1 and id2!");
}
var query = { $or: [{ id1: id1, id2: id2 }, { id2: id1, id1: id2 }] };
db.collection(conversationCollectionName).findOne(query, function(
err,
result
) {
if (err || result == null) {
response.send("Cannot find conversation " + err);
}
var conv_query = { conversation: result._id };
db.collection(messageCollectionName)
.find(conv_query)
.sort({ created: -1 })
.toArray(function(err, results) {
if (err) {
response.send("Cannot fetch messages" + err);
}
result.messages = results;
response.send(result);
});
});
}
});
app.post("/newmessage", (request, response) => {
var cid = request.body.conversation;
var senderid = request.body.sender;
var content = request.body.content;
if (!cid || !senderid || !content) {
response.send("Need to provide conv id, sender id and content!");
}
db.collection(userCollectionName).findOne({ _id: senderid }, function(
err,
result
) {
if (err || result == null) {
response.send("Could not retrieve user " + err);
}
db.collection(conversationCollectionName).findOne({ _id: cid }, function(
err,
conv_result
) {
if (err || conv_result == null) {
response.send("Could not retrieve conversation " + err);
}
if (!conv_result.isApproved && !conv_result.toApprove) {
response.send("Cannot send message in this conversation");
}
// create message
var message = {
_id: mongoose.Types.ObjectId().toString(),
content: content,
sender: senderid,
conversation: cid
};
var insertMessage = Message(message);
db.collection(messageCollectionName).insert(insertMessage, function(
err,
result
) {
if (err) {
response.send("Could not make message " + err);
}
var query = { _id: cid };
conv_result.updated = new Date().toISOString();
var update = conv_result;
var options = { returnNewDocument: true, upsert: true };
db.collection(conversationCollectionName).findOneAndUpdate(
query,
update,
options,
function(err, conv_result) {
if (err) {
response.send("Could not update conversation");
}
if (conv_result) {
response.send(result);
}
}
);
});
});
});
});
app.listen(port, () => {
console.log("Server listening on port " + port);
});