action.go 18 KB

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