Skip to content

Commit

Permalink
Add message endpoint + logout endpoint + control script
Browse files Browse the repository at this point in the history
  • Loading branch information
Esben Gade Hellmuth authored and emhama committed Feb 12, 2020
1 parent 5cf8737 commit 9d9e426
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion control.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if [ $1 = "init" ]; then
python -c"from minitwit import init_db;init_db()"
elif [ $1 = "start" ]; then
echo "Starting minitwit..."
nohup python minitwit.py > /tmp/out.log 2>&1 &
nohup nodejs minitwit.js > /tmp/out.log 2>&1 &
echo $! > /tmp/minitwit.pid
elif [ $1 = "stop" ]; then
echo "Stopping minitwit..."
Expand Down
39 changes: 39 additions & 0 deletions minitwit.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,36 @@ app.get('/public', async function(req, res) {
});
});

// Add message handler
app.post('/add_message', async function(req, res) {
const user_id = req.session.user_id;
const text = req.body.text;
if(!user_id) {
return res.status(401).send();
} else {
await insertOne(
'INSERT INTO message(author_id, text, pub_date) VALUES(?, ?, ?)',
[user_id, text, Date.now()])
let messages = await selectAll(
`SELECT message.*, user.* FROM message, user
WHERE message.author_id = user.user_id AND (
user.user_id = ? OR
user.user_id IN (
SELECT whom_id FROM follower
WHERE who_id = ?))
ORDER BY message.pub_date DESC LIMIT ?`,
[req.session.user_id, req.session.user_id, PER_PAGE]
);
return res.render('pages/timeline', {
flashes: ['Your message was recorded'],
session_user_id: req.session.user_id,
session_username: req.session.username,
messages: messages,
myFeed: true
});
}
});

// Register page
app.get('/register', function(req, res) {
res.render('pages/register', {
Expand Down Expand Up @@ -159,6 +189,15 @@ app.post('/login', async function(req, res) {
});
});

// Logout
app.get('/logout', function(req, res) {
req.session.user_id = null;
req.session.username = null;
res.render('pages/timeline', {
flashes: ['You were logged out']
});
});

// User timeline page
app.get('/:username', async function(req, res) {
const { username } = req.params;
Expand Down

0 comments on commit 9d9e426

Please sign in to comment.