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.

132 lines
3.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. bs, err := json.Marshal(commits)
  52. if err != nil {
  53. return err
  54. }
  55. // Add feeds for user self and all watchers.
  56. watches, err := GetWatches(repoId)
  57. if err != nil {
  58. return err
  59. }
  60. watches = append(watches, Watch{UserId: userId})
  61. for i := range watches {
  62. if userId == watches[i].UserId && i > 0 {
  63. continue // Do not add twice in case author watches his/her repository.
  64. }
  65. _, err = orm.InsertOne(&Action{
  66. UserId: watches[i].UserId,
  67. ActUserId: userId,
  68. ActUserName: userName,
  69. OpType: OP_COMMIT_REPO,
  70. Content: string(bs),
  71. RepoId: repoId,
  72. RepoName: repoName,
  73. RefName: refName,
  74. })
  75. return err
  76. }
  77. // Update repository last update time.
  78. repo, err := GetRepositoryByName(userId, repoName)
  79. if err != nil {
  80. return err
  81. }
  82. repo.IsBare = false
  83. if err = UpdateRepository(repo); err != nil {
  84. return err
  85. }
  86. log.Trace("action.CommitRepoAction: %d/%s", userId, repo.LowerName)
  87. return nil
  88. }
  89. // NewRepoAction records action for create repository.
  90. func NewRepoAction(user *User, repo *Repository) error {
  91. _, err := orm.InsertOne(&Action{
  92. UserId: user.Id,
  93. ActUserId: user.Id,
  94. ActUserName: user.Name,
  95. OpType: OP_CREATE_REPO,
  96. RepoId: repo.Id,
  97. RepoName: repo.Name,
  98. })
  99. log.Trace("action.NewRepoAction: %s/%s", user.LowerName, repo.LowerName)
  100. return err
  101. }
  102. // GetFeeds returns action list of given user in given context.
  103. func GetFeeds(userid, offset int64, isProfile bool) ([]Action, error) {
  104. actions := make([]Action, 0, 20)
  105. sess := orm.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  106. if isProfile {
  107. sess.And("act_user_id=?", userid)
  108. } else {
  109. sess.And("act_user_id!=?", userid)
  110. }
  111. err := sess.Find(&actions)
  112. return actions, err
  113. }