code.gitea.io/gitea@v1.21.7/models/system/notice.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package system 5 6 import ( 7 "context" 8 "fmt" 9 "time" 10 11 "code.gitea.io/gitea/models/db" 12 "code.gitea.io/gitea/modules/log" 13 "code.gitea.io/gitea/modules/storage" 14 "code.gitea.io/gitea/modules/timeutil" 15 "code.gitea.io/gitea/modules/util" 16 ) 17 18 // NoticeType describes the notice type 19 type NoticeType int 20 21 const ( 22 // NoticeRepository type 23 NoticeRepository NoticeType = iota + 1 24 // NoticeTask type 25 NoticeTask 26 ) 27 28 // Notice represents a system notice for admin. 29 type Notice struct { 30 ID int64 `xorm:"pk autoincr"` 31 Type NoticeType 32 Description string `xorm:"TEXT"` 33 CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` 34 } 35 36 func init() { 37 db.RegisterModel(new(Notice)) 38 } 39 40 // TrStr returns a translation format string. 41 func (n *Notice) TrStr() string { 42 return fmt.Sprintf("admin.notices.type_%d", n.Type) 43 } 44 45 // CreateNotice creates new system notice. 46 func CreateNotice(ctx context.Context, tp NoticeType, desc string, args ...any) error { 47 if len(args) > 0 { 48 desc = fmt.Sprintf(desc, args...) 49 } 50 n := &Notice{ 51 Type: tp, 52 Description: desc, 53 } 54 return db.Insert(ctx, n) 55 } 56 57 // CreateRepositoryNotice creates new system notice with type NoticeRepository. 58 func CreateRepositoryNotice(desc string, args ...any) error { 59 // Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled 60 return CreateNotice(db.DefaultContext, NoticeRepository, desc, args...) 61 } 62 63 // RemoveAllWithNotice removes all directories in given path and 64 // creates a system notice when error occurs. 65 func RemoveAllWithNotice(ctx context.Context, title, path string) { 66 if err := util.RemoveAll(path); err != nil { 67 desc := fmt.Sprintf("%s [%s]: %v", title, path, err) 68 log.Warn(title+" [%s]: %v", path, err) 69 // Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled 70 if err = CreateNotice(db.DefaultContext, NoticeRepository, desc); err != nil { 71 log.Error("CreateRepositoryNotice: %v", err) 72 } 73 } 74 } 75 76 // RemoveStorageWithNotice removes a file from the storage and 77 // creates a system notice when error occurs. 78 func RemoveStorageWithNotice(ctx context.Context, bucket storage.ObjectStorage, title, path string) { 79 if err := bucket.Delete(path); err != nil { 80 desc := fmt.Sprintf("%s [%s]: %v", title, path, err) 81 log.Warn(title+" [%s]: %v", path, err) 82 83 // Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled 84 if err = CreateNotice(db.DefaultContext, NoticeRepository, desc); err != nil { 85 log.Error("CreateRepositoryNotice: %v", err) 86 } 87 } 88 } 89 90 // CountNotices returns number of notices. 91 func CountNotices() int64 { 92 count, _ := db.GetEngine(db.DefaultContext).Count(new(Notice)) 93 return count 94 } 95 96 // Notices returns notices in given page. 97 func Notices(ctx context.Context, page, pageSize int) ([]*Notice, error) { 98 notices := make([]*Notice, 0, pageSize) 99 return notices, db.GetEngine(ctx). 100 Limit(pageSize, (page-1)*pageSize). 101 Desc("created_unix"). 102 Find(¬ices) 103 } 104 105 // DeleteNotice deletes a system notice by given ID. 106 func DeleteNotice(ctx context.Context, id int64) error { 107 _, err := db.GetEngine(ctx).ID(id).Delete(new(Notice)) 108 return err 109 } 110 111 // DeleteNotices deletes all notices with ID from start to end (inclusive). 112 func DeleteNotices(ctx context.Context, start, end int64) error { 113 if start == 0 && end == 0 { 114 _, err := db.GetEngine(ctx).Exec("DELETE FROM notice") 115 return err 116 } 117 118 sess := db.GetEngine(ctx).Where("id >= ?", start) 119 if end > 0 { 120 sess.And("id <= ?", end) 121 } 122 _, err := sess.Delete(new(Notice)) 123 return err 124 } 125 126 // DeleteNoticesByIDs deletes notices by given IDs. 127 func DeleteNoticesByIDs(ctx context.Context, ids []int64) error { 128 if len(ids) == 0 { 129 return nil 130 } 131 _, err := db.GetEngine(ctx). 132 In("id", ids). 133 Delete(new(Notice)) 134 return err 135 } 136 137 // DeleteOldSystemNotices deletes all old system notices from database. 138 func DeleteOldSystemNotices(ctx context.Context, olderThan time.Duration) (err error) { 139 if olderThan <= 0 { 140 return nil 141 } 142 143 _, err = db.GetEngine(ctx).Where("created_unix < ?", time.Now().Add(-olderThan).Unix()).Delete(&Notice{}) 144 return err 145 }