You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

action.go 3.5 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_PULL_REQUEST
  19. )
  20. // Action represents user operation type and information to the repository.
  21. type Action struct {
  22. Id int64
  23. UserId int64 // Receiver user id.
  24. OpType int // Operations: CREATE DELETE STAR ...
  25. ActUserId int64 // Action user id.
  26. ActUserName string // Action user name.
  27. RepoId int64
  28. RepoName string
  29. RefName string
  30. Content string `xorm:"TEXT"`
  31. Created time.Time `xorm:"created"`
  32. }
  33. func (a Action) GetOpType() int {
  34. return a.OpType
  35. }
  36. func (a Action) GetActUserName() string {
  37. return a.ActUserName
  38. }
  39. func (a Action) GetRepoName() string {
  40. return a.RepoName
  41. }
  42. func (a Action) GetBranch() string {
  43. return a.RefName
  44. }
  45. func (a Action) GetContent() string {
  46. return a.Content
  47. }
  48. // CommitRepoAction records action for commit repository.
  49. func CommitRepoAction(userId int64, userName string,
  50. repoId int64, repoName string, refName string, commits *base.PushCommits) error {
  51. log.Trace("action.CommitRepoAction: %d/%s", userId, repoName)
  52. bs, err := json.Marshal(commits)
  53. if err != nil {
  54. log.Error("action.CommitRepoAction(json): %d/%s", userId, repoName)
  55. return err
  56. }
  57. // Add feeds for user self and all watchers.
  58. watches, err := GetWatches(repoId)
  59. if err != nil {
  60. log.Error("action.CommitRepoAction(get watches): %d/%s", userId, repoName)
  61. return err
  62. }
  63. watches = append(watches, Watch{UserId: userId})
  64. for i := range watches {
  65. if userId == watches[i].UserId && i > 0 {
  66. continue // Do not add twice in case author watches his/her repository.
  67. }
  68. _, err = orm.InsertOne(&Action{
  69. UserId: watches[i].UserId,
  70. ActUserId: userId,
  71. ActUserName: userName,
  72. OpType: OP_COMMIT_REPO,
  73. Content: string(bs),
  74. RepoId: repoId,
  75. RepoName: repoName,
  76. RefName: refName,
  77. })
  78. if err != nil {
  79. log.Error("action.CommitRepoAction(notify watches): %d/%s", userId, repoName)
  80. }
  81. return err
  82. }
  83. // Update repository last update time.
  84. repo, err := GetRepositoryByName(userId, repoName)
  85. if err != nil {
  86. log.Error("action.CommitRepoAction(GetRepositoryByName): %d/%s", userId, repoName)
  87. return err
  88. }
  89. repo.IsBare = false
  90. if err = UpdateRepository(repo); err != nil {
  91. log.Error("action.CommitRepoAction(UpdateRepository): %d/%s", userId, repoName)
  92. return err
  93. }
  94. return nil
  95. }
  96. // NewRepoAction records action for create repository.
  97. func NewRepoAction(user *User, repo *Repository) error {
  98. _, err := orm.InsertOne(&Action{
  99. UserId: user.Id,
  100. ActUserId: user.Id,
  101. ActUserName: user.Name,
  102. OpType: OP_CREATE_REPO,
  103. RepoId: repo.Id,
  104. RepoName: repo.Name,
  105. })
  106. log.Trace("action.NewRepoAction: %s/%s", user.LowerName, repo.LowerName)
  107. return err
  108. }
  109. // GetFeeds returns action list of given user in given context.
  110. func GetFeeds(userid, offset int64, isProfile bool) ([]Action, error) {
  111. actions := make([]Action, 0, 20)
  112. sess := orm.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  113. if isProfile {
  114. sess.And("act_user_id=?", userid)
  115. } else {
  116. sess.And("act_user_id!=?", userid)
  117. }
  118. err := sess.Find(&actions)
  119. return actions, err
  120. }