home.go 9.4 KB

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