-
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
Showing
13 changed files
with
385 additions
and
4 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,10 @@ | ||
'use strict'; | ||
|
||
var Message = require('../models/message'); | ||
|
||
exports.index = function(req, res){ | ||
Message.find(res.locals.user._id, req.query, function(err, messages){ | ||
res.render('messages/index', {messages:messages, query:req.query}); | ||
}); | ||
}; | ||
|
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,55 @@ | ||
'use strict'; | ||
|
||
var User = require('./user'), | ||
async = require('async'); | ||
|
||
function Message(fromId, toId, body){ | ||
this.fromId = fromId; | ||
this.toId = toId; | ||
this.body = body; | ||
this.sent = new Date(); | ||
this.isRead = false; | ||
} | ||
|
||
Object.defineProperty(Message, 'collection', { | ||
get: function(){return global.mongodb.collection('messages');} | ||
}); | ||
|
||
Message.find = function(toId, query, cb){ | ||
var filter = {toId:toId}, | ||
sort = {}; | ||
sort.sent = (query.sort) ? query.sort * 1 : 1; | ||
Message.collection.find(filter).sort(sort).toArray(function(err, objs){ | ||
// console.log('***objs', objs); | ||
async.map(objs, getSenderInfo, function(err2, fullMessages){ | ||
// console.log(fullMessages); | ||
cb(null, fullMessages); | ||
}); | ||
}); | ||
}; | ||
|
||
Message.findOne = function(query, cb){ | ||
Message.collection.findOne(query, cb); | ||
}; | ||
|
||
Message.prototype.save = function(cb){ | ||
Message.collection.save(this, cb); | ||
}; | ||
|
||
Message.countUnreadForUser = function(id, cb){ | ||
Message.collection.count({toId:id, isRead:false}, cb); | ||
}; | ||
|
||
module.exports = Message; | ||
|
||
// helper functions | ||
function getSenderInfo(message, done){ | ||
User.findById(message.fromId, function(err, user){ | ||
console.log(user); | ||
message.fromName = user.name; | ||
message.fromEmail = user.email; | ||
// console.log(message); | ||
done(null, message); | ||
}); | ||
} | ||
|
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
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
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,22 @@ | ||
extends ../shared/template | ||
block content | ||
h2 Messages | ||
.panel.panel-default | ||
.panel-body | ||
.row | ||
.col-xs-12 | ||
table.table | ||
thead | ||
tr | ||
th From | ||
th Date | ||
th Message | ||
tbody | ||
each message in messages | ||
tr | ||
td: a(href='/users/#{message.fromEmail}')= message.fromName | ||
td= message.sent | ||
td= message.body | ||
|
||
block scripts | ||
|
Empty file.
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
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,25 @@ | ||
{ | ||
"_id":{"$oid":"a00000000000000000000001"}, | ||
"sent":{"$date":477295200000}, | ||
"body":"Hi Bob, long time no see.\n You want to stop and get a drink sometime after work?", | ||
"toId":{"$oid":"000000000000000000000001"}, | ||
"fromId" : {"$oid":"000000000000000000000002"}, | ||
"isRead":true | ||
} | ||
{ | ||
"_id":{"$oid":"a00000000000000000000002"}, | ||
"sent":{"$date":477295200000}, | ||
"body":"Hi Bob, following up on drinks after work. Give me a call any time, or message me with this sweet app.", | ||
"toId":{"$oid":"000000000000000000000001"}, | ||
"fromId" : {"$oid":"000000000000000000000002"}, | ||
"isRead":false | ||
} | ||
{ | ||
"_id":{"$oid":"a00000000000000000000003"}, | ||
"sent":{"$date":477295200000}, | ||
"body":"Hi Sue, greate work on that project.\n If you don't get that promotion they are plain crazy.", | ||
"toId":{"$oid":"000000000000000000000002"}, | ||
"fromId" : {"$oid":"000000000000000000000003"}, | ||
"isRead":true | ||
} | ||
|
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,146 @@ | ||
/* global describe, before, beforeEach, it */ | ||
|
||
'use strict'; | ||
|
||
process.env.DB = 'facebook-test'; | ||
|
||
var expect = require('chai').expect, | ||
cp = require('child_process'), | ||
app = require('../../app/index'), | ||
cookie = null, | ||
request = require('supertest'); | ||
|
||
describe('users', function(){ | ||
before(function(done){ | ||
request(app).get('/').end(done); | ||
}); | ||
|
||
beforeEach(function(done){ | ||
cp.execFile(__dirname + '/../scripts/clean-db.sh', [process.env.DB], {cwd:__dirname + '/../scripts'}, function(err, stdout, stderr){ | ||
request(app) | ||
.post('/login') | ||
.send('email=nodeapptest%2Bbob%40gmail.com') | ||
.send('password=1234') | ||
.end(function(err, res){ | ||
cookie = res.headers['set-cookie'][0]; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('get /profile/edit', function(){ | ||
it('should show the edit profile page', function(done){ | ||
request(app) | ||
.get('/profile/edit') | ||
.set('cookie', cookie) | ||
.end(function(err, res){ | ||
expect(res.status).to.equal(200); | ||
expect(res.text).to.include('nodeapptest+bob@gmail.com'); | ||
expect(res.text).to.include('Email'); | ||
expect(res.text).to.include('Phone'); | ||
expect(res.text).to.include('Visible'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('put /profile', function(){ | ||
it('should edit the profile in database', function(done){ | ||
request(app) | ||
.post('/profile') | ||
.send('_method=put&visible=public&email=a%40b.com&phone=555-555-5555&photo=someUrl&tagline=some+tagline&facebook=facebookUrl&twitter=%40twitterhandle') | ||
.set('cookie', cookie) | ||
.end(function(err, res){ | ||
expect(res.status).to.equal(302); | ||
expect(res.headers.location).to.equal('/profile'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('get /profile', function(){ | ||
it('should show the logged in users profile page', function(done){ | ||
request(app) | ||
.post('/profile') | ||
.send('_method=put&visible=public&email=a%40b.com&phone=555-555-5555&photo=someUrl&tagline=some+tagline&facebook=facebookUrl&twitter=%40twitterhandle') | ||
.set('cookie', cookie) | ||
.end(function(err, res){ | ||
request(app) | ||
.get('/profile') | ||
.set('cookie', cookie) | ||
.end(function(err, res){ | ||
expect(res.status).to.equal(200); | ||
expect(res.text).to.include('Phone'); | ||
expect(res.text).to.include('Twitter'); | ||
expect(res.text).to.include('Facebook'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('get /users', function(){ | ||
it('should show links to public user profiles', function(done){ | ||
request(app) | ||
.get('/users') | ||
.set('cookie', cookie) | ||
.end(function(err, res){ | ||
expect(res.status).to.equal(200); | ||
expect(res.text).to.not.include('Bob'); | ||
expect(res.text).to.include('John'); | ||
expect(res.text).to.not.include('Sue'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('get /users/email', function(){ | ||
it('should return the profile page for a public profile', function(done){ | ||
request(app) | ||
.get('/users/nodeapptest+john@gmail.com') | ||
.set('cookie', cookie) | ||
.end(function(err, res){ | ||
expect(res.status).to.equal(200); | ||
expect(res.text).to.include('John'); | ||
done(); | ||
}); | ||
}); | ||
it('should redirect away from private profile', function(done){ | ||
request(app) | ||
.get('/users/nodeapptest+sue@gmail.com') | ||
.set('cookie', cookie) | ||
.end(function(err, res){ | ||
expect(res.status).to.equal(302); | ||
expect(res.headers.location).to.equal('/users'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('post /message/userId', function(){ | ||
it('should send a text message to recipient', function(done){ | ||
request(app) | ||
.post('/message/000000000000000000000003') | ||
.send('mtype=text&message=hey') | ||
.set('cookie', cookie) | ||
.end(function(err, res){ | ||
expect(res.status).to.equal(302); | ||
expect(res.headers.location).to.equal('/users/nodeapptest+john@gmail.com'); | ||
done(); | ||
}); | ||
}); | ||
it('should send an email message to recipient', function(done){ | ||
request(app) | ||
.post('/message/000000000000000000000003') | ||
.send('mtype=email&message=hey') | ||
.set('cookie', cookie) | ||
.end(function(err, res){ | ||
expect(res.status).to.equal(302); | ||
expect(res.headers.location).to.equal('/users/nodeapptest+john@gmail.com'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
}); | ||
|
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
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
Oops, something went wrong.