home.go 7.2 KB

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