action.go 4.3 KB

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