action.go 3.4 KB

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