Skip to content

Commit ac97130

Browse files
committed
Merge pull request #31 from MusementCollective/dev
New Version v0.1.2 . Added Hearts(likes)
2 parents a5435d4 + 4925141 commit ac97130

File tree

14 files changed

+7402
-58
lines changed

14 files changed

+7402
-58
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Musement [![Build Status](https://travis-ci.com/lmrarturo/Musement.svg?token=8VCEpdV8M5U1zxzCUDq1&branch=dev)](https://travis-ci.com/lmrarturo/Musement)
1+
# Musement
22

33
<img src=http://i.imgur.com/pG2FYHp.jpg">
44
###### Musement is a community from people who want to share their ideas and projects. Creating the moment.

config/createmoment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var insertMoment = function(req) {
99
newMoment.description = req.body.description;
1010
newMoment.timelapse = req.body.totalTime;
1111
newMoment.user = req.user.id;
12+
newMoment.heart = 0;
1213

1314
if(req.file != null)
1415
newMoment.attachement.push(req.file.path);

config/heart.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var Moment = require("models/moment.js");
2+
var ObjectId = require('mongodb').ObjectID;
3+
4+
var heartMoment = function(req){
5+
var condition = {"_id":req.params.id}, update = {$inc: {heart: 1}}, options = {multi: true};
6+
Moment.update(condition, update, options, callback);
7+
8+
function callback(err, rowsAffected){
9+
Moment.update({"_id":ObjectId(req.params.id)}, {$push: {usersHeart: req.user.id}}, function(err){
10+
if(err)
11+
console.log(err);
12+
});
13+
};
14+
}
15+
16+
module.exports = {heartMoment:heartMoment}

config/sockets.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
const socketio = require('socket.io');
22

33
module.exports.listen = function(server){
4-
io = socketio.listen(server)
5-
console.log('Connected ${socket.id}');
4+
io = socketio.listen(server);
65

76
io.on('connection', function (socket){
7+
console.log(`Connected ${socket.id}`);
88
socket.on('chat message', function(msg, imageUser, userName){
99
io.emit('chat message', msg, imageUser, userName);
1010
});
1111

12+
socket.on('ping', function(){
13+
socket.emit("pong");
14+
});
15+
1216
});
1317

18+
19+
20+
1421
return io;
1522
}

controllers/home.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ var upload = multer({
66
dest: './uploads/user/u0000/u0'
77
});
88

9-
const newMoment = require('config/createmoment.js')
9+
const newMoment = require('config/createmoment.js');
10+
const heart = require('config/heart.js');
1011

1112
//================================== MIDDLEWARES ===============================
1213
var Moment = require("models/moment.js");
@@ -35,4 +36,12 @@ router.post('/', ensureAuth, upload.single('fileName'), function(req, res) {
3536
res.redirect('/home');
3637
});
3738

39+
/* Heart moment */
40+
router.post('/vote/:id', ensureAuth, function(req, res){
41+
heart.heartMoment(req);
42+
console.log("Succesful Post");
43+
44+
return;
45+
});
46+
3847
module.exports = router;

controllers/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ module.exports = function(app, passport) {
6262
});
6363
});
6464

