-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
313 lines (259 loc) · 8.5 KB
/
index.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
const express = require('express');
const bodyParser = require("body-parser"); //post request
const urlencodedParser = bodyParser.urlencoded({extended: false});
const port = process.env.PORT || 3007;
const app = express();
const utils = require('./utils');
const adminEmail = "michelegrande";
/**
* todo: logout, likes, friends
*/const cors = require('cors');
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, email, authtoken, Accept");
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
next();
});
let { userModel, postModel } = require('./model');
app.get('/', (req, res, next) => {
res.send({active: true, updatedOn: new Date()});
});
app.post('/signup', async (req, res, next) => {
console.log("signup")
const {firstName, lastName, password, email} = req.body;
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
try {
if(!firstName || !lastName || firstName.length < 3 || lastName.length < 3){
res.send({success: false, message: "First and Last Name Should be 3 character Long" });}
if(!password || password < 5 ){
res.send({success: false, message: "Password Must be 5 Characters or Longer" });
}
if(!email || !emailRegex.test(String(email).toLowerCase())){
res.send({success: false, message: "Invalid Email" });
}
if(firstName.length > 31 || email.length > 31 || lastName.length > 31 ){
res.send({success: false, message: "31 is the Character Limit" });
}
let user = await new userModel(req.body);
await user.save();
user = await userModel.findOne({_id: user._id});
//there is always one token when user is
//created for the first time
let token = await user.tokens[0].identifier;
user = await user.toJson();
await res.send({success: true, user: user, token });
}
catch(err) {
next(err);
}
});
app.post('/login', async (req, res, next) => {
try {
let user = await userModel.findOne({email: req.body.email}).sort({createdAt: 1});
if (!user)
return next("user with email doesn't exist");
if (!(user.password === req.body.password))
return next("Authentication failed")
let token = utils.createToken();
user.tokens.push(token);
await user.save();
res.send({success: true, user: user.toJson(), token: token.identifier});
}
catch(err) {
next(err);
}
})
//authentication wall
app.use(async function(req, res, next){
try {
let user = await userModel.findUserWithToken(req.headers.authtoken, req.headers.email);
if (!user)
return next("user not found");
req.user = user;
next();
}
catch(err) {
next(err);
}
});
app.get('/check-token', async (req, res) => {
let user =await userModel.findOne({email: req.headers.email});
user = user.toObject()
delete user.password;
delete user.tokens;
res.send({success: true, user: user});
});
//this returns my post
app.get('/posts', async (req, res, next) => {
try {
let data = await postModel.getPostsOfUser(req.user._id);
res.send({success: true, data: data});
}
catch(err) {
next(err);
}
});
app.get('/posts/all', async (req, res, next) => {
try {
let data = await postModel.getPosts();
res.send({success: true, data: data});
}
catch(err) {
next(err);
}
});
//this returns all posts ... friends later
app.post('/posts/user', async (req, res, next) => {
try {
let data =await postModel.getPostsOfOtherUser(req.body._id);
await res.send({success: true, data: data});
}
catch(err) {
next(err);
}
});
//this returns all posts ... friends later
app.post('/details', async (req, res, next) => {
try {
let data =await await userModel.findOne({_id: req.body._id}).sort({createdAt: 1});
if(data != null){
data = data.toObject()
delete data.tokens;
delete data.password;
delete data.email;
res.send({success: true, data})
}
else {
res.send({success: false})
}
}
catch(err) {
next(err);
}
});
app.post('/posts', async (req, res, next) => {
req.body.userId = req.user._id;
let {videoUrl, imageUrl, caption, visibility} = req.body;
visibility = visibility=== undefined || visibility === null? "private" : visibility.toLowerCase();
await new postModel(req.body).save();
if(visibility === "public")
res.send({success: true, modal: false});
else {
res.send({success: true, modal: true});
}
});
// { text: "" }
app.put('/comment', async (req, res, next) => {
try {
console.log(req)
let post = await postModel.findOne({_id: req.body._id});
if (!post)
return next("not found");
let comment = {
text: req.body.text,
userId: req.user._id,
createdAt: new Date()
};
post.comments.push(comment);
await post.save();
await res.send({ success: true });
}
catch(err) {
next(err);
}
});
// { image_url: [ String ] }
app.put('/posts/:id/image', async (req, res, next) => {
try {
let data = await postModel.findOne({_id: req.params.id});
if (!data)
return next("not found");
data.image_url = [ ...data.image_url, ...req.body.image_url ];
await data.save();
res.send({success: true});
}
catch(err) {
next(err);
}
});
app.put('/save-post', async (req, res, next) => {
try {
let data = await userModel.findOne({email: req.headers.email});
let posts = await data.savedPosts;
function objectChecker(posts,_id){
posts.map((post)=>{
if(post._id.valueOf() === _id.valueOf())
return true;
console.log(post._id );
});
return false;
}
let alreadySaved = objectChecker(posts, req.body._id);
if(!alreadySaved){
posts = [...posts, {_id: req.body._id}];
await userModel.updateOne({email: req.headers.email}, {$set: {savedPosts: posts}}, {upsert: true})
.then(r =>(res.send({success: true})))
.catch((err) => {console.log(err), res.send({success: false})});
}else{
res.send({success: false, message: "Already Saved!"});
}
}
catch(err) {
next(err);
}
});
app.get('/saved-posts', async (req, res, next) => {
try {
let data = await userModel.findOne({email: req.headers.email});
let posts = await data.savedPosts;
let postsToSend = [];
let temp = await postModel.getPost({_id: posts[0]._id});
temp = await postModel.getSavedPosts(posts);
await res.send({success: true, data: temp});
}
catch(err) {
next(err);
}
});
app.put('/delete-post', async (req, res, next) => {
try {
// _id: req.headers.email
let user = await userModel.findOne({email: req.headers.email});
let userId = await user._id;
let data = await postModel.remove({_id: req.body._id, userId: userId});
let hasPost = await postModel.count({_id: req.body._id});
res.send({success: hasPost == 0, message: "Post Could not be Deleted"});
}
catch(err) {
next(err);
}
});
app.put('/posts/:id/likes', async (req, res, next) => {
//todo
});
// { }
app.post('/change', async (req, res, next) => {
let {firstName, lastName, imageUrl, email, _id, facebook, twitter, instagram, work} = req.body;
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
try {
if(!firstName || !lastName || firstName.length < 3 || lastName.length < 3){
res.send({success: false, message: "First and Last Name Should be 3 character Long" });}
else{
let data = await userModel.updateOne({_id: _id }, {$set: {firstName, lastName, imageUrl, facebook, twitter, instagram, work}}, {upsert: true})
.then(r =>(res.send({success: true})))
.catch((err) => {console.log(err), res.send({success: false, message: err})});
}
} catch(err){
next(err);
}
});
app.use(function(error, req, res, next) {
console.log(error);
res.send({success: false, message: error.message || error });
});
app.listen(port, ()=>{console.log(`Server Running on Port:${port}.`); });
require('mongoose').connect("mongodb://aayush:[email protected]:43062/dreammy_2")
.then(() => { console.log("connection succesfull"); });