Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Dec 6, 2020
1 parent 84e855c commit 22ec0af
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 9 deletions.
102 changes: 94 additions & 8 deletions controllers/tweet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const User = require('../models/User');

//we want to validate the token sent in the header
// then we want to grab the user's tweets through the association we setup

const months = ['Jan', 'Feb', 'Mar',
'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sep', 'Oct',
'Nov', 'Dec'];

exports.create_tweet = async function(req, res, next) {
let errors = await validateTweet({}, req);
if (!isEmpty(errors)) next(errors);
Expand Down Expand Up @@ -37,8 +43,29 @@ exports.show_timeline = async function(req, res, next) {
const userId = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET).id;
const userFriends = await models.Follower.findAll({where: {follower_id: userId}});
const userIds = userFriends.map(friend => friend.user_id);
const friendTweets = await models.Tweet.findAll({where: {user_id: userIds}});
res.status(200).send({timeline: friendTweets});
const friendTweets = await models.Tweet.findAll({
where: {user_id: userIds},
order: [['createdAt', 'DESC']]
});
const timeline = await friendTweets.map(async tweet => {
const tweetObj = {};
const user = await models.User.findOne({where: {id: tweet.user_id}});
const date = new Date(tweet.createdAt);
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
const currDate = new Date();
let formattedDate;
if (currDate.getFullYear() == year) formattedDate = `${months[month]} ${day}`;
else formattedDate = `${months[month]} ${day} ${year}`;
tweetObj['username'] = user.userName;
tweetObj['displayName'] = user.name;
tweetObj['text'] = tweet.content;
tweetObj['date'] = formattedDate;
return tweetObj;
})
const resolvedTimeline = await Promise.all(timeline);
res.status(200).send({timeline: resolvedTimeline});
}
}

Expand All @@ -51,8 +78,27 @@ exports.show_tweeths = async function(req, res, next) {
else {
const username = req.params.username;
const user = await models.User.findOne({where: {userName: username}});
const userTweeths = await models.Tweet.findAll({where: {user_id: user.id, isTweeth: true}});
res.status(200).send({tweeths: userTweeths});
const userTweeths = await models.Tweet.findAll({
where: {user_id: user.id, isTweeth: true},
order: [['createdAt', 'DESC']]
});
const tweeths = userTweeths.map(tweeth => {
const tweetObj = {};
const date = new Date(tweeth.createdAt);
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
const currDate = new Date();
let formattedDate;
if (currDate.getFullYear() == year) formattedDate = `${months[month]} ${day}`;
else formattedDate = `${months[month]} ${day} ${year}`;
tweetObj['username'] = user.userName;
tweetObj['displayName'] = user.name;
tweetObj['text'] = tweeth.content;
tweetObj['date'] = formattedDate;
return tweetObj;
})
res.status(200).send({tweeths: tweeths});
}
}

Expand All @@ -62,8 +108,27 @@ exports.show_tweets = async function(req, res, next) {
else {
const username = req.params.username;
const user = await models.User.findOne({where: {userName: username}});
const userTweets = await models.Tweet.findAll({where: {user_id: user.id, isTweeth: false}});
res.status(200).send({tweets: userTweets});
const userTweets = await models.Tweet.findAll({
where: {user_id: user.id, isTweeth: false},
order: [['createdAt', 'DESC']]
});
const tweets = userTweets.map(tweet => {
const tweetObj = {};
const date = new Date(tweet.createdAt);
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
const currDate = new Date();
let formattedDate;
if (currDate.getFullYear() == year) formattedDate = `${months[month]} ${day}`;
else formattedDate = `${months[month]} ${day} ${year}`;
tweetObj['username'] = user.userName;
tweetObj['displayName'] = user.name;
tweetObj['text'] = tweet.content;
tweetObj['date'] = formattedDate;
return tweetObj;
});
res.status(200).send({tweets: tweets});
}
}

Expand All @@ -75,7 +140,28 @@ exports.show_likes = async function(req, res, next) {
const user = await models.User.findOne({where: {userName: username}});
const likes = await models.Like.findAll({where: {user_id: user.id}});
const tweetIds = likes.map(like => like.tweet_id);
const likedTweets = await models.Tweet.findAll({where: {id: tweetIds}});
res.status(200).send({likes: likedTweets});
const likedTweets = await models.Tweet.findAll({
where: {id: tweetIds},
order: [['createdAt', 'DESC']]
});
const payload = await likedTweets.map(async tweet => {
const tweetObj = {};
const user = await models.User.findOne({where: {id: tweet.user_id}});
const date = new Date(tweet.createdAt);
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
const currDate = new Date();
let formattedDate;
if (currDate.getFullYear() == year) formattedDate = `${months[month]} ${day}`;
else formattedDate = `${months[month]} ${day} ${year}`;
tweetObj['username'] = user.userName;
tweetObj['displayName'] = user.name;
tweetObj['text'] = tweet.content;
tweetObj['date'] = formattedDate;
return tweetObj;
});
const finalPayload = await Promise.all(payload);
res.status(200).send({likes: finalPayload});
}
}
22 changes: 22 additions & 0 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const jwt = require('jsonwebtoken');
const { validateSignup, validateLogin, validateToken } = require('../validators/userValidations');
const { isEmpty } = require('lodash');
const models = require('../models');
const { Op } = require('sequelize');

const generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
Expand Down Expand Up @@ -53,4 +54,25 @@ exports.show_user = async function (req, res, next) {
userInfo['isFollowed'] = isFollowed;
res.status(200).send(userInfo);
}
}

exports.show_users = async function (req, res, next) {
const errors = await validateToken({}, req);
if (!isEmpty(errors)) next(errors);
else {
console.log(req.query.match);
const users = req.query.match ?
await models.User.findAll({
where: {
userName: {
[Op.like]: req.query.match + '%'
}
}
}) :
await models.User.findAll();
const userInfo = users.map(user => {
return {name: user.name, userName: user.userName}
});
res.status(200).send(userInfo);
}
}
3 changes: 2 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const follower = require('../controllers/follower');

router.post('/signup', user.signup);
router.post('/login', user.login);
router.get('/users/:username', user.show_user)
router.get('/users/:username', user.show_user);
router.get('/users', user.show_users);


// follow routes
Expand Down

0 comments on commit 22ec0af

Please sign in to comment.