diff --git a/.DS_Store b/.DS_Store index 762b4ed3..aae1b97b 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/app.js b/app.js index ae70a632..f66af294 100644 --- a/app.js +++ b/app.js @@ -39,6 +39,9 @@ var handlebars = hbs.create({ ifEquals: function(arg1, arg2, options) { return (arg1 == arg2) ? options.fn(this) : options.inverse(this); }, + ifNotEquals: function(arg1, arg2, options) { + return (arg1 != arg2) ? options.fn(this) : options.inverse(this); + } }, extname: 'hbs', defaultLayout:'layout.hbs', diff --git a/routes/chat.js b/routes/chat.js index 0767b659..441ae0e5 100644 --- a/routes/chat.js +++ b/routes/chat.js @@ -21,7 +21,10 @@ router.use(bodyParser.urlencoded({ extended: false })) /* GET home page. */ -router.get('/', checkAuthentication, setupSocketListeners, function(req, res, next) { +router.get('/', (req, res, next) => { + req.session.authorigin = 'chat?' + querystring.stringify(req.query); + next(); + }, checkAuthentication, setupSocketListeners, function(req, res, next) { // Setup chat data and render chat page preselectedThread = req.query.preselectedThread ? req.query.preselectedThread : ''; chatData.find({allParticipants: @@ -95,40 +98,53 @@ function setupSocketListeners(req, res, next) { next(); } -router.post('/openThread', checkAuthentication, function(req,res){ - chatData.findOne({item: req.body.itemId, interestedBuyer:req.session.passport.user._json.email}).then(doc => { - if (!doc) { - // Start a new thread - var current_millies = new Date().getTime(); - var current_timestamp = Long.fromNumber(current_millies); - var date = new Date(current_millies); - var dateReadable = date.toString(); - var chat = { - item: mongoose.Types.ObjectId(req.body.itemId), - seller: req.body.seller, - interestedBuyer: req.session.passport.user._json.email, - allParticipants:[ {email: req.session.passport.user._json.email}, {email: req.body.seller} ], - dateChatCreated: current_timestamp, - dateChatCreatedComputed: dateReadable +router.get('/openThread', (req, res, next) => { + req.session.authorigin = 'chat/openThread?' + querystring.stringify(req.query); + next(); + }, checkAuthentication, function(req,res) { + var {itemId, seller} = req.query; + var currentUser = req.session.passport.user._json.email; + // Ensure the seller is not opening a thread on their own listing + itemData.findOne({_id: itemId, postedBy:currentUser}).then(doc => { + if (!doc) { + // The seller ≠ the buyer + // Create a new thread or open the existing one + chatData.findOne({item: itemId, interestedBuyer:currentUser}).then(doc => { + if (!doc) { + // Start a new thread + var current_millies = new Date().getTime(); + var current_timestamp = Long.fromNumber(current_millies); + var date = new Date(current_millies); + var dateReadable = date.toString(); + var chat = { + item: mongoose.Types.ObjectId(itemId), + seller: seller, + interestedBuyer: req.session.passport.user._json.email, + allParticipants:[ {email: currentUser}, {email: seller} ], + dateChatCreated: current_timestamp, + dateChatCreatedComputed: dateReadable + } + var data = new chatData(chat); + data.save((err, doc) => { + res.redirect('/chat?' + querystring.stringify({'preselectedThread':doc.id})); + }); + } else { + // Open the existing thread + res.redirect('/chat?' + querystring.stringify({'preselectedThread':doc.id})); + } + }); + } else { + // The seller = the buyer + res.redirect('/?' + querystring.stringify({'alert':"You own this listing! You cannot buy your own listing!"})); } - var data = new chatData(chat); - data.save((err, doc) => { - res.redirect('/chat?' + querystring.stringify({'preselectedThread':doc.id})); - }); - } else { - // Open the existing thread - res.redirect('/chat?' + querystring.stringify({'preselectedThread':doc.id})); - } - }) + }); }); //authenticate a user is logged in function checkAuthentication(req,res,next){ if(req.isAuthenticated()){ - //req.isAuthenticated() will return true if user is logged in next(); } else{ - req.session.authorigin = 'chat'; res.redirect('/auth/google/callback') } } diff --git a/routes/index.js b/routes/index.js index 842c65a7..1c7b2190 100644 --- a/routes/index.js +++ b/routes/index.js @@ -29,7 +29,7 @@ router.get('/' ,function(req, res, next) { } results.find({sold:false}).sort( { datePosted: -1 } ) .then(function(doc) { - res.render('index', { title: 'DrewUse', items:doc, currentSession: req.session, filters:filters}); + res.render('index', { title: 'DrewUse', items:doc, currentSession: req.session, filters:filters, queryParams:req.query}); }); }); diff --git a/views/chat.hbs b/views/chat.hbs index 4ff2cf5f..2a567671 100644 --- a/views/chat.hbs +++ b/views/chat.hbs @@ -216,7 +216,7 @@ img{ max-width:100%;}
- +
@@ -225,16 +225,15 @@ img{ max-width:100%;} \ No newline at end of file diff --git a/views/index.hbs b/views/index.hbs index 1a73a87e..28d1e969 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -115,9 +115,9 @@ -{{#if currentSession.passport}} -
- {{# each items }} +
+ {{# each items }} + {{#ifNotEquals this.postedBy ../currentSession.passport.user._json.email}}
Card image cap @@ -127,45 +127,23 @@

{{ this.description }}

A {{ this.booktype }} in {{ this.condition }} condition.


- {{#if listingIsSameAsUser}} - - {{else}} -
+
- {{/if}}
- {{/each}} -
-{{else}} -
- {{# each items }} -
-
- Card image cap -
-
{{ this.title }}
-

Price: ${{ this.price }}

-

{{ this.description }}

-

A {{ this.booktype }} in {{ this.condition }} condition.

-
-
-
-
-
- -
-
- {{/each}} -
-{{/if}} -
+ {{/ifNotEquals}} + {{/each}} + + +{{#if queryParams.alert}} + +{{/if}} \ No newline at end of file