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.

home.go 5.7KB

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