您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

action.go 2.9KB

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