admin.go 3.2 KB

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