-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6d7826b
Showing
2,008 changed files
with
384,295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DATABASE=mongodb+srv://ajayghiyad:[email protected]/myFirstDatabase?retryWrites=true&w=majority | ||
JWT_SECRET=AFADADAFSHFSGAD646646464AFADDSFSHFSHADFADFA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"workbench.colorCustomizations": { | ||
"activityBar.background": "#202030", | ||
"titleBar.activeBackground": "#2A2A3E", | ||
"titleBar.activeForeground": "#FBFBFD" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import User from "../models/user"; | ||
import jwt from "jsonwebtoken"; | ||
|
||
import { hashPassword, comparePassword } from "../utils/auth"; | ||
export const signup = async (req, res) => { | ||
try { | ||
const { name, email, username, password } = req.body; | ||
// console.log(name, email, username, password); | ||
|
||
if (!name) return res.status(400).send("Name is required"); | ||
if (!password || password.length < 6) | ||
return res | ||
.status(400) | ||
.send("Password is required and should be min of 6 characters"); | ||
let userExist = await User.findOne({ email }).exec(); | ||
if (userExist) return res.status(400).send("Email is taken"); | ||
userExist = await User.findOne({ username }).exec(); | ||
if (userExist) return res.status(400).send("Username is taken"); | ||
const hashedPassword = await hashPassword(password); | ||
const user = new User({ name, email, username, password: hashedPassword }); | ||
await user.save(); | ||
|
||
res.json({ ok: true }); | ||
} catch (err) { | ||
console.log(err); | ||
return res.status(400).send("Error... Try Again..."); | ||
} | ||
}; | ||
|
||
export const login = async (req, res) => { | ||
try { | ||
const { username, email, password } = req.body; | ||
// console.log(username, email, password); | ||
let user; | ||
if (username != undefined) { | ||
user = await User.findOne({ username }).exec(); | ||
if (!user) return res.status(400).send("No user found"); | ||
console.log("Login with username"); | ||
} | ||
if (email != undefined) { | ||
user = await User.findOne({ email }).exec(); | ||
if (!user) return res.status(400).send("No user found"); | ||
console.log("Login with email"); | ||
} | ||
// console.log(user); | ||
|
||
const match = await comparePassword(password, user.password); | ||
if (!match) return res.status(400).send("Wrong Password!!!"); | ||
const token = jwt.sign({ _id: user._id }, process.env.JWT_SECRET, { | ||
expiresIn: "1d", | ||
}); | ||
|
||
//dont send password | ||
user.password = undefined; | ||
|
||
//send token | ||
res.cookie("token", token, { | ||
httpOnly: true, | ||
//secure: true, | ||
//secure will only work in HTTPS | ||
}); | ||
res.json(user); | ||
} catch (err) { | ||
console.log(err); | ||
return res.status(400).send("Error... Try Again..."); | ||
} | ||
}; | ||
|
||
export const logout = async (req, res) => { | ||
try { | ||
res.clearCookie("token"); | ||
return res.json({ message: "Signout Sucess..." }); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import User from "../models/user"; | ||
import Tweet from "../models/tweet"; | ||
|
||
export const tweet = async (req, res) => { | ||
try { | ||
const userId = req.user._id; | ||
const { content } = req.body; | ||
const tweet = new Tweet({ content, postedBy: userId }); | ||
await tweet.save(); | ||
console.log(tweet); | ||
|
||
return res.json({ ok: true }); | ||
} catch (err) { | ||
console.log(err); | ||
return res.status(400).send("Error... Try again..."); | ||
} | ||
}; | ||
export const likeTweet = async (req, res) => { | ||
try { | ||
const { tweetId } = req.params; | ||
const userId = req.user._id; | ||
const user = await User.findById(userId); | ||
// console.log(user); | ||
var isLiked = user.likes && user.likes.includes(tweetId); | ||
var option = isLiked ? "$pull" : "$addToSet"; | ||
req.user = await User.findByIdAndUpdate( | ||
userId, | ||
{ [option]: { likes: tweetId } }, | ||
{ new: true } | ||
).catch((error) => { | ||
console.log(error); | ||
res.sendStatus(400); | ||
}); | ||
|
||
const tweet = await Tweet.findByIdAndUpdate( | ||
tweetId, | ||
{ [option]: { likes: userId } }, | ||
{ new: true } | ||
).catch((error) => { | ||
console.log(error); | ||
res.sendStatus(400); | ||
}); | ||
console.log(tweet); | ||
res.json({ ok: true }); | ||
} catch (err) { | ||
console.log(err); | ||
return res.status(400).send("Error... Try again..."); | ||
} | ||
}; | ||
export const reTweet = async (req, res) => { | ||
try { | ||
const { tweetId } = req.params; | ||
const userId = req.user._id; | ||
const user = await User.findById(userId); | ||
// console.log(user); | ||
var isRetweet = user.likes && user.retweets.includes(tweetId); | ||
var option = isRetweet ? "$pull" : "$addToSet"; | ||
req.user = await User.findByIdAndUpdate( | ||
userId, | ||
{ [option]: { retweets: tweetId } }, | ||
{ new: true } | ||
).catch((error) => { | ||
console.log(error); | ||
res.sendStatus(400); | ||
}); | ||
// console.log(req.user); | ||
const tweet = await Tweet.findByIdAndUpdate( | ||
tweetId, | ||
{ [option]: { retweetUsers: userId } }, | ||
{ new: true } | ||
).catch((error) => { | ||
console.log(error); | ||
res.sendStatus(400); | ||
}); | ||
console.log(tweet); | ||
res.json({ ok: true }); | ||
} catch (err) { | ||
console.log(err); | ||
return res.status(400).send("Error... Try again..."); | ||
} | ||
}; | ||
export const replyTo = async (req, res) => { | ||
try { | ||
const { content } = req.body; | ||
const userId = req.user._id; | ||
const { tweetId } = req.params; | ||
const tweet = new Tweet({ content, postedBy: userId, replyTo: tweetId }); | ||
await tweet.save(); | ||
|
||
console.log(tweet); | ||
res.json({ ok: true }); | ||
} catch (err) { | ||
console.log(err); | ||
return res.status(400).send("Error... Try again..."); | ||
} | ||
}; | ||
|
||
export const reTweetWithData = async (req, res) => { | ||
try { | ||
const { content } = req.body; | ||
const userId = req.user._id; | ||
const { tweetId } = req.params; | ||
const tweet = new Tweet({ | ||
content, | ||
postedBy: userId, | ||
retweetData: tweetId, | ||
}); | ||
await tweet.save(); | ||
console.log(tweet); | ||
res.json({ ok: true }); | ||
} catch (err) { | ||
console.log(err); | ||
return res.status(400).send("Error... Try again..."); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import Tweet from "../models/tweet"; | ||
import User from "../models/user"; | ||
|
||
export const follow = async (req, res) => { | ||
const { userId } = req.params; | ||
|
||
const user = await User.findById(userId); | ||
if (req.user._id === userId) { | ||
return res.status(400).send("You can't follow yourself i guess"); | ||
} | ||
if (user == null) return res.sendStatus(404); | ||
console.log(req.user); | ||
var isFollowing = user.follower && user.follower.includes(req.user._id); | ||
var option = isFollowing ? "$pull" : "$addToSet"; | ||
|
||
req.user = await User.findByIdAndUpdate( | ||
req.user._id, | ||
{ [option]: { following: userId } }, | ||
{ new: true } | ||
).catch((error) => { | ||
console.log(error); | ||
res.sendStatus(400); | ||
}); | ||
|
||
User.findByIdAndUpdate(userId, { | ||
[option]: { follower: req.user._id }, | ||
}).catch((error) => { | ||
console.log(error); | ||
res.sendStatus(400); | ||
}); | ||
|
||
return res.status(200).send(req.user); | ||
}; | ||
|
||
export const following = async (req, res) => { | ||
try { | ||
console.log(req.params); | ||
const { userId } = req.params; | ||
User.findById(userId) | ||
.populate("following", "_id name username") | ||
.exec() | ||
.then((following) => { | ||
return res.status(200).send(following); | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
return res.send("Error... Try again..."); | ||
} | ||
}; | ||
|
||
export const follower = async (req, res) => { | ||
try { | ||
// console.log(req.params); | ||
const { userId } = req.params; | ||
// const follower = await | ||
User.findById(userId) | ||
.populate("follower", "_id name username") | ||
.exec() | ||
.then((follower) => { | ||
return res.status(200).send(follower); | ||
}); | ||
// return res.status(200).send(follower); | ||
} catch (err) { | ||
console.log(err); | ||
return res.send("Error... Try again..."); | ||
} | ||
}; | ||
|
||
export const feed = async (req, res) => { | ||
try { | ||
const userId = req.user._id; | ||
const user = await User.findById(userId); | ||
let tweet = []; | ||
console.log(user.following); | ||
for (let i = 0; i < user.following.length; i++) { | ||
console.log(user.following[i]); | ||
const array = await Tweet.find({ postedBy: user.following[i] }).exec(); | ||
// console.log(array); | ||
for (let i = 0; i < array.length; i++) { | ||
tweet.push(array[i]); | ||
// console.log(array[i]); | ||
} | ||
} | ||
// await user.following.map((f) => { | ||
// Tweet.find({ postedBy: f }); | ||
// }); | ||
// console.log(tweet); | ||
tweet.sort(function (a, b) { | ||
if (a.createdAt > b.createdAt) { | ||
return -1; | ||
} else if (a.createdAt < b.createdAt) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
res.json(tweet); | ||
} catch (err) { | ||
console.log(err); | ||
return res.send("Error... Try again..."); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import expressJwt from "express-jwt"; | ||
export const requireSignin = expressJwt({ | ||
getToken: (req, res) => req.cookies.token, | ||
secret: process.env.JWT_SECRET, | ||
algorithms: ["HS256"], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import mongoose from "mongoose"; | ||
const { Schema } = mongoose; | ||
const { ObjectId } = Schema; | ||
const tweetSchema = new Schema( | ||
{ | ||
content: { type: String, trim: true }, | ||
postedBy: { type: ObjectId, ref: "User" }, | ||
likes: [{ type: ObjectId, ref: "User" }], | ||
retweetUsers: [{ type: ObjectId, ref: "User" }], | ||
retweetData: { type: ObjectId, ref: "Tweet" }, | ||
replyTo: { type: ObjectId, ref: "Tweet" }, | ||
pinned: Boolean, | ||
}, | ||
{ timestamps: true } | ||
); | ||
export default mongoose.model("Tweet", tweetSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import mongoose from "mongoose"; | ||
const { Schema } = mongoose; | ||
const { ObjectId } = Schema; | ||
const userSchema = new Schema( | ||
{ | ||
username: { type: String, required: true, trim: true, unique: true }, | ||
email: { type: String, required: true, trim: true, unique: true }, | ||
name: { type: String, required: true, trim: true }, | ||
password: { type: String, required: true, min: 6, max: 64 }, | ||
following: [{ type: ObjectId, ref: "User" }], | ||
follower: [{ type: ObjectId, ref: "User" }], | ||
likes: [{ type: ObjectId, ref: "Tweet" }], | ||
retweets: [{ type: ObjectId, ref: "Tweet" }], | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
export default mongoose.model("User", userSchema); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.