code.gitea.io/gitea@v1.22.3/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(ctx context.Context) int64 {
    92  	count, _ := db.GetEngine(ctx).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(&notices)
   103  }
   104  
   105  // DeleteNotices deletes all notices with ID from start to end (inclusive).
   106  func DeleteNotices(ctx context.Context, start, end int64) error {
   107  	if start == 0 && end == 0 {
   108  		_, err := db.GetEngine(ctx).Exec("DELETE FROM notice")
   109  		return err
   110  	}
   111  
   112  	sess := db.GetEngine(ctx).Where("id >= ?", start)
   113  	if end > 0 {
   114  		sess.And("id <= ?", end)
   115  	}
   116  	_, err := sess.Delete(new(Notice))
   117  	return err
   118  }
   119  
   120  // DeleteOldSystemNotices deletes all old system notices from database.
   121  func DeleteOldSystemNotices(ctx context.Context, olderThan time.Duration) (err error) {
   122  	if olderThan <= 0 {
   123  		return nil
   124  	}
   125  
   126  	_, err = db.GetEngine(ctx).Where("created_unix < ?", time.Now().Add(-olderThan).Unix()).Delete(&Notice{})
   127  	return err
   128  }