@@ -164,45 +164,60 @@ router.get('/snippet/:ref?/:version?', (req, res, next) => {
164164 let ref = req . params . ref ;
165165 let version = req . params . version ;
166166
167- if ( ref === undefined || version === undefined ) {
168- req . flash ( 'warning' , `Missing ref and/or version number ` ) ;
167+ if ( ref === undefined ) {
168+ req . flash ( 'warning' , `Missing ref` ) ;
169169 res . redirect ( baseURL + '/view/snippet' ) ;
170170 return ;
171171 }
172172
173173 Snippet . getCond ( { ref} ) . then ( doc => {
174- Version . getVersion ( ref , version ) . then ( ver => {
175- if ( doc === null || ver === null ) {
176- res . redirect ( baseURL + '/view/snippet' ) ;
177- return ;
178- }
174+ if ( doc === null ) {
175+ res . redirect ( baseURL + '/view/snippet' ) ;
176+ return ;
177+ }
179178
180- Snippet . getTags ( doc . id ) . then ( tags => {
181- if ( doc . owner !== req . session . user . id ) {
182- res . redirect ( baseURL + '/view/snippet' ) ;
179+ Snippet . getTags ( doc . id ) . then ( tags => {
180+ if ( doc . owner !== req . session . user . id ) {
181+ res . redirect ( baseURL + '/view/snippet' ) ;
182+ } else {
183+ if ( version !== undefined ) {
184+ Version . getVersion ( ref , version ) . then ( ver => {
185+ if ( ver === null ) {
186+ res . redirect ( baseURL + '/view/snippet' ) ;
187+ }
188+ doc . description = ver . description ; // Replace snippet description with revision's description
189+ res . render ( 'edit/snippet' , _ . merge ( defaultVars , { snippet : doc , version : _ . merge ( ver , { revision : true } ) , tags, socket : ':' + settings . general . socket , title : `Edit Snippet ${ doc . name } ` } ) ) ;
190+ } ) ;
183191 } else {
184- doc . code = fs . readFileSync ( `./data/snippets/${ doc . id } -${ ver . version } .snippet` ) ; // Read snippet src into this...
185- res . render ( 'edit/snippet' , _ . merge ( defaultVars , { snippet : doc , tags, socket : ':' + settings . general . socket , title : `Edit Snippet ${ doc . name } ` } ) ) ;
192+ res . render ( 'edit/snippet' , _ . merge ( defaultVars , { snippet : doc , version : { version : 1 , revision : false } , tags, socket : ':' + settings . general . socket , title : `Edit Snippet ${ doc . name } ` } ) ) ;
186193 }
187- } ) ;
194+ }
188195 } ) ;
189196 } ) ;
190197} ) ;
191198
192199router . post ( '/snippet/:ref?/:version?' , ( req , res , next ) => {
193- let ref = req . params . ref ;
194- let version = req . params . version ;
200+ let ref = req . params . ref ;
201+ let version = req . params . version ;
202+ let versionFmt ;
203+ if ( version !== undefined ) {
204+ versionFmt = "/" + version ;
205+ props = [ 'rename' , 'description' ] ;
206+ req . body . tags = "" ;
207+ } else {
208+ props = [ 'rename' , 'tags' , 'description' ] ;
209+ }
195210 let description = req . body . description ;
196211
197- if ( ref === undefined || version === undefined ) {
198- req . flash ( 'warning' , `Missing ref and/or version number ` ) ;
199- res . redirect ( `${ baseURL } /edit/snippet/${ ref } / ${ version } ` ) ;
212+ if ( ref === undefined ) {
213+ req . flash ( 'warning' , `Missing ref` ) ;
214+ res . redirect ( `${ baseURL } /edit/snippet/${ ref } ${ versionFmt } ${ versionFmt } ` ) ;
200215 return ;
201216 }
202217
203- if ( missingProps ( req . body , [ 'rename' , 'tags' , 'description' ] ) ) {
218+ if ( missingProps ( req . body , props ) ) {
204219 req . flash ( 'warning' , 'Missing expected form properties' ) ;
205- res . redirect ( `${ baseURL } /edit/snippet/${ ref } / ${ version } ` ) ;
220+ res . redirect ( `${ baseURL } /edit/snippet/${ ref } ${ versionFmt } ` ) ;
206221 return ;
207222 }
208223
@@ -221,34 +236,46 @@ router.post('/snippet/:ref?/:version?', (req, res, next) => {
221236 if ( name === undefined || name === "" ) name = doc . name ;
222237 if ( doc . published === 1 && name !== doc . name ) {
223238 req . flash ( 'warning' , 'Names can\'t be changed for Published Snippets' ) ;
224- res . redirect ( `${ baseURL } /edit/snippet/${ ref } / ${ version } ` ) ;
239+ res . redirect ( `${ baseURL } /edit/snippet/${ ref } ${ versionFmt } ` ) ;
225240
226241 } else if ( name . match ( / ^ [ a - z A - Z 0 - 9 _ \- \. + \[ \] \{ \} \( \) ] { 1 , 32 } $ / ) === null ) {
227242 req . flash ( 'warning' , 'Only 32 chars max please! Accepted chars: a-Z0-9 _-.+[]{}()' ) ;
228- res . redirect ( `${ baseURL } /edit/snippet/${ ref } / ${ version } ` ) ;
243+ res . redirect ( `${ baseURL } /edit/snippet/${ ref } ${ versionFmt } ` ) ;
229244
230245 } else if ( rejects . length > 0 || req . body . tags . length > 255 ) {
231246 req . flash ( 'warning' , 'Snippet tags invalid! 32 chars per tag, accepted chars: a-Z0-9 _-.+[]{}()' ) ;
232- res . redirect ( `${ baseURL } /edit/snippet/${ ref } / ${ version } ` ) ;
247+ res . redirect ( `${ baseURL } /edit/snippet/${ ref } ${ versionFmt } ` ) ;
233248
234249 } else if ( description . length > 65535 ) {
235250 req . flash ( 'warning' , 'Description is too large' ) ;
236- res . redirect ( `${ baseURL } /edit/snippet/${ ref } / ${ version } ` ) ;
251+ res . redirect ( `${ baseURL } /edit/snippet/${ ref } ${ versionFmt } ` ) ;
237252
238253 } else {
239254 Snippet . getCond ( { name : req . body . rename , owner : req . session . user . id } ) . then ( dup => {
240255 if ( req . body . rename !== doc . name && dup !== null ) {
241256 req . flash ( 'warning' , `You already have another snippet named ${ req . body . rename } ` ) ;
242- res . redirect ( `${ baseURL } /edit/snippet/${ ref } / ${ version } ` ) ;
257+ res . redirect ( `${ baseURL } /edit/snippet/${ ref } ${ versionFmt } ` ) ;
243258 return ;
244259 }
245260
246- Snippet . update ( { name, description} , doc . id ) . then ( ( ) => {
247- Snippet . updateTags ( tags , doc . id ) . then ( ( ) => {
248- req . flash ( 'info' , `Snippet ${ name } was updated successfully!` ) ;
249- res . redirect ( baseURL + '/view/snippet' + ( doc . published === 1 ? "#publish" : "" ) ) ;
261+ // Revision edit
262+ if ( version !== undefined ) {
263+ Version . getVersion ( ref , version ) . then ( ver => {
264+ Version . update ( { description} , ver . id ) . then ( ( ) => {
265+ Snippet . modified ( doc . id ) . then ( ( ) => {
266+ req . flash ( 'info' , `Snippet ${ name } was updated successfully!` ) ;
267+ res . redirect ( baseURL + '/view/snippet#publish' ) ;
268+ } ) ;
269+ } ) ;
250270 } ) ;
251- } ) ;
271+ } else {
272+ Snippet . update ( { name, description} , doc . id ) . then ( ( ) => {
273+ Snippet . updateTags ( tags , doc . id ) . then ( ( ) => {
274+ req . flash ( 'info' , `Snippet ${ name } was updated successfully!` ) ;
275+ res . redirect ( baseURL + '/view/snippet' + ( doc . published === 1 ? "#publish" : "" ) ) ;
276+ } ) ;
277+ } ) ;
278+ }
252279 } ) ;
253280 }
254281 } ) ;
0 commit comments