@@ -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+
87160app . 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 } ) ;
0 commit comments