You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.6 KiB

  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. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/util"
  9. "github.com/Unknwon/com"
  10. )
  11. //NoticeType describes the notice type
  12. type NoticeType int
  13. const (
  14. //NoticeRepository type
  15. NoticeRepository NoticeType = iota + 1
  16. )
  17. // Notice represents a system notice for admin.
  18. type Notice struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. Type NoticeType
  21. Description string `xorm:"TEXT"`
  22. CreatedUnix util.TimeStamp `xorm:"INDEX created"`
  23. }
  24. // TrStr returns a translation format string.
  25. func (n *Notice) TrStr() string {
  26. return "admin.notices.type_" + com.ToStr(n.Type)
  27. }
  28. // CreateNotice creates new system notice.
  29. func CreateNotice(tp NoticeType, desc string) error {
  30. return createNotice(x, tp, desc)
  31. }
  32. func createNotice(e Engine, tp NoticeType, desc string) error {
  33. n := &Notice{
  34. Type: tp,
  35. Description: desc,
  36. }
  37. _, err := e.Insert(n)
  38. return err
  39. }
  40. // CreateRepositoryNotice creates new system notice with type NoticeRepository.
  41. func CreateRepositoryNotice(desc string) error {
  42. return createNotice(x, NoticeRepository, desc)
  43. }
  44. // RemoveAllWithNotice removes all directories in given path and
  45. // creates a system notice when error occurs.
  46. func RemoveAllWithNotice(title, path string) {
  47. removeAllWithNotice(x, title, path)
  48. }
  49. func removeAllWithNotice(e Engine, title, path string) {
  50. if err := util.RemoveAll(path); err != nil {
  51. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  52. log.Warn(desc)
  53. if err = createNotice(e, NoticeRepository, desc); err != nil {
  54. log.Error(4, "CreateRepositoryNotice: %v", err)
  55. }
  56. }
  57. }
  58. // CountNotices returns number of notices.
  59. func CountNotices() int64 {
  60. count, _ := x.Count(new(Notice))
  61. return count
  62. }
  63. // Notices returns notices in given page.
  64. func Notices(page, pageSize int) ([]*Notice, error) {
  65. notices := make([]*Notice, 0, pageSize)
  66. return notices, x.
  67. Limit(pageSize, (page-1)*pageSize).
  68. Desc("id").
  69. Find(&notices)
  70. }
  71. // DeleteNotice deletes a system notice by given ID.
  72. func DeleteNotice(id int64) error {
  73. _, err := x.ID(id).Delete(new(Notice))
  74. return err
  75. }
  76. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  77. func DeleteNotices(start, end int64) error {
  78. sess := x.Where("id >= ?", start)
  79. if end > 0 {
  80. sess.And("id <= ?", end)
  81. }
  82. _, err := sess.Delete(new(Notice))
  83. return err
  84. }
  85. // DeleteNoticesByIDs deletes notices by given IDs.
  86. func DeleteNoticesByIDs(ids []int64) error {
  87. if len(ids) == 0 {
  88. return nil
  89. }
  90. _, err := x.
  91. In("id", ids).
  92. Delete(new(Notice))
  93. return err
  94. }