home.go 6.2 KB

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