home.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. var err error
  19. ctx.Data["MyRepos"], err = models.GetRepositories(ctx.User.Id, true)
  20. if err != nil {
  21. ctx.Handle(500, "home.Dashboard(GetRepositories)", err)
  22. return
  23. }
  24. ctx.Data["CollaborativeRepos"], err = models.GetCollaborativeRepos(ctx.User.Name)
  25. if err != nil {
  26. ctx.Handle(500, "home.Dashboard(GetCollaborativeRepos)", err)
  27. return
  28. }
  29. actions, err := models.GetFeeds(ctx.User.Id, 0, false)
  30. if err != nil {
  31. ctx.Handle(500, "home.Dashboard(GetFeeds)", err)
  32. return
  33. }
  34. // Check access of private repositories.
  35. feeds := make([]*models.Action, 0, len(actions))
  36. for _, act := range actions {
  37. if act.IsPrivate {
  38. if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName,
  39. models.AU_READABLE); !has {
  40. continue
  41. }
  42. }
  43. feeds = append(feeds, act)
  44. }
  45. ctx.Data["Feeds"] = feeds
  46. ctx.HTML(200, "user/dashboard")
  47. }
  48. func Profile(ctx *middleware.Context, params martini.Params) {
  49. ctx.Data["Title"] = "Profile"
  50. ctx.Data["PageIsUserProfile"] = true
  51. user, err := models.GetUserByName(params["username"])
  52. if err != nil {
  53. if err == models.ErrUserNotExist {
  54. ctx.Handle(404, "user.Profile(GetUserByName)", err)
  55. } else {
  56. ctx.Handle(500, "user.Profile(GetUserByName)", err)
  57. }
  58. return
  59. }
  60. ctx.Data["Owner"] = user
  61. tab := ctx.Query("tab")
  62. ctx.Data["TabName"] = tab
  63. switch tab {
  64. case "activity":
  65. ctx.Data["Feeds"], err = models.GetFeeds(user.Id, 0, true)
  66. if err != nil {
  67. ctx.Handle(500, "user.Profile(GetFeeds)", err)
  68. return
  69. }
  70. default:
  71. ctx.Data["Repos"], err = models.GetRepositories(user.Id, ctx.IsSigned && ctx.User.Id == user.Id)
  72. if err != nil {
  73. ctx.Handle(500, "user.Profile(GetRepositories)", err)
  74. return
  75. }
  76. }
  77. ctx.HTML(200, "user/profile")
  78. }
  79. func Email2User(ctx *middleware.Context) {
  80. u, err := models.GetUserByEmail(ctx.Query("email"))
  81. if err != nil {
  82. if err == models.ErrUserNotExist {
  83. ctx.Handle(404, "user.Email2User(GetUserByEmail)", err)
  84. } else {
  85. ctx.Handle(500, "user.Email2User(GetUserByEmail)", err)
  86. }
  87. return
  88. }
  89. ctx.Redirect("/user/" + u.Name)
  90. }
  91. const (
  92. TPL_FEED = `<i class="icon fa fa-%s"></i>
  93. <div class="info"><span class="meta">%s</span><br>%s</div>`
  94. )
  95. func Feeds(ctx *middleware.Context, form auth.FeedsForm) {
  96. actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
  97. if err != nil {
  98. ctx.JSON(500, err)
  99. return
  100. }
  101. feeds := make([]string, 0, len(actions))
  102. for _, act := range actions {
  103. if act.IsPrivate {
  104. if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName,
  105. models.AU_READABLE); !has {
  106. continue
  107. }
  108. }
  109. feeds = append(feeds, fmt.Sprintf(TPL_FEED, base.ActionIcon(act.OpType),
  110. base.TimeSince(act.Created), base.ActionDesc(act)))
  111. }
  112. ctx.JSON(200, &feeds)
  113. }
  114. func Issues(ctx *middleware.Context) {
  115. ctx.Data["Title"] = "Your Issues"
  116. viewType := ctx.Query("type")
  117. types := []string{"assigned", "created_by"}
  118. if !com.IsSliceContainsStr(types, viewType) {
  119. viewType = "all"
  120. }
  121. isShowClosed := ctx.Query("state") == "closed"
  122. var filterMode int
  123. switch viewType {
  124. case "assigned":
  125. filterMode = models.FM_ASSIGN
  126. case "created_by":
  127. filterMode = models.FM_CREATE
  128. }
  129. repoId, _ := 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.Id, true)
  133. if err != nil {
  134. ctx.Handle(500, "user.Issues(GetRepositories)", err)
  135. return
  136. }
  137. repoIds := 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. repoIds = append(repoIds, 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 repoId > 0 {
  163. repoIds = []int64{repoId}
  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, repoId, isShowClosed, page, filterMode)
  173. default:
  174. ius, err = models.GetIssueUserPairsByRepoIds(repoIds, 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.Warn("user.Issues(GetIssueById #%d): issue not exist", ius[i].IssueId)
  186. continue
  187. } else {
  188. ctx.Handle(500, fmt.Sprintf("user.Issues(GetIssueById #%d)", ius[i].IssueId), err)
  189. return
  190. }
  191. }
  192. issues[i].Repo, err = models.GetRepositoryById(issues[i].RepoId)
  193. if err != nil {
  194. if err == models.ErrRepoNotExist {
  195. log.Warn("user.Issues(GetRepositoryById #%d): repository not exist", issues[i].RepoId)
  196. continue
  197. } else {
  198. ctx.Handle(500, fmt.Sprintf("user.Issues(GetRepositoryById #%d)", issues[i].RepoId), err)
  199. return
  200. }
  201. }
  202. if err = issues[i].Repo.GetOwner(); err != nil {
  203. ctx.Handle(500, "user.Issues(GetOwner)", err)
  204. return
  205. }
  206. if err = issues[i].GetPoster(); err != nil {
  207. ctx.Handle(500, "user.Issues(GetUserById)", err)
  208. return
  209. }
  210. }
  211. ctx.Data["RepoId"] = repoId
  212. ctx.Data["Repos"] = showRepos
  213. ctx.Data["Issues"] = issues
  214. ctx.Data["ViewType"] = viewType
  215. ctx.Data["IssueStats"] = issueStats
  216. ctx.Data["IsShowClosed"] = isShowClosed
  217. if isShowClosed {
  218. ctx.Data["State"] = "closed"
  219. ctx.Data["ShowCount"] = issueStats.ClosedCount
  220. } else {
  221. ctx.Data["ShowCount"] = issueStats.OpenCount
  222. }
  223. ctx.HTML(200, "user/issue")
  224. }
  225. func Pulls(ctx *middleware.Context) {
  226. ctx.HTML(200, "user/pulls")
  227. }
  228. func Stars(ctx *middleware.Context) {
  229. ctx.HTML(200, "user/stars")
  230. }