home.go 7.2 KB

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