action.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. Content string `xorm:"TEXT"`
  38. Created time.Time `xorm:"created"`
  39. }
  40. func (a Action) GetOpType() int {
  41. return a.OpType
  42. }
  43. func (a Action) GetActUserName() string {
  44. return a.ActUserName
  45. }
  46. func (a Action) GetActEmail() string {
  47. return a.ActEmail
  48. }
  49. func (a Action) GetRepoName() string {
  50. return a.RepoName
  51. }
  52. func (a Action) GetBranch() string {
  53. return a.RefName
  54. }
  55. func (a Action) GetContent() string {
  56. return a.Content
  57. }
  58. // CommitRepoAction adds new action for committing repository.
  59. func CommitRepoAction(userId int64, userName, actEmail string,
  60. repoId int64, repoName string, refName string, commit *base.PushCommits) error {
  61. // log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
  62. opType := OP_COMMIT_REPO
  63. // Check it's tag push or branch.
  64. if strings.HasPrefix(refName, "refs/tags/") {
  65. opType = OP_PUSH_TAG
  66. commit = &base.PushCommits{}
  67. }
  68. refName = git.RefEndName(refName)
  69. bs, err := json.Marshal(commit)
  70. if err != nil {
  71. log.Error("action.CommitRepoAction(json): %d/%s", userId, repoName)
  72. return err
  73. }
  74. // Change repository bare status and update last updated time.
  75. repo, err := GetRepositoryByName(userId, repoName)
  76. if err != nil {
  77. log.Error("action.CommitRepoAction(GetRepositoryByName): %d/%s", userId, repoName)
  78. return err
  79. }
  80. repo.IsBare = false
  81. if err = UpdateRepository(repo); err != nil {
  82. log.Error("action.CommitRepoAction(UpdateRepository): %d/%s", userId, repoName)
  83. return err
  84. }
  85. if !repo.IsPrivate {
  86. if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, ActEmail: actEmail,
  87. OpType: opType, Content: string(bs), RepoId: repoId, RepoName: repoName, RefName: refName}); err != nil {
  88. log.Error("action.CommitRepoAction(notify watchers): %d/%s", userId, repoName)
  89. return err
  90. }
  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 repo.IsPrivate {
  98. return nil
  99. }
  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}); 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 repo.IsPrivate {
  111. return nil
  112. }
  113. if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
  114. OpType: OP_TRANSFER_REPO, RepoId: repo.Id, RepoName: repo.Name, Content: newUser.Name}); err != nil {
  115. log.Error("action.TransferRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
  116. return err
  117. }
  118. log.Trace("action.TransferRepoAction: %s/%s", user.LowerName, repo.LowerName)
  119. return err
  120. }
  121. // GetFeeds returns action list of given user in given context.
  122. func GetFeeds(userid, offset int64, isProfile bool) ([]Action, error) {
  123. actions := make([]Action, 0, 20)
  124. sess := orm.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  125. if isProfile {
  126. sess.And("act_user_id=?", userid)
  127. } else {
  128. sess.And("act_user_id!=?", userid)
  129. }
  130. err := sess.Find(&actions)
  131. return actions, err
  132. }