本站源代码
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.

114 lines
2.7KB

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