action.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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
  22. OpType int
  23. RepoId int64
  24. Content string
  25. Created time.Time `xorm:"created"`
  26. }
  27. type NewRepoContent struct {
  28. UserName string
  29. RepoName string
  30. }
  31. // NewRepoAction inserts action for create repository.
  32. func NewRepoAction(user *User, repo *Repository) error {
  33. content, err := json.Marshal(&NewRepoContent{user.Name, repo.Name})
  34. if err != nil {
  35. return err
  36. }
  37. _, err = orm.InsertOne(&Action{
  38. UserId: user.Id,
  39. OpType: OP_CREATE_REPO,
  40. RepoId: repo.Id,
  41. Content: string(content),
  42. })
  43. return err
  44. }
  45. func GetFeeds(userid, offset int64) ([]Action, error) {
  46. actions := make([]Action, 0, 20)
  47. err := orm.Limit(20, int(offset)).Desc("id").Where("user_id=?", userid).Find(&actions)
  48. return actions, err
  49. }