action.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/git"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/log"
  11. )
  12. // Operation types of user action.
  13. const (
  14. OP_CREATE_REPO = iota + 1
  15. OP_DELETE_REPO
  16. OP_STAR_REPO
  17. OP_FOLLOW_REPO
  18. OP_COMMIT_REPO
  19. OP_CREATE_ISSUE
  20. OP_PULL_REQUEST
  21. OP_TRANSFER_REPO
  22. OP_PUSH_TAG
  23. )
  24. // Action represents user operation type and other information to repository.,
  25. // it implemented interface base.Actioner so that can be used in template render.
  26. type Action struct {
  27. Id int64
  28. UserId int64 // Receiver user id.
  29. OpType int // Operations: CREATE DELETE STAR ...
  30. ActUserId int64 // Action user id.
  31. ActUserName string // Action user name.
  32. ActEmail string
  33. RepoId int64
  34. RepoName string
  35. RefName string
  36. Content string `xorm:"TEXT"`
  37. Created time.Time `xorm:"created"`
  38. }
  39. func (a Action) GetOpType() int {
  40. return a.OpType
  41. }
  42. func (a Action) GetActUserName() string {
  43. return a.ActUserName
  44. }
  45. func (a Action) GetActEmail() string {
  46. return a.ActEmail
  47. }
  48. func (a Action) GetRepoName() string {
  49. return a.RepoName
  50. }
  51. func (a Action) GetBranch() string {
  52. return a.RefName
  53. }
  54. func (a Action) GetContent() string {
  55. return a.Content
  56. }
  57. // CommitRepoAction adds new action for committing repository.
  58. func CommitRepoAction(userId int64, userName, actEmail string,
  59. repoId int64, repoName string, refName string, commit *base.PushCommits) error {
  60. // log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
  61. opType := OP_COMMIT_REPO
  62. // Check it's tag push or branch.
  63. // if git.IsTagExist(RepoPath(userName, repoName), refName) {
  64. // opType = OP_PUSH_TAG
  65. // commit = &base.PushCommits{}
  66. // }
  67. bs, err := json.Marshal(commit)
  68. if err != nil {
  69. log.Error("action.CommitRepoAction(json): %d/%s", userId, repoName)
  70. return err
  71. }
  72. if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, ActEmail: actEmail,
  73. OpType: opType, Content: string(bs), RepoId: repoId, RepoName: repoName, RefName: refName}); err != nil {
  74. log.Error("action.CommitRepoAction(notify watchers): %d/%s", userId, repoName)
  75. return err
  76. }
  77. // Change repository bare status and update last updated time.
  78. repo, err := GetRepositoryByName(userId, repoName)
  79. if err != nil {
  80. log.Error("action.CommitRepoAction(GetRepositoryByName): %d/%s", userId, repoName)
  81. return err
  82. }
  83. repo.IsBare = false
  84. if err = UpdateRepository(repo); err != nil {
  85. log.Error("action.CommitRepoAction(UpdateRepository): %d/%s", userId, repoName)
  86. return err
  87. }
  88. log.Trace("action.CommitRepoAction(end): %d/%s", userId, repoName)
  89. return nil
  90. }
  91. // NewRepoAction adds new action for creating repository.
  92. func NewRepoAction(user *User, repo *Repository) (err error) {
  93. if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
  94. OpType: OP_CREATE_REPO, RepoId: repo.Id, RepoName: repo.Name}); err != nil {
  95. log.Error("action.NewRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
  96. return err
  97. }
  98. log.Trace("action.NewRepoAction: %s/%s", user.LowerName, repo.LowerName)
  99. return err
  100. }
  101. // TransferRepoAction adds new action for transfering repository.
  102. func TransferRepoAction(user, newUser *User, repo *Repository) (err error) {
  103. if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
  104. OpType: OP_TRANSFER_REPO, RepoId: repo.Id, RepoName: repo.Name, Content: newUser.Name}); err != nil {
  105. log.Error("action.TransferRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
  106. return err
  107. }
  108. log.Trace("action.TransferRepoAction: %s/%s", user.LowerName, repo.LowerName)
  109. return err
  110. }
  111. // GetFeeds returns action list of given user in given context.
  112. func GetFeeds(userid, offset int64, isProfile bool) ([]Action, error) {
  113. actions := make([]Action, 0, 20)
  114. sess := orm.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  115. if isProfile {
  116. sess.And("act_user_id=?", userid)
  117. } else {
  118. sess.And("act_user_id!=?", userid)
  119. }
  120. err := sess.Find(&actions)
  121. return actions, err
  122. }