admin.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "strings"
  7. "time"
  8. "github.com/Unknwon/com"
  9. "github.com/gogits/gogs/modules/base"
  10. )
  11. type NoticeType int
  12. const (
  13. NOTICE_REPOSITORY NoticeType = iota + 1
  14. )
  15. // Notice represents a system notice for admin.
  16. type Notice struct {
  17. ID int64 `xorm:"pk autoincr"`
  18. Type NoticeType
  19. Description string `xorm:"TEXT"`
  20. Created time.Time `xorm:"CREATED"`
  21. }
  22. // TrStr returns a translation format string.
  23. func (n *Notice) TrStr() string {
  24. return "admin.notices.type_" + com.ToStr(n.Type)
  25. }
  26. // CreateNotice creates new system notice.
  27. func CreateNotice(tp NoticeType, desc string) error {
  28. n := &Notice{
  29. Type: tp,
  30. Description: desc,
  31. }
  32. _, err := x.Insert(n)
  33. return err
  34. }
  35. // CreateRepositoryNotice creates new system notice with type NOTICE_REPOSITORY.
  36. func CreateRepositoryNotice(desc string) error {
  37. return CreateNotice(NOTICE_REPOSITORY, desc)
  38. }
  39. // CountNotices returns number of notices.
  40. func CountNotices() int64 {
  41. count, _ := x.Count(new(Notice))
  42. return count
  43. }
  44. // Notices returns number of notices in given page.
  45. func Notices(page, pageSize int) ([]*Notice, error) {
  46. notices := make([]*Notice, 0, pageSize)
  47. return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices)
  48. }
  49. // DeleteNotice deletes a system notice by given ID.
  50. func DeleteNotice(id int64) error {
  51. _, err := x.Id(id).Delete(new(Notice))
  52. return err
  53. }
  54. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  55. func DeleteNotices(start, end int64) error {
  56. sess := x.Where("id >= ?", start)
  57. if end > 0 {
  58. sess.And("id <= ?", end)
  59. }
  60. _, err := sess.Delete(new(Notice))
  61. return err
  62. }
  63. // DeleteNoticesByIDs deletes notices by given IDs.
  64. func DeleteNoticesByIDs(ids []int64) error {
  65. if len(ids) == 0 {
  66. return nil
  67. }
  68. _, err := x.Where("id IN (" + strings.Join(base.Int64sToStrings(ids), ",") + ")").Delete(new(Notice))
  69. return err
  70. }