home.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. uname := ctx.Params(":username")
  106. // Special handle for FireFox requests favicon.ico.
  107. if uname == "favicon.ico" {
  108. ctx.Redirect("/img/favicon.png")
  109. return
  110. }
  111. u, err := models.GetUserByName(uname)
  112. if err != nil {
  113. if err == models.ErrUserNotExist {
  114. ctx.Handle(404, "GetUserByName", err)
  115. } else {
  116. ctx.Handle(500, "GetUserByName", err)
  117. }
  118. return
  119. }
  120. if u.IsOrganization() {
  121. ctx.Redirect("/org/" + u.Name)
  122. return
  123. }
  124. // For security reason, hide e-mail address for anonymous visitors.
  125. if !ctx.IsSigned {
  126. u.Email = ""
  127. }
  128. ctx.Data["Owner"] = u
  129. tab := ctx.Query("tab")
  130. ctx.Data["TabName"] = tab
  131. switch tab {
  132. case "activity":
  133. ctx.Data["Feeds"], err = models.GetFeeds(u.Id, 0, true)
  134. if err != nil {
  135. ctx.Handle(500, "GetFeeds", err)
  136. return
  137. }
  138. default:
  139. ctx.Data["Repos"], err = models.GetRepositories(u.Id, ctx.IsSigned && ctx.User.Id == u.Id)
  140. if err != nil {
  141. ctx.Handle(500, "GetRepositories", err)
  142. return
  143. }
  144. }
  145. ctx.HTML(200, PROFILE)
  146. }
  147. func Email2User(ctx *middleware.Context) {
  148. u, err := models.GetUserByEmail(ctx.Query("email"))
  149. if err != nil {
  150. if err == models.ErrUserNotExist {
  151. ctx.Handle(404, "user.Email2User(GetUserByEmail)", err)
  152. } else {
  153. ctx.Handle(500, "user.Email2User(GetUserByEmail)", err)
  154. }
  155. return
  156. }
  157. ctx.Redirect("/user/" + u.Name)
  158. }
  159. const (
  160. TPL_FEED = `<i class="icon fa fa-%s"></i>
  161. <div class="info"><span class="meta">%s</span><br>%s</div>`
  162. )
  163. // func Feeds(ctx *middleware.Context, form auth.FeedsForm) {
  164. // actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
  165. // if err != nil {
  166. // ctx.JSON(500, err)
  167. // return
  168. // }
  169. // feeds := make([]string, 0, len(actions))
  170. // for _, act := range actions {
  171. // if act.IsPrivate {
  172. // if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName,
  173. // models.READABLE); !has {
  174. // continue
  175. // }
  176. // }
  177. // feeds = append(feeds, fmt.Sprintf(TPL_FEED, base.ActionIcon(act.OpType),
  178. // base.TimeSince(act.Created), base.ActionDesc(act)))
  179. // }
  180. // ctx.JSON(200, &feeds)
  181. // }
  182. func Issues(ctx *middleware.Context) {
  183. ctx.Data["Title"] = "Your Issues"
  184. viewType := ctx.Query("type")
  185. types := []string{"assigned", "created_by"}
  186. if !com.IsSliceContainsStr(types, viewType) {
  187. viewType = "all"
  188. }
  189. isShowClosed := ctx.Query("state") == "closed"
  190. var filterMode int
  191. switch viewType {
  192. case "assigned":
  193. filterMode = models.FM_ASSIGN
  194. case "created_by":
  195. filterMode = models.FM_CREATE
  196. }
  197. repoId, _ := com.StrTo(ctx.Query("repoid")).Int64()
  198. issueStats := models.GetUserIssueStats(ctx.User.Id, filterMode)
  199. // Get all repositories.
  200. repos, err := models.GetRepositories(ctx.User.Id, true)
  201. if err != nil {
  202. ctx.Handle(500, "user.Issues(GetRepositories)", err)
  203. return
  204. }
  205. repoIds := make([]int64, 0, len(repos))
  206. showRepos := make([]*models.Repository, 0, len(repos))
  207. for _, repo := range repos {
  208. if repo.NumIssues == 0 {
  209. continue
  210. }
  211. repoIds = append(repoIds, repo.Id)
  212. repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
  213. issueStats.AllCount += int64(repo.NumOpenIssues)
  214. if isShowClosed {
  215. if repo.NumClosedIssues > 0 {
  216. if filterMode == models.FM_CREATE {
  217. repo.NumClosedIssues = int(models.GetIssueCountByPoster(ctx.User.Id, repo.Id, isShowClosed))
  218. }
  219. showRepos = append(showRepos, repo)
  220. }
  221. } else {
  222. if repo.NumOpenIssues > 0 {
  223. if filterMode == models.FM_CREATE {
  224. repo.NumOpenIssues = int(models.GetIssueCountByPoster(ctx.User.Id, repo.Id, isShowClosed))
  225. }
  226. showRepos = append(showRepos, repo)
  227. }
  228. }
  229. }
  230. if repoId > 0 {
  231. repoIds = []int64{repoId}
  232. }
  233. page, _ := com.StrTo(ctx.Query("page")).Int()
  234. // Get all issues.
  235. var ius []*models.IssueUser
  236. switch viewType {
  237. case "assigned":
  238. fallthrough
  239. case "created_by":
  240. ius, err = models.GetIssueUserPairsByMode(ctx.User.Id, repoId, isShowClosed, page, filterMode)
  241. default:
  242. ius, err = models.GetIssueUserPairsByRepoIds(repoIds, isShowClosed, page)
  243. }
  244. if err != nil {
  245. ctx.Handle(500, "user.Issues(GetAllIssueUserPairs)", err)
  246. return
  247. }
  248. issues := make([]*models.Issue, len(ius))
  249. for i := range ius {
  250. issues[i], err = models.GetIssueById(ius[i].IssueId)
  251. if err != nil {
  252. if err == models.ErrIssueNotExist {
  253. log.Warn("user.Issues(GetIssueById #%d): issue not exist", ius[i].IssueId)
  254. continue
  255. } else {
  256. ctx.Handle(500, fmt.Sprintf("user.Issues(GetIssueById #%d)", ius[i].IssueId), err)
  257. return
  258. }
  259. }
  260. issues[i].Repo, err = models.GetRepositoryById(issues[i].RepoId)
  261. if err != nil {
  262. if err == models.ErrRepoNotExist {
  263. log.Warn("user.Issues(GetRepositoryById #%d): repository not exist", issues[i].RepoId)
  264. continue
  265. } else {
  266. ctx.Handle(500, fmt.Sprintf("user.Issues(GetRepositoryById #%d)", issues[i].RepoId), err)
  267. return
  268. }
  269. }
  270. if err = issues[i].Repo.GetOwner(); err != nil {
  271. ctx.Handle(500, "user.Issues(GetOwner)", err)
  272. return
  273. }
  274. if err = issues[i].GetPoster(); err != nil {
  275. ctx.Handle(500, "user.Issues(GetUserById)", err)
  276. return
  277. }
  278. }
  279. ctx.Data["RepoId"] = repoId
  280. ctx.Data["Repos"] = showRepos
  281. ctx.Data["Issues"] = issues
  282. ctx.Data["ViewType"] = viewType
  283. ctx.Data["IssueStats"] = issueStats
  284. ctx.Data["IsShowClosed"] = isShowClosed
  285. if isShowClosed {
  286. ctx.Data["State"] = "closed"
  287. ctx.Data["ShowCount"] = issueStats.ClosedCount
  288. } else {
  289. ctx.Data["ShowCount"] = issueStats.OpenCount
  290. }
  291. ctx.HTML(200, ISSUES)
  292. }