Skip to content

Commit

Permalink
Merge branch 'simplifying-chat-schema' to roll out new schema changes
Browse files Browse the repository at this point in the history
  • Loading branch information
KaranErry committed Apr 16, 2019
2 parents 0899838 + e31f6ad commit 71c6e98
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
12 changes: 6 additions & 6 deletions models/chat_model.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
let mongoose = require('mongoose');
var Schema = mongoose.Schema;
var itemData = require('../models/item_model');

var chatDataSchema= new Schema({
// Initial info
users:[{email:String}],
item: { type: Schema.Types.ObjectId, ref: 'itemData'},
interestedBuyer: String,
seller: String,
messages:[{message:String, byWho: String, timestamp:Number}],
allParticipants:[{email:String}],
dateChatCreated: Number,
dateChatCreatedComputed: String,
dateChatStartedBy: String,
selectedImageUrl: String,
selectedPostTitle: String,
selectedPersonSelling: String
dateChatCreatedComputed: String
},{collection:'chats'});


Expand Down
21 changes: 10 additions & 11 deletions routes/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,29 @@ router.use(bodyParser.urlencoded({ extended: false }))
/* GET home page. */
router.get('/', checkAuthentication, function(req, res, next) {
preselectedThread = req.query.preselectedThread ? req.query.preselectedThread : '';
chatData.find({users:
chatData.find({allParticipants:
{$elemMatch :
{
email: req.session.passport.user._json.email
}
}
}
).sort({ dateChatCreated: -1 }).then(function(doc) {
res.render('chat', { title: 'DrewUse', currentSession: req.session, chats:doc, preselectedThread:preselectedThread});
).populate('item').sort({ dateChatCreated: -1 }).then(function(doc) {
res.render('chat', { title: 'DrewUse', currentUser: req.session.passport.user._json.email, chats:doc, preselectedThread:preselectedThread});
});
});

router.post('/newMessage', checkAuthentication, function(req,res){
var current_timestamp = Long.fromNumber(current_millies);
var date = new Date(current_millies);
var dateReadable = date.toString();
var chat ={
users:[{email: req.session.passport.user._json.email},{email: req.body.selectedPersonSelling} ],
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,
dateChatStartedBy: req.session.passport.user._json.email,
selectedImageUrl: req.body.selectedImageUrl,
selectedPostTitle: req.body.selectedPostTitle,
selectedPersonSelling: req.body.selectedPersonSelling
dateChatCreatedComputed: dateReadable
}
var data = new chatData(chat);
data.save((err, doc) => {
Expand All @@ -59,7 +58,7 @@ router.post('/messages/getInfo', checkAuthentication, (req, res) => {
console.log(req.body.threadId);
chatData.find({_id: req.body.threadId})
.then(function(doc) {
console.log(doc);
// console.log(doc);
res.io.emit('allMessages', doc);
});

Expand Down
8 changes: 4 additions & 4 deletions views/chat.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ img{ max-width:100%;}
<div class="chat_list">
{{# each chats }}
<div class="chat_people" threadId="{{this._id}}">
<span class="chat_img"> <img src="{{this.selectedImageUrl}}" alt="sunil"> </span>
<span class="chat_img"> <img src="{{this.item.image_url}}" alt="Image for product '{{this.item.title}}'"> </span>
<span class="chat_ib">
<input id = "threadId" type="hidden" class="form-control disabled" value="{{this._id}}">
<strong><h5>{{this.selectedPostTitle}}</h5></strong>
{{#ifEquals this.selectedPersonSelling ../currentSession.passport.user._json.email}}
<strong><h5>{{this.item.title}}</h5></strong>
{{#ifEquals this.seller ../currentUser}}
<span class="badge badge-success">Interested Buyer</span>
{{/ifEquals}}
{{#ifEquals this.dateChatStartedBy ../currentSession.passport.user._json.email}}
{{#ifEquals this.interestedBuyer ../currentUser}}
<span class="badge badge-info">Product Seller</span>
{{/ifEquals}}
<p class="chat_date">Last chatted: </p>
Expand Down
5 changes: 2 additions & 3 deletions views/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@

{{else}}
<form action="/chat/newMessage" method="POST">
<input class="card-text" name="selectedImageUrl" value="{{this.image_url}}" style="display:none;"></input>
<input class="card-text" name="selectedPostTitle" value="{{this.title}}" style="display:none;"></input>
<input class="card-text" name="selectedPersonSelling" value="{{this.postedBy}}" style="display:none;"></input>
<input class="card-text" name="itemId" value="{{this._id}}" style="display:none;"></input>
<input class="card-text" name="seller" value="{{this.postedBy}}" style="display:none;"></input>
<center><button type="submit" class="btn btn-success">I'm Interested</button></center>
</form>
{{/if}}
Expand Down

0 comments on commit 71c6e98

Please sign in to comment.