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

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