home.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package user
  7. import (
  8. "bytes"
  9. "fmt"
  10. "gitote/gitote/models"
  11. "gitote/gitote/models/errors"
  12. "gitote/gitote/pkg/context"
  13. "gitote/gitote/pkg/setting"
  14. "github.com/Unknwon/com"
  15. "gitlab.com/yoginth/paginater"
  16. )
  17. const (
  18. // DashboardTPL page template
  19. DashboardTPL = "user/dashboard/dashboard"
  20. // NewsFeedTPL page template
  21. NewsFeedTPL = "user/dashboard/feeds"
  22. // IssuesTPL page template
  23. IssuesTPL = "user/dashboard/issues"
  24. // ProfileTPL page template
  25. ProfileTPL = "user/profile"
  26. // OrgHomeTPL page template
  27. OrgHomeTPL = "org/home"
  28. )
  29. // getDashboardContextUser finds out dashboard is viewing as which context user.
  30. func getDashboardContextUser(c *context.Context) *models.User {
  31. ctxUser := c.User
  32. orgName := c.Params(":org")
  33. if len(orgName) > 0 {
  34. // Organization.
  35. org, err := models.GetUserByName(orgName)
  36. if err != nil {
  37. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  38. return nil
  39. }
  40. ctxUser = org
  41. }
  42. c.Data["ContextUser"] = ctxUser
  43. if err := c.User.GetOrganizations(true); err != nil {
  44. c.Handle(500, "GetOrganizations", err)
  45. return nil
  46. }
  47. c.Data["Orgs"] = c.User.Orgs
  48. return ctxUser
  49. }
  50. // retrieveFeeds loads feeds from database by given context user.
  51. // The user could be organization so it is not always the logged in user,
  52. // which is why we have to explicitly pass the context user ID.
  53. func retrieveFeeds(c *context.Context, ctxUser *models.User, userID int64, isProfile bool) {
  54. actions, err := models.GetFeeds(ctxUser, userID, c.QueryInt64("after_id"), isProfile)
  55. if err != nil {
  56. c.Handle(500, "GetFeeds", err)
  57. return
  58. }
  59. // Check access of private repositories.
  60. feeds := make([]*models.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 := models.GetUserByName(act.ActUserName)
  67. if err != nil {
  68. if errors.IsUserNotExist(err) {
  69. continue
  70. }
  71. c.Handle(500, "GetUserByName", err)
  72. return
  73. }
  74. unameAvatars[act.ActUserName] = u.RelAvatarLink()
  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.HTML(200, NewsFeedTPL)
  97. return
  98. }
  99. c.Data["Title"] = ctxUser.DisplayName() + " - " + c.Tr("dashboard")
  100. c.Data["Owner"] = ctxUser
  101. c.Data["PageIsDashboard"] = true
  102. c.Data["PageIsNews"] = true
  103. // Only user can have collaborative repositories.
  104. if !ctxUser.IsOrganization() {
  105. collaborateRepos, err := c.User.GetAccessibleRepositories(setting.UI.User.RepoPagingNum)
  106. if err != nil {
  107. c.Handle(500, "GetAccessibleRepositories", err)
  108. return
  109. } else if err = models.RepositoryList(collaborateRepos).LoadAttributes(); err != nil {
  110. c.Handle(500, "RepositoryList.LoadAttributes", err)
  111. return
  112. }
  113. c.Data["CollaborativeRepos"] = collaborateRepos
  114. }
  115. var err error
  116. var repos, mirrors []*models.Repository
  117. var repoCount int64
  118. if ctxUser.IsOrganization() {
  119. repos, repoCount, err = ctxUser.GetUserRepositories(c.User.ID, 1, 5)
  120. if err != nil {
  121. c.Handle(500, "GetUserRepositories", err)
  122. return
  123. }
  124. mirrors, err = ctxUser.GetUserMirrorRepositories(c.User.ID)
  125. if err != nil {
  126. c.Handle(500, "GetUserMirrorRepositories", err)
  127. return
  128. }
  129. } else {
  130. if err = ctxUser.GetRepositories(1, 5); err != nil {
  131. c.Handle(500, "GetRepositories", err)
  132. return
  133. }
  134. repos = ctxUser.Repos
  135. repoCount = int64(ctxUser.NumRepos)
  136. mirrors, err = ctxUser.GetMirrorRepositories()
  137. if err != nil {
  138. c.Handle(500, "GetMirrorRepositories", err)
  139. return
  140. }
  141. }
  142. c.Data["Repos"] = repos
  143. c.Data["RepoCount"] = repoCount
  144. c.Data["MaxShowRepoNum"] = 5
  145. if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil {
  146. c.Handle(500, "MirrorRepositoryList.LoadAttributes", err)
  147. return
  148. }
  149. c.Data["MirrorCount"] = len(mirrors)
  150. c.Data["Mirrors"] = mirrors
  151. c.HTML(200, DashboardTPL)
  152. }
  153. func Issues(c *context.Context) {
  154. isPullList := c.Params(":type") == "pulls"
  155. if isPullList {
  156. c.Data["Title"] = c.Tr("pull_requests")
  157. c.Data["PageIsPulls"] = true
  158. } else {
  159. c.Data["Title"] = c.Tr("issues")
  160. c.Data["PageIsIssues"] = true
  161. }
  162. ctxUser := getDashboardContextUser(c)
  163. if c.Written() {
  164. return
  165. }
  166. var (
  167. sortType = c.Query("sort")
  168. filterMode = models.FILTER_MODE_YOUR_REPOS
  169. )
  170. // Note: Organization does not have view type and filter mode.
  171. if !ctxUser.IsOrganization() {
  172. viewType := c.Query("type")
  173. types := []string{
  174. string(models.FILTER_MODE_YOUR_REPOS),
  175. string(models.FILTER_MODE_ASSIGN),
  176. string(models.FILTER_MODE_CREATE),
  177. }
  178. if !com.IsSliceContainsStr(types, viewType) {
  179. viewType = string(models.FILTER_MODE_YOUR_REPOS)
  180. }
  181. filterMode = models.FilterMode(viewType)
  182. }
  183. page := c.QueryInt("page")
  184. if page <= 1 {
  185. page = 1
  186. }
  187. repoID := c.QueryInt64("repo")
  188. isShowClosed := c.Query("state") == "closed"
  189. // Get repositories.
  190. var (
  191. err error
  192. repos []*models.Repository
  193. userRepoIDs []int64
  194. showRepos = make([]*models.Repository, 0, 10)
  195. )
  196. if ctxUser.IsOrganization() {
  197. repos, _, err = ctxUser.GetUserRepositories(c.User.ID, 1, ctxUser.NumRepos)
  198. if err != nil {
  199. c.Handle(500, "GetRepositories", err)
  200. return
  201. }
  202. } else {
  203. if err := ctxUser.GetRepositories(1, c.User.NumRepos); err != nil {
  204. c.Handle(500, "GetRepositories", err)
  205. return
  206. }
  207. repos = ctxUser.Repos
  208. }
  209. userRepoIDs = make([]int64, 0, len(repos))
  210. for _, repo := range repos {
  211. userRepoIDs = append(userRepoIDs, repo.ID)
  212. if filterMode != models.FILTER_MODE_YOUR_REPOS {
  213. continue
  214. }
  215. if isPullList {
  216. if isShowClosed && repo.NumClosedPulls == 0 ||
  217. !isShowClosed && repo.NumOpenPulls == 0 {
  218. continue
  219. }
  220. } else {
  221. if !repo.EnableIssues || repo.EnableExternalTracker ||
  222. isShowClosed && repo.NumClosedIssues == 0 ||
  223. !isShowClosed && repo.NumOpenIssues == 0 {
  224. continue
  225. }
  226. }
  227. showRepos = append(showRepos, repo)
  228. }
  229. // Filter repositories if the page shows issues.
  230. if !isPullList {
  231. userRepoIDs, err = models.FilterRepositoryWithIssues(userRepoIDs)
  232. if err != nil {
  233. c.Handle(500, "FilterRepositoryWithIssues", err)
  234. return
  235. }
  236. }
  237. issueOptions := &models.IssuesOptions{
  238. RepoID: repoID,
  239. Page: page,
  240. IsClosed: isShowClosed,
  241. IsPull: isPullList,
  242. SortType: sortType,
  243. }
  244. switch filterMode {
  245. case models.FILTER_MODE_YOUR_REPOS:
  246. // Get all issues from repositories from this user.
  247. if userRepoIDs == nil {
  248. issueOptions.RepoIDs = []int64{-1}
  249. } else {
  250. issueOptions.RepoIDs = userRepoIDs
  251. }
  252. case models.FILTER_MODE_ASSIGN:
  253. // Get all issues assigned to this user.
  254. issueOptions.AssigneeID = ctxUser.ID
  255. case models.FILTER_MODE_CREATE:
  256. // Get all issues created by this user.
  257. issueOptions.PosterID = ctxUser.ID
  258. }
  259. issues, err := models.Issues(issueOptions)
  260. if err != nil {
  261. c.Handle(500, "Issues", err)
  262. return
  263. }
  264. if repoID > 0 {
  265. repo, err := models.GetRepositoryByID(repoID)
  266. if err != nil {
  267. c.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d] %v", repoID, err))
  268. return
  269. }
  270. if err = repo.GetOwner(); err != nil {
  271. c.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", repoID, err))
  272. return
  273. }
  274. // Check if user has access to given repository.
  275. if !repo.IsOwnedBy(ctxUser.ID) && !repo.HasAccess(ctxUser.ID) {
  276. c.Handle(404, "Issues", fmt.Errorf("#%d", repoID))
  277. return
  278. }
  279. }
  280. for _, issue := range issues {
  281. if err = issue.Repo.GetOwner(); err != nil {
  282. c.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", issue.RepoID, err))
  283. return
  284. }
  285. }
  286. issueStats := models.GetUserIssueStats(repoID, ctxUser.ID, userRepoIDs, filterMode, isPullList)
  287. var total int
  288. if !isShowClosed {
  289. total = int(issueStats.OpenCount)
  290. } else {
  291. total = int(issueStats.ClosedCount)
  292. }
  293. c.Data["Issues"] = issues
  294. c.Data["Repos"] = showRepos
  295. c.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  296. c.Data["IssueStats"] = issueStats
  297. c.Data["ViewType"] = string(filterMode)
  298. c.Data["SortType"] = sortType
  299. c.Data["RepoID"] = repoID
  300. c.Data["IsShowClosed"] = isShowClosed
  301. if isShowClosed {
  302. c.Data["State"] = "closed"
  303. } else {
  304. c.Data["State"] = "open"
  305. }
  306. c.HTML(200, IssuesTPL)
  307. }
  308. func ShowSSHKeys(c *context.Context, uid int64) {
  309. keys, err := models.ListPublicKeys(uid)
  310. if err != nil {
  311. c.Handle(500, "ListPublicKeys", err)
  312. return
  313. }
  314. var buf bytes.Buffer
  315. for i := range keys {
  316. buf.WriteString(keys[i].OmitEmail())
  317. buf.WriteString("\n")
  318. }
  319. c.PlainText(200, buf.Bytes())
  320. }
  321. func showOrgProfile(c *context.Context) {
  322. c.SetParams(":org", c.Params(":username"))
  323. context.HandleOrgAssignment(c)
  324. if c.Written() {
  325. return
  326. }
  327. org := c.Org.Organization
  328. c.Data["Title"] = org.FullName
  329. page := c.QueryInt("page")
  330. if page <= 0 {
  331. page = 1
  332. }
  333. var (
  334. repos []*models.Repository
  335. count int64
  336. err error
  337. )
  338. if c.IsLogged && !c.User.IsAdmin {
  339. repos, count, err = org.GetUserRepositories(c.User.ID, page, setting.UI.User.RepoPagingNum)
  340. if err != nil {
  341. c.Handle(500, "GetUserRepositories", err)
  342. return
  343. }
  344. c.Data["Repos"] = repos
  345. } else {
  346. showPrivate := c.IsLogged && c.User.IsAdmin
  347. repos, err = models.GetUserRepositories(&models.UserRepoOptions{
  348. UserID: org.ID,
  349. Private: showPrivate,
  350. Page: page,
  351. PageSize: setting.UI.User.RepoPagingNum,
  352. })
  353. if err != nil {
  354. c.Handle(500, "GetRepositories", err)
  355. return
  356. }
  357. c.Data["Repos"] = repos
  358. count = models.CountUserRepositories(org.ID, showPrivate)
  359. }
  360. c.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  361. if err := org.GetMembers(); err != nil {
  362. c.Handle(500, "GetMembers", err)
  363. return
  364. }
  365. c.Data["Members"] = org.Members
  366. c.Data["Teams"] = org.Teams
  367. c.Data["PageIsOrgHome"] = true
  368. c.HTML(200, OrgHomeTPL)
  369. }
  370. func Email2User(c *context.Context) {
  371. u, err := models.GetUserByEmail(c.Query("email"))
  372. if err != nil {
  373. c.NotFoundOrServerError("GetUserByEmail", errors.IsUserNotExist, err)
  374. return
  375. }
  376. c.Redirect(setting.AppSubURL + "/user/" + u.Name)
  377. }