@@ -67,27 +67,19 @@ commentRoutes clientId =
67
67
, (" writeReply" , writeReplyHandler clientId)
68
68
]
69
69
70
- getFrequentParams :: Int -> ClientId -> Snap (User , BuildMode , FilePath )
70
+ data ParamsGetType = GetFromHash | InCommentables | NotInCommentables Bool deriving (Eq )
71
+
72
+ getFrequentParams :: ParamsGetType -> ClientId -> Snap (User , BuildMode , FilePath )
71
73
getFrequentParams getType clientId = do
72
74
user <- getUser clientId
73
75
mode <- getBuildMode
74
76
case getType of
75
- 1 -> do
76
- Just path' <- fmap (splitDirectories . BC. unpack) <$> getParam " path"
77
- Just name <- getParam " name"
78
- let projectId = nameToProjectId $ T. decodeUtf8 name
79
- finalDir = joinPath $ map (dirBase . nameToDirId . T. pack) path'
80
- file = userProjectDir mode (userId user) </> finalDir </> projectFile projectId
81
- commentFolder <- liftIO $ (<.> " comments" ) . BC. unpack <$> B. readFile file
82
- case (length path', path' !! 0 ) of
83
- (0 , _) -> return (user, mode, commentFolder)
84
- (_, x) | x /= " commentables" -> return (user, mode, commentFolder)
85
- 2 -> do
77
+ GetFromHash -> do
86
78
Just commentHash <- fmap (CommentId . T. decodeUtf8) <$> getParam " chash"
87
79
commentFolder <- liftIO $
88
80
BC. unpack <$> B. readFile (commentHashRootDir mode </> commentHashLink commentHash)
89
81
return (user, mode, commentFolder)
90
- 3 -> do
82
+ InCommentables -> do
91
83
Just path' <- fmap (splitDirectories . BC. unpack) <$> getParam " path"
92
84
Just name <- getParam " name"
93
85
let projectId = nameToProjectId $ T. decodeUtf8 name
@@ -98,19 +90,25 @@ getFrequentParams getType clientId = do
98
90
(sharedCommentsDir mode (userId user) </> cDir </> commentProjectLink projectId)
99
91
commentFolder <- BC. unpack <$> B. readFile commentHashFile
100
92
return (user, mode, commentFolder)
101
- _ -> do
93
+ NotInCommentables x -> do
102
94
Just path' <- fmap (splitDirectories . BC. unpack) <$> getParam " path"
103
95
Just name <- getParam " name"
104
96
let projectId = nameToProjectId $ T. decodeUtf8 name
105
97
finalDir = joinPath $ map (dirBase . nameToDirId . T. pack) path'
106
98
file = userProjectDir mode (userId user) </> finalDir </> projectFile projectId
107
- case (length path', path' !! 0 ) of
108
- (0 , _) -> return (user, mode, file)
109
- (_, x) | x /= " commentables" -> return (user, mode, file)
99
+ case (length path', path' !! 0 , x) of
100
+ (0 , _, True ) -> do
101
+ commentFolder <- liftIO $ (<.> " comments" ) . BC. unpack <$> B. readFile file
102
+ return (user, mode, commentFolder)
103
+ (0 , _, False ) -> return (user, mode, file)
104
+ (_, x', True ) | x' /= " commentables" -> do
105
+ commentFolder <- liftIO $ (<.> " comments" ) . BC. unpack <$> B. readFile file
106
+ return (user, mode, commentFolder)
107
+ (_, x', False ) | x' /= " commentables" -> return (user, mode, file)
110
108
111
109
addSharedCommentHandler :: ClientId -> Snap ()
112
110
addSharedCommentHandler clientId = do
113
- (user, mode, commentFolder) <- getFrequentParams 2 clientId
111
+ (user, mode, commentFolder) <- getFrequentParams GetFromHash clientId
114
112
Just path' <- fmap (splitDirectories . BC. unpack) <$> getParam " path"
115
113
case path' !! 0 of
116
114
" commentables" -> do
@@ -139,13 +137,13 @@ addSharedCommentHandler clientId = do
139
137
140
138
commentShareHandler :: ClientId -> Snap ()
141
139
commentShareHandler clientId = do
142
- (_, _, commentFolder) <- getFrequentParams 1 clientId
140
+ (_, _, commentFolder) <- getFrequentParams ( NotInCommentables True ) clientId
143
141
modifyResponse $ setContentType " text/plain"
144
142
writeBS . T. encodeUtf8 . unCommentId . nameToCommentHash $ commentFolder
145
143
146
144
deleteCommentHandler :: ClientId -> Snap ()
147
145
deleteCommentHandler clientId = do
148
- (user, mode, commentFolder) <- getFrequentParams 3 clientId
146
+ (user, mode, commentFolder) <- getFrequentParams InCommentables clientId
149
147
Just (versionNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " versionNo"
150
148
Just (lineNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " lineNo"
151
149
Just (comment' :: CommentDesc ) <- (decodeStrict =<< ) <$> getParam " comment"
@@ -171,7 +169,7 @@ deleteCommentHandler clientId = do
171
169
172
170
deleteOwnerCommentHandler :: ClientId -> Snap ()
173
171
deleteOwnerCommentHandler clientId = do
174
- (user, mode, commentFolder) <- getFrequentParams 1 clientId
172
+ (user, mode, commentFolder) <- getFrequentParams ( NotInCommentables True ) clientId
175
173
Just (versionNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " versionNo"
176
174
Just (lineNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " lineNo"
177
175
Just (comment' :: CommentDesc ) <- (decodeStrict =<< ) <$> getParam " comment"
@@ -196,7 +194,7 @@ deleteOwnerCommentHandler clientId = do
196
194
197
195
deleteOwnerReplyHandler :: ClientId -> Snap ()
198
196
deleteOwnerReplyHandler clientId = do
199
- (user, mode, commentFolder) <- getFrequentParams 1 clientId
197
+ (user, mode, commentFolder) <- getFrequentParams ( NotInCommentables True ) clientId
200
198
Just (versionNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " versionNo"
201
199
Just (lineNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " lineNo"
202
200
Just (comment' :: CommentDesc ) <- (decodeStrict =<< ) <$> getParam " comment"
@@ -222,7 +220,7 @@ deleteOwnerReplyHandler clientId = do
222
220
223
221
deleteReplyHandler :: ClientId -> Snap ()
224
222
deleteReplyHandler clientId = do
225
- (user, mode, commentFolder) <- getFrequentParams 3 clientId
223
+ (user, mode, commentFolder) <- getFrequentParams InCommentables clientId
226
224
Just (versionNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " versionNo"
227
225
Just (lineNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " lineNo"
228
226
Just (comment' :: CommentDesc ) <- (decodeStrict =<< ) <$> getParam " comment"
@@ -249,7 +247,7 @@ deleteReplyHandler clientId = do
249
247
250
248
getUserIdentHandler :: ClientId -> Snap ()
251
249
getUserIdentHandler clientId = do
252
- (user, mode, commentFolder) <- getFrequentParams 3 clientId
250
+ (user, mode, commentFolder) <- getFrequentParams InCommentables clientId
253
251
let commentHash = nameToCommentHash commentFolder
254
252
commentHashPath = commentHashRootDir mode </> commentHashLink commentHash
255
253
Just (currentUsers :: [UserDump ]) <- liftIO $
@@ -266,7 +264,7 @@ getUserIdentHandler clientId = do
266
264
267
265
getOwnerUserIdentHandler :: ClientId -> Snap ()
268
266
getOwnerUserIdentHandler clientId = do
269
- (user, _, commentFolder) <- getFrequentParams 1 clientId
267
+ (user, _, commentFolder) <- getFrequentParams ( NotInCommentables True ) clientId
270
268
let projectPath = take (length commentFolder - 9 ) commentFolder
271
269
Just (currentUsers :: [UserDump ]) <- liftIO $
272
270
decodeStrict <$> B. readFile (projectPath <.> " users" )
@@ -282,25 +280,25 @@ getOwnerUserIdentHandler clientId = do
282
280
283
281
listCommentsHandler :: ClientId -> Snap ()
284
282
listCommentsHandler clientId = do
285
- (_, _, commentFolder) <- getFrequentParams 3 clientId
283
+ (_, _, commentFolder) <- getFrequentParams InCommentables clientId
286
284
modifyResponse $ setContentType " application/json"
287
285
writeLBS =<< (liftIO $ encode <$> listDirectory commentFolder)
288
286
289
287
listOwnerCommentsHandler :: ClientId -> Snap ()
290
288
listOwnerCommentsHandler clientId = do
291
- (_, _, commentFolder) <- getFrequentParams 1 clientId
289
+ (_, _, commentFolder) <- getFrequentParams ( NotInCommentables True ) clientId
292
290
modifyResponse $ setContentType " application/json"
293
291
writeLBS =<< (liftIO $ encode <$> listDirectory commentFolder)
294
292
295
293
listOwnerVersionsHandler :: ClientId -> Snap ()
296
294
listOwnerVersionsHandler clientId = do
297
- (_, _, commentFolder) <- getFrequentParams 1 clientId
295
+ (_, _, commentFolder) <- getFrequentParams ( NotInCommentables True ) clientId
298
296
modifyResponse $ setContentType " application/json"
299
297
writeLBS =<< (liftIO $ encode <$> listDirectory (commentFolder <.> " versions" ))
300
298
301
299
listUnreadCommentsHandler :: ClientId -> Snap ()
302
300
listUnreadCommentsHandler clientId = do
303
- (user, mode, commentFolder) <- getFrequentParams 3 clientId
301
+ (user, mode, commentFolder) <- getFrequentParams InCommentables clientId
304
302
Just (versionNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " versionNo"
305
303
let commentHash = nameToCommentHash commentFolder
306
304
commentHashPath = commentHashRootDir mode </> commentHashLink commentHash
@@ -320,7 +318,7 @@ listUnreadCommentsHandler clientId = do
320
318
321
319
listUnreadOwnerCommentsHandler :: ClientId -> Snap ()
322
320
listUnreadOwnerCommentsHandler clientId = do
323
- (user, _, commentFolder) <- getFrequentParams 1 clientId
321
+ (user, _, commentFolder) <- getFrequentParams ( NotInCommentables True ) clientId
324
322
let projectPath = take (length commentFolder - 9 ) commentFolder
325
323
Just (currentUsers :: [UserDump ]) <- liftIO $
326
324
decodeStrict <$> B. readFile (projectPath <.> " users" )
@@ -339,13 +337,13 @@ listUnreadOwnerCommentsHandler clientId = do
339
337
340
338
listVersionsHandler :: ClientId -> Snap ()
341
339
listVersionsHandler clientId = do
342
- (_, _, commentFolder) <- getFrequentParams 3 clientId
340
+ (_, _, commentFolder) <- getFrequentParams InCommentables clientId
343
341
modifyResponse $ setContentType " application/json"
344
342
writeLBS =<< (liftIO $ encode <$> listDirectory (commentFolder <.> " versions" ))
345
343
346
344
readCommentHandler :: ClientId -> Snap ()
347
345
readCommentHandler clientId = do
348
- (user, mode, commentFolder) <- getFrequentParams 3 clientId
346
+ (user, mode, commentFolder) <- getFrequentParams InCommentables clientId
349
347
Just (versionNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " versionNo"
350
348
Just (lineNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " lineNo"
351
349
let commentHash = nameToCommentHash commentFolder
@@ -367,7 +365,7 @@ readCommentHandler clientId = do
367
365
368
366
readOwnerCommentHandler :: ClientId -> Snap ()
369
367
readOwnerCommentHandler clientId = do
370
- (user, _, commentFolder) <- getFrequentParams 1 clientId
368
+ (user, _, commentFolder) <- getFrequentParams ( NotInCommentables True ) clientId
371
369
let projectPath = take (length commentFolder - 9 ) commentFolder
372
370
Just (currentUsers :: [UserDump ]) <- liftIO $
373
371
decodeStrict <$> B. readFile (projectPath <.> " users" )
@@ -388,23 +386,23 @@ readOwnerCommentHandler clientId = do
388
386
389
387
viewCommentSourceHandler :: ClientId -> Snap ()
390
388
viewCommentSourceHandler clientId = do
391
- (_, _, commentFolder) <- getFrequentParams 3 clientId
389
+ (_, _, commentFolder) <- getFrequentParams InCommentables clientId
392
390
Just (versionNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " versionNo"
393
391
currentSource <- liftIO $ B. readFile (commentFolder <.> " versions" </> show versionNo')
394
392
modifyResponse $ setContentType " text/x-haskell"
395
393
writeBS currentSource
396
394
397
395
viewOwnerCommentSourceHandler :: ClientId -> Snap ()
398
396
viewOwnerCommentSourceHandler clientId = do
399
- (_, _, commentFolder) <- getFrequentParams 1 clientId
397
+ (_, _, commentFolder) <- getFrequentParams ( NotInCommentables True ) clientId
400
398
Just (versionNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " versionNo"
401
399
currentSource <- liftIO $ B. readFile (commentFolder <.> " versions" </> show versionNo')
402
400
modifyResponse $ setContentType " text/x-haskell"
403
401
writeBS currentSource
404
402
405
403
writeCommentHandler :: ClientId -> Snap ()
406
404
writeCommentHandler clientId = do
407
- (user, mode, commentFolder) <- getFrequentParams 3 clientId
405
+ (user, mode, commentFolder) <- getFrequentParams InCommentables clientId
408
406
Just (versionNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " versionNo"
409
407
Just (lineNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " lineNo"
410
408
Just (comment' :: Text ) <- fmap (T. decodeUtf8) <$> getParam " comment"
@@ -427,7 +425,7 @@ writeCommentHandler clientId = do
427
425
428
426
writeOwnerCommentHandler :: ClientId -> Snap ()
429
427
writeOwnerCommentHandler clientId = do
430
- (user, _, commentFolder) <- getFrequentParams 1 clientId
428
+ (user, _, commentFolder) <- getFrequentParams ( NotInCommentables True ) clientId
431
429
let projectPath = take (length commentFolder - 9 ) commentFolder
432
430
Just (currentUsers :: [UserDump ]) <- liftIO $
433
431
decodeStrict <$> B. readFile (projectPath <.> " users" )
@@ -450,7 +448,7 @@ writeOwnerCommentHandler clientId = do
450
448
451
449
writeOwnerReplyHandler :: ClientId -> Snap ()
452
450
writeOwnerReplyHandler clientId = do
453
- (user, _, commentFolder) <- getFrequentParams 1 clientId
451
+ (user, _, commentFolder) <- getFrequentParams ( NotInCommentables True ) clientId
454
452
let projectPath = take (length commentFolder - 9 ) commentFolder
455
453
Just (currentUsers :: [UserDump ]) <- liftIO $
456
454
decodeStrict <$> B. readFile (projectPath <.> " users" )
@@ -473,7 +471,7 @@ writeOwnerReplyHandler clientId = do
473
471
474
472
writeReplyHandler :: ClientId -> Snap ()
475
473
writeReplyHandler clientId = do
476
- (user, mode, commentFolder) <- getFrequentParams 3 clientId
474
+ (user, mode, commentFolder) <- getFrequentParams InCommentables clientId
477
475
Just (versionNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " versionNo"
478
476
Just (lineNo' :: Int ) <- fmap (read . BC. unpack) <$> getParam " lineNo"
479
477
Just (comment' :: CommentDesc ) <- (decodeStrict =<< ) <$> getParam " comment"
0 commit comments