action.go 2.0 KB

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