notices.go 2.9 KB

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