home.go 10 KB

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