You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

home.go 7.1KB

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