home.go 10 KB

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