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.

275 lines
6.7 KiB

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