home.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. "github.com/Unknwon/com"
  9. "github.com/Unknwon/paginater"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/models/errors"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/context"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. const (
  17. DASHBOARD base.TplName = "user/dashboard/dashboard"
  18. NEWS_FEED base.TplName = "user/dashboard/feeds"
  19. ISSUES base.TplName = "user/dashboard/issues"
  20. PROFILE base.TplName = "user/profile"
  21. ORG_HOME base.TplName = "org/home"
  22. )
  23. // getDashboardContextUser finds out dashboard is viewing as which context user.
  24. func getDashboardContextUser(ctx *context.Context) *models.User {
  25. ctxUser := ctx.User
  26. orgName := ctx.Params(":org")
  27. if len(orgName) > 0 {
  28. // Organization.
  29. org, err := models.GetUserByName(orgName)
  30. if err != nil {
  31. ctx.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  32. return nil
  33. }
  34. ctxUser = org
  35. }
  36. ctx.Data["ContextUser"] = ctxUser
  37. if err := ctx.User.GetOrganizations(true); err != nil {
  38. ctx.Handle(500, "GetOrganizations", err)
  39. return nil
  40. }
  41. ctx.Data["Orgs"] = ctx.User.Orgs
  42. return ctxUser
  43. }
  44. // retrieveFeeds loads feeds from database by given context user.
  45. // The user could be organization so it is not always the logged in user,
  46. // which is why we have to explicitly pass the context user ID.
  47. func retrieveFeeds(ctx *context.Context, ctxUser *models.User, userID int64, isProfile bool) {
  48. actions, err := models.GetFeeds(ctxUser, userID, ctx.QueryInt64("after_id"), isProfile)
  49. if err != nil {
  50. ctx.Handle(500, "GetFeeds", err)
  51. return
  52. }
  53. // Check access of private repositories.
  54. feeds := make([]*models.Action, 0, len(actions))
  55. unameAvatars := make(map[string]string)
  56. for _, act := range actions {
  57. // Cache results to reduce queries.
  58. _, ok := unameAvatars[act.ActUserName]
  59. if !ok {
  60. u, err := models.GetUserByName(act.ActUserName)
  61. if err != nil {
  62. if errors.IsUserNotExist(err) {
  63. continue
  64. }
  65. ctx.Handle(500, "GetUserByName", err)
  66. return
  67. }
  68. unameAvatars[act.ActUserName] = u.RelAvatarLink()
  69. }
  70. act.ActAvatar = unameAvatars[act.ActUserName]
  71. feeds = append(feeds, act)
  72. }
  73. ctx.Data["Feeds"] = feeds
  74. if len(feeds) > 0 {
  75. afterID := feeds[len(feeds)-1].ID
  76. ctx.Data["AfterID"] = afterID
  77. ctx.Header().Set("X-AJAX-URL", fmt.Sprintf("%s?after_id=%d", ctx.Data["Link"], afterID))
  78. }
  79. }
  80. func Dashboard(ctx *context.Context) {
  81. ctxUser := getDashboardContextUser(ctx)
  82. if ctx.Written() {
  83. return
  84. }
  85. retrieveFeeds(ctx, ctxUser, ctx.User.ID, false)
  86. if ctx.Written() {
  87. return
  88. }
  89. if ctx.Req.Header.Get("X-AJAX") == "true" {
  90. ctx.HTML(200, NEWS_FEED)
  91. return
  92. }
  93. ctx.Data["Title"] = ctxUser.DisplayName() + " - " + ctx.Tr("dashboard")
  94. ctx.Data["PageIsDashboard"] = true
  95. ctx.Data["PageIsNews"] = true
  96. // Only user can have collaborative repositories.
  97. if !ctxUser.IsOrganization() {
  98. collaborateRepos, err := ctx.User.GetAccessibleRepositories(setting.UI.User.RepoPagingNum)
  99. if err != nil {
  100. ctx.Handle(500, "GetAccessibleRepositories", err)
  101. return
  102. } else if err = models.RepositoryList(collaborateRepos).LoadAttributes(); err != nil {
  103. ctx.Handle(500, "RepositoryList.LoadAttributes", err)
  104. return
  105. }
  106. ctx.Data["CollaborativeRepos"] = collaborateRepos
  107. }
  108. var err error
  109. var repos, mirrors []*models.Repository
  110. var repoCount int64
  111. if ctxUser.IsOrganization() {
  112. repos, repoCount, err = ctxUser.GetUserRepositories(ctx.User.ID, 1, setting.UI.User.RepoPagingNum)
  113. if err != nil {
  114. ctx.Handle(500, "GetUserRepositories", err)
  115. return
  116. }
  117. mirrors, err = ctxUser.GetUserMirrorRepositories(ctx.User.ID)
  118. if err != nil {
  119. ctx.Handle(500, "GetUserMirrorRepositories", err)
  120. return
  121. }
  122. } else {
  123. if err = ctxUser.GetRepositories(1, setting.UI.User.RepoPagingNum); err != nil {
  124. ctx.Handle(500, "GetRepositories", err)
  125. return
  126. }
  127. repos = ctxUser.Repos
  128. repoCount = int64(ctxUser.NumRepos)
  129. mirrors, err = ctxUser.GetMirrorRepositories()
  130. if err != nil {
  131. ctx.Handle(500, "GetMirrorRepositories", err)
  132. return
  133. }
  134. }
  135. ctx.Data["Repos"] = repos
  136. ctx.Data["RepoCount"] = repoCount
  137. ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum
  138. if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil {
  139. ctx.Handle(500, "MirrorRepositoryList.LoadAttributes", err)
  140. return
  141. }
  142. ctx.Data["MirrorCount"] = len(mirrors)
  143. ctx.Data["Mirrors"] = mirrors
  144. ctx.HTML(200, DASHBOARD)
  145. }
  146. func Issues(ctx *context.Context) {
  147. isPullList := ctx.Params(":type") == "pulls"
  148. if isPullList {
  149. ctx.Data["Title"] = ctx.Tr("pull_requests")
  150. ctx.Data["PageIsPulls"] = true
  151. } else {
  152. ctx.Data["Title"] = ctx.Tr("issues")
  153. ctx.Data["PageIsIssues"] = true
  154. }
  155. ctxUser := getDashboardContextUser(ctx)
  156. if ctx.Written() {
  157. return
  158. }
  159. var (
  160. sortType = ctx.Query("sort")
  161. filterMode = models.FILTER_MODE_YOUR_REPOS
  162. )
  163. // Note: Organization does not have view type and filter mode.
  164. if !ctxUser.IsOrganization() {
  165. viewType := ctx.Query("type")
  166. types := []string{
  167. string(models.FILTER_MODE_YOUR_REPOS),
  168. string(models.FILTER_MODE_ASSIGN),
  169. string(models.FILTER_MODE_CREATE),
  170. }
  171. if !com.IsSliceContainsStr(types, viewType) {
  172. viewType = string(models.FILTER_MODE_YOUR_REPOS)
  173. }
  174. filterMode = models.FilterMode(viewType)
  175. }
  176. page := ctx.QueryInt("page")
  177. if page <= 1 {
  178. page = 1
  179. }
  180. repoID := ctx.QueryInt64("repo")
  181. isShowClosed := ctx.Query("state") == "closed"
  182. // Get repositories.
  183. var (
  184. err error
  185. repos []*models.Repository
  186. userRepoIDs []int64
  187. showRepos = make([]*models.Repository, 0, 10)
  188. )
  189. if ctxUser.IsOrganization() {
  190. repos, _, err = ctxUser.GetUserRepositories(ctx.User.ID, 1, ctxUser.NumRepos)
  191. if err != nil {
  192. ctx.Handle(500, "GetRepositories", err)
  193. return
  194. }
  195. } else {
  196. if err := ctxUser.GetRepositories(1, ctx.User.NumRepos); err != nil {
  197. ctx.Handle(500, "GetRepositories", err)
  198. return
  199. }
  200. repos = ctxUser.Repos
  201. }
  202. userRepoIDs = make([]int64, 0, len(repos))
  203. for _, repo := range repos {
  204. userRepoIDs = append(userRepoIDs, repo.ID)
  205. if filterMode != models.FILTER_MODE_YOUR_REPOS {
  206. continue
  207. }
  208. if isPullList {
  209. if isShowClosed && repo.NumClosedPulls == 0 ||
  210. !isShowClosed && repo.NumOpenPulls == 0 {
  211. continue
  212. }
  213. } else {
  214. if !repo.EnableIssues || repo.EnableExternalTracker ||
  215. isShowClosed && repo.NumClosedIssues == 0 ||
  216. !isShowClosed && repo.NumOpenIssues == 0 {
  217. continue
  218. }
  219. }
  220. showRepos = append(showRepos, repo)
  221. }
  222. // Filter repositories if the page shows issues.
  223. if !isPullList {
  224. userRepoIDs, err = models.FilterRepositoryWithIssues(userRepoIDs)
  225. if err != nil {
  226. ctx.Handle(500, "FilterRepositoryWithIssues", err)
  227. return
  228. }
  229. }
  230. issueOptions := &models.IssuesOptions{
  231. RepoID: repoID,
  232. Page: page,
  233. IsClosed: isShowClosed,
  234. IsPull: isPullList,
  235. SortType: sortType,
  236. }
  237. switch filterMode {
  238. case models.FILTER_MODE_YOUR_REPOS:
  239. // Get all issues from repositories from this user.
  240. if userRepoIDs == nil {
  241. issueOptions.RepoIDs = []int64{-1}
  242. } else {
  243. issueOptions.RepoIDs = userRepoIDs
  244. }
  245. case models.FILTER_MODE_ASSIGN:
  246. // Get all issues assigned to this user.
  247. issueOptions.AssigneeID = ctxUser.ID
  248. case models.FILTER_MODE_CREATE:
  249. // Get all issues created by this user.
  250. issueOptions.PosterID = ctxUser.ID
  251. }
  252. issues, err := models.Issues(issueOptions)
  253. if err != nil {
  254. ctx.Handle(500, "Issues", err)
  255. return
  256. }
  257. if repoID > 0 {
  258. repo, err := models.GetRepositoryByID(repoID)
  259. if err != nil {
  260. ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d] %v", repoID, err))
  261. return
  262. }
  263. if err = repo.GetOwner(); err != nil {
  264. ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", repoID, err))
  265. return
  266. }
  267. // Check if user has access to given repository.
  268. if !repo.IsOwnedBy(ctxUser.ID) && !repo.HasAccess(ctxUser.ID) {
  269. ctx.Handle(404, "Issues", fmt.Errorf("#%d", repoID))
  270. return
  271. }
  272. }
  273. for _, issue := range issues {
  274. if err = issue.Repo.GetOwner(); err != nil {
  275. ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", issue.RepoID, err))
  276. return
  277. }
  278. }
  279. issueStats := models.GetUserIssueStats(repoID, ctxUser.ID, userRepoIDs, filterMode, isPullList)
  280. var total int
  281. if !isShowClosed {
  282. total = int(issueStats.OpenCount)
  283. } else {
  284. total = int(issueStats.ClosedCount)
  285. }
  286. ctx.Data["Issues"] = issues
  287. ctx.Data["Repos"] = showRepos
  288. ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  289. ctx.Data["IssueStats"] = issueStats
  290. ctx.Data["ViewType"] = string(filterMode)
  291. ctx.Data["SortType"] = sortType
  292. ctx.Data["RepoID"] = repoID
  293. ctx.Data["IsShowClosed"] = isShowClosed
  294. if isShowClosed {
  295. ctx.Data["State"] = "closed"
  296. } else {
  297. ctx.Data["State"] = "open"
  298. }
  299. ctx.HTML(200, ISSUES)
  300. }
  301. func ShowSSHKeys(ctx *context.Context, uid int64) {
  302. keys, err := models.ListPublicKeys(uid)
  303. if err != nil {
  304. ctx.Handle(500, "ListPublicKeys", err)
  305. return
  306. }
  307. var buf bytes.Buffer
  308. for i := range keys {
  309. buf.WriteString(keys[i].OmitEmail())
  310. buf.WriteString("\n")
  311. }
  312. ctx.PlainText(200, buf.Bytes())
  313. }
  314. func showOrgProfile(ctx *context.Context) {
  315. ctx.SetParams(":org", ctx.Params(":username"))
  316. context.HandleOrgAssignment(ctx)
  317. if ctx.Written() {
  318. return
  319. }
  320. org := ctx.Org.Organization
  321. ctx.Data["Title"] = org.FullName
  322. page := ctx.QueryInt("page")
  323. if page <= 0 {
  324. page = 1
  325. }
  326. var (
  327. repos []*models.Repository
  328. count int64
  329. err error
  330. )
  331. if ctx.IsSigned && !ctx.User.IsAdmin {
  332. repos, count, err = org.GetUserRepositories(ctx.User.ID, page, setting.UI.User.RepoPagingNum)
  333. if err != nil {
  334. ctx.Handle(500, "GetUserRepositories", err)
  335. return
  336. }
  337. ctx.Data["Repos"] = repos
  338. } else {
  339. showPrivate := ctx.IsSigned && ctx.User.IsAdmin
  340. repos, err = models.GetUserRepositories(&models.UserRepoOptions{
  341. UserID: org.ID,
  342. Private: showPrivate,
  343. Page: page,
  344. PageSize: setting.UI.User.RepoPagingNum,
  345. })
  346. if err != nil {
  347. ctx.Handle(500, "GetRepositories", err)
  348. return
  349. }
  350. ctx.Data["Repos"] = repos
  351. count = models.CountUserRepositories(org.ID, showPrivate)
  352. }
  353. ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  354. if err := org.GetMembers(); err != nil {
  355. ctx.Handle(500, "GetMembers", err)
  356. return
  357. }
  358. ctx.Data["Members"] = org.Members
  359. ctx.Data["Teams"] = org.Teams
  360. ctx.HTML(200, ORG_HOME)
  361. }
  362. func Email2User(ctx *context.Context) {
  363. u, err := models.GetUserByEmail(ctx.Query("email"))
  364. if err != nil {
  365. ctx.NotFoundOrServerError("GetUserByEmail", errors.IsUserNotExist, err)
  366. return
  367. }
  368. ctx.Redirect(setting.AppSubUrl + "/user/" + u.Name)
  369. }