action.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 models
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "path"
  9. "regexp"
  10. "strings"
  11. "time"
  12. "unicode"
  13. "github.com/Unknwon/com"
  14. "github.com/go-xorm/xorm"
  15. log "gopkg.in/clog.v1"
  16. "github.com/gogits/git-module"
  17. api "github.com/gogits/go-gogs-client"
  18. "github.com/gogits/gogs/modules/base"
  19. "github.com/gogits/gogs/modules/setting"
  20. )
  21. type ActionType int
  22. // To maintain backward compatibility only append to the end of list
  23. const (
  24. ACTION_CREATE_REPO ActionType = iota + 1 // 1
  25. ACTION_RENAME_REPO // 2
  26. ACTION_STAR_REPO // 3
  27. ACTION_WATCH_REPO // 4
  28. ACTION_COMMIT_REPO // 5
  29. ACTION_CREATE_ISSUE // 6
  30. ACTION_CREATE_PULL_REQUEST // 7
  31. ACTION_TRANSFER_REPO // 8
  32. ACTION_PUSH_TAG // 9
  33. ACTION_COMMENT_ISSUE // 10
  34. ACTION_MERGE_PULL_REQUEST // 11
  35. ACTION_CLOSE_ISSUE // 12
  36. ACTION_REOPEN_ISSUE // 13
  37. ACTION_CLOSE_PULL_REQUEST // 14
  38. ACTION_REOPEN_PULL_REQUEST // 15
  39. ACTION_CREATE_BRANCH // 16
  40. ACTION_DELETE_BRANCH // 17
  41. ACTION_DELETE_TAG // 18
  42. ACTION_FORK_REPO // 19
  43. )
  44. var (
  45. // Same as Github. See https://help.github.com/articles/closing-issues-via-commit-messages
  46. IssueCloseKeywords = []string{"close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved"}
  47. IssueReopenKeywords = []string{"reopen", "reopens", "reopened"}
  48. IssueCloseKeywordsPat, IssueReopenKeywordsPat *regexp.Regexp
  49. IssueReferenceKeywordsPat *regexp.Regexp
  50. )
  51. func assembleKeywordsPattern(words []string) string {
  52. return fmt.Sprintf(`(?i)(?:%s) \S+`, strings.Join(words, "|"))
  53. }
  54. func init() {
  55. IssueCloseKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(IssueCloseKeywords))
  56. IssueReopenKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(IssueReopenKeywords))
  57. IssueReferenceKeywordsPat = regexp.MustCompile(`(?i)(?:)(^| )\S+`)
  58. }
  59. // Action represents user operation type and other information to repository,
  60. // it implemented interface base.Actioner so that can be used in template render.
  61. type Action struct {
  62. ID int64
  63. UserID int64 // Receiver user id.
  64. OpType ActionType
  65. ActUserID int64 // Action user id.
  66. ActUserName string // Action user name.
  67. ActAvatar string `xorm:"-"`
  68. RepoID int64
  69. RepoUserName string
  70. RepoName string
  71. RefName string
  72. IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
  73. Content string `xorm:"TEXT"`
  74. Created time.Time `xorm:"-"`
  75. CreatedUnix int64
  76. }
  77. func (a *Action) BeforeInsert() {
  78. a.CreatedUnix = time.Now().Unix()
  79. }
  80. func (a *Action) AfterSet(colName string, _ xorm.Cell) {
  81. switch colName {
  82. case "created_unix":
  83. a.Created = time.Unix(a.CreatedUnix, 0).Local()
  84. }
  85. }
  86. func (a *Action) GetOpType() int {
  87. return int(a.OpType)
  88. }
  89. func (a *Action) GetActUserName() string {
  90. return a.ActUserName
  91. }
  92. func (a *Action) ShortActUserName() string {
  93. return base.EllipsisString(a.ActUserName, 20)
  94. }
  95. func (a *Action) GetRepoUserName() string {
  96. return a.RepoUserName
  97. }
  98. func (a *Action) ShortRepoUserName() string {
  99. return base.EllipsisString(a.RepoUserName, 20)
  100. }
  101. func (a *Action) GetRepoName() string {
  102. return a.RepoName
  103. }
  104. func (a *Action) ShortRepoName() string {
  105. return base.EllipsisString(a.RepoName, 33)
  106. }
  107. func (a *Action) GetRepoPath() string {
  108. return path.Join(a.RepoUserName, a.RepoName)
  109. }
  110. func (a *Action) ShortRepoPath() string {
  111. return path.Join(a.ShortRepoUserName(), a.ShortRepoName())
  112. }
  113. func (a *Action) GetRepoLink() string {
  114. if len(setting.AppSubUrl) > 0 {
  115. return path.Join(setting.AppSubUrl, a.GetRepoPath())
  116. }
  117. return "/" + a.GetRepoPath()
  118. }
  119. func (a *Action) GetBranch() string {
  120. return a.RefName
  121. }
  122. func (a *Action) GetContent() string {
  123. return a.Content
  124. }
  125. func (a *Action) GetCreate() time.Time {
  126. return a.Created
  127. }
  128. func (a *Action) GetIssueInfos() []string {
  129. return strings.SplitN(a.Content, "|", 2)
  130. }
  131. func (a *Action) GetIssueTitle() string {
  132. index := com.StrTo(a.GetIssueInfos()[0]).MustInt64()
  133. issue, err := GetIssueByIndex(a.RepoID, index)
  134. if err != nil {
  135. log.Error(4, "GetIssueByIndex: %v", err)
  136. return "500 when get issue"
  137. }
  138. return issue.Title
  139. }
  140. func (a *Action) GetIssueContent() string {
  141. index := com.StrTo(a.GetIssueInfos()[0]).MustInt64()
  142. issue, err := GetIssueByIndex(a.RepoID, index)
  143. if err != nil {
  144. log.Error(4, "GetIssueByIndex: %v", err)
  145. return "500 when get issue"
  146. }
  147. return issue.Content
  148. }
  149. func newRepoAction(e Engine, doer, owner *User, repo *Repository) (err error) {
  150. opType := ACTION_CREATE_REPO
  151. if repo.IsFork {
  152. opType = ACTION_FORK_REPO
  153. }
  154. return notifyWatchers(e, &Action{
  155. ActUserID: doer.ID,
  156. ActUserName: doer.Name,
  157. OpType: opType,
  158. RepoID: repo.ID,
  159. RepoUserName: repo.Owner.Name,
  160. RepoName: repo.Name,
  161. IsPrivate: repo.IsPrivate,
  162. })
  163. }
  164. // NewRepoAction adds new action for creating repository.
  165. func NewRepoAction(doer, owner *User, repo *Repository) (err error) {
  166. return newRepoAction(x, doer, owner, repo)
  167. }
  168. func renameRepoAction(e Engine, actUser *User, oldRepoName string, repo *Repository) (err error) {
  169. if err = notifyWatchers(e, &Action{
  170. ActUserID: actUser.ID,
  171. ActUserName: actUser.Name,
  172. OpType: ACTION_RENAME_REPO,
  173. RepoID: repo.ID,
  174. RepoUserName: repo.Owner.Name,
  175. RepoName: repo.Name,
  176. IsPrivate: repo.IsPrivate,
  177. Content: oldRepoName,
  178. }); err != nil {
  179. return fmt.Errorf("notify watchers: %v", err)
  180. }
  181. log.Trace("action.renameRepoAction: %s/%s", actUser.Name, repo.Name)
  182. return nil
  183. }
  184. // RenameRepoAction adds new action for renaming a repository.
  185. func RenameRepoAction(actUser *User, oldRepoName string, repo *Repository) error {
  186. return renameRepoAction(x, actUser, oldRepoName, repo)
  187. }
  188. func issueIndexTrimRight(c rune) bool {
  189. return !unicode.IsDigit(c)
  190. }
  191. type PushCommit struct {
  192. Sha1 string
  193. Message string
  194. AuthorEmail string
  195. AuthorName string
  196. CommitterEmail string
  197. CommitterName string
  198. Timestamp time.Time
  199. }
  200. type PushCommits struct {
  201. Len int
  202. Commits []*PushCommit
  203. CompareURL string
  204. avatars map[string]string
  205. }
  206. func NewPushCommits() *PushCommits {
  207. return &PushCommits{
  208. avatars: make(map[string]string),
  209. }
  210. }
  211. func (pc *PushCommits) ToApiPayloadCommits(repoLink string) []*api.PayloadCommit {
  212. commits := make([]*api.PayloadCommit, len(pc.Commits))
  213. for i, commit := range pc.Commits {
  214. authorUsername := ""
  215. author, err := GetUserByEmail(commit.AuthorEmail)
  216. if err == nil {
  217. authorUsername = author.Name
  218. }
  219. committerUsername := ""
  220. committer, err := GetUserByEmail(commit.CommitterEmail)
  221. if err == nil {
  222. // TODO: check errors other than email not found.
  223. committerUsername = committer.Name
  224. }
  225. commits[i] = &api.PayloadCommit{
  226. ID: commit.Sha1,
  227. Message: commit.Message,
  228. URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1),
  229. Author: &api.PayloadUser{
  230. Name: commit.AuthorName,
  231. Email: commit.AuthorEmail,
  232. UserName: authorUsername,
  233. },
  234. Committer: &api.PayloadUser{
  235. Name: commit.CommitterName,
  236. Email: commit.CommitterEmail,
  237. UserName: committerUsername,
  238. },
  239. Timestamp: commit.Timestamp,
  240. }
  241. }
  242. return commits
  243. }
  244. // AvatarLink tries to match user in database with e-mail
  245. // in order to show custom avatar, and falls back to general avatar link.
  246. func (push *PushCommits) AvatarLink(email string) string {
  247. _, ok := push.avatars[email]
  248. if !ok {
  249. u, err := GetUserByEmail(email)
  250. if err != nil {
  251. push.avatars[email] = base.AvatarLink(email)
  252. if !IsErrUserNotExist(err) {
  253. log.Error(4, "GetUserByEmail: %v", err)
  254. }
  255. } else {
  256. push.avatars[email] = u.RelAvatarLink()
  257. }
  258. }
  259. return push.avatars[email]
  260. }
  261. // UpdateIssuesCommit checks if issues are manipulated by commit message.
  262. func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit) error {
  263. // Commits are appended in the reverse order.
  264. for i := len(commits) - 1; i >= 0; i-- {
  265. c := commits[i]
  266. refMarked := make(map[int64]bool)
  267. for _, ref := range IssueReferenceKeywordsPat.FindAllString(c.Message, -1) {
  268. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  269. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  270. if len(ref) == 0 {
  271. continue
  272. }
  273. // Add repo name if missing
  274. if ref[0] == '#' {
  275. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  276. } else if !strings.Contains(ref, "/") {
  277. // FIXME: We don't support User#ID syntax yet
  278. // return ErrNotImplemented
  279. continue
  280. }
  281. issue, err := GetIssueByRef(ref)
  282. if err != nil {
  283. if IsErrIssueNotExist(err) {
  284. continue
  285. }
  286. return err
  287. }
  288. if refMarked[issue.ID] {
  289. continue
  290. }
  291. refMarked[issue.ID] = true
  292. msgLines := strings.Split(c.Message, "\n")
  293. shortMsg := msgLines[0]
  294. if len(msgLines) > 2 {
  295. shortMsg += "..."
  296. }
  297. message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, repo.Link(), c.Sha1, shortMsg)
  298. if err = CreateRefComment(doer, repo, issue, message, c.Sha1); err != nil {
  299. return err
  300. }
  301. }
  302. refMarked = make(map[int64]bool)
  303. // FIXME: can merge this one and next one to a common function.
  304. for _, ref := range IssueCloseKeywordsPat.FindAllString(c.Message, -1) {
  305. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  306. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  307. if len(ref) == 0 {
  308. continue
  309. }
  310. // Add repo name if missing
  311. if ref[0] == '#' {
  312. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  313. } else if !strings.Contains(ref, "/") {
  314. // We don't support User#ID syntax yet
  315. // return ErrNotImplemented
  316. continue
  317. }
  318. issue, err := GetIssueByRef(ref)
  319. if err != nil {
  320. if IsErrIssueNotExist(err) {
  321. continue
  322. }
  323. return err
  324. }
  325. if refMarked[issue.ID] {
  326. continue
  327. }
  328. refMarked[issue.ID] = true
  329. if issue.RepoID != repo.ID || issue.IsClosed {
  330. continue
  331. }
  332. if err = issue.ChangeStatus(doer, repo, true); err != nil {
  333. return err
  334. }
  335. }
  336. // It is conflict to have close and reopen at same time, so refsMarkd doesn't need to reinit here.
  337. for _, ref := range IssueReopenKeywordsPat.FindAllString(c.Message, -1) {
  338. ref = ref[strings.IndexByte(ref, byte(' '))+1:]
  339. ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
  340. if len(ref) == 0 {
  341. continue
  342. }
  343. // Add repo name if missing
  344. if ref[0] == '#' {
  345. ref = fmt.Sprintf("%s%s", repo.FullName(), ref)
  346. } else if !strings.Contains(ref, "/") {
  347. // We don't support User#ID syntax yet
  348. // return ErrNotImplemented
  349. continue
  350. }
  351. issue, err := GetIssueByRef(ref)
  352. if err != nil {
  353. if IsErrIssueNotExist(err) {
  354. continue
  355. }
  356. return err
  357. }
  358. if refMarked[issue.ID] {
  359. continue
  360. }
  361. refMarked[issue.ID] = true
  362. if issue.RepoID != repo.ID || !issue.IsClosed {
  363. continue
  364. }
  365. if err = issue.ChangeStatus(doer, repo, false); err != nil {
  366. return err
  367. }
  368. }
  369. }
  370. return nil
  371. }
  372. type CommitRepoActionOptions struct {
  373. PusherName string
  374. RepoOwnerID int64
  375. RepoName string
  376. RefFullName string
  377. OldCommitID string
  378. NewCommitID string
  379. Commits *PushCommits
  380. }
  381. // CommitRepoAction adds new commit actio to the repository, and prepare corresponding webhooks.
  382. func CommitRepoAction(opts CommitRepoActionOptions) error {
  383. pusher, err := GetUserByName(opts.PusherName)
  384. if err != nil {
  385. return fmt.Errorf("GetUserByName [%s]: %v", opts.PusherName, err)
  386. }
  387. repo, err := GetRepositoryByName(opts.RepoOwnerID, opts.RepoName)
  388. if err != nil {
  389. return fmt.Errorf("GetRepositoryByName [owner_id: %d, name: %s]: %v", opts.RepoOwnerID, opts.RepoName, err)
  390. }
  391. // Change repository bare status and update last updated time.
  392. repo.IsBare = false
  393. if err = UpdateRepository(repo, false); err != nil {
  394. return fmt.Errorf("UpdateRepository: %v", err)
  395. }
  396. isNewRef := opts.OldCommitID == git.EMPTY_SHA
  397. isDelRef := opts.NewCommitID == git.EMPTY_SHA
  398. opType := ACTION_COMMIT_REPO
  399. // Check if it's tag push or branch.
  400. if strings.HasPrefix(opts.RefFullName, git.TAG_PREFIX) {
  401. opType = ACTION_PUSH_TAG
  402. } else {
  403. // if not the first commit, set the compare URL.
  404. if !isNewRef && !isDelRef {
  405. opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
  406. }
  407. if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits); err != nil {
  408. log.Error(2, "UpdateIssuesCommit: %v", err)
  409. }
  410. }
  411. if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
  412. opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
  413. }
  414. data, err := json.Marshal(opts.Commits)
  415. if err != nil {
  416. return fmt.Errorf("Marshal: %v", err)
  417. }
  418. refName := git.RefEndName(opts.RefFullName)
  419. action := &Action{
  420. ActUserID: pusher.ID,
  421. ActUserName: pusher.Name,
  422. Content: string(data),
  423. RepoID: repo.ID,
  424. RepoUserName: repo.MustOwner().Name,
  425. RepoName: repo.Name,
  426. RefName: refName,
  427. IsPrivate: repo.IsPrivate,
  428. }
  429. apiRepo := repo.APIFormat(nil)
  430. apiPusher := pusher.APIFormat()
  431. switch opType {
  432. case ACTION_COMMIT_REPO: // Push
  433. if isDelRef {
  434. if err = PrepareWebhooks(repo, HOOK_EVENT_DELETE, &api.DeletePayload{
  435. Ref: refName,
  436. RefType: "branch",
  437. PusherType: api.PUSHER_TYPE_USER,
  438. Repo: apiRepo,
  439. Sender: apiPusher,
  440. }); err != nil {
  441. return fmt.Errorf("PrepareWebhooks.(delete branch): %v", err)
  442. }
  443. action.OpType = ACTION_DELETE_BRANCH
  444. if err = NotifyWatchers(action); err != nil {
  445. return fmt.Errorf("NotifyWatchers.(delete branch): %v", err)
  446. }
  447. // Delete branch doesn't have anything to push or compare
  448. return nil
  449. }
  450. compareURL := setting.AppUrl + opts.Commits.CompareURL
  451. if isNewRef {
  452. compareURL = ""
  453. if err = PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  454. Ref: refName,
  455. RefType: "branch",
  456. DefaultBranch: repo.DefaultBranch,
  457. Repo: apiRepo,
  458. Sender: apiPusher,
  459. }); err != nil {
  460. return fmt.Errorf("PrepareWebhooks.(new branch): %v", err)
  461. }
  462. action.OpType = ACTION_CREATE_BRANCH
  463. if err = NotifyWatchers(action); err != nil {
  464. return fmt.Errorf("NotifyWatchers.(new branch): %v", err)
  465. }
  466. }
  467. if err = PrepareWebhooks(repo, HOOK_EVENT_PUSH, &api.PushPayload{
  468. Ref: opts.RefFullName,
  469. Before: opts.OldCommitID,
  470. After: opts.NewCommitID,
  471. CompareURL: compareURL,
  472. Commits: opts.Commits.ToApiPayloadCommits(repo.HTMLURL()),
  473. Repo: apiRepo,
  474. Pusher: apiPusher,
  475. Sender: apiPusher,
  476. }); err != nil {
  477. return fmt.Errorf("PrepareWebhooks.(new commit): %v", err)
  478. }
  479. action.OpType = ACTION_COMMIT_REPO
  480. if err = NotifyWatchers(action); err != nil {
  481. return fmt.Errorf("NotifyWatchers.(new commit): %v", err)
  482. }
  483. case ACTION_PUSH_TAG: // Tag
  484. if isDelRef {
  485. if err = PrepareWebhooks(repo, HOOK_EVENT_DELETE, &api.DeletePayload{
  486. Ref: refName,
  487. RefType: "tag",
  488. PusherType: api.PUSHER_TYPE_USER,
  489. Repo: apiRepo,
  490. Sender: apiPusher,
  491. }); err != nil {
  492. return fmt.Errorf("PrepareWebhooks.(delete tag): %v", err)
  493. }
  494. action.OpType = ACTION_DELETE_TAG
  495. if err = NotifyWatchers(action); err != nil {
  496. return fmt.Errorf("NotifyWatchers.(delete tag): %v", err)
  497. }
  498. return nil
  499. }
  500. if err = PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{
  501. Ref: refName,
  502. RefType: "tag",
  503. DefaultBranch: repo.DefaultBranch,
  504. Repo: apiRepo,
  505. Sender: apiPusher,
  506. }); err != nil {
  507. return fmt.Errorf("PrepareWebhooks.(new tag): %v", err)
  508. }
  509. action.OpType = ACTION_PUSH_TAG
  510. if err = NotifyWatchers(action); err != nil {
  511. return fmt.Errorf("NotifyWatchers.(new tag): %v", err)
  512. }
  513. }
  514. return nil
  515. }
  516. func transferRepoAction(e Engine, doer, oldOwner *User, repo *Repository) (err error) {
  517. if err = notifyWatchers(e, &Action{
  518. ActUserID: doer.ID,
  519. ActUserName: doer.Name,
  520. OpType: ACTION_TRANSFER_REPO,
  521. RepoID: repo.ID,
  522. RepoUserName: repo.Owner.Name,
  523. RepoName: repo.Name,
  524. IsPrivate: repo.IsPrivate,
  525. Content: path.Join(oldOwner.Name, repo.Name),
  526. }); err != nil {
  527. return fmt.Errorf("notifyWatchers: %v", err)
  528. }
  529. // Remove watch for organization.
  530. if oldOwner.IsOrganization() {
  531. if err = watchRepo(e, oldOwner.ID, repo.ID, false); err != nil {
  532. return fmt.Errorf("watchRepo [false]: %v", err)
  533. }
  534. }
  535. return nil
  536. }
  537. // TransferRepoAction adds new action for transferring repository,
  538. // the Owner field of repository is assumed to be new owner.
  539. func TransferRepoAction(doer, oldOwner *User, repo *Repository) error {
  540. return transferRepoAction(x, doer, oldOwner, repo)
  541. }
  542. func mergePullRequestAction(e Engine, doer *User, repo *Repository, issue *Issue) error {
  543. return notifyWatchers(e, &Action{
  544. ActUserID: doer.ID,
  545. ActUserName: doer.Name,
  546. OpType: ACTION_MERGE_PULL_REQUEST,
  547. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
  548. RepoID: repo.ID,
  549. RepoUserName: repo.Owner.Name,
  550. RepoName: repo.Name,
  551. IsPrivate: repo.IsPrivate,
  552. })
  553. }
  554. // MergePullRequestAction adds new action for merging pull request.
  555. func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error {
  556. return mergePullRequestAction(x, actUser, repo, pull)
  557. }
  558. // GetFeeds returns action list of given user in given context.
  559. // actorID is the user who's requesting, ctxUserID is the user/org that is requested.
  560. // actorID can be -1 when isProfile is true or to skip the permission check.
  561. func GetFeeds(ctxUser *User, actorID, offset int64, isProfile bool) ([]*Action, error) {
  562. actions := make([]*Action, 0, 20)
  563. sess := x.Limit(20, int(offset)).Desc("id").Where("user_id = ?", ctxUser.ID)
  564. if isProfile {
  565. sess.And("is_private = ?", false).And("act_user_id = ?", ctxUser.ID)
  566. } else if actorID != -1 && ctxUser.IsOrganization() {
  567. // FIXME: only need to get IDs here, not all fields of repository.
  568. repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)
  569. if err != nil {
  570. return nil, fmt.Errorf("GetUserRepositories: %v", err)
  571. }
  572. var repoIDs []int64
  573. for _, repo := range repos {
  574. repoIDs = append(repoIDs, repo.ID)
  575. }
  576. if len(repoIDs) > 0 {
  577. sess.In("repo_id", repoIDs)
  578. }
  579. }
  580. err := sess.Find(&actions)
  581. return actions, err
  582. }