Skip to content

Commit bdb0222

Browse files
added 'accept' and 'decline' logic
1 parent 028be72 commit bdb0222

File tree

5 files changed

+105
-18
lines changed

5 files changed

+105
-18
lines changed

public/js/app.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ $(function(){
1010
if(data == 1) {
1111
$(self).attr('href', $(self).attr('href').replace('join','leave'));
1212
$(self).text('Leave project');
13-
$(self).parent().siblings('.users').append('<li><a href="https://twitter.com/"'+window.username+'><img src="http://avatars.io/twitter/'+ window.username +'" />@'+window.user+'</a></li>');
1413
} else if(data == 2) {
1514
$(self).attr('href', $(self).attr('href').replace('leave','join'));
1615
$(self).text('Join project');
1716
$(self).parent().siblings('.users').children('li').each(function(){
18-
var src = $(this).find('img').attr('src');
19-
src = src.split('/');
20-
src = src[src.length - 1];
21-
console.log(src);
17+
var src = $(this).find('span').text();
18+
src = src.substr(1);
2219
if(src === window.username) $(this).remove();
2320
});
2421
}

public/styles/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ body {
55

66
a {
77
color: #00B7FF;
8-
}
8+
}

routes/index.js

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ app.get('/dashboard', isAuth, function(req, res){
3434
projects = projects.map(function(project){
3535
return JSON.parse(project);
3636
});
37-
console.log(projects);
3837
res.render('dashboard', {projects: projects, user: req.user});
3938
});
4039
});
@@ -46,15 +45,11 @@ app.get('/projects/join/:id', function(req, res){
4645
} else {
4746
client.get('hhba:projects:' + req.params.id, function(err, project) {
4847
project = JSON.parse(project);
49-
if(project.contributors.indexOf(req.user.username) !== -1) {
48+
if(project.contributors.indexOf(req.user.username) !== -1 && project.pendent.indexOf(req.user.username) !== -1) {
5049
res.end('1');
5150
} else {
52-
project.contributors.push(req.user.username);
51+
project.pending.push(req.user.username);
5352
client.set('hhba:projects:' + req.params.id, JSON.stringify(project), function(){
54-
search.remove(req.params.id, function(){
55-
search.index(project.title + ' ' + project.description + ' ' + project.contributors.join(' ')
56-
, req.params.id);
57-
});
5853
res.end('1');
5954
});
6055
}
@@ -77,13 +72,91 @@ app.get('/projects/leave/:id', function(req, res){
7772
, req.params.id);
7873
});
7974
});
75+
} else if(project.pending.indexOf(req.user.username) !== -1) {
76+
project.pending.splice(project.pending.indexOf(req.user.username), 1);
77+
client.set('hhba:projects:' + req.params.id, JSON.stringify(project), function(){
78+
res.end('2');
79+
search.remove(req.params.id, function(){
80+
search.index(project.title + ' ' + project.description + ' ' + project.contributors.join(' ')
81+
, req.params.id);
82+
});
83+
});
8084
} else {
8185
res.end('2');
8286
}
8387
});
8488
}
8589
});
8690

