-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
179 lines (149 loc) · 4.4 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
require('dotenv').config()
const express = require('express')
const path = require('path')
const redis = require('redis')
const bcrypt = require('bcrypt')
const session = require('express-session')
const { promisify } = require('util')
const { formatDistance } = require('date-fns')
const RedisStore = require('connect-redis')(session)
const app = express()
const client = redis.createClient(process.env.REDIS_URL)
const ahget = promisify(client.hget).bind(client)
const asmembers = promisify(client.smembers).bind(client)
const ahkeys = promisify(client.hkeys).bind(client)
const aincr = promisify(client.incr).bind(client)
const alrange = promisify(client.lrange).bind(client)
app.use(express.urlencoded({ extended: true }))
app.use(
session({
store: new RedisStore({ client: client }),
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 36000000,
httpOnly: false,
secure: false,
},
secret: process.env.API_KEY,
})
)
app.set('view engine', 'pug')
app.set('views', path.join(__dirname, 'views'))
app.get('/', async (req, res) => {
if (req.session.userid) {
const currentUserName = await ahget(`user:${req.session.userid}`, 'username')
const following = await asmembers(`following:${currentUserName}`)
const users = await ahkeys('users')
const timeline = []
const posts = await alrange(`timeline:${currentUserName}`, 0, 100)
for (post of posts) {
const timestamp = await ahget(`post:${post}`, 'timestamp')
const timeString = formatDistance(new Date(), new Date(parseInt(timestamp)))
timeline.push({
message: await ahget(`post:${post}`, 'message'),
author: await ahget(`post:${post}`, 'username'),
timeString: timeString,
})
}
res.render('dashboard', {
users: users.filter(user => user !== currentUserName && following.indexOf(user) === -1),
currentUserName,
timeline,
})
} else {
res.render('login')
}
})
app.post('/', (req, res) => {
const { username, password } = req.body
if (!username || !password) {
res.render('error', {
message: 'Please set both username and password',
})
return
}
console.log(req.body, username, password)
client.hget('users', username, (err, userid) => {
if (!userid) {
//user does not exist, signup procedure
client.incr('userid', async (err, userid) => {
client.hset('users', username, userid)
const saltRounds = 10
const hash = await bcrypt.hash(password, saltRounds)
client.hset(`user:${userid}`, 'hash', hash, 'username', username)
saveSessionAndRenderDashboard(userid)
})
} else {
//user exists, login procedure
client.hget(`user:${userid}`, 'hash', async (err, hash) => {
const result = await bcrypt.compare(password, hash)
if (result) {
saveSessionAndRenderDashboard(userid)
} else {
res.render('error', {
message: 'Incorrect password',
})
return
}
})
}
})
const saveSessionAndRenderDashboard = userid => {
req.session.userid = userid
req.session.save()
res.redirect('/')
}
})
app.get('/post', (req,res) => {
if (req.session.userid) {
res.render('post')
} else {
res.render('login')
}
})
app.post('/post', async (req,res) => {
if(!req.session.userid) {
res.render('login')
return
}
const { message } = req.body
const currentUserName = await ahget(`user:${req.session.userid}`, 'username')
const postid = await aincr('postid')
client.hmset(
`post:${postid}`,
'userid',
req.session.userid,
'username',
currentUserName,
'message',
message,
'timestamp',
Date.now()
)
client.lpush(`timeline:${currentUserName}`, postid)
const followers = await asmembers(`followers:${currentUserName}`)
for (follower of followers) {
client.lpush(`timeline:${follower}`, postid)
}
res.redirect('/')
})
app.post('/follow', (req, res) => {
if(!req.session.userid) {
res.render('login')
return
}
const { username } = req.body
client.hget(
`user:${req.session.userid}`,
'username',
(err, currentUserName) => {
client.sadd(`following:${currentUserName}`, username)
client.sadd(`followers:${username}`, currentUserName)
},
)
res.redirect('/')
})
app.listen(process.env.PORT, () => {
console.log(`Server listening on port ${process.env.PORT}... `)
})