home.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/Unknwon/com"
  8. "github.com/go-martini/martini"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/middleware"
  13. )
  14. func Dashboard(ctx *middleware.Context) {
  15. ctx.Data["Title"] = "Dashboard"
  16. ctx.Data["PageIsUserDashboard"] = true
  17. repos, err := models.GetRepositories(&models.User{Id: ctx.User.Id}, true)
  18. if err != nil {
  19. ctx.Handle(500, "user.Dashboard", err)
  20. return
  21. }
  22. ctx.Data["MyRepos"] = repos
  23. feeds, err := models.GetFeeds(ctx.User.Id, 0, false)
  24. if err != nil {
  25. ctx.Handle(500, "user.Dashboard", err)
  26. return
  27. }
  28. ctx.Data["Feeds"] = feeds
  29. ctx.HTML(200, "user/dashboard")
  30. }
  31. func Profile(ctx *middleware.Context, params martini.Params) {
  32. ctx.Data["Title"] = "Profile"
  33. // TODO: Need to check view self or others.
  34. user, err := models.GetUserByName(params["username"])
  35. if err != nil {
  36. ctx.Handle(500, "user.Profile", err)
  37. return
  38. }
  39. ctx.Data["Owner"] = user
  40. tab := ctx.Query("tab")
  41. ctx.Data["TabName"] = tab
  42. switch tab {
  43. case "activity":
  44. feeds, err := models.GetFeeds(user.Id, 0, true)
  45. if err != nil {
  46. ctx.Handle(500, "user.Profile", err)
  47. return
  48. }
  49. ctx.Data["Feeds"] = feeds
  50. default:
  51. repos, err := models.GetRepositories(user, ctx.IsSigned && ctx.User.Id == user.Id)
  52. if err != nil {
  53. ctx.Handle(500, "user.Profile", err)
  54. return
  55. }
  56. ctx.Data["Repos"] = repos
  57. }
  58. ctx.Data["PageIsUserProfile"] = true
  59. ctx.HTML(200, "user/profile")
  60. }
  61. func Email2User(ctx *middleware.Context) {
  62. u, err := models.GetUserByEmail(ctx.Query("email"))
  63. if err != nil {
  64. if err == models.ErrUserNotExist {
  65. ctx.Handle(404, "user.Email2User", err)
  66. } else {
  67. ctx.Handle(500, "user.Email2User(GetUserByEmail)", err)
  68. }
  69. return
  70. }
  71. ctx.Redirect("/user/" + u.Name)
  72. }
  73. const (
  74. TPL_FEED = `<i class="icon fa fa-%s"></i>
  75. <div class="info"><span class="meta">%s</span><br>%s</div>`
  76. )
  77. func Feeds(ctx *middleware.Context, form auth.FeedsForm) {
  78. actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
  79. if err != nil {
  80. ctx.JSON(500, err)
  81. }
  82. feeds := make([]string, len(actions))
  83. for i := range actions {
  84. feeds[i] = fmt.Sprintf(TPL_FEED, base.ActionIcon(actions[i].OpType),
  85. base.TimeSince(actions[i].Created), base.ActionDesc(actions[i]))
  86. }
  87. ctx.JSON(200, &feeds)
  88. }
  89. func Issues(ctx *middleware.Context) {
  90. ctx.Data["Title"] = "Your Issues"
  91. viewType := ctx.Query("type")
  92. types := []string{"assigned", "created_by"}
  93. if !com.IsSliceContainsStr(types, viewType) {
  94. viewType = "all"
  95. }
  96. isShowClosed := ctx.Query("state") == "closed"
  97. var assigneeId, posterId int64
  98. var filterMode int
  99. switch viewType {
  100. case "assigned":
  101. assigneeId = ctx.User.Id
  102. filterMode = models.FM_ASSIGN
  103. case "created_by":
  104. posterId = ctx.User.Id
  105. filterMode = models.FM_CREATE
  106. }
  107. _, _ = assigneeId, posterId
  108. // page, _ := base.StrTo(ctx.Query("page")).Int()
  109. // repoId, _ := base.StrTo(ctx.Query("repoid")).Int64()
  110. // ctx.Data["RepoId"] = repoId
  111. // var posterId int64 = 0
  112. // if ctx.Query("type") == "created_by" {
  113. // posterId = ctx.User.Id
  114. // ctx.Data["ViewType"] = "created_by"
  115. // }
  116. rid, _ := base.StrTo(ctx.Query("repoid")).Int64()
  117. issueStats := models.GetUserIssueStats(ctx.User.Id, filterMode)
  118. // Get all repositories.
  119. repos, err := models.GetRepositories(ctx.User, true)
  120. if err != nil {
  121. ctx.Handle(500, "user.Issues(get repositories)", err)
  122. return
  123. }
  124. showRepos := make([]*models.Repository, 0, len(repos))
  125. // Get all issues.
  126. // allIssues := make([]models.Issue, 0, 5*len(repos))
  127. for _, repo := range repos {
  128. if repo.NumIssues == 0 {
  129. continue
  130. }
  131. repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
  132. issueStats.AllCount += int64(repo.NumOpenIssues)
  133. // switch filterMode{
  134. // case models.FM_ASSIGN:
  135. // }
  136. if isShowClosed {
  137. if repo.NumClosedIssues > 0 {
  138. showRepos = append(showRepos, repo)
  139. }
  140. } else {
  141. if repo.NumOpenIssues > 0 {
  142. showRepos = append(showRepos, repo)
  143. }
  144. }
  145. // issues, err := models.GetIssues(0, repo.Id, posterId, 0, page, isShowClosed, "", "")
  146. // if err != nil {
  147. // ctx.Handle(200, "user.Issues(get issues)", err)
  148. // return
  149. // }
  150. }
  151. // allIssueCount += repo.NumIssues
  152. // closedIssueCount += repo.NumClosedIssues
  153. // // Set repository information to issues.
  154. // for j := range issues {
  155. // issues[j].Repo = &repos[i]
  156. // }
  157. // allIssues = append(allIssues, issues...)
  158. // repos[i].NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
  159. // if repos[i].NumOpenIssues > 0 {
  160. // showRepos = append(showRepos, repos[i])
  161. // }
  162. // }
  163. // showIssues := make([]models.Issue, 0, len(allIssues))
  164. // ctx.Data["IsShowClosed"] = isShowClosed
  165. // // Get posters and filter issues.
  166. // for i := range allIssues {
  167. // u, err := models.GetUserById(allIssues[i].PosterId)
  168. // if err != nil {
  169. // ctx.Handle(200, "user.Issues(get poster): %v", err)
  170. // return
  171. // }
  172. // allIssues[i].Poster = u
  173. // if u.Id == ctx.User.Id {
  174. // createdByCount++
  175. // }
  176. // if repoId > 0 && repoId != allIssues[i].Repo.Id {
  177. // continue
  178. // }
  179. // if isShowClosed == allIssues[i].IsClosed {
  180. // showIssues = append(showIssues, allIssues[i])
  181. // }
  182. // }
  183. ctx.Data["RepoId"] = rid
  184. ctx.Data["Repos"] = showRepos
  185. ctx.Data["ViewType"] = viewType
  186. ctx.Data["IssueStats"] = issueStats
  187. ctx.Data["IsShowClosed"] = isShowClosed
  188. if isShowClosed {
  189. ctx.Data["State"] = "closed"
  190. ctx.Data["ShowCount"] = issueStats.ClosedCount
  191. } else {
  192. ctx.Data["ShowCount"] = issueStats.OpenCount
  193. }
  194. ctx.HTML(200, "issue/user")
  195. }
  196. func Pulls(ctx *middleware.Context) {
  197. ctx.HTML(200, "user/pulls")
  198. }
  199. func Stars(ctx *middleware.Context) {
  200. ctx.HTML(200, "user/stars")
  201. }