Skip to content

Commit

Permalink
messaging ready for dave
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicafraines committed Sep 7, 2014
2 parents d48f57f + 62d6ef5 commit 38b38ca
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 12 deletions.
28 changes: 28 additions & 0 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,31 @@ exports.browse = function(req, res){
res.render('users/browse', {users:users});
});
};

exports.lickIndex = function(req, res){
User.displayLicks(req.user._id, function(licks){
User.displayProposals(req.user._id, function(proposals, users){
res.render('users/licks', {licks: licks, proposals: proposals, users: users});
});
});
};

exports.propose = function(req, res){
User.propose(req.params.lickeeId, req.user._id, function(){
res.redirect('/user/licks');
});
};

exports.acceptProposal = function(req, res){
req.user.acceptProposal(req.params.fromId, req.body.proposalId, function(){
res.redirect('/user/licks');
});
};

exports.declineProposal = function(req, res){
req.user.declineProposal(req.params.fromId, req.body.proposalId, function(){
res.redirect('/user/licks');
});
};


30 changes: 30 additions & 0 deletions app/models/proposal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';


var Mongo = require('mongodb'),
_ = require('underscore-contrib');

function Proposal(){
}

Object.defineProperty(Proposal, 'collection', {
get: function(){return global.mongodb.collection('proposals');}
});

Proposal.findById = function(id, cb){
var _id = Mongo.ObjectID(id);
Proposal.collection.findOne({_id:_id}, function(err, obj){
var user = Object.create(Proposal.prototype);
user = _.extend(user, obj);
cb(err, user);
});
};

Proposal.find = function(userId, cb){
var _id = Mongo.ObjectID(userId);
Proposal.collection.find({receiverId: _id}).toArray(cb);
};


module.exports = Proposal;

80 changes: 77 additions & 3 deletions app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var bcrypt = require('bcrypt'),
Mailgun = require('mailgun-js'),
fs = require('fs'),
path = require('path'),
async = require('async');
async = require('async'),
Proposal = require('./proposal');

function User(){
}
Expand Down Expand Up @@ -173,8 +174,70 @@ User.prototype.send = function(receiver, obj, cb){
}
};

User.displayLicks = function(userId, cb){
User.findById(userId, function(err, user){
if(!user.licks) { return cb([]); }

async.map(user.licks, function(lick, cb){
User.findById(lick, function(err, u){
cb(null, u);
});
}, function(err, licks){
cb(licks);
});
});

};


User.displayProposals = function(userId, cb){
Proposal.find(userId, function(err, proposals){
if(!proposals.length) { return cb([]); }

async.map(proposals, function(from, cb){
User.findById(from.fromId, function(err, u){
cb(null, u);
});
}, function(err, users){
cb(proposals, users);
});
});

};

//NEEDS TESTING
User.propose = function(to, from, cb){
var p = new Proposal();
p.receiverId = to;
p.fromId = from;

Proposal.collection.save(p, cb);
};

module.exports = User;

//NEEDS TESTING
User.prototype.acceptProposal = function(fromId, proposalId, cb){
var self = this;
User.findById(fromId, function(err, user){
var body = (user.username || user.email) + ', ' + (self.username || 'a Furry Farm user') + ' has accepted your date proposal. Way to go!';
txtMsg(user.phone, body, function(err, response){
Proposal.collection.remove({_id: Mongo.ObjectID(proposalId)}, cb);
});
});
};

//NEEDS TESTING
User.prototype.declineProposal = function(fromId, proposalId, cb){
var self = this;
User.findById(fromId, function(err, user){
var body = (user.username || user.email) + ', ' + (self.username || 'a Furry Farm user') + ' has declined your date proposal. There is plenty of fish though. Don\'t give up!';
txtMsg(user.phone, body, function(err, response){
Proposal.collection.remove({_id: Mongo.ObjectID(proposalId)}, cb);
});
});
};

