admin.go 3.0 KB

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