Skip to content

Commit 139d75a

Browse files
committed
replace frequent param extraction type with a data type instead of int
1 parent b722062 commit 139d75a

File tree

3 files changed

+62
-60
lines changed

3 files changed

+62
-60
lines changed

codeworld-server/src/Collaboration.hs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ collabRoutes socketIOHandler clientId =
6565
, ("socket.io", socketIOHandler)
6666
]
6767

68-
getFrequentParams :: Int -> ClientId -> Snap (User, BuildMode, FilePath)
68+
data ParamsGetType = GetFromHash | NotInCommentables deriving (Eq)
69+
70+
getFrequentParams :: ParamsGetType -> ClientId -> Snap (User, BuildMode, FilePath)
6971
getFrequentParams getType clientId = do
7072
user <- getUser clientId
7173
mode <- getBuildMode
7274
case getType of
73-
1 -> do
75+
NotInCommentables -> do
7476
Just path' <- fmap (splitDirectories . BC.unpack) <$> getParam "path"
7577
Just name <- getParam "name"
7678
let projectId = nameToProjectId $ T.decodeUtf8 name
@@ -79,14 +81,14 @@ getFrequentParams getType clientId = do
7981
case (length path', path' !! 0) of
8082
(0, _) -> return (user, mode, file)
8183
(_, x) | x /= "commentables" -> return (user, mode, file)
82-
_ -> do
84+
GetFromHash -> do
8385
Just collabHash <- fmap (CollabId . T.decodeUtf8) <$> getParam "collabHash"
8486
let collabHashPath = collabHashRootDir mode </> collabHashLink collabHash <.> "cw"
8587
return (user, mode, collabHashPath)
8688

8789
addToCollaborateHandler :: ClientId -> Snap ()
8890
addToCollaborateHandler clientId = do
89-
(user, mode, collabHashPath) <- getFrequentParams 2 clientId
91+
(user, mode, collabHashPath) <- getFrequentParams GetFromHash clientId
9092
Just path' <- fmap (splitDirectories . BC.unpack) <$> getParam "path"
9193
case length path' of
9294
x | x /= 0 && path' !! 0 == "commentables" -> do
@@ -109,14 +111,14 @@ addToCollaborateHandler clientId = do
109111

110112
collabShareHandler :: ClientId -> Snap ()
111113
collabShareHandler clientId = do
112-
(_, _, filePath) <- getFrequentParams 1 clientId
114+
(_, _, filePath) <- getFrequentParams NotInCommentables clientId
113115
collabHashFile <- liftIO $ takeFileName . BC.unpack <$> B.readFile filePath
114116
modifyResponse $ setContentType "text/plain"
115117
writeBS . BC.pack . take (length collabHashFile - 3) $ collabHashFile
116118

117119
listCurrentOwnersHandler :: ClientId -> Snap ()
118120
listCurrentOwnersHandler clientId = do
119-
(_, _, filePath) <- getFrequentParams 1 clientId
121+
(_, _, filePath) <- getFrequentParams NotInCommentables clientId
120122
collabHashPath <- liftIO $ BC.unpack <$> B.readFile filePath
121123
Just (currentUsers :: [UserDump]) <- liftIO $ decodeStrict <$>
122124
B.readFile (collabHashPath <.> "users")
@@ -134,7 +136,7 @@ initCollabServer = do
134136

135137
initCollaborationHandler :: CollabServerState -> ClientId -> Snap (Text, Text, CollabId)
136138
initCollaborationHandler state clientId = do
137-
(user, _, filePath) <- getFrequentParams 1 clientId
139+
(user, _, filePath) <- getFrequentParams NotInCommentables clientId
138140
collabHashPath <- liftIO $ BC.unpack <$> B.readFile filePath
139141
let collabHash = take (length collabHashPath - 3) . takeFileName $ collabHashPath
140142
Just (currentUsers :: [UserDump]) <- liftIO $ decodeStrict <$>