91+
app.get('/accept/:pid/:uid', function(req, res){
92+
if(!req.isAuthenticated()){
93+
res.end('0');
94+
} else {
95+
client.get('hhba:projects:' + req.params.pid, function(err, project) {
96+
project = JSON.parse(project);
97+
if(project.owner_id != req.user.id) {
98+
res.end('1');
99+
} else if(project.pending.indexOf(req.params.uid) === -1) {
100+
res.end('2');
101+
} else {
102+
project.contributors.push(req.params.uid);
103+
project.pending.splice(project.pending.indexOf(req.params.uid), 1);
104+
client.set('hhba:projects:' + req.params.pid, JSON.stringify(project), function(){
105+
search.remove(req.params.pid, function(){
106+
search.index(project.title + ' ' + project.description + ' ' + project.contributors.join(' ')
107+
, req.params.id);
108+
});
109+
res.end('3');
110+
});
111+
}
112+
});
113+
}
114+
});
115+
116+
app.get('/decline/:pid/:uid', function(req, res){
117+
if(!req.isAuthenticated()){
118+
res.end('0');
119+
} else {
120+
client.get('hhba:projects:' + req.params.pid, function(err, project) {
121+
project = JSON.parse(project);
122+
if(project.owner_id != req.user.id) {
123+
res.end('0');
124+
} else if(project.pending.indexOf(req.params.uid) === -1) {
125+
res.end('1');
126+
} else {
127+
project.pending.splice(project.pending.indexOf(req.params.uid), 1);
128+
client.set('hhba:projects:' + req.params.pid, JSON.stringify(project), function(){
129+
res.end('1');
130+
search.remove(req.params.pid, function(){
131+
search.index(project.title + ' ' + project.description + ' ' + project.contributors.join(' ')
132+
, req.params.id);
133+
});
134+
});
135+
}
136+
});
137+
}
138+
});
139+
140+
app.get('/decline/:pid/:uid', function(req, res){
141+
if(!req.isAuthenticated()){
142+
res.end('0');
143+
} else {
144+
client.get('hhba:projects:' + req.params.id, function(err, project) {
145+
project = JSON.parse(project);
146+
if(project.owner_id != req.user.id) {
147+
res.end('0');
148+
} else if(project.pending.indexOf(req.user.username) === -1) {
149+
res.end('1');
150+
} else {
151+
project.pending.splice(project.pending.indexOf(req.user.username), 1);
152+
client.set('hhba:projects:' + req.params.id, JSON.stringify(project), function(){
153+
res.end('1');
154+
});
155+
}
156+
});
157+
}
158+
});
159+
87160
app.post('/projects/new', isAuth, function(req, res){
88161
if(req.body.title && req.body.description){
89162
var hash = Math.floor(Math.random() * 9999999 + 1);
@@ -96,6 +169,7 @@ app.post('/projects/new', isAuth, function(req, res){
96169
, description: req.body.description
97170
, links: req.body.links.split(',') || []
98171
, contributors: [req.user.username]
172+
, pending: []
99173
};
100174

101175
client.set('hhba:projects:' + hash, JSON.stringify(project), function(){
@@ -134,7 +208,7 @@ app.post('/projects/edit/:id', isAuth, isOwner, function(req, res){
134208
});
135209
});
136210
} else {
137-
211+
res.redirect('/dashboard');
138212
}
139213
});
140214

@@ -151,11 +225,13 @@ app.get('/search', isAuth, function(req, res){
151225
.end(function(err, ids){
152226
ids = ids.map(function(id){ return 'hhba:projects:' + id; });
153227
client.mget(ids, function(err, projects){
154-
console.log(projects);
155228
projects = projects || [];
156229
projects = projects.map(function(project){
157230
return JSON.parse(project);
158231
});
232+
projects = projects.filter(function(project){
233+
return !!project;
234+
});
159235
res.render('dashboard', {projects: projects, user: req.user});
160236
});
161237
});

views/dashboard.jade

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ block content
4444
each contrib in project.contributors
4545
li
4646
a(href="https://twitter.com/" + contrib)
47-
img(src="http://avatars.io/twitter/" + contrib)
48-
br
4947
span @#{contrib}
5048

5149
#newProject.modal.hide

views/edit.jade

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
form.form-horizontal(action="/projects/edit/" + project.id, method="POST")
22
fieldset
3-
legend New Project
3+
legend Edit Project
44
.control-group
55
label.control-label(for="title") Title
66
.controls
@@ -20,3 +20,19 @@ form.form-horizontal(action="/projects/edit/" + project.id, method="POST")
2020
button.btn.btn-primary(type="submit") Edit project
2121
button.btn.cancel Cancel
2222
a.btn.btn-danger(href="/projects/remove/" + project.id) Remove Project
23+
24+
h3 Pending users
25+
26+
-if(!project.pending.length){
27+
strong No users to moderate
28+
-}
29+
30+
table.table.pending
31+
tbody
32+
each user in project.pending
33+
tr
34+
td= user
35+
td
36+
a.btn-success.btn(href="/accept/"+project.id+"/"+user) Accept
37+
td
38+
a.btn-danger.btn(href="/decline/"+project.id+"/"+user) Decline

0 commit comments

Comments
 (0)