home.go 10.0 KB

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