65+
app.get('/api/user_data', function(req, res) {
66+
if (req.user === undefined) {
67+
// The user is not logged in
68+
res.json({});
69+
} else {
70+
res.json({
71+
userid: req.user.id
72+
});
73+
}
74+
});
75+
6576
// process the signup form
6677
app.post('/signup', passport.authenticate('local-signup', {
6778
successRedirect: '/home', // redirect to the secure PROFILE section --- CHANGE FOR PROFILE

models/moment.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ var momentSchema = new mongoose.Schema({
1818
type: mongoose.Schema.Types.ObjectId,
1919
ref: 'User',
2020
required: true
21-
}
21+
},
22+
heart: {
23+
type: Number,
24+
required: true
25+
},
26+
usersHeart: [{
27+
type: mongoose.Schema.Types.ObjectId, /* Object ID from moment */
28+
ref: 'Moment' /* Moment Schema. Remember to define it as this in the export module */
29+
}]
2230
});
2331

2432
module.exports = mongoose.model('Moment', momentSchema);

public/css/master.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ textarea:focus, input:focus{
280280
/*Moments*/
281281
.icon-heart_white{
282282
cursor: pointer;
283+
padding-left: 0.5rem;
283284
}
284285
.icon-heart_white:before{
285286
font-family: 'moments' !important;
@@ -296,6 +297,7 @@ textarea:focus, input:focus{
296297
font-size: 1rem;
297298
}
298299
.icon-heart_red:before{
300+
padding-left: 0.5rem;
299301
font-family: 'moments' !important;
300302
content: "\e912";
301303
color: #FEC06C;
@@ -304,6 +306,7 @@ textarea:focus, input:focus{
304306
}
305307
.heart-number{
306308
margin-top: 0.3em;
309+
text-align: center;
307310
}
308311
.tipe_moment{
309312
margin-bottom: 1vh;

public/js/home.js

Lines changed: 84 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,91 @@ function populateWall(momentlist) {
2525
/* Temporal computation for speed purposes */
2626
var timelapse = 0;
2727

28-
// jQuery AJAX call for JSON
29-
$.getJSON(momentlist, function(data) {
30-
// For each item in our JSON, add an 'article.moment'
31-
$.each(data, function() {
32-
wallContent += '<article class="moment">';
33-
wallContent += '<div class="has">';
34-
// wallContent += '<div class="heart">'
35-
// wallContent += '<div class="icon-heart_white"></div>'
36-
// wallContent += '<div class="heart-number">10</div></div>'
37-
wallContent += '<img src="' + this.user.image + '" alt=""/>';
38-
wallContent += '<div class="text_has">';
39-
wallContent += this.user.username ;
40-
wallContent += ' <span class="transparent"> tuvo un </span>';
41-
wallContent += ' momento <span class="transparent"> de </span>' ;
42-
43-
timelapse = Math.floor(this.timelapse/30);
44-
ipsos += timelapse;
45-
wallContent += timelapse;
46-
wallContent += ' ipsos</div></div>'
47-
wallContent += '<div class="right_moment">';
48-
49-
if(this.attachement != ''){
50-
wallContent += '<div class="tipe_moment image">';
51-
wallContent += '<img src="' + this.attachement[0] + '".jpg alt=""/>';
52-
}
53-
54-
wallContent += '<div class="tipe_moment text"><span class="icon-Comillas-10"></span>' + this.description + '</div></div></article>';
55-
});
28+
/* User id */
29+
var userid;
5630

57-
// Inject the whole content string into our existing HTML section
58-
$('#wall').html(wallContent);
59-
60-
// If we are in profile
61-
if(momentlist === '/profile/momentList'){
62-
if(Object.keys(data).length === 0)
63-
$('#momentsQuantity').html("Es hora de idear. ¡Inicia un nuevo momento!");
64-
else{
65-
$('#momentsQuantity').html(Object.keys(data).length + " momentos logrados");
66-
$('#ipsos').html(ipsos + ' ipsos');
67-
}
68-
}
31+
$.getJSON("api/user_data", function(user) {
32+
// Make sure the data contains the username as expected before using it
33+
userid = user.userid;
6934

70-
//================================== CHANGE COLOR HEART ===============================
71-
$(".icon-heart_white").click(function(){
72-
$(this).removeClass("icon-heart_white");
73-
$(this).addClass("icon-heart_red");
74-
});
35+
// jQuery AJAX call for JSON
36+
$.getJSON(momentlist, function(data) {
37+
// For each item in our JSON, add an 'article.moment'
38+
$.each(data, function() {
39+
/* Replace undefined hearts with zeroes */
40+
if(this.heart == null)
41+
this.heart = 0;
42+
43+
var usersHeart = this.usersHeart;
44+
var specifiedClass = "icon-heart_white";
45+
46+
/* Class depending if it was clickable or not */
47+
for(var i in usersHeart){
48+
var id = usersHeart[i];
49+
if(id == userid)
50+
specifiedClass = "icon-heart_red";
51+
}
52+
53+
wallContent += '<article class="moment">';
54+
wallContent += '<div class="has">';
55+
wallContent += '<div class="heart" onClick="addHref(\''+ this._id +'\')">';
56+
57+
58+
wallContent += '<div id=' + this._id + "c" + ' class=' + specifiedClass + '></div>';
59+
60+
wallContent += '<div id=' + this._id + ' class="heart-number">' + this.heart + '</div></div>';
61+
wallContent += '<img src="' + this.user.image + '" alt=""/>';
62+
wallContent += '<div class="text_has">';
63+
wallContent += this.user.username ;
64+
wallContent += ' <span class="transparent"> tuvo un </span>';
65+
wallContent += ' momento <span class="transparent"> de </span>' ;
66+
67+
timelapse = Math.floor(this.timelapse/30);
68+
ipsos += timelapse;
69+
wallContent += timelapse;
70+
wallContent += ' ipsos</div></div>'
71+
wallContent += '<div class="right_moment">';
72+
73+
if(this.attachement != ''){
74+
wallContent += '<div class="tipe_moment image">';
75+
wallContent += '<img src="' + this.attachement[0] + '".jpg alt=""/>';
76+
}
77+
78+
wallContent += '<div class="tipe_moment text"><span class="icon-Comillas-10"></span>' + this.description + '</div></div></article>';
79+
80+
// Inject the whole content string into our existing HTML section
81+
$('#wall').html(wallContent);
82+
83+
//================================== CHANGE COLOR HEART ===============================
84+
$(".icon-heart_white").click(function(){
85+
/* $(this).removeClass("icon-heart_white");
86+
$(this).addClass("icon-heart_red");*/
87+
});
88+
});
89+
});
7590
});
91+
92+
93+
94+
// If we are in profile
95+
if(momentlist === '/profile/momentList'){
96+
if(Object.keys(data).length === 0)
97+
$('#momentsQuantity').html("Es hora de idear. ¡Inicia un nuevo momento!");
98+
else{
99+
$('#momentsQuantity').html(Object.keys(data).length + " momentos logrados");
100+
$('#ipsos').html(ipsos + ' ipsos');
101+
}
102+
}
76103
};
104+
105+
function addHref(clickedID){
106+
if($("#" + clickedID + "c").prop('className') == "icon-heart_white"){
107+
$.post('home/vote/' + clickedID, function(){
108+
109+
});
110+
$("#" + clickedID).html(parseInt($("#" + clickedID).html()) + 1);
111+
112+
$("#" + clickedID + "c").removeClass("icon-heart_white");
113+
$("#" + clickedID + "c").addClass("icon-heart_red");
114+
}
115+
}

0 commit comments

Comments
 (0)