1
0

notices.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package database
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "time"
  8. "gorm.io/gorm"
  9. log "unknwon.dev/clog/v2"
  10. )
  11. // NoticesStore is the storage layer for system notices.
  12. type NoticesStore struct {
  13. db *gorm.DB
  14. }
  15. func newNoticesStore(db *gorm.DB) *NoticesStore {
  16. return &NoticesStore{db: db}
  17. }
  18. // Create creates a system notice with the given type and description.
  19. func (s *NoticesStore) Create(ctx context.Context, typ NoticeType, desc string) error {
  20. return s.db.WithContext(ctx).Create(
  21. &Notice{
  22. Type: typ,
  23. Description: desc,
  24. },
  25. ).Error
  26. }
  27. // DeleteByIDs deletes system notices by given IDs.
  28. func (s *NoticesStore) DeleteByIDs(ctx context.Context, ids ...int64) error {
  29. return s.db.WithContext(ctx).Where("id IN (?)", ids).Delete(&Notice{}).Error
  30. }
  31. // DeleteAll deletes all system notices.
  32. func (s *NoticesStore) DeleteAll(ctx context.Context) error {
  33. return s.db.WithContext(ctx).Where("TRUE").Delete(&Notice{}).Error
  34. }
  35. // List returns a list of system notices. Results are paginated by given page
  36. // and page size, and sorted by primary key (id) in descending order.
  37. func (s *NoticesStore) List(ctx context.Context, page, pageSize int) ([]*Notice, error) {
  38. notices := make([]*Notice, 0, pageSize)
  39. return notices, s.db.WithContext(ctx).
  40. Limit(pageSize).Offset((page - 1) * pageSize).
  41. Order("id DESC").
  42. Find(&notices).
  43. Error
  44. }
  45. // Count returns the total number of system notices.
  46. func (s *NoticesStore) Count(ctx context.Context) int64 {
  47. var count int64
  48. s.db.WithContext(ctx).Model(&Notice{}).Count(&count)
  49. return count
  50. }
  51. type NoticeType int
  52. const (
  53. NoticeTypeRepository NoticeType = iota + 1
  54. )
  55. // TrStr returns a translation format string.
  56. func (t NoticeType) TrStr() string {
  57. return "admin.notices.type_" + strconv.Itoa(int(t))
  58. }
  59. // Notice represents a system notice for admin.
  60. type Notice struct {
  61. ID int64 `gorm:"primaryKey"`
  62. Type NoticeType
  63. Description string `xorm:"TEXT" gorm:"type:TEXT"`
  64. Created time.Time `xorm:"-" gorm:"-" json:"-"`
  65. CreatedUnix int64
  66. }
  67. // BeforeCreate implements the GORM create hook.
  68. func (n *Notice) BeforeCreate(tx *gorm.DB) error {
  69. if n.CreatedUnix == 0 {
  70. n.CreatedUnix = tx.NowFunc().Unix()
  71. }
  72. return nil
  73. }
  74. // AfterFind implements the GORM query hook.
  75. func (n *Notice) AfterFind(*gorm.DB) error {
  76. n.Created = time.Unix(n.CreatedUnix, 0).Local()
  77. return nil
  78. }
  79. // RemoveAllWithNotice is a helper function to remove all directories in given
  80. // path and creates a system notice in case of an error.
  81. func RemoveAllWithNotice(title, path string) {
  82. if err := os.RemoveAll(path); err != nil {
  83. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  84. if err = Handle.Notices().Create(context.Background(), NoticeTypeRepository, desc); err != nil {
  85. log.Error("Failed to create repository notice: %v", err)
  86. }
  87. }
  88. }