home.go 8.9 KB

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