codeworld-server/src/Comment.hs

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,19 @@ commentRoutes clientId =
6767
, ("writeReply", writeReplyHandler clientId)
6868
]
6969

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)
7173
getFrequentParams getType clientId = do
7274
user <- getUser clientId
7375
mode <- getBuildMode
7476
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
8678
Just commentHash <- fmap (CommentId . T.decodeUtf8) <$> getParam "chash"
8779
commentFolder <- liftIO $
8880
BC.unpack <$> B.readFile (commentHashRootDir mode </> commentHashLink commentHash)
8981
return (user, mode, commentFolder)
90-
3 -> do
82+
InCommentables -> do
9183
Just path' <- fmap (splitDirectories . BC.unpack) <$> getParam "path"
9284
Just name <- getParam "name"
9385
let projectId = nameToProjectId $ T.decodeUtf8 name
@@ -98,19 +90,25 @@ getFrequentParams getType clientId = do
9890
(sharedCommentsDir mode (userId user) </> cDir </> commentProjectLink projectId)
9991
commentFolder <- BC.unpack <$> B.readFile commentHashFile
10092
return (user, mode, commentFolder)
101-
_ -> do
93+
NotInCommentables x -> do
10294
Just path' <- fmap (splitDirectories . BC.unpack) <$> getParam "path"
10395
Just name <- getParam "name"
10496
let projectId = nameToProjectId $ T.decodeUtf8 name
10597
finalDir = joinPath $ map (dirBase . nameToDirId . T.pack) path'
10698
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)
110108

111109
addSharedCommentHandler :: ClientId -> Snap ()
112110
addSharedCommentHandler clientId = do
113-
(user, mode, commentFolder) <- getFrequentParams 2 clientId
111+
(user, mode, commentFolder) <- getFrequentParams GetFromHash clientId
114112
Just path' <- fmap (splitDirectories . BC.unpack) <$> getParam "path"
115113
case path' !! 0 of
116114
"commentables" -> do
@@ -139,13 +137,13 @@ addSharedCommentHandler clientId = do
139137

140138
commentShareHandler :: ClientId -> Snap ()
141139
commentShareHandler clientId = do
142-
(_, _, commentFolder) <- getFrequentParams 1 clientId
140+
(_, _, commentFolder) <- getFrequentParams (NotInCommentables True) clientId
143141
modifyResponse $ setContentType "text/plain"
144142
writeBS . T.encodeUtf8 . unCommentId . nameToCommentHash $ commentFolder
145143

