home.go 7.1 KB

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