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.

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