admin.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 db
  5. import (
  6. "fmt"
  7. "os"
  8. "strings"
  9. "time"
  10. "github.com/unknwon/com"
  11. log "gopkg.in/clog.v1"
  12. "xorm.io/xorm"
  13. "gogs.io/gogs/internal/tool"
  14. )
  15. type NoticeType int
  16. const (
  17. NOTICE_REPOSITORY NoticeType = iota + 1
  18. )
  19. // Notice represents a system notice for admin.
  20. type Notice struct {
  21. ID int64
  22. Type NoticeType
  23. Description string `xorm:"TEXT"`
  24. Created time.Time `xorm:"-" json:"-"`
  25. CreatedUnix int64
  26. }
  27. func (n *Notice) BeforeInsert() {
  28. n.CreatedUnix = time.Now().Unix()
  29. }
  30. func (n *Notice) AfterSet(colName string, _ xorm.Cell) {
  31. switch colName {
  32. case "created_unix":
  33. n.Created = time.Unix(n.CreatedUnix, 0).Local()
  34. }
  35. }
  36. // TrStr returns a translation format string.
  37. func (n *Notice) TrStr() string {
  38. return "admin.notices.type_" + com.ToStr(n.Type)
  39. }
  40. // CreateNotice creates new system notice.
  41. func CreateNotice(tp NoticeType, desc string) error {
  42. // Prevent panic if database connection is not available at this point
  43. if x == nil {
  44. return fmt.Errorf("could not save notice due database connection not being available: %d %s", tp, desc)
  45. }
  46. n := &Notice{
  47. Type: tp,
  48. Description: desc,
  49. }
  50. _, err := x.Insert(n)
  51. return err
  52. }
  53. // CreateRepositoryNotice creates new system notice with type NOTICE_REPOSITORY.
  54. func CreateRepositoryNotice(desc string) error {
  55. return CreateNotice(NOTICE_REPOSITORY, desc)
  56. }
  57. // RemoveAllWithNotice removes all directories in given path and
  58. // creates a system notice when error occurs.
  59. func RemoveAllWithNotice(title, path string) {
  60. if err := os.RemoveAll(path); err != nil {
  61. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  62. log.Warn(desc)
  63. if err = CreateRepositoryNotice(desc); err != nil {
  64. log.Error(2, "CreateRepositoryNotice: %v", err)
  65. }
  66. }
  67. }
  68. // CountNotices returns number of notices.
  69. func CountNotices() int64 {
  70. count, _ := x.Count(new(Notice))
  71. return count
  72. }
  73. // Notices returns number of notices in given page.
  74. func Notices(page, pageSize int) ([]*Notice, error) {
  75. notices := make([]*Notice, 0, pageSize)
  76. return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices)
  77. }
  78. // DeleteNotice deletes a system notice by given ID.
  79. func DeleteNotice(id int64) error {
  80. _, err := x.Id(id).Delete(new(Notice))
  81. return err
  82. }
  83. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  84. func DeleteNotices(start, end int64) error {
  85. sess := x.Where("id >= ?", start)
  86. if end > 0 {
  87. sess.And("id <= ?", end)
  88. }
  89. _, err := sess.Delete(new(Notice))
  90. return err
  91. }
  92. // DeleteNoticesByIDs deletes notices by given IDs.
  93. func DeleteNoticesByIDs(ids []int64) error {
  94. if len(ids) == 0 {
  95. return nil
  96. }
  97. _, err := x.Where("id IN (" + strings.Join(tool.Int64sToStrings(ids), ",") + ")").Delete(new(Notice))
  98. return err
  99. }