home.go 9.2 KB

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