home.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. "gogs.io/gogs/internal/context"
  11. "gogs.io/gogs/internal/db"
  12. "gogs.io/gogs/internal/db/errors"
  13. "gogs.io/gogs/internal/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(c *context.Context) *db.User {
  24. ctxUser := c.User
  25. orgName := c.Params(":org")
  26. if len(orgName) > 0 {
  27. // Organization.
  28. org, err := db.GetUserByName(orgName)
  29. if err != nil {
  30. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  31. return nil
  32. }
  33. ctxUser = org
  34. }
  35. c.Data["ContextUser"] = ctxUser
  36. if err := c.User.GetOrganizations(true); err != nil {
  37. c.Handle(500, "GetOrganizations", err)
  38. return nil
  39. }
  40. c.Data["Orgs"] = c.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(c *context.Context, ctxUser *db.User, userID int64, isProfile bool) {
  47. actions, err := db.GetFeeds(ctxUser, userID, c.QueryInt64("after_id"), isProfile)
  48. if err != nil {
  49. c.Handle(500, "GetFeeds", err)
  50. return
  51. }
  52. // Check access of private repositories.
  53. feeds := make([]*db.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 := db.GetUserByName(act.ActUserName)
  60. if err != nil {
  61. if errors.IsUserNotExist(err) {
  62. continue
  63. }
  64. c.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. c.Data["Feeds"] = feeds
  73. if len(feeds) > 0 {
  74. afterID := feeds[len(feeds)-1].ID
  75. c.Data["AfterID"] = afterID
  76. c.Header().Set("X-AJAX-URL", fmt.Sprintf("%s?after_id=%d", c.Data["Link"], afterID))
  77. }
  78. }
  79. func Dashboard(c *context.Context) {
  80. ctxUser := getDashboardContextUser(c)
  81. if c.Written() {
  82. return
  83. }
  84. retrieveFeeds(c, ctxUser, c.User.ID, false)
  85. if c.Written() {
  86. return
  87. }
  88. if c.Req.Header.Get("X-AJAX") == "true" {
  89. c.HTML(200, NEWS_FEED)
  90. return
  91. }
  92. c.Data["Title"] = ctxUser.DisplayName() + " - " + c.Tr("dashboard")
  93. c.Data["PageIsDashboard"] = true
  94. c.Data["PageIsNews"] = true
  95. // Only user can have collaborative repositories.
  96. if !ctxUser.IsOrganization() {
  97. collaborateRepos, err := c.User.GetAccessibleRepositories(setting.UI.User.RepoPagingNum)
  98. if err != nil {
  99. c.Handle(500, "GetAccessibleRepositories", err)
  100. return
  101. } else if err = db.RepositoryList(collaborateRepos).LoadAttributes(); err != nil {
  102. c.Handle(500, "RepositoryList.LoadAttributes", err)
  103. return
  104. }
  105. c.Data["CollaborativeRepos"] = collaborateRepos
  106. }
  107. var err error
  108. var repos, mirrors []*db.Repository
  109. var repoCount int64
  110. if ctxUser.IsOrganization() {
  111. repos, repoCount, err = ctxUser.GetUserRepositories(c.User.ID, 1, setting.UI.User.RepoPagingNum)
  112. if err != nil {
  113. c.Handle(500, "GetUserRepositories", err)
  114. return
  115. }
  116. mirrors, err = ctxUser.GetUserMirrorRepositories(c.User.ID)
  117. if err != nil {
  118. c.Handle(500, "GetUserMirrorRepositories", err)
  119. return
  120. }
  121. } else {
  122. if err = ctxUser.GetRepositories(1, setting.UI.User.RepoPagingNum); err != nil {
  123. c.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. c.Handle(500, "GetMirrorRepositories", err)
  131. return
  132. }
  133. }
  134. c.Data["Repos"] = repos
  135. c.Data["RepoCount"] = repoCount
  136. c.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum
  137. if err := db.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil {
  138. c.Handle(500, "MirrorRepositoryList.LoadAttributes", err)
  139. return
  140. }
  141. c.Data["MirrorCount"] = len(mirrors)
  142. c.Data["Mirrors"] = mirrors
  143. c.HTML(200, DASHBOARD)
  144. }
  145. func Issues(c *context.Context) {
  146. isPullList := c.Params(":type") == "pulls"
  147. if isPullList {
  148. c.Data["Title"] = c.Tr("pull_requests")
  149. c.Data["PageIsPulls"] = true
  150. } else {
  151. c.Data["Title"] = c.Tr("issues")
  152. c.Data["PageIsIssues"] = true
  153. }
  154. ctxUser := getDashboardContextUser(c)
  155. if c.Written() {
  156. return
  157. }
  158. var (
  159. sortType = c.Query("sort")
  160. filterMode = db.FILTER_MODE_YOUR_REPOS
  161. )
  162. // Note: Organization does not have view type and filter mode.
  163. if !ctxUser.IsOrganization() {
  164. viewType := c.Query("type")
  165. types := []string{
  166. string(db.FILTER_MODE_YOUR_REPOS),
  167. string(db.FILTER_MODE_ASSIGN),
  168. string(db.FILTER_MODE_CREATE),
  169. }
  170. if !com.IsSliceContainsStr(types, viewType) {
  171. viewType = string(db.FILTER_MODE_YOUR_REPOS)
  172. }
  173. filterMode = db.FilterMode(viewType)
  174. }
  175. page := c.QueryInt("page")
  176. if page <= 1 {
  177. page = 1
  178. }
  179. repoID := c.QueryInt64("repo")
  180. isShowClosed := c.Query("state") == "closed"
  181. // Get repositories.
  182. var (
  183. err error
  184. repos []*db.Repository
  185. userRepoIDs []int64
  186. showRepos = make([]*db.Repository, 0, 10)
  187. )
  188. if ctxUser.IsOrganization() {
  189. repos, _, err = ctxUser.GetUserRepositories(c.User.ID, 1, ctxUser.NumRepos)
  190. if err != nil {
  191. c.Handle(500, "GetRepositories", err)
  192. return
  193. }
  194. } else {
  195. if err := ctxUser.GetRepositories(1, c.User.NumRepos); err != nil {
  196. c.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 != db.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 = db.FilterRepositoryWithIssues(userRepoIDs)
  224. if err != nil {
  225. c.Handle(500, "FilterRepositoryWithIssues", err)
  226. return
  227. }
  228. }
  229. issueOptions := &db.IssuesOptions{
  230. RepoID: repoID,
  231. Page: page,
  232. IsClosed: isShowClosed,
  233. IsPull: isPullList,
  234. SortType: sortType,
  235. }
  236. switch filterMode {
  237. case db.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 db.FILTER_MODE_ASSIGN:
  245. // Get all issues assigned to this user.
  246. issueOptions.AssigneeID = ctxUser.ID
  247. case db.FILTER_MODE_CREATE:
  248. // Get all issues created by this user.
  249. issueOptions.PosterID = ctxUser.ID
  250. }
  251. issues, err := db.Issues(issueOptions)
  252. if err != nil {
  253. c.Handle(500, "Issues", err)
  254. return
  255. }
  256. if repoID > 0 {
  257. repo, err := db.GetRepositoryByID(repoID)
  258. if err != nil {
  259. c.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d] %v", repoID, err))
  260. return
  261. }
  262. if err = repo.GetOwner(); err != nil {
  263. c.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. c.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. c.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", issue.RepoID, err))
  275. return
  276. }
  277. }
  278. issueStats := db.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. c.Data["Issues"] = issues
  286. c.Data["Repos"] = showRepos
  287. c.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  288. c.Data["IssueStats"] = issueStats
  289. c.Data["ViewType"] = string(filterMode)
  290. c.Data["SortType"] = sortType
  291. c.Data["RepoID"] = repoID
  292. c.Data["IsShowClosed"] = isShowClosed
  293. if isShowClosed {
  294. c.Data["State"] = "closed"
  295. } else {
  296. c.Data["State"] = "open"
  297. }
  298. c.HTML(200, ISSUES)
  299. }
  300. func ShowSSHKeys(c *context.Context, uid int64) {
  301. keys, err := db.ListPublicKeys(uid)
  302. if err != nil {
  303. c.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. c.PlainText(200, buf.Bytes())
  312. }
  313. func showOrgProfile(c *context.Context) {
  314. c.SetParams(":org", c.Params(":username"))
  315. context.HandleOrgAssignment(c)
  316. if c.Written() {
  317. return
  318. }
  319. org := c.Org.Organization
  320. c.Data["Title"] = org.FullName
  321. page := c.QueryInt("page")
  322. if page <= 0 {
  323. page = 1
  324. }
  325. var (
  326. repos []*db.Repository
  327. count int64
  328. err error
  329. )
  330. if c.IsLogged && !c.User.IsAdmin {
  331. repos, count, err = org.GetUserRepositories(c.User.ID, page, setting.UI.User.RepoPagingNum)
  332. if err != nil {
  333. c.Handle(500, "GetUserRepositories", err)
  334. return
  335. }
  336. c.Data["Repos"] = repos
  337. } else {
  338. showPrivate := c.IsLogged && c.User.IsAdmin
  339. repos, err = db.GetUserRepositories(&db.UserRepoOptions{
  340. UserID: org.ID,
  341. Private: showPrivate,
  342. Page: page,
  343. PageSize: setting.UI.User.RepoPagingNum,
  344. })
  345. if err != nil {
  346. c.Handle(500, "GetRepositories", err)
  347. return
  348. }
  349. c.Data["Repos"] = repos
  350. count = db.CountUserRepositories(org.ID, showPrivate)
  351. }
  352. c.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  353. if err := org.GetMembers(); err != nil {
  354. c.Handle(500, "GetMembers", err)
  355. return
  356. }
  357. c.Data["Members"] = org.Members
  358. c.Data["Teams"] = org.Teams
  359. c.HTML(200, ORG_HOME)
  360. }
  361. func Email2User(c *context.Context) {
  362. u, err := db.GetUserByEmail(c.Query("email"))
  363. if err != nil {
  364. c.NotFoundOrServerError("GetUserByEmail", errors.IsUserNotExist, err)
  365. return
  366. }
  367. c.Redirect(setting.AppSubURL + "/user/" + u.Name)
  368. }