Skip to content

Commit a7eb2ea

Browse files
committed
Merge branch 'mmuraki-master'
2 parents eb1fde7 + 892ae25 commit a7eb2ea

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

src/main/scala/gitbucket/gist/controller/GistController.scala

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.apache.commons.io.FileUtils
2424
import org.eclipse.jgit.api.Git
2525
import org.eclipse.jgit.lib._
2626
import org.scalatra.Ok
27+
import play.twirl.api.Html
2728

2829
class GistController extends GistControllerBase with GistService with GistCommentService with AccountService
2930
with GistEditorAuthenticator with UsersAuthenticator
@@ -47,25 +48,7 @@ trait GistControllerBase extends ControllerBase {
4748
if(context.loginAccount.isDefined){
4849
val gists = getRecentGists(context.loginAccount.get.userName, 0, 4)
4950
html.edit(gists, None, Seq(("", JGitUtil.ContentInfo("text", None, Some("UTF-8")))))
50-
} else {
51-
val page = request.getParameter("page") match {
52-
case ""|null => 1
53-
case s => s.toInt
54-
}
55-
val result = getPublicGists((page - 1) * Limit, Limit)
56-
val count = countPublicGists()
57-
58-
val gists: Seq[(Gist, GistInfo)] = result.map { gist =>
59-
val userName = gist.userName
60-
val repoName = gist.repositoryName
61-
val files = getGistFiles(userName, repoName)
62-
val (fileName, source) = files.head
63-
64-
(gist, GistInfo(fileName, getLines(source), files.length, getForkedCount(userName, repoName), getCommentCount(userName, repoName)))
65-
}
66-
67-
html.list(None, gists, page, page * Limit < count)
68-
}
51+
} else _discoverGists()
6952
}
7053

7154
get("/gist/:userName/:repoName"){
@@ -292,6 +275,10 @@ trait GistControllerBase extends ControllerBase {
292275
html.editor(count, "", JGitUtil.ContentInfo("text", None, Some("UTF-8")))
293276
}
294277

278+
get("/gist/discover"){
279+
_discoverGists()
280+
}
281+
295282
////////////////////////////////////////////////////////////////////////////////
296283
//
297284
// Fork Actions
@@ -403,9 +390,8 @@ trait GistControllerBase extends ControllerBase {
403390
} getOrElse {
404391
contentType = formats("json")
405392
org.json4s.jackson.Serialization.write(
406-
Map("content" -> gitbucket.core.view.Markdown.toHtml(comment.content,
407-
gist.toRepositoryInfo, false, true, true, true) // TODO isEditableこれでいいのか?
408-
))
393+
Map("content" -> gitbucket.core.view.Markdown.toHtml(comment.content, gist.toRepositoryInfo, false, true, true, true))
394+
)
409395
}
410396
}
411397
} getOrElse NotFound
@@ -427,8 +413,27 @@ trait GistControllerBase extends ControllerBase {
427413
// Private Methods
428414
//
429415
////////////////////////////////////////////////////////////////////////////////
416+
private def _discoverGists(): Html = {
417+
val page = request.getParameter("page") match {
418+
case ""|null => 1
419+
case s => s.toInt
420+
}
421+
val result = getVisibleGists((page - 1) * Limit, Limit, None)
422+
val count = countPublicGists()
423+
424+
val gists: Seq[(Gist, GistInfo)] = result.map { gist =>
425+
val userName = gist.userName
426+
val repoName = gist.repositoryName
427+
val files = getGistFiles(userName, repoName)
428+
val (fileName, source) = files.head
429+
430+
(gist, GistInfo(fileName, getLines(source), files.length, getForkedCount(userName, repoName), getCommentCount(userName, repoName)))
431+
}
432+
433+
html.list(None, gists, page, page * Limit < count)
434+
}
430435

431-
private def _gist(userName: String, repoName: Option[String] = None, revision: String = "master") = {
436+
private def _gist(userName: String, repoName: Option[String] = None, revision: String = "master"): Html = {
432437
repoName match {
433438
case None => {
434439
val page = params.get("page") match {

src/main/scala/gitbucket/gist/service/GistService.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package gitbucket.gist.service
22

3+
import gitbucket.core.model.Account
34
import gitbucket.gist.model.Gist
45
import gitbucket.gist.model.Profile._
56
import profile.simple._
@@ -9,8 +10,14 @@ trait GistService {
910
def getRecentGists(userName: String, offset: Int, limit: Int)(implicit s: Session): Seq[Gist] =
1011
Gists.filter(_.userName === userName.bind).sortBy(_.registeredDate desc).drop(offset).take(limit).list
1112

12-
def getPublicGists(offset: Int, limit: Int)(implicit s: Session): Seq[Gist] =
13-
Gists.filter(_.isPrivate === false.bind).sortBy(_.registeredDate desc).drop(offset).take(limit).list
13+
def getVisibleGists(offset: Int, limit: Int, account: Option[Account])(implicit s: Session): Seq[Gist] = {
14+
val query = account.map { x =>
15+
Gists.filter { t => (t.isPrivate === false.bind) || (t.userName === x.userName.bind) }
16+
} getOrElse {
17+
Gists.filter { t => (t.isPrivate === false.bind) }
18+
}
19+
query.sortBy(_.registeredDate desc).drop(offset).take(limit).list
20+
}
1421

1522
def countPublicGists()(implicit s: Session): Int =
1623
Query(Gists.filter(_.isPrivate === false.bind).length).first

0 commit comments

Comments
 (0)