action.go 3.0 KB

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