admin.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package models
  7. import (
  8. "fmt"
  9. "gitote/gitote/pkg/tool"
  10. "os"
  11. "strings"
  12. "time"
  13. raven "github.com/getsentry/raven-go"
  14. "github.com/go-xorm/xorm"
  15. "gitlab.com/gitote/com"
  16. log "gopkg.in/clog.v1"
  17. )
  18. //NoticeType describes the notice type
  19. type NoticeType int
  20. const (
  21. //NoticeRepository type
  22. NoticeRepository NoticeType = iota + 1
  23. )
  24. // Notice represents a system notice for admin.
  25. type Notice struct {
  26. ID int64
  27. Type NoticeType
  28. Description string `xorm:"TEXT"`
  29. Created time.Time `xorm:"-" json:"-"`
  30. CreatedUnix int64
  31. }
  32. // BeforeInsert will be invoked by XORM before inserting a record
  33. func (n *Notice) BeforeInsert() {
  34. n.CreatedUnix = time.Now().Unix()
  35. }
  36. // AfterSet is invoked from XORM after setting the values of all fields of this object.
  37. func (n *Notice) AfterSet(colName string, _ xorm.Cell) {
  38. switch colName {
  39. case "created_unix":
  40. n.Created = time.Unix(n.CreatedUnix, 0).Local()
  41. }
  42. }
  43. // TrStr returns a translation format string.
  44. func (n *Notice) TrStr() string {
  45. return "admin.notices.type_" + com.ToStr(n.Type)
  46. }
  47. // CreateNotice creates new system notice.
  48. func CreateNotice(tp NoticeType, desc string) error {
  49. // Prevent panic if database connection is not available at this point
  50. if x == nil {
  51. return fmt.Errorf("could not save notice due database connection not being available: %d %s", tp, desc)
  52. }
  53. n := &Notice{
  54. Type: tp,
  55. Description: desc,
  56. }
  57. _, err := x.Insert(n)
  58. return err
  59. }
  60. // CreateRepositoryNotice creates new system notice with type NoticeRepository.
  61. func CreateRepositoryNotice(desc string) error {
  62. return CreateNotice(NoticeRepository, desc)
  63. }
  64. // RemoveAllWithNotice removes all directories in given path and
  65. // creates a system notice when error occurs.
  66. func RemoveAllWithNotice(title, path string) {
  67. if err := os.RemoveAll(path); err != nil {
  68. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  69. log.Warn(desc)
  70. if err = CreateRepositoryNotice(desc); err != nil {
  71. raven.CaptureErrorAndWait(err, nil)
  72. log.Error(2, "CreateRepositoryNotice: %v", err)
  73. }
  74. }
  75. }
  76. // CountNotices returns number of notices.
  77. func CountNotices() int64 {
  78. count, _ := x.Count(new(Notice))
  79. return count
  80. }
  81. // Notices returns number of notices in given page.
  82. func Notices(page, pageSize int) ([]*Notice, error) {
  83. notices := make([]*Notice, 0, pageSize)
  84. return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices)
  85. }
  86. // DeleteNotice deletes a system notice by given ID.
  87. func DeleteNotice(id int64) error {
  88. _, err := x.Id(id).Delete(new(Notice))
  89. return err
  90. }
  91. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  92. func DeleteNotices(start, end int64) error {
  93. sess := x.Where("id >= ?", start)
  94. if end > 0 {
  95. sess.And("id <= ?", end)
  96. }
  97. _, err := sess.Delete(new(Notice))
  98. return err
  99. }
  100. // DeleteNoticesByIDs deletes notices by given IDs.
  101. func DeleteNoticesByIDs(ids []int64) error {
  102. if len(ids) == 0 {
  103. return nil
  104. }
  105. _, err := x.Where("id IN (" + strings.Join(tool.Int64sToStrings(ids), ",") + ")").Delete(new(Notice))
  106. return err
  107. }