146144
deleteCommentHandler :: ClientId -> Snap ()
147145
deleteCommentHandler clientId = do
148-
(user, mode, commentFolder) <- getFrequentParams 3 clientId
146+
(user, mode, commentFolder) <- getFrequentParams InCommentables clientId
149147
Just (versionNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "versionNo"
150148
Just (lineNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "lineNo"
151149
Just (comment' :: CommentDesc) <- (decodeStrict =<<) <$> getParam "comment"
@@ -171,7 +169,7 @@ deleteCommentHandler clientId = do
171169

172170
deleteOwnerCommentHandler :: ClientId -> Snap ()
173171
deleteOwnerCommentHandler clientId = do
174-
(user, mode, commentFolder) <- getFrequentParams 1 clientId
172+
(user, mode, commentFolder) <- getFrequentParams (NotInCommentables True) clientId
175173
Just (versionNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "versionNo"
176174
Just (lineNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "lineNo"
177175
Just (comment' :: CommentDesc) <- (decodeStrict =<<) <$> getParam "comment"
@@ -196,7 +194,7 @@ deleteOwnerCommentHandler clientId = do
196194

197195
deleteOwnerReplyHandler :: ClientId -> Snap ()
198196
deleteOwnerReplyHandler clientId = do
199-
(user, mode, commentFolder) <- getFrequentParams 1 clientId
197+
(user, mode, commentFolder) <- getFrequentParams (NotInCommentables True) clientId
200198
Just (versionNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "versionNo"
201199
Just (lineNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "lineNo"
202200
Just (comment' :: CommentDesc) <- (decodeStrict =<<) <$> getParam "comment"
@@ -222,7 +220,7 @@ deleteOwnerReplyHandler clientId = do
222220

223221
deleteReplyHandler :: ClientId -> Snap ()
224222
deleteReplyHandler clientId = do
225-
(user, mode, commentFolder) <- getFrequentParams 3 clientId
223+
(user, mode, commentFolder) <- getFrequentParams InCommentables clientId
226224
Just (versionNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "versionNo"
227225
Just (lineNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "lineNo"
228226
Just (comment' :: CommentDesc) <- (decodeStrict =<<) <$> getParam "comment"
@@ -249,7 +247,7 @@ deleteReplyHandler clientId = do
249247

250248
getUserIdentHandler :: ClientId -> Snap ()
251249
getUserIdentHandler clientId = do
252-
(user, mode, commentFolder) <- getFrequentParams 3 clientId
250+
(user, mode, commentFolder) <- getFrequentParams InCommentables clientId
253251
let commentHash = nameToCommentHash commentFolder
254252
commentHashPath = commentHashRootDir mode </> commentHashLink commentHash
255253
Just (currentUsers :: [UserDump]) <- liftIO $
@@ -266,7 +264,7 @@ getUserIdentHandler clientId = do
266264

267265
getOwnerUserIdentHandler :: ClientId -> Snap ()
268266
getOwnerUserIdentHandler clientId = do
269-
(user, _, commentFolder) <- getFrequentParams 1 clientId
267+
(user, _, commentFolder) <- getFrequentParams (NotInCommentables True) clientId
270268
let projectPath = take (length commentFolder - 9) commentFolder
271269
Just (currentUsers :: [UserDump]) <- liftIO $
272270
decodeStrict <$> B.readFile (projectPath <.> "users")
@@ -282,25 +280,25 @@ getOwnerUserIdentHandler clientId = do
282280

283281
listCommentsHandler :: ClientId -> Snap ()
284282
listCommentsHandler clientId = do
285-
(_, _, commentFolder) <- getFrequentParams 3 clientId
283+
(_, _, commentFolder) <- getFrequentParams InCommentables clientId
286284
modifyResponse $ setContentType "application/json"
287285
writeLBS =<< (liftIO $ encode <$> listDirectory commentFolder)
288286

289287
listOwnerCommentsHandler :: ClientId -> Snap ()
290288
listOwnerCommentsHandler clientId = do
291-
(_, _, commentFolder) <- getFrequentParams 1 clientId
289+
(_, _, commentFolder) <- getFrequentParams (NotInCommentables True) clientId
292290
modifyResponse $ setContentType "application/json"
293291
writeLBS =<< (liftIO $ encode <$> listDirectory commentFolder)
294292

295293
listOwnerVersionsHandler :: ClientId -> Snap ()
296294
listOwnerVersionsHandler clientId = do
297-
(_, _, commentFolder) <- getFrequentParams 1 clientId
295+
(_, _, commentFolder) <- getFrequentParams (NotInCommentables True) clientId
298296
modifyResponse $ setContentType "application/json"
299297
writeLBS =<< (liftIO $ encode <$> listDirectory (commentFolder <.> "versions"))
300298

301299
listUnreadCommentsHandler :: ClientId -> Snap ()
302300
listUnreadCommentsHandler clientId = do
303-
(user, mode, commentFolder) <- getFrequentParams 3 clientId
301+
(user, mode, commentFolder) <- getFrequentParams InCommentables clientId
304302
Just (versionNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "versionNo"
305303
let commentHash = nameToCommentHash commentFolder
306304
commentHashPath = commentHashRootDir mode </> commentHashLink commentHash
@@ -320,7 +318,7 @@ listUnreadCommentsHandler clientId = do
320318

321319
listUnreadOwnerCommentsHandler :: ClientId -> Snap ()
322320
listUnreadOwnerCommentsHandler clientId = do
323-
(user, _, commentFolder) <- getFrequentParams 1 clientId
321+
(user, _, commentFolder) <- getFrequentParams (NotInCommentables True) clientId
324322
let projectPath = take (length commentFolder - 9) commentFolder
325323
Just (currentUsers :: [UserDump]) <- liftIO $
326324
decodeStrict <$> B.readFile (projectPath <.> "users")
@@ -339,13 +337,13 @@ listUnreadOwnerCommentsHandler clientId = do
339337

340338
listVersionsHandler :: ClientId -> Snap ()
341339
listVersionsHandler clientId = do
342-
(_, _, commentFolder) <- getFrequentParams 3 clientId
340+
(_, _, commentFolder) <- getFrequentParams InCommentables clientId
343341
modifyResponse $ setContentType "application/json"
344342
writeLBS =<< (liftIO $ encode <$> listDirectory (commentFolder <.> "versions"))
345343

346344
readCommentHandler :: ClientId -> Snap ()
347345
readCommentHandler clientId = do
348-
(user, mode, commentFolder) <- getFrequentParams 3 clientId
346+
(user, mode, commentFolder) <- getFrequentParams InCommentables clientId
349347
Just (versionNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "versionNo"
350348
Just (lineNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "lineNo"
351349
let commentHash = nameToCommentHash commentFolder
@@ -367,7 +365,7 @@ readCommentHandler clientId = do
367365

368366
readOwnerCommentHandler :: ClientId -> Snap ()
369367
readOwnerCommentHandler clientId = do
370-
(user, _, commentFolder) <- getFrequentParams 1 clientId
368+
(user, _, commentFolder) <- getFrequentParams (NotInCommentables True) clientId
371369
let projectPath = take (length commentFolder - 9) commentFolder
372370
Just (currentUsers :: [UserDump]) <- liftIO $
373371
decodeStrict <$> B.readFile (projectPath <.> "users")
@@ -388,23 +386,23 @@ readOwnerCommentHandler clientId = do
388386

389387
viewCommentSourceHandler :: ClientId -> Snap ()
390388
viewCommentSourceHandler clientId = do
391-
(_, _, commentFolder) <- getFrequentParams 3 clientId
389+
(_, _, commentFolder) <- getFrequentParams InCommentables clientId
392390
Just (versionNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "versionNo"
393391
currentSource <- liftIO $ B.readFile (commentFolder <.> "versions" </> show versionNo')
394392
modifyResponse $ setContentType "text/x-haskell"
395393
writeBS currentSource
396394

397395
viewOwnerCommentSourceHandler :: ClientId -> Snap()
398396
viewOwnerCommentSourceHandler clientId = do
399-
(_, _, commentFolder) <- getFrequentParams 1 clientId
397+
(_, _, commentFolder) <- getFrequentParams (NotInCommentables True) clientId
400398
Just (versionNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "versionNo"
401399
currentSource <- liftIO $ B.readFile (commentFolder <.> "versions" </> show versionNo')
402400
modifyResponse $ setContentType "text/x-haskell"
403401
writeBS currentSource
404402

405403
writeCommentHandler :: ClientId -> Snap ()
406404
writeCommentHandler clientId = do
407-
(user, mode, commentFolder) <- getFrequentParams 3 clientId
405+
(user, mode, commentFolder) <- getFrequentParams InCommentables clientId
408406
Just (versionNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "versionNo"
409407
Just (lineNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "lineNo"
410408
Just (comment' :: Text) <- fmap (T.decodeUtf8) <$> getParam "comment"
@@ -427,7 +425,7 @@ writeCommentHandler clientId = do
427425

428426
writeOwnerCommentHandler :: ClientId -> Snap ()
429427
writeOwnerCommentHandler clientId = do
430-
(user, _, commentFolder) <- getFrequentParams 1 clientId
428+
(user, _, commentFolder) <- getFrequentParams (NotInCommentables True) clientId
431429
let projectPath = take (length commentFolder - 9) commentFolder
432430
Just (currentUsers :: [UserDump]) <- liftIO $
433431
decodeStrict <$> B.readFile (projectPath <.> "users")
@@ -450,7 +448,7 @@ writeOwnerCommentHandler clientId = do
450448

451449
writeOwnerReplyHandler :: ClientId -> Snap ()
452450
writeOwnerReplyHandler clientId = do
453-
(user, _, commentFolder) <- getFrequentParams 1 clientId
451+
(user, _, commentFolder) <- getFrequentParams (NotInCommentables True) clientId
454452
let projectPath = take (length commentFolder - 9) commentFolder
455453
Just (currentUsers :: [UserDump]) <- liftIO $
456454
decodeStrict <$> B.readFile (projectPath <.> "users")
@@ -473,7 +471,7 @@ writeOwnerReplyHandler clientId = do
473471

474472
writeReplyHandler :: ClientId -> Snap ()
475473
writeReplyHandler clientId = do
476-
(user, mode, commentFolder) <- getFrequentParams 3 clientId
474+
(user, mode, commentFolder) <- getFrequentParams InCommentables clientId
477475
Just (versionNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "versionNo"
478476
Just (lineNo' :: Int) <- fmap (read . BC.unpack) <$> getParam "lineNo"
479477
Just (comment' :: CommentDesc) <- (decodeStrict =<<) <$> getParam "comment"

0 commit comments

Comments
 (0)