notices.go 3.3 KB

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