Skip to content

Commit

Permalink
Merge pull request #15 from samdsk/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
samdsk authored Apr 26, 2023
2 parents bfb8a8d + 18f5f1d commit 41d8ada
Show file tree
Hide file tree
Showing 44 changed files with 375 additions and 445 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ credentials.txt
*.log
*.sql
*.sqlite

.DS_Store
4 changes: 2 additions & 2 deletions controllers/contact.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const recaptcha = require('../utils/recaptcha')
const sendEmail = require('../utils/sendEmail')

// render contact page
const getContact = async (req,res,next) =>{
if(!req.session.username || !req.session.email)
res.render('pages/contact',{contact:true})
else
res.render('pages/contact',{contact:true,logout:true})

}

// post message to webmaster
const postContact = async (req,res,next) =>{
console.log("Contact: new message received.");

Expand Down Expand Up @@ -39,5 +40,4 @@ const postContact = async (req,res,next) =>{
})
}


module.exports = {getContact,postContact}
1 change: 1 addition & 0 deletions controllers/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// redirecting to history page
const getDashboard = async (req,res,next) =>{
res.redirect('/dashboard/history')
}
Expand Down
7 changes: 3 additions & 4 deletions controllers/forgot_psw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const jwt = require('jsonwebtoken')
const sendEmail = require('../utils/sendEmail')
const User = require('../models/User')

// token expires in 30 minutes
const JWT_EXP = '30m'

// FIXME change the email address

