home.go 6.8 KB

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