home.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. if ctxUser.IsOrganization() {
  111. repos, _, 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. mirrors, err = ctxUser.GetMirrorRepositories()
  128. if err != nil {
  129. ctx.Handle(500, "GetMirrorRepositories", err)
  130. return
  131. }
  132. }
  133. ctx.Data["Repos"] = repos
  134. ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum
  135. if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil {
  136. ctx.Handle(500, "MirrorRepositoryList.LoadAttributes", err)
  137. return
  138. }
  139. ctx.Data["MirrorCount"] = len(mirrors)
  140. ctx.Data["Mirrors"] = mirrors
  141. ctx.HTML(200, DASHBOARD)
  142. }
  143. func Issues(ctx *context.Context) {
  144. isPullList := ctx.Params(":type") == "pulls"
  145. if isPullList {
  146. ctx.Data["Title"] = ctx.Tr("pull_requests")
  147. ctx.Data["PageIsPulls"] = true
  148. } else {
  149. ctx.Data["Title"] = ctx.Tr("issues")
  150. ctx.Data["PageIsIssues"] = true
  151. }
  152. ctxUser := getDashboardContextUser(ctx)
  153. if ctx.Written() {
  154. return
  155. }
  156. var (
  157. sortType = ctx.Query("sort")
  158. filterMode = models.FILTER_MODE_YOUR_REPOS
  159. )
  160. // Note: Organization does not have view type and filter mode.
  161. if !ctxUser.IsOrganization() {
  162. viewType := ctx.Query("type")
  163. types := []string{
  164. string(models.FILTER_MODE_YOUR_REPOS),
  165. string(models.FILTER_MODE_ASSIGN),
  166. string(models.FILTER_MODE_CREATE),
  167. }
  168. if !com.IsSliceContainsStr(types, viewType) {
  169. viewType = string(models.FILTER_MODE_YOUR_REPOS)
  170. }
  171. filterMode = models.FilterMode(viewType)
  172. }
  173. page := ctx.QueryInt("page")
  174. if page <= 1 {
  175. page = 1
  176. }
  177. repoID := ctx.QueryInt64("repo")
  178. isShowClosed := ctx.Query("state") == "closed"
  179. // Get repositories.
  180. var (
  181. err error
  182. repos []*models.Repository
  183. userRepoIDs []int64
  184. showRepos = make([]*models.Repository, 0, 10)
  185. )
  186. if ctxUser.IsOrganization() {
  187. repos, _, err = ctxUser.GetUserRepositories(ctx.User.ID, 1, ctxUser.NumRepos)
  188. if err != nil {
  189. ctx.Handle(500, "GetRepositories", err)
  190. return
  191. }
  192. } else {
  193. if err := ctxUser.GetRepositories(1, ctx.User.NumRepos); err != nil {
  194. ctx.Handle(500, "GetRepositories", err)
  195. return
  196. }
  197. repos = ctxUser.Repos
  198. }
  199. userRepoIDs = make([]int64, 0, len(repos))
  200. for _, repo := range repos {
  201. userRepoIDs = append(userRepoIDs, repo.ID)
  202. if filterMode != models.FILTER_MODE_YOUR_REPOS {
  203. continue
  204. }
  205. if isPullList {
  206. if isShowClosed && repo.NumClosedPulls == 0 ||
  207. !isShowClosed && repo.NumOpenPulls == 0 {
  208. continue
  209. }
  210. } else {
  211. if !repo.EnableIssues || repo.EnableExternalTracker ||
  212. isShowClosed && repo.NumClosedIssues == 0 ||
  213. !isShowClosed && repo.NumOpenIssues == 0 {
  214. continue
  215. }
  216. }
  217. showRepos = append(showRepos, repo)
  218. }
  219. // Filter repositories if the page shows issues.
  220. if !isPullList {
  221. userRepoIDs, err = models.FilterRepositoryWithIssues(userRepoIDs)
  222. if err != nil {
  223. ctx.Handle(500, "FilterRepositoryWithIssues", err)
  224. return
  225. }
  226. }
  227. issueOptions := &models.IssuesOptions{
  228. RepoID: repoID,
  229. Page: page,
  230. IsClosed: isShowClosed,
  231. IsPull: isPullList,
  232. SortType: sortType,
  233. }
  234. switch filterMode {
  235. case models.FILTER_MODE_YOUR_REPOS:
  236. // Get all issues from repositories from this user.
  237. if userRepoIDs == nil {
  238. issueOptions.RepoIDs = []int64{-1}
  239. } else {
  240. issueOptions.RepoIDs = userRepoIDs
  241. }
  242. case models.FILTER_MODE_ASSIGN:
  243. // Get all issues assigned to this user.
  244. issueOptions.AssigneeID = ctxUser.ID
  245. case models.FILTER_MODE_CREATE:
  246. // Get all issues created by this user.
  247. issueOptions.PosterID = ctxUser.ID
  248. }
  249. issues, err := models.Issues(issueOptions)
  250. if err != nil {
  251. ctx.Handle(500, "Issues", err)
  252. return
  253. }
  254. if repoID > 0 {
  255. repo, err := models.GetRepositoryByID(repoID)
  256. if err != nil {
  257. ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d] %v", repoID, err))
  258. return
  259. }
  260. if err = repo.GetOwner(); err != nil {
  261. ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", repoID, err))
  262. return
  263. }
  264. // Check if user has access to given repository.
  265. if !repo.IsOwnedBy(ctxUser.ID) && !repo.HasAccess(ctxUser.ID) {
  266. ctx.Handle(404, "Issues", fmt.Errorf("#%d", repoID))
  267. return
  268. }
  269. }
  270. for _, issue := range issues {
  271. if err = issue.Repo.GetOwner(); err != nil {
  272. ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", issue.RepoID, err))
  273. return
  274. }
  275. }
  276. issueStats := models.GetUserIssueStats(repoID, ctxUser.ID, userRepoIDs, filterMode, isPullList)
  277. var total int
  278. if !isShowClosed {
  279. total = int(issueStats.OpenCount)
  280. } else {
  281. total = int(issueStats.ClosedCount)
  282. }
  283. ctx.Data["Issues"] = issues
  284. ctx.Data["Repos"] = showRepos
  285. ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  286. ctx.Data["IssueStats"] = issueStats
  287. ctx.Data["ViewType"] = string(filterMode)
  288. ctx.Data["SortType"] = sortType
  289. ctx.Data["RepoID"] = repoID
  290. ctx.Data["IsShowClosed"] = isShowClosed
  291. if isShowClosed {
  292. ctx.Data["State"] = "closed"
  293. } else {
  294. ctx.Data["State"] = "open"
  295. }
  296. ctx.HTML(200, ISSUES)
  297. }
  298. func ShowSSHKeys(ctx *context.Context, uid int64) {
  299. keys, err := models.ListPublicKeys(uid)
  300. if err != nil {
  301. ctx.Handle(500, "ListPublicKeys", err)
  302. return
  303. }
  304. var buf bytes.Buffer
  305. for i := range keys {
  306. buf.WriteString(keys[i].OmitEmail())
  307. buf.WriteString("\n")
  308. }
  309. ctx.PlainText(200, buf.Bytes())
  310. }
  311. func showOrgProfile(ctx *context.Context) {
  312. ctx.SetParams(":org", ctx.Params(":username"))
  313. context.HandleOrgAssignment(ctx)
  314. if ctx.Written() {
  315. return
  316. }
  317. org := ctx.Org.Organization
  318. ctx.Data["Title"] = org.FullName
  319. page := ctx.QueryInt("page")
  320. if page <= 0 {
  321. page = 1
  322. }
  323. var (
  324. repos []*models.Repository
  325. count int64
  326. err error
  327. )
  328. if ctx.IsSigned && !ctx.User.IsAdmin {
  329. repos, count, err = org.GetUserRepositories(ctx.User.ID, page, setting.UI.User.RepoPagingNum)
  330. if err != nil {
  331. ctx.Handle(500, "GetUserRepositories", err)
  332. return
  333. }
  334. ctx.Data["Repos"] = repos
  335. } else {
  336. showPrivate := ctx.IsSigned && ctx.User.IsAdmin
  337. repos, err = models.GetUserRepositories(&models.UserRepoOptions{
  338. UserID: org.ID,
  339. Private: showPrivate,
  340. Page: page,
  341. PageSize: setting.UI.User.RepoPagingNum,
  342. })
  343. if err != nil {
  344. ctx.Handle(500, "GetRepositories", err)
  345. return
  346. }
  347. ctx.Data["Repos"] = repos
  348. count = models.CountUserRepositories(org.ID, showPrivate)
  349. }
  350. ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  351. if err := org.GetMembers(); err != nil {
  352. ctx.Handle(500, "GetMembers", err)
  353. return
  354. }
  355. ctx.Data["Members"] = org.Members
  356. ctx.Data["Teams"] = org.Teams
  357. ctx.HTML(200, ORG_HOME)
  358. }
  359. func Email2User(ctx *context.Context) {
  360. u, err := models.GetUserByEmail(ctx.Query("email"))
  361. if err != nil {
  362. ctx.NotFoundOrServerError("GetUserByEmail", errors.IsUserNotExist, err)
  363. return
  364. }
  365. ctx.Redirect(setting.AppSubUrl + "/user/" + u.Name)
  366. }