Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

admin.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 models
  5. import (
  6. "fmt"
  7. "time"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/util"
  10. "github.com/Unknwon/com"
  11. "github.com/go-xorm/xorm"
  12. )
  13. //NoticeType describes the notice type
  14. type NoticeType int
  15. const (
  16. //NoticeRepository type
  17. NoticeRepository NoticeType = iota + 1
  18. )
  19. // Notice represents a system notice for admin.
  20. type Notice struct {
  21. ID int64 `xorm:"pk autoincr"`
  22. Type NoticeType
  23. Description string `xorm:"TEXT"`
  24. Created time.Time `xorm:"-"`
  25. CreatedUnix int64 `xorm:"INDEX"`
  26. }
  27. // BeforeInsert is invoked from XORM before inserting an object of this type.
  28. func (n *Notice) BeforeInsert() {
  29. n.CreatedUnix = time.Now().Unix()
  30. }
  31. // AfterSet is invoked from XORM after setting the value of a field of this object.
  32. func (n *Notice) AfterSet(colName string, _ xorm.Cell) {
  33. switch colName {
  34. case "created_unix":
  35. n.Created = time.Unix(n.CreatedUnix, 0).Local()
  36. }
  37. }
  38. // TrStr returns a translation format string.
  39. func (n *Notice) TrStr() string {
  40. return "admin.notices.type_" + com.ToStr(n.Type)
  41. }
  42. // CreateNotice creates new system notice.
  43. func CreateNotice(tp NoticeType, desc string) error {
  44. return createNotice(x, tp, desc)
  45. }
  46. func createNotice(e Engine, tp NoticeType, desc string) error {
  47. n := &Notice{
  48. Type: tp,
  49. Description: desc,
  50. }
  51. _, err := e.Insert(n)
  52. return err
  53. }
  54. // CreateRepositoryNotice creates new system notice with type NoticeRepository.
  55. func CreateRepositoryNotice(desc string) error {
  56. return createNotice(x, NoticeRepository, desc)
  57. }
  58. // RemoveAllWithNotice removes all directories in given path and
  59. // creates a system notice when error occurs.
  60. func RemoveAllWithNotice(title, path string) {
  61. removeAllWithNotice(x, title, path)
  62. }
  63. func removeAllWithNotice(e Engine, title, path string) {
  64. if err := util.RemoveAll(path); err != nil {
  65. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  66. log.Warn(desc)
  67. if err = createNotice(e, NoticeRepository, desc); err != nil {
  68. log.Error(4, "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 notices in given page.
  78. func Notices(page, pageSize int) ([]*Notice, error) {
  79. notices := make([]*Notice, 0, pageSize)
  80. return notices, x.
  81. Limit(pageSize, (page-1)*pageSize).
  82. Desc("id").
  83. Find(&notices)
  84. }
  85. // DeleteNotice deletes a system notice by given ID.
  86. func DeleteNotice(id int64) error {
  87. _, err := x.Id(id).Delete(new(Notice))
  88. return err
  89. }
  90. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  91. func DeleteNotices(start, end int64) error {
  92. sess := x.Where("id >= ?", start)
  93. if end > 0 {
  94. sess.And("id <= ?", end)
  95. }
  96. _, err := sess.Delete(new(Notice))
  97. return err
  98. }
  99. // DeleteNoticesByIDs deletes notices by given IDs.
  100. func DeleteNoticesByIDs(ids []int64) error {
  101. if len(ids) == 0 {
  102. return nil
  103. }
  104. _, err := x.
  105. In("id", ids).
  106. Delete(new(Notice))
  107. return err
  108. }