You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
4.7 KiB

10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "fmt"
  7. "github.com/go-martini/martini"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/auth"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/middleware"
  12. )
  13. func Dashboard(ctx *middleware.Context) {
  14. ctx.Data["Title"] = "Dashboard"
  15. ctx.Data["PageIsUserDashboard"] = true
  16. repos, err := models.GetRepositories(&models.User{Id: ctx.User.Id}, true)
  17. if err != nil {
  18. ctx.Handle(500, "user.Dashboard", err)
  19. return
  20. }
  21. ctx.Data["MyRepos"] = repos
  22. feeds, err := models.GetFeeds(ctx.User.Id, 0, false)
  23. if err != nil {
  24. ctx.Handle(500, "user.Dashboard", err)
  25. return
  26. }
  27. ctx.Data["Feeds"] = feeds
  28. ctx.HTML(200, "user/dashboard")
  29. }
  30. func Profile(ctx *middleware.Context, params martini.Params) {
  31. ctx.Data["Title"] = "Profile"
  32. // TODO: Need to check view self or others.
  33. user, err := models.GetUserByName(params["username"])
  34. if err != nil {
  35. ctx.Handle(500, "user.Profile", err)
  36. return
  37. }
  38. ctx.Data["Owner"] = user
  39. tab := ctx.Query("tab")
  40. ctx.Data["TabName"] = tab
  41. switch tab {
  42. case "activity":
  43. feeds, err := models.GetFeeds(user.Id, 0, true)
  44. if err != nil {
  45. ctx.Handle(500, "user.Profile", err)
  46. return
  47. }
  48. ctx.Data["Feeds"] = feeds
  49. default:
  50. repos, err := models.GetRepositories(user, ctx.IsSigned && ctx.User.Id == user.Id)
  51. if err != nil {
  52. ctx.Handle(500, "user.Profile", err)
  53. return
  54. }
  55. ctx.Data["Repos"] = repos
  56. }
  57. ctx.Data["PageIsUserProfile"] = true
  58. ctx.HTML(200, "user/profile")
  59. }
  60. func Email2User(ctx *middleware.Context) {
  61. u, err := models.GetUserByEmail(ctx.Query("email"))
  62. if err != nil {
  63. if err == models.ErrUserNotExist {
  64. ctx.Handle(404, "user.Email2User", err)
  65. } else {
  66. ctx.Handle(500, "user.Email2User(GetUserByEmail)", err)
  67. }
  68. return
  69. }
  70. ctx.Redirect("/user/" + u.Name)
  71. }
  72. const (
  73. TPL_FEED = `<i class="icon fa fa-%s"></i>
  74. <div class="info"><span class="meta">%s</span><br>%s</div>`
  75. )
  76. func Feeds(ctx *middleware.Context, form auth.FeedsForm) {
  77. actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
  78. if err != nil {
  79. ctx.JSON(500, err)
  80. }
  81. feeds := make([]string, len(actions))
  82. for i := range actions {
  83. feeds[i] = fmt.Sprintf(TPL_FEED, base.ActionIcon(actions[i].OpType),
  84. base.TimeSince(actions[i].Created), base.ActionDesc(actions[i]))
  85. }
  86. ctx.JSON(200, &feeds)
  87. }
  88. func Issues(ctx *middleware.Context) {
  89. ctx.Data["Title"] = "Your Issues"
  90. ctx.Data["ViewType"] = "all"
  91. page, _ := base.StrTo(ctx.Query("page")).Int()
  92. repoId, _ := base.StrTo(ctx.Query("repoid")).Int64()
  93. ctx.Data["RepoId"] = repoId
  94. var posterId int64 = 0
  95. if ctx.Query("type") == "created_by" {
  96. posterId = ctx.User.Id
  97. ctx.Data["ViewType"] = "created_by"
  98. }
  99. // Get all repositories.
  100. repos, err := models.GetRepositories(ctx.User, true)
  101. if err != nil {
  102. ctx.Handle(200, "user.Issues(get repositories)", err)
  103. return
  104. }
  105. showRepos := make([]models.Repository, 0, len(repos))
  106. isShowClosed := ctx.Query("state") == "closed"
  107. var closedIssueCount, createdByCount, allIssueCount int
  108. // Get all issues.
  109. allIssues := make([]models.Issue, 0, 5*len(repos))
  110. for i, repo := range repos {
  111. issues, err := models.GetIssues(0, repo.Id, posterId, 0, page, isShowClosed, false, "", "")
  112. if err != nil {
  113. ctx.Handle(200, "user.Issues(get issues)", err)
  114. return
  115. }
  116. allIssueCount += repo.NumIssues
  117. closedIssueCount += repo.NumClosedIssues
  118. // Set repository information to issues.
  119. for j := range issues {
  120. issues[j].Repo = &repos[i]
  121. }
  122. allIssues = append(allIssues, issues...)
  123. repos[i].NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
  124. if repos[i].NumOpenIssues > 0 {
  125. showRepos = append(showRepos, repos[i])
  126. }
  127. }
  128. showIssues := make([]models.Issue, 0, len(allIssues))
  129. ctx.Data["IsShowClosed"] = isShowClosed
  130. // Get posters and filter issues.
  131. for i := range allIssues {
  132. u, err := models.GetUserById(allIssues[i].PosterId)
  133. if err != nil {
  134. ctx.Handle(200, "user.Issues(get poster): %v", err)
  135. return
  136. }
  137. allIssues[i].Poster = u
  138. if u.Id == ctx.User.Id {
  139. createdByCount++
  140. }
  141. if repoId > 0 && repoId != allIssues[i].Repo.Id {
  142. continue
  143. }
  144. if isShowClosed == allIssues[i].IsClosed {
  145. showIssues = append(showIssues, allIssues[i])
  146. }
  147. }
  148. ctx.Data["Repos"] = showRepos
  149. ctx.Data["Issues"] = showIssues
  150. ctx.Data["AllIssueCount"] = allIssueCount
  151. ctx.Data["ClosedIssueCount"] = closedIssueCount
  152. ctx.Data["OpenIssueCount"] = allIssueCount - closedIssueCount
  153. ctx.Data["CreatedByCount"] = createdByCount
  154. ctx.HTML(200, "issue/user")
  155. }
  156. func Pulls(ctx *middleware.Context) {
  157. ctx.HTML(200, "user/pulls")
  158. }
  159. func Stars(ctx *middleware.Context) {
  160. ctx.HTML(200, "user/stars")
  161. }