home.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. unameAvatars := make(map[string]string)
  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. // Cache results to reduce queries.
  109. _, ok := unameAvatars[act.ActUserName]
  110. if !ok {
  111. u, err := models.GetUserByName(act.ActUserName)
  112. if err != nil {
  113. if models.IsErrUserNotExist(err) {
  114. continue
  115. }
  116. ctx.Handle(500, "GetUserByName", err)
  117. return
  118. }
  119. unameAvatars[act.ActUserName] = u.AvatarLink()
  120. }
  121. act.ActAvatar = unameAvatars[act.ActUserName]
  122. feeds = append(feeds, act)
  123. }
  124. ctx.Data["Feeds"] = feeds
  125. ctx.HTML(200, DASHBOARD)
  126. }
  127. func Issues(ctx *middleware.Context) {
  128. isPullList := ctx.Params(":type") == "pulls"
  129. if isPullList {
  130. ctx.Data["Title"] = ctx.Tr("pull_requests")
  131. ctx.Data["PageIsPulls"] = true
  132. } else {
  133. ctx.Data["Title"] = ctx.Tr("issues")
  134. ctx.Data["PageIsIssues"] = true
  135. }
  136. ctxUser := getDashboardContextUser(ctx)
  137. if ctx.Written() {
  138. return
  139. }
  140. // Organization does not have view type and filter mode.
  141. var (
  142. viewType string
  143. sortType = ctx.Query("sort")
  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 (isPullList && repo.NumPulls == 0) ||
  178. (!isPullList && repo.NumIssues == 0) {
  179. continue
  180. }
  181. repoIDs = append(repoIDs, repo.ID)
  182. if isPullList {
  183. allCount += repo.NumOpenPulls
  184. repo.NumOpenIssues = repo.NumOpenPulls
  185. repo.NumClosedIssues = repo.NumClosedPulls
  186. } else {
  187. allCount += repo.NumOpenIssues
  188. }
  189. if filterMode != models.FM_ALL {
  190. // Calculate repository issue count with filter mode.
  191. numOpen, numClosed := repo.IssueStats(ctxUser.Id, filterMode, isPullList)
  192. repo.NumOpenIssues, repo.NumClosedIssues = int(numOpen), int(numClosed)
  193. }
  194. if repo.ID == repoID ||
  195. (isShowClosed && repo.NumClosedIssues > 0) ||
  196. (!isShowClosed && repo.NumOpenIssues > 0) {
  197. showRepos = append(showRepos, repo)
  198. }
  199. }
  200. ctx.Data["Repos"] = showRepos
  201. issueStats := models.GetUserIssueStats(repoID, ctxUser.Id, repoIDs, filterMode, isPullList)
  202. issueStats.AllCount = int64(allCount)
  203. page := ctx.QueryInt("page")
  204. if page <= 1 {
  205. page = 1
  206. }
  207. var total int
  208. if !isShowClosed {
  209. total = int(issueStats.OpenCount)
  210. } else {
  211. total = int(issueStats.ClosedCount)
  212. }
  213. ctx.Data["Page"] = paginater.New(total, setting.IssuePagingNum, page, 5)
  214. // Get issues.
  215. issues, err := models.Issues(&models.IssuesOptions{
  216. UserID: ctxUser.Id,
  217. AssigneeID: assigneeID,
  218. RepoID: repoID,
  219. PosterID: posterID,
  220. RepoIDs: repoIDs,
  221. Page: page,
  222. IsClosed: isShowClosed,
  223. IsPull: isPullList,
  224. SortType: sortType,
  225. })
  226. if err != nil {
  227. ctx.Handle(500, "Issues: %v", err)
  228. return
  229. }
  230. // Get posters and repository.
  231. for i := range issues {
  232. issues[i].Repo, err = models.GetRepositoryByID(issues[i].RepoID)
  233. if err != nil {
  234. ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d]%v", issues[i].ID, err))
  235. return
  236. }
  237. if err = issues[i].Repo.GetOwner(); err != nil {
  238. ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d]%v", issues[i].ID, err))
  239. return
  240. }
  241. if err = issues[i].GetPoster(); err != nil {
  242. ctx.Handle(500, "GetPoster", fmt.Errorf("[#%d]%v", issues[i].ID, err))
  243. return
  244. }
  245. }
  246. ctx.Data["Issues"] = issues
  247. ctx.Data["IssueStats"] = issueStats
  248. ctx.Data["ViewType"] = viewType
  249. ctx.Data["SortType"] = sortType
  250. ctx.Data["RepoID"] = repoID
  251. ctx.Data["IsShowClosed"] = isShowClosed
  252. if isShowClosed {
  253. ctx.Data["State"] = "closed"
  254. } else {
  255. ctx.Data["State"] = "open"
  256. }
  257. ctx.HTML(200, ISSUES)
  258. }
  259. func ShowSSHKeys(ctx *middleware.Context, uid int64) {
  260. keys, err := models.ListPublicKeys(uid)
  261. if err != nil {
  262. ctx.Handle(500, "ListPublicKeys", err)
  263. return
  264. }
  265. var buf bytes.Buffer
  266. for i := range keys {
  267. buf.WriteString(keys[i].OmitEmail())
  268. buf.WriteString("\n")
  269. }
  270. ctx.PlainText(200, buf.Bytes())
  271. }
  272. func Profile(ctx *middleware.Context) {
  273. ctx.Data["Title"] = "Profile"
  274. ctx.Data["PageIsUserProfile"] = true
  275. uname := ctx.Params(":username")
  276. // Special handle for FireFox requests favicon.ico.
  277. if uname == "favicon.ico" {
  278. ctx.Redirect(setting.AppSubUrl + "/img/favicon.png")
  279. return
  280. }
  281. isShowKeys := false
  282. if strings.HasSuffix(uname, ".keys") {
  283. isShowKeys = true
  284. uname = strings.TrimSuffix(uname, ".keys")
  285. }
  286. u, err := models.GetUserByName(uname)
  287. if err != nil {
  288. if models.IsErrUserNotExist(err) {
  289. ctx.Handle(404, "GetUserByName", err)
  290. } else {
  291. ctx.Handle(500, "GetUserByName", err)
  292. }
  293. return
  294. }
  295. // Show SSH keys.
  296. if isShowKeys {
  297. ShowSSHKeys(ctx, u.Id)
  298. return
  299. }
  300. if u.IsOrganization() {
  301. ctx.Redirect(setting.AppSubUrl + "/org/" + u.Name)
  302. return
  303. }
  304. ctx.Data["Owner"] = u
  305. tab := ctx.Query("tab")
  306. ctx.Data["TabName"] = tab
  307. switch tab {
  308. case "activity":
  309. actions, err := models.GetFeeds(u.Id, 0, false)
  310. if err != nil {
  311. ctx.Handle(500, "GetFeeds", err)
  312. return
  313. }
  314. feeds := make([]*models.Action, 0, len(actions))
  315. for _, act := range actions {
  316. if act.IsPrivate {
  317. if !ctx.IsSigned {
  318. continue
  319. }
  320. // This prevents having to retrieve the repository for each action
  321. repo := &models.Repository{ID: act.RepoID, IsPrivate: true}
  322. if act.RepoUserName != ctx.User.LowerName {
  323. if has, _ := models.HasAccess(ctx.User, repo, models.ACCESS_MODE_READ); !has {
  324. continue
  325. }
  326. }
  327. }
  328. // FIXME: cache results?
  329. u, err := models.GetUserByName(act.ActUserName)
  330. if err != nil {
  331. if models.IsErrUserNotExist(err) {
  332. continue
  333. }
  334. ctx.Handle(500, "GetUserByName", err)
  335. return
  336. }
  337. act.ActAvatar = u.AvatarLink()
  338. feeds = append(feeds, act)
  339. }
  340. ctx.Data["Feeds"] = feeds
  341. default:
  342. ctx.Data["Repos"], err = models.GetRepositories(u.Id, ctx.IsSigned && ctx.User.Id == u.Id)
  343. if err != nil {
  344. ctx.Handle(500, "GetRepositories", err)
  345. return
  346. }
  347. }
  348. ctx.HTML(200, PROFILE)
  349. }
  350. func Email2User(ctx *middleware.Context) {
  351. u, err := models.GetUserByEmail(ctx.Query("email"))
  352. if err != nil {
  353. if models.IsErrUserNotExist(err) {
  354. ctx.Handle(404, "GetUserByEmail", err)
  355. } else {
  356. ctx.Handle(500, "GetUserByEmail", err)
  357. }
  358. return
  359. }
  360. ctx.Redirect(setting.AppSubUrl + "/user/" + u.Name)
  361. }