action.go 3.2 KB

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