home.go 9.9 KB

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