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