// sends password reset link
const postEmail = async (req,res,next) => {
console.log("psw forgot request recieved");
console.log("Forgot Psw: request recieved");

if(req.body.email){
Auth.findOne({email:req.body.email},async (err,auth)=>{
Expand Down Expand Up @@ -47,7 +47,6 @@ const postEmail = async (req,res,next) => {
}else{
return res.sendStatus(500)
}

}

module.exports = {postEmail}
8 changes: 5 additions & 3 deletions controllers/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ const Auth = require('../models/Auth')
const User = require('../models/User')
const SearchResults = require('../models/SearchResults')

// return search records of given email
const getSearchedResults = async (email) => {
let user = await Auth.findOne({email:email})
let searched = await User.findOne({_id:user._id},'searched')
searched = searched.searched
const search_ids = []
const results = []
const projections = `_id date name user_img username
start_date end_date followings followers total_tweets total.count`;

await Promise.all(
searched.map( async (id) => {
let temp = await SearchResults.findById(id,projections)
search_ids.push(temp)
results.push(temp)
})
)

return search_ids
return results
}

// render history page
const getHistory = async (req,res,next) => {

let results = await getSearchedResults(req.session.email)
Expand Down
9 changes: 6 additions & 3 deletions controllers/login.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const Auth = require('../models/Auth')
const bcrypt = require('bcrypt')

const User = require('../models/User')
const SessionDuration = 1000 * 60 * 60 * 60

const bcrypt = require('bcrypt')
const md5 = require('md5')

// Remember me session duration
const SessionDuration = 1000 * 60 * 60 * 48 // ms s m h 48hours

// login process
const login = async (req,res,next) => {
const {email, password, remember} = req.body
console.log("Login: new request received for:",email);
Expand Down
5 changes: 2 additions & 3 deletions controllers/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const User = require("../models/User")
const SearchResults = require('../models/SearchResults')
const recaptcha = require('../utils/recaptcha')

// render profile page
const getProfile = async (req,res,next) => {

res.render('pages/profile',{
logout:true,
username:req.session.username,
Expand All @@ -16,6 +16,7 @@ const getProfile = async (req,res,next) => {
})
}

// update password
const updateProfile = async (req,res,next) => {
console.log("Profile: update psw request received.");

Expand Down Expand Up @@ -51,7 +52,6 @@ const deleteProfile = async (req,res,next) => {

if(req.session.email){
Auth.findOne({email:req.session.email},async function(err,auth){
console.log(req.body.password);
await bcrypt.compare(req.body.password,auth.password).then(async (check)=>{

if(!check) return res.json({error:"Credentials are not valid"})
Expand Down Expand Up @@ -80,7 +80,6 @@ const deleteProfile = async (req,res,next) => {
User.findByIdAndRemove(auth._id).exec((err,data)=>{
if(err) {
return res.json({error:"Profile delete: User error"})

}
console.log("pass: delete user");
})
Expand Down
6 changes: 3 additions & 3 deletions controllers/reset_psw.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const recaptcha = require('../utils/recaptcha')
const sendEmail = require('../utils/sendEmail')
const JWT_EXP = '30m'

// render reset password page
const getReset = async (req,res,next) => {
const {email,token} = req.params

Expand All @@ -27,15 +28,14 @@ const getReset = async (req,res,next) => {

}


// resetting password
const putReset = async (req,res,next) =>{
console.log("reset request recieved");
console.log("Reset Psw: request recieved");
const {email,password,password_confirm} = req.body

let catpcha = await recaptcha(req.body['g-recaptcha-response'])
if(!catpcha) return res.json({error:"Invalid captcha!"})


if(!email || !password || !password_confirm) return res.json({error:"Invalid request"})
if(password !== password_confirm) return res.json({error:"Passwords don't match"})

Expand Down
24 changes: 11 additions & 13 deletions controllers/results.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const mongoose = require('mongoose')

const Auth = require('../models/Auth')
const User = require('../models/User')
const SearchResults = require('../models/SearchResults')

// returns true if id belongs to this user
async function validateID(id,email){

if(!mongoose.isValidObjectId(id)) return false

let user_id = await Auth.findOne({email:email})
Expand All @@ -17,7 +18,7 @@ async function validateID(id,email){
return true
}


// sends search record data as json obj
const getResults = async (req,res,next) => {

let email = req.session.email
Expand All @@ -30,13 +31,11 @@ const getResults = async (req,res,next) => {
return res.json({error:"Invalid id"})

let result = await SearchResults.findById(req.query.id)

return res.json(result)
}else{
console.log("Results: new compare request received.");

if(!req.query.id.length) return res.json({error:"Must provide two ids"})
if(req.query.id.length != 2) return res.json({error:"Must provide two ids"})
if(!req.query.id.length || req.query.id.length != 2) return res.json({error:"Must provide two ids"})

if(!(await validateID(req.query.id[0],email)
&& await validateID(req.query.id[1],email)))
Expand All @@ -50,28 +49,27 @@ const getResults = async (req,res,next) => {
}
}


// removes/deletes the given search record from this user
const removeResult = async (req,res,next) => {
console.log("Results: delete request received.");
if(!req.body.id) return res.json({error:"Missing id"})
if(! await validateID(req.body.id,req.session.email)) return res.json({error:"Invalid id"})

// Validating ObjectID
if(! await validateID(req.body.id,req.session.email))
return res.json({error:"Invalid id"})
// Removing search record from DB
SearchResults.findByIdAndRemove(req.body.id).exec(function(err,item){

if(!item)res.json({error:"Search record not found"})
if(!item) return res.json({error:"Search record not found"})

Auth.findOne({email:req.session.email},async (err,auth)=>{

if(!auth) return res.json({error:"Invalid request"})
User.updateOne({_id:auth._id},{$pull : {searched:req.body.id}},(err,user)=>{
if(err) return res.json({error:"User search record not found"})
return res.sendStatus(200)
})

})

})

return res.sendStatus(200)
}

module.exports = {getResults,removeResult}
1 change: 1 addition & 0 deletions controllers/search.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// render search page
const getSearch = async (req,res,next) => {

res.render('pages/search',{
Expand Down
13 changes: 8 additions & 5 deletions controllers/signup.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const Auth = require('../models/Auth')
const User = require('../models/User')
const bcrypt = require('bcrypt')
const mongoose = require('mongoose')
const recaptcha = require('../utils/recaptcha')
const sendEmail = require('../utils/sendEmail')
const Auth = require('../models/Auth')
const User = require('../models/User')

// creating a new user (making a new db record)
const createUser = async (req,res,next) => {
console.log("Signup: request recieved");

// console.log(req.body);
if( !req.body.email ||
!req.body.password ||
!req.body.password_confirm ||
!req.body.name ||
!req.body.terms) return res.json({error:"Missing fields"})

Expand Down Expand Up @@ -49,8 +51,8 @@ const createUser = async (req,res,next) => {
html:`
<h4 class="h4">Welcome ${req.body.name}</h4>
<p>You've successfully created an account!</p>
<p>Email ${req.body.email}</p>
<p>Password ${req.body.password}</p>
<p>Email: ${req.body.email}</p>
<p>Password: ${req.body.password}</p>
`
}

Expand All @@ -64,6 +66,7 @@ const createUser = async (req,res,next) => {
})
}

// render signup page
const signupPage = async (req,res) => {
if(!req.session.username || !req.session.email)
res.render('pages/singup',{signup:true})
Expand Down
23 changes: 17 additions & 6 deletions controllers/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const User = require('../models/User')
const Auth = require('../models/Auth')
const recaptcha = require('../utils/recaptcha')

const DEBUG = false
const filename = './data.json'

// tweet fields
const tweet_fields = [
'attachments',
'author_id',
Expand Down Expand Up @@ -49,6 +51,7 @@ const tweet_user_fields = [
"withheld"
]

// Twitter API search function
const search = async (ID) => {
res = await app.search(`from:${ID}`,{
'tweet.fields':tweet_fields,
Expand All @@ -58,10 +61,12 @@ const search = async (ID) => {
let DATA = await res.fetchLast()

if(DEBUG){
return new Promise( (resolve,reject) => fs.writeFile(filename,JSON.stringify(DATA), (err)=>{
if(err) reject(err)
resolve(data_process(DATA))
}))
return new Promise( (resolve,reject) => {
// Writing received data to a file
fs.writeFile(filename,JSON.stringify(DATA), (err)=>{
resolve(data_process(DATA))
})
})
}else{
return new Promise( (resolve,reject) => {
resolve(data_process(DATA))
Expand All @@ -70,6 +75,7 @@ const search = async (ID) => {
}

// verify the search limit of an user
// return true if limit is exceeded
const searchLimit = async (auth)=>{

let user = await User.findById(auth._id)
Expand All @@ -81,16 +87,17 @@ const searchLimit = async (auth)=>{
return false
}

// handles the twitter search request and sends data as json obj
const postTwitter = async(req,res,next) => {

// recaptcha validation
let catpcha = await recaptcha(req.body['g-recaptcha-response'])
if(!catpcha) return res.json({error:"Invalid captcha!"})

//user search limit validation

let auth = await Auth.findOne({email:req.session.email})
if(!auth) return res.json({error:"Twitter: Invalid session"})

//user search limit validation
let limit = await searchLimit(auth)
console.log("Twitter: limit",limit);

Expand All @@ -101,6 +108,7 @@ const postTwitter = async(req,res,next) => {
const user = await app.userByUsername(req.body.handler,{"user.fields":tweet_user_fields});
if(user?.errors) return res.json({error:`Invalid user`})


if(DEBUG)
console.log("Twitter: request for ->",user.data)

Expand All @@ -110,6 +118,7 @@ const postTwitter = async(req,res,next) => {
if(DEBUG)
fs.writeFileSync("./output_metions.json",JSON.stringify(mentions))

// executing search for given twitter account
search(req.body.handler)
.then( async (data,err) => {
if(err) return res.json({error:"Twitter search: "+err.message})
Expand All @@ -129,10 +138,12 @@ const postTwitter = async(req,res,next) => {
// Limit display
console.log("Twitter Limit:",data.limit.limit,data.limit.remaining,data.limit.reset)

// updating db
console.log('Twitter: creating search results')
await SearchResults.create(data)
await User.findOneAndUpdate({_id:auth._id},{$push : {searched:data._id}})

// deleting object id from data
delete(data._id)

if(DEBUG){
Expand Down
10 changes: 2 additions & 8 deletions db/connect.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
const mongoose = require('mongoose')
mongoose.set('strictQuery', true)

// Connecting to MongoDB
const connectDB = (db_connection_url) => {
return mongoose.connect(db_connection_url).then(()=>console.log('OK: successfully connected to db')).catch((e)=>console.log(e) )
}


module.exports = connectDB

// useNewUrlParser :true,
// useCreateIndex: true,
// useFindAndModify:false,
// useUnifiedTopology: true

module.exports = connectDB
Loading

0 comments on commit 41d8ada

Please sign in to comment.