You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

action.go 19KB

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