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