home.go 6.9 KB

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