Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polished chat #38

Merged
merged 3 commits into from
May 3, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
seller can no longer open chat for his own listing; special case
KaranErry committed May 2, 2019

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit cea35ce82c869f3c48c1426699e529b4ca9182a1
53 changes: 32 additions & 21 deletions routes/chat.js
Original file line number Diff line number Diff line change
@@ -101,32 +101,43 @@ function setupSocketListeners(req, res, next) {
router.get('/openThread', (req, res, next) => {
req.session.authorigin = 'chat/openThread?' + querystring.stringify(req.query);
next();
}, checkAuthentication, function(req,res){
}, checkAuthentication, function(req,res) {
var {itemId, seller} = req.query;
chatData.findOne({item: itemId, interestedBuyer:req.session.passport.user._json.email}).then(doc => {
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) {
// 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: req.session.passport.user._json.email}, {email: seller} ],
dateChatCreated: current_timestamp,
dateChatCreatedComputed: dateReadable
}
var data = new chatData(chat);
data.save((err, doc) => {
res.redirect('/chat?' + querystring.stringify({'preselectedThread':doc.id}));
// 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 {
// Open the existing thread
res.redirect('/chat?' + querystring.stringify({'preselectedThread':doc.id}));
// The seller = the buyer
res.redirect('/?' + querystring.stringify({'alert':"You own this listing! You cannot buy your own listing!"}));
}
})
});
});

//authenticate a user is logged in
2 changes: 1 addition & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
@@ -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});
});
});

7 changes: 6 additions & 1 deletion views/index.hbs
Original file line number Diff line number Diff line change
@@ -141,4 +141,9 @@
{{/ifNotEquals}}
{{/each}}
</div>
<br>

{{#if queryParams.alert}}
<script>
alert('{{queryParams.alert}}');
</script>
{{/if}}