Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

action.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. )
  9. // Operation types of user action.
  10. const (
  11. OP_CREATE_REPO = iota + 1
  12. OP_DELETE_REPO
  13. OP_STAR_REPO
  14. OP_FOLLOW_REPO
  15. OP_COMMIT_REPO
  16. OP_PULL_REQUEST
  17. )
  18. // Action represents user operation type and information to the repository.
  19. type Action struct {
  20. Id int64
  21. UserId int64 // Receiver user id.
  22. OpType int // Operations: CREATE DELETE STAR ...
  23. ActUserId int64 // Action user id.
  24. ActUserName string // Action user name.
  25. RepoId int64
  26. RepoName string
  27. Content string
  28. Created time.Time `xorm:"created"`
  29. }
  30. func (a Action) GetOpType() int {
  31. return a.OpType
  32. }
  33. func (a Action) GetActUserName() string {
  34. return a.ActUserName
  35. }
  36. func (a Action) GetRepoName() string {
  37. return a.RepoName
  38. }
  39. func (a Action) GetContent() string {
  40. return a.Content
  41. }
  42. // CommitRepoAction records action for commit repository.
  43. func CommitRepoAction(userId int64, userName string,
  44. repoId int64, repoName string, commits [][]string) error {
  45. bs, err := json.Marshal(commits)
  46. if err != nil {
  47. return err
  48. }
  49. // Add feeds for user self and all watchers.
  50. watches, err := GetWatches(repoId)
  51. if err != nil {
  52. return err
  53. }
  54. watches = append(watches, Watch{UserId: userId})
  55. for i := range watches {
  56. if userId == watches[i].UserId && i > 0 {
  57. continue // Do not add twice in case author watches his/her repository.
  58. }
  59. _, err = orm.InsertOne(&Action{
  60. UserId: watches[i].UserId,
  61. ActUserId: userId,
  62. ActUserName: userName,
  63. OpType: OP_COMMIT_REPO,
  64. Content: string(bs),
  65. RepoId: repoId,
  66. RepoName: repoName,
  67. })
  68. return err
  69. }
  70. // Update repository last update time.
  71. repo, err := GetRepositoryByName(userId, repoName)
  72. if err != nil {
  73. return err
  74. }
  75. repo.Updated = time.Now()
  76. if err = UpdateRepository(repo); err != nil {
  77. return err
  78. }
  79. return nil
  80. }
  81. // NewRepoAction records action for create repository.
  82. func NewRepoAction(user *User, repo *Repository) error {
  83. _, err := orm.InsertOne(&Action{
  84. UserId: user.Id,
  85. ActUserId: user.Id,
  86. ActUserName: user.Name,
  87. OpType: OP_CREATE_REPO,
  88. RepoId: repo.Id,
  89. RepoName: repo.Name,
  90. })
  91. return err
  92. }
  93. // GetFeeds returns action list of given user in given context.
  94. func GetFeeds(userid, offset int64, isProfile bool) ([]Action, error) {
  95. actions := make([]Action, 0, 20)
  96. sess := orm.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  97. if isProfile {
  98. sess.And("act_user_id=?", userid)
  99. } else {
  100. sess.And("act_user_id!=?", userid)
  101. }
  102. err := sess.Find(&actions)
  103. return actions, err
  104. }