action.go 2.9 KB

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