admin.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "github.com/G-Node/gogs/pkg/setting"
  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 err := os.RemoveAll(path); err != nil {
  62. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  63. log.Warn(desc)
  64. if err = CreateRepositoryNotice(desc); err != nil {
  65. log.Error(2, "CreateRepositoryNotice: %v", err)
  66. }
  67. }
  68. }
  69. // CountNotices returns number of notices.
  70. func CountNotices() int64 {
  71. count, _ := x.Count(new(Notice))
  72. return count
  73. }
  74. // Notices returns number of notices in given page.
  75. func Notices(page, pageSize int) ([]*Notice, error) {
  76. notices := make([]*Notice, 0, pageSize)
  77. return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices)
  78. }
  79. // DeleteNotice deletes a system notice by given ID.
  80. func DeleteNotice(id int64) error {
  81. _, err := x.Id(id).Delete(new(Notice))
  82. return err
  83. }
  84. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  85. func DeleteNotices(start, end int64) error {
  86. sess := x.Where("id >= ?", start)
  87. if end > 0 {
  88. sess.And("id <= ?", end)
  89. }
  90. _, err := sess.Delete(new(Notice))
  91. return err
  92. }
  93. // DeleteNoticesByIDs deletes notices by given IDs.
  94. func DeleteNoticesByIDs(ids []int64) error {
  95. if len(ids) == 0 {
  96. return nil
  97. }
  98. _, err := x.Where("id IN (" + strings.Join(tool.Int64sToStrings(ids), ",") + ")").Delete(new(Notice))
  99. return err
  100. }