action.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "time"
  7. )
  8. // Operation types of user action.
  9. const (
  10. OP_CREATE_REPO = iota + 1
  11. OP_DELETE_REPO
  12. OP_STAR_REPO
  13. OP_FOLLOW_REPO
  14. OP_COMMIT_REPO
  15. OP_PULL_REQUEST
  16. )
  17. // An Action represents
  18. type Action struct {
  19. Id int64
  20. UserId int64 // Receiver user id.
  21. OpType int
  22. ActUserId int64 // Action user id.
  23. ActUserName string // Action user name.
  24. RepoId int64
  25. RepoName string
  26. Content string
  27. Created time.Time `xorm:"created"`
  28. }
  29. func (a Action) GetOpType() int {
  30. return a.OpType
  31. }
  32. func (a Action) GetActUserName() string {
  33. return a.ActUserName
  34. }
  35. func (a Action) GetRepoName() string {
  36. return a.RepoName
  37. }
  38. // CommitRepoAction records action for commit repository.
  39. func CommitRepoAction(userId int64, userName string,
  40. repoId int64, repoName string, msg string) error {
  41. _, err := orm.InsertOne(&Action{
  42. UserId: userId,
  43. ActUserId: userId,
  44. ActUserName: userName,
  45. OpType: OP_COMMIT_REPO,
  46. Content: msg,
  47. RepoId: repoId,
  48. RepoName: repoName,
  49. })
  50. return err
  51. }
  52. // NewRepoAction records action for create repository.
  53. func NewRepoAction(user *User, repo *Repository) error {
  54. _, err := orm.InsertOne(&Action{
  55. UserId: user.Id,
  56. ActUserId: user.Id,
  57. ActUserName: user.Name,
  58. OpType: OP_CREATE_REPO,
  59. RepoId: repo.Id,
  60. RepoName: repo.Name,
  61. })
  62. return err
  63. }
  64. // GetFeeds returns action list of given user in given context.
  65. func GetFeeds(userid, offset int64, isProfile bool) ([]Action, error) {
  66. actions := make([]Action, 0, 20)
  67. sess := orm.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid)
  68. if isProfile {
  69. sess.And("act_user_id=?", userid)
  70. } else {
  71. sess.And("act_user_id!=?", userid)
  72. }
  73. err := sess.Find(&actions)
  74. return actions, err
  75. }