action.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. _, err = orm.InsertOne(&Action{
  50. UserId: userId,
  51. ActUserId: userId,
  52. ActUserName: userName,
  53. OpType: OP_COMMIT_REPO,
  54. Content: string(bs),
  55. RepoId: repoId,
  56. RepoName: repoName,
  57. })
  58. return err
  59. }
  60. // NewRepoAction records action for create repository.
  61. func NewRepoAction(user *User, repo *Repository) error {
  62. _, err := orm.InsertOne(&Action{
  63. UserId: user.Id,
  64. ActUserId: user.Id,
  65. ActUserName: user.Name,
  66. OpType: OP_CREATE_REPO,
  67. RepoId: repo.Id,
  68. RepoName: repo.Name,
  69. })
  70. return err
  71. }
  72. // GetFeeds returns action list of given user in given context.
  73. func GetFeeds(userid, offset int64, isProfile bool) ([]Action, error) {
  74. actions := make([]Action, 0, 20)
  75. sess := orm.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  76. if isProfile {
  77. sess.And("act_user_id=?", userid)
  78. } else {
  79. sess.And("act_user_id!=?", userid)
  80. }
  81. err := sess.Find(&actions)
  82. return actions, err
  83. }