//Private Functions
function userIterator(userId, cb){
var userList;
Expand All @@ -198,8 +261,19 @@ function sendText(to, body, cb){
function sendEmail(from, to, subject, message, cb){
var mailgun = new Mailgun({apiKey:process.env.MGKEY, domain:process.env.MGDOM}),
data = {from:'admin@furryfarm.com', to:'jessicafraines@gmail.com', subject:subject, text:message};
console.log(process.env.MGKEY);
console.log(mailgun);
console.log(process.env.MGKEY);
console.log(mailgun);

mailgun.messages().send(data, cb);
}

function txtMsg(to, body, cb){
if(!to){return cb();}

var accountSid = process.env.TWSID,
authToken = process.env.TWTOK,
from = process.env.FROM,
client = require('twilio')(accountSid, authToken);

client.messages.create({to:to, from:from, body:body}, cb);
}
12 changes: 9 additions & 3 deletions app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module.exports = function(app, express){
app.get('/auth/facebook/callback', passport.authenticate('facebook', {successRedirect:'/', failureRedirect:'/login', successFlash:'Facebook got you in!', failureFlash:'Sorry, your Facebook login did not work'}));
app.get('/messages', users.messages);
app.get('/messages/:msgId', users.message);
app.get('/auth/facebook/callback', passport.authenticate('facebook', {successRedirect:'/', failureRedirect:'/login', successFlash:'Facebook got you in!', failureFlash:'Sorry, your Facebook login did not work'}));


//security
app.use(security.bounce);
Expand All @@ -52,14 +54,18 @@ module.exports = function(app, express){
app.put('/users/edit', users.update);
app.post('/users/edit/photo', users.uploadPhoto);
app.get('/browse', users.browse);

app.get('/messages', users.messages);
app.get('/messages/:msgId', users.message);
app.get('/farm/users/:userId', users.displayProfile);
app.post('/user/:toId/wag', users.wag);
app.post('/user/:lickee/lick', users.lick);
app.get('/messages', users.messages);
app.get('/messages/:msgId', users.message);
app.get('/messages/:toId/send', users.contact);
app.post('/messages/:toId/send', users.send);
app.get('/user/licks', users.lickIndex);
app.post('/user/:lickeeId/propose', users.propose);
app.delete('/proposal/:fromId/accept', users.acceptProposal);
app.delete('/proposal/:fromId/decline', users.declineProposal);


console.log('Express: Routes Loaded');
};
Expand Down
2 changes: 2 additions & 0 deletions app/static/js/user/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

$(document).ready(function(){

$('#background').tubular({videoId: 'Z2wThqRVnso'});

var pos = getPosition(),
positions = getPositions();

Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/nav.jade
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
li.unread= user.unreadMessages > 0 ? user.unreadMessages : null
li: a(href='/browse') The Farm
li: a(href='/users/edit') Edit Profile
li: a(href='/licks') Favorites
li: a(href='/user/licks') Your Licks
li
form.navbar-form(method='post', action='/logout')
.form-group
Expand Down
10 changes: 8 additions & 2 deletions app/views/users/edit.jade
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ block content
.form-group
label(for='tagline') Tagline:
input.form-control#tagline(type='text', name='tagline', value=(user.tagline || 'tag line?'))
.form-group
label(for='tagline') Phone:
input.form-control#phone(type='phone', name='phone', value=(user.phone || ''))
.col-xs-1
.col-xs-4
.row
Expand All @@ -69,8 +72,11 @@ block content
#editPrimary(style='background-image: url(#{user.primaryPhoto})')
.header2 Other Photos
.photoDiv
each photo in user.photos
.profilePhotos.thumbnail(style='background-image:url(#{photo});')
if user.photos
each photo in user.photos
.profilePhotos.thumbnail(style='background-image:url(#{photo});')
else
p You haven't added any photos...
.col-xs-2
.row
.col-xs-5
Expand Down
52 changes: 52 additions & 0 deletions app/views/users/licks.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
extends ../shared/template
block content
h2 People You've Licked
.row
.col-xs-3
.col-xs-6
if licks
table.table
tr
th Display Name
th Photo
th Date Request

each lickee in licks
tr
td= lickee.username
td: img(src=lickee.primaryPhoto || '/img/no.jpeg' style='width: 25%')
td
form(method='post' action='/user/#{lickee._id}/propose')
button.btn.btn-danger(type='submit') Propose a date!
else
.row You haven't licked anyone...


if proposals
table.table
tr
th Display Name
th Photo
th Accept
th Decline
each proposal, index in proposals
tr
td= users[index].username
td: img(src=users[index].primaryPhoto || '/img/no.jpeg' style='width: 25%')
td
form(method='post' action='/proposal/#{proposal.fromId}/accept')
input(type='hidden', name='proposalId' value=proposal._id)
input(type='hidden', name='_method', value='delete')

button.btn.btn-danger(type='submit') Accept
td
form(method='post' action='/proposal/#{proposal.fromId}/decline')
input(type='hidden', name='proposalId' value=proposal._id)
input(type='hidden', name='_method', value='delete')
button.btn.btn-danger(type='submit') Decline
else
.row Nobody has sent you a date proposal...
.col-xs-3


block scripts
17 changes: 17 additions & 0 deletions db/proposals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"_id" : {"$oid":"000000000000000000000011"},
"receiverId" : {"$oid":"000000000000000000000001"},
"fromId" : {"$oid":"000000000000000000000003"}
}

{
"_id" : {"$oid":"000000000000000000000022"},
"receiverId" : {"$oid":"000000000000000000000001"},
"fromId" : {"$oid":"000000000000000000000002"}
}

{
"_id" : {"$oid":"000000000000000000000033"},
"receiverId" : {"$oid":"000000000000000000000003"},
"fromId" : {"$oid":"000000000000000000000001"}
}
2 changes: 1 addition & 1 deletion db/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"primaryPhoto": "http://fc01.deviantart.net/fs8/i/2005/306/2/7/Harvey_Birdman_by_Walmsley.jpg",
"description": "I'm a bird first and a man second. BIIIIIIIIIIRRRD MAAANNNNNN!",
"wags": ["000000000000000000000002", "000000000000000000000003"],
"licks": []
"licks": ["000000000000000000000003"]
}
{
"_id" : {"$oid":"000000000000000000000002"},
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"request": "^2.40.0",
"twilio": "^1.7.0",
"tablesort": "^1.7.0",
"underscore-contrib": "^0.3.0"
"underscore-contrib": "^0.3.0",
"twilio": "~1.7.0"
},
"devDependencies": {
"blanket": "^1.1.6",
Expand Down
17 changes: 17 additions & 0 deletions test/acceptance/npm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/node/bin/node', '/usr/local/node/bin/npm', 'test' ]
2 info using npm@1.4.14
3 info using node@v0.10.29
4 error Error: ENOENT, open '/home/nss/apps/code/furry-farm/test/acceptance/package.json'
5 error If you need help, you may report this *entire* log,
5 error including the npm and node versions, at:
5 error <http://github.com/npm/npm/issues>
6 error System Linux 3.13.0-24-generic
7 error command "/usr/local/node/bin/node" "/usr/local/node/bin/npm" "test"
8 error cwd /home/nss/apps/code/furry-farm/test/acceptance
9 error node -v v0.10.29
10 error npm -v 1.4.14
11 error path /home/nss/apps/code/furry-farm/test/acceptance/package.json
12 error code ENOENT
13 error errno 34
14 verbose exit [ 34, true ]
24 changes: 24 additions & 0 deletions test/acceptance/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ describe('users', function(){
.send('mtype=text&message=hello')
.end(function(err, res){
expect(res.status).to.equal(302);
});
});
});

describe('get /user/licks', function(){
it('should take the user to the messages page', function(done){
request(app)
.get('/user/licks')
.set('cookie', cookie)
.end(function(err, res){
expect(res.status).to.equal(200);
done();
});
});
Expand All @@ -239,6 +250,19 @@ describe('users', function(){
});
});


describe('post /user/:lickeeId/propose', function(){
it('should should add favorite to someones list', function(done){
request(app)
.post('/user/000000000000000000000003/propose')
.set('cookie', cookie)
.end(function(err, res){
expect(res.status).to.equal(302);
done();
});
});
});

describe('get /messages/000000000000000000000001/send', function(){
it('should send an email message to a user', function(done){
request(app)
Expand Down
1 change: 1 addition & 0 deletions test/scripts/clean-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ fi

mongoimport --jsonArray --drop --db $1 --collection messages --file ../../db/messages.json
mongoimport --jsonArray --drop --db $1 --collection users --file ../../db/users.json
mongoimport --jsonArray --drop --db $1 --collection proposals --file ../../db/proposals.json

0 comments on commit 38b38ca

Please sign in to comment.