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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "strings"
  8. "time"
  9. "github.com/gogits/git"
  10. qlog "github.com/qiniu/log"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  13. )
  14. // Operation types of user action.
  15. const (
  16. OP_CREATE_REPO = iota + 1
  17. OP_DELETE_REPO
  18. OP_STAR_REPO
  19. OP_FOLLOW_REPO
  20. OP_COMMIT_REPO
  21. OP_CREATE_ISSUE
  22. OP_PULL_REQUEST
  23. OP_TRANSFER_REPO
  24. OP_PUSH_TAG
  25. )
  26. // Action represents user operation type and other information to repository.,
  27. // it implemented interface base.Actioner so that can be used in template render.
  28. type Action struct {
  29. Id int64
  30. UserId int64 // Receiver user id.
  31. OpType int // Operations: CREATE DELETE STAR ...
  32. ActUserId int64 // Action user id.
  33. ActUserName string // Action user name.
  34. ActEmail string
  35. RepoId int64
  36. RepoUserName string
  37. RepoName string
  38. RefName string
  39. IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
  40. Content string `xorm:"TEXT"`
  41. Created time.Time `xorm:"created"`
  42. }
  43. func (a Action) GetOpType() int {
  44. return a.OpType
  45. }
  46. func (a Action) GetActUserName() string {
  47. return a.ActUserName
  48. }
  49. func (a Action) GetActEmail() string {
  50. return a.ActEmail
  51. }
  52. func (a Action) GetRepoName() string {
  53. return a.RepoName
  54. }
  55. func (a Action) GetBranch() string {
  56. return a.RefName
  57. }
  58. func (a Action) GetContent() string {
  59. return a.Content
  60. }
  61. // CommitRepoAction adds new action for committing repository.
  62. func CommitRepoAction(userId, repoUserId int64, userName, actEmail string,
  63. repoId int64, repoUserName, repoName string, refName string, commit *base.PushCommits) error {
  64. // log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
  65. opType := OP_COMMIT_REPO
  66. // Check it's tag push or branch.
  67. if strings.HasPrefix(refName, "refs/tags/") {
  68. opType = OP_PUSH_TAG
  69. commit = &base.PushCommits{}
  70. }
  71. refName = git.RefEndName(refName)
  72. bs, err := json.Marshal(commit)
  73. if err != nil {
  74. qlog.Error("action.CommitRepoAction(json): %d/%s", repoUserId, repoName)
  75. return err
  76. }
  77. // Change repository bare status and update last updated time.
  78. repo, err := GetRepositoryByName(repoUserId, repoName)
  79. if err != nil {
  80. qlog.Error("action.CommitRepoAction(GetRepositoryByName): %d/%s", repoUserId, repoName)
  81. return err
  82. }
  83. repo.IsBare = false
  84. if err = UpdateRepository(repo); err != nil {
  85. qlog.Error("action.CommitRepoAction(UpdateRepository): %d/%s", repoUserId, repoName)
  86. return err
  87. }
  88. if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, ActEmail: actEmail,
  89. OpType: opType, Content: string(bs), RepoId: repoId, RepoUserName: repoUserName,
  90. RepoName: repoName, RefName: refName,
  91. IsPrivate: repo.IsPrivate}); err != nil {
  92. qlog.Error("action.CommitRepoAction(notify watchers): %d/%s", userId, repoName)
  93. return err
  94. }
  95. qlog.Info("action.CommitRepoAction(end): %d/%s", repoUserId, repoName)
  96. return nil
  97. }
  98. // NewRepoAction adds new action for creating repository.
  99. func NewRepoAction(user *User, repo *Repository) (err error) {
  100. if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
  101. OpType: OP_CREATE_REPO, RepoId: repo.Id, RepoName: repo.Name, IsPrivate: repo.IsPrivate}); err != nil {
  102. log.Error("action.NewRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
  103. return err
  104. }
  105. log.Trace("action.NewRepoAction: %s/%s", user.LowerName, repo.LowerName)
  106. return err
  107. }
  108. // TransferRepoAction adds new action for transfering repository.
  109. func TransferRepoAction(user, newUser *User, repo *Repository) (err error) {
  110. if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
  111. OpType: OP_TRANSFER_REPO, RepoId: repo.Id, RepoName: repo.Name, Content: newUser.Name,
  112. IsPrivate: repo.IsPrivate}); err != nil {
  113. log.Error("action.TransferRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
  114. return err
  115. }
  116. log.Trace("action.TransferRepoAction: %s/%s", user.LowerName, repo.LowerName)
  117. return err
  118. }
  119. // GetFeeds returns action list of given user in given context.
  120. func GetFeeds(userid, offset int64, isProfile bool) ([]Action, error) {
  121. actions := make([]Action, 0, 20)
  122. sess := orm.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  123. if isProfile {
  124. sess.Where("is_private=?", false).And("act_user_id=?", userid)
  125. } else {
  126. sess.And("act_user_id!=?", userid)
  127. }
  128. err := sess.Find(&actions)
  129. return actions, err
  130. }