11package gitbucket .gist .controller
22
33import java .io .File
4+ import gitbucket .core .view .helpers
45import jp .sf .amateras .scalatra .forms ._
56
67import gitbucket .core .controller .ControllerBase
78import gitbucket .core .service .AccountService
9+ import gitbucket .core .service .RepositoryService .RepositoryInfo
810import gitbucket .core .util ._
911import gitbucket .core .util .Directory ._
1012import gitbucket .core .util .ControlUtil ._
1113import gitbucket .core .util .Implicits ._
1214import gitbucket .core .view .helpers ._
1315
1416import gitbucket .gist .model ._
15- import gitbucket .gist .service .GistService
17+ import gitbucket .gist .service ._
1618import gitbucket .gist .util ._
1719import gitbucket .gist .util .GistUtils ._
1820import gitbucket .gist .util .Configurations ._
@@ -21,12 +23,19 @@ import gitbucket.gist.html
2123import org .apache .commons .io .FileUtils
2224import org .eclipse .jgit .api .Git
2325import org .eclipse .jgit .lib ._
26+ import org .scalatra .Ok
2427
25- class GistController extends GistControllerBase with GistService with AccountService
28+ class GistController extends GistControllerBase with GistService with GistCommentService with AccountService
2629 with GistEditorAuthenticator with UsersAuthenticator
2730
2831trait GistControllerBase extends ControllerBase {
29- self : GistService with AccountService with GistEditorAuthenticator with UsersAuthenticator =>
32+ self : GistService with GistCommentService with AccountService with GistEditorAuthenticator with UsersAuthenticator =>
33+
34+ case class CommentForm (content : String )
35+
36+ val commentForm = mapping(
37+ " content" -> trim(label(" Comment" , text(required)))
38+ )(CommentForm .apply)
3039
3140 get(" /gist" ){
3241 if (context.loginAccount.isDefined){
@@ -41,10 +50,12 @@ trait GistControllerBase extends ControllerBase {
4150 val count = countPublicGists()
4251
4352 val gists : Seq [(Gist , GistInfo )] = result.map { gist =>
44- val files = getGistFiles(gist.userName, gist.repositoryName)
53+ val userName = gist.userName
54+ val repoName = gist.repositoryName
55+ val files = getGistFiles(userName, repoName)
4556 val (fileName, source) = files.head
4657
47- (gist, GistInfo (fileName, source, files.length, getForkedCount(gist. userName, gist.repositoryName )))
58+ (gist, GistInfo (fileName, source, files.length, getForkedCount(userName, repoName), getCommentCount(userName, repoName )))
4859 }
4960
5061 html.list(None , gists, page, page * Limit < count)
@@ -318,6 +329,82 @@ trait GistControllerBase extends ControllerBase {
318329 } getOrElse NotFound
319330 }
320331
332+ post(" /gist/:userName/:repoName/_preview" ){
333+ val userName = params(" userName" )
334+ val repoName = params(" repoName" )
335+
336+ contentType = " text/html"
337+ helpers.markdown(params(" content" ),
338+ RepositoryInfo (
339+ owner = userName,
340+ name = repoName,
341+ httpUrl = " " ,
342+ repository = null ,
343+ issueCount = 0 ,
344+ pullCount = 0 ,
345+ commitCount = 0 ,
346+ forkedCount = 0 ,
347+ branchList = Nil ,
348+ tags = Nil ,
349+ managers = Nil
350+ ), false , false , false , false )
351+ }
352+
353+ post(" /gist/:userName/:repoName/_comment" , commentForm)(usersOnly { form =>
354+ val userName = params(" userName" )
355+ val repoName = params(" repoName" )
356+ val loginAccount = context.loginAccount.get
357+
358+ getGist(userName, repoName).map { gist =>
359+ registerGistComment(userName, repoName, form.content, loginAccount.userName)
360+ redirect(s " ${context.path}/gist/ ${userName}/ ${repoName}" )
361+ } getOrElse NotFound
362+ })
363+
364+ ajaxPost(" /gist/:userName/:repoName/_comments/:commentId/_delete" )(usersOnly {
365+ val userName = params(" userName" )
366+ val repoName = params(" repoName" )
367+ val commentId = params(" commentId" ).toInt
368+
369+ // TODO Access check
370+
371+ Ok (deleteGistComment(userName, repoName, commentId))
372+ })
373+
374+ ajaxGet(" /gist/:userName/:repoName/_comments/:commentId" )(usersOnly {
375+ val userName = params(" userName" )
376+ val repoName = params(" repoName" )
377+ val commentId = params(" commentId" ).toInt
378+
379+ // TODO Access check
380+ getGist(userName, repoName).flatMap { gist =>
381+ getGistComment(userName, repoName, commentId).map { comment =>
382+ params.get(" dataType" ) collect {
383+ case t if t == " html" => gitbucket.gist.html.commentedit(
384+ comment.content, comment.commentId, comment.userName, comment.repositoryName)
385+ } getOrElse {
386+ contentType = formats(" json" )
387+ org.json4s.jackson.Serialization .write(
388+ Map (" content" -> gitbucket.core.view.Markdown .toHtml(comment.content,
389+ gist.toRepositoryInfo, false , true , true , true ) // TODO isEditableこれでいいのか?
390+ ))
391+ }
392+ }
393+ } getOrElse NotFound
394+ })
395+
396+ ajaxPost(" /gist/:userName/:repoName/_comments/:commentId/_update" , commentForm)(usersOnly { form =>
397+ val userName = params(" userName" )
398+ val repoName = params(" repoName" )
399+ val commentId = params(" commentId" ).toInt
400+
401+ // TODO Access check
402+
403+ updateGistComment(userName, repoName, commentId, form.content)
404+ redirect(s " /gist/ ${userName}/ ${repoName}/_comments/ ${commentId}" )
405+ })
406+
407+
321408 private def _gist (userName : String , repoName : Option [String ] = None , revision : String = " master" ) = {
322409 repoName match {
323410 case None => {
@@ -335,7 +422,7 @@ trait GistControllerBase extends ControllerBase {
335422 val repoName = gist.repositoryName
336423 val files = getGistFiles(userName, repoName, revision)
337424 val (fileName, source) = files.head
338- (gist, GistInfo (fileName, source, files.length, getForkedCount(userName, repoName)))
425+ (gist, GistInfo (fileName, source, files.length, getForkedCount(userName, repoName), getCommentCount(userName, repoName) ))
339426 }
340427
341428 val fullName = getAccountByUserName(userName).get.fullName
@@ -352,6 +439,7 @@ trait GistControllerBase extends ControllerBase {
352439 GistRepositoryURL (gist, baseUrl, context.settings),
353440 revision,
354441 getGistFiles(userName, repoName, revision),
442+ getGistComments(userName, repoName),
355443 isEditable(userName)
356444 )
357445 }
0 commit comments