home.go 8.1 KB

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