You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.2 KiB

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