1
1
// response
2
2
// external modules
3
3
import * as request from "request" ;
4
+ import { Request , Response } from "express" ;
5
+ import { Includeable } from "sequelize" ;
4
6
// core
5
7
import config from "./config" ;
6
8
import { logger } from "./logger" ;
@@ -9,9 +11,9 @@ import {createNoteWithRevision} from "./services/note";
9
11
import * as utils from "./utils" ;
10
12
import * as history from "./history" ;
11
13
12
- export function errorForbidden ( req , res ) {
14
+ export function errorForbidden ( req : Request , res : Response ) : void {
13
15
if ( req . user ) {
14
- responseError ( res , ' 403' , 'Forbidden' , 'oh no.' )
16
+ responseError ( res , 403 , 'Forbidden' , 'oh no.' )
15
17
} else {
16
18
const nextURL = new URL ( '' , config . serverURL )
17
19
nextURL . search = ( new URLSearchParams ( { next : req . originalUrl } ) ) . toString ( )
@@ -20,27 +22,27 @@ export function errorForbidden(req, res) {
20
22
}
21
23
}
22
24
23
- export function errorNotFound ( req , res ) {
24
- responseError ( res , ' 404' , 'Not Found' , 'oops.' )
25
+ export function errorNotFound ( req : Request , res : Response ) : void {
26
+ responseError ( res , 404 , 'Not Found' , 'oops.' )
25
27
}
26
28
27
- export function errorBadRequest ( req , res ) {
28
- responseError ( res , ' 400' , 'Bad Request' , 'something not right.' )
29
+ export function errorBadRequest ( req : Request , res : Response ) : void {
30
+ responseError ( res , 400 , 'Bad Request' , 'something not right.' )
29
31
}
30
32
31
- export function errorTooLong ( req , res ) {
32
- responseError ( res , ' 413' , 'Payload Too Large' , 'Shorten your note!' )
33
+ export function errorTooLong ( req : Request , res : Response ) : void {
34
+ responseError ( res , 413 , 'Payload Too Large' , 'Shorten your note!' )
33
35
}
34
36
35
- export function errorInternalError ( req , res ) {
36
- responseError ( res , ' 500' , 'Internal Error' , 'wtf.' )
37
+ export function errorInternalError ( req : Request , res : Response ) : void {
38
+ responseError ( res , 500 , 'Internal Error' , 'wtf.' )
37
39
}
38
40
39
- export function errorServiceUnavailable ( req , res ) {
41
+ export function errorServiceUnavailable ( req : Request , res : Response ) : void {
40
42
res . status ( 503 ) . send ( 'I\'m busy right now, try again later.' )
41
43
}
42
44
43
- export function responseError ( res , code , detail , msg ) {
45
+ export function responseError ( res : Response , code : number , detail : string , msg : string ) : void {
44
46
res . status ( code ) . render ( 'error.ejs' , {
45
47
title : code + ' ' + detail + ' ' + msg ,
46
48
code : code ,
@@ -49,7 +51,7 @@ export function responseError(res, code, detail, msg) {
49
51
} )
50
52
}
51
53
52
- export function responseCodiMD ( res , note ) {
54
+ export function responseCodiMD ( res : Response , note : Note ) : void {
53
55
const body = note . content
54
56
const extracted = Note . extractMeta ( body )
55
57
const meta = Note . parseMeta ( extracted . meta )
@@ -64,13 +66,17 @@ export function responseCodiMD(res, note) {
64
66
} )
65
67
}
66
68
67
- function updateHistory ( userId , note , document , time ?: any ) {
69
+ function updateHistory ( userId , note , document , time ?: number ) {
68
70
const noteId = note . alias ? note . alias : Note . encodeNoteId ( note . id )
69
71
history . updateHistory ( userId , noteId , document , time )
70
72
logger . info ( 'history updated' )
71
73
}
72
74
73
- export function newNote ( req , res , next ?: any ) {
75
+ type NewNoteReq = Request & {
76
+ alias ?: string
77
+ }
78
+
79
+ export function newNote ( req : NewNoteReq , res : Response ) : void {
74
80
let owner = null
75
81
let body = ''
76
82
if ( req . body && req . body . length > config . documentMaxLength ) {
@@ -100,7 +106,7 @@ export function newNote(req, res, next?: any) {
100
106
} )
101
107
}
102
108
103
- export function newCheckViewPermission ( note , isLogin , userId ) {
109
+ export function newCheckViewPermission ( note : Note , isLogin : boolean , userId : string ) : boolean {
104
110
if ( note . permission === 'private' ) {
105
111
return note . ownerId === userId
106
112
}
@@ -110,25 +116,17 @@ export function newCheckViewPermission(note, isLogin, userId) {
110
116
return true
111
117
}
112
118
113
- export function checkViewPermission ( req , note ) {
119
+ export function checkViewPermission ( req : Request , note : Note ) : boolean {
114
120
if ( note . permission === 'private' ) {
115
- if ( ! req . isAuthenticated ( ) || note . ownerId !== req . user . id ) {
116
- return false
117
- } else {
118
- return true
119
- }
120
- } else if ( note . permission === 'limited' || note . permission === 'protected' ) {
121
- if ( ! req . isAuthenticated ( ) ) {
122
- return false
123
- } else {
124
- return true
125
- }
126
- } else {
127
- return true
121
+ return ! ( ! req . isAuthenticated ( ) || note . ownerId !== req . user . id ) ;
128
122
}
123
+ if ( note . permission === 'limited' || note . permission === 'protected' ) {
124
+ return req . isAuthenticated ( ) ;
125
+ }
126
+ return true
129
127
}
130
128
131
- function findNote ( req , res , callback , include ?: any ) {
129
+ function findNote ( req , res , callback : ( note : Note ) => void , include ?: Includeable [ ] | null ) {
132
130
const noteId = req . params . noteId
133
131
const id = req . params . noteId || req . params . shortid
134
132
Note . parseNoteId ( id , function ( err , _id ) {
@@ -164,8 +162,7 @@ function findNote(req, res, callback, include?: any) {
164
162
165
163
function actionDownload ( req , res , note ) {
166
164
const body = note . content
167
- const title = Note . decodeTitle ( note . title )
168
- let filename = title
165
+ let filename = Note . decodeTitle ( note . title )
169
166
filename = encodeURIComponent ( filename )
170
167
res . set ( {
171
168
'Access-Control-Allow-Origin' : '*' , // allow CORS as API
@@ -179,7 +176,11 @@ function actionDownload(req, res, note) {
179
176
res . send ( body )
180
177
}
181
178
182
- export function publishNoteActions ( req , res , next ) {
179
+ interface PublishActionParams {
180
+ action : 'download' | 'edit'
181
+ }
182
+
183
+ export function publishNoteActions ( req : Request < PublishActionParams > , res : Response ) : void {
183
184
findNote ( req , res , function ( note ) {
184
185
const action = req . params . action
185
186
switch ( action ) {
@@ -196,7 +197,7 @@ export function publishNoteActions(req, res, next) {
196
197
} )
197
198
}
198
199
199
- export function publishSlideActions ( req , res , next ) {
200
+ export function publishSlideActions ( req : Request < PublishActionParams > , res : Response ) : void {
200
201
findNote ( req , res , function ( note ) {
201
202
const action = req . params . action
202
203
switch ( action ) {
@@ -210,7 +211,12 @@ export function publishSlideActions(req, res, next) {
210
211
} )
211
212
}
212
213
213
- export function githubActions ( req , res , next ) {
214
+ interface GithubActionParams extends Record < string , string > {
215
+ action : 'gist'
216
+ noteId : string
217
+ }
218
+
219
+ export function githubActions ( req : Request < GithubActionParams > , res : Response ) : void {
214
220
const noteId = req . params . noteId
215
221
findNote ( req , res , function ( note ) {
216
222
const action = req . params . action
@@ -225,7 +231,7 @@ export function githubActions(req, res, next) {
225
231
} )
226
232
}
227
233
228
- function githubActionGist ( req , res , note ) {
234
+ function githubActionGist ( req : Request , res : Response , note : Note ) {
229
235
const code = req . query . code
230
236
const state = req . query . state
231
237
if ( ! code || ! state ) {
@@ -282,13 +288,18 @@ function githubActionGist(req, res, note) {
282
288
}
283
289
}
284
290
285
- export function gitlabActions ( req , res , next ) {
291
+ interface GitLabParams extends Record < string , string > {
292
+ noteId : string
293
+ action : 'projects'
294
+ }
295
+
296
+ export function gitlabActions ( req : Request < GitLabParams > , res : Response ) : void {
286
297
const noteId = req . params . noteId
287
- findNote ( req , res , function ( note ) {
298
+ findNote ( req , res , function ( ) {
288
299
const action = req . params . action
289
300
switch ( action ) {
290
301
case 'projects' :
291
- gitlabActionProjects ( req , res , note )
302
+ gitlabActionProjects ( req , res )
292
303
break
293
304
default :
294
305
res . redirect ( config . serverURL + '/' + noteId )
@@ -297,7 +308,15 @@ export function gitlabActions(req, res, next) {
297
308
} )
298
309
}
299
310
300
- function gitlabActionProjects ( req , res , note ) {
311
+ interface GitLabActionResponse {
312
+ baseURL : string
313
+ version : string
314
+ accesstoken : string
315
+ profileid : string
316
+ projects ?: Record < string , string >
317
+ }
318
+
319
+ function gitlabActionProjects ( req : Request , res : Response ) {
301
320
if ( req . isAuthenticated ( ) ) {
302
321
User . findOne ( {
303
322
where : {
@@ -307,7 +326,7 @@ function gitlabActionProjects(req, res, note) {
307
326
if ( ! user ) {
308
327
return errorNotFound ( req , res )
309
328
}
310
- const ret : any = { baseURL : config . gitlab . baseURL , version : config . gitlab . version }
329
+ const ret : Partial < GitLabActionResponse > = { baseURL : config . gitlab . baseURL , version : config . gitlab . version }
311
330
ret . accesstoken = user . accessToken
312
331
ret . profileid = user . profileid
313
332
request (
@@ -330,7 +349,7 @@ function gitlabActionProjects(req, res, note) {
330
349
}
331
350
}
332
351
333
- export function showPublishSlide ( req , res , next ) {
352
+ export function showPublishSlide ( req : Request , res : Response ) : void {
334
353
const include = [ {
335
354
model : User ,
336
355
as : 'owner'
0 commit comments