home.go 9.2 KB

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