home.go 7.2 KB

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