2021-11-18 11:28:42 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2014-10-09 03:59:18 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-11-18 11:28:42 +05:30
|
|
|
package admin
|
2014-10-09 03:59:18 +05:30
|
|
|
|
|
|
|
import (
|
2021-11-18 11:28:42 +05:30
|
|
|
"context"
|
2016-02-06 00:41:53 +05:30
|
|
|
"fmt"
|
2014-10-09 03:59:18 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2017-03-29 07:35:23 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-08-18 09:53:45 +05:30
|
|
|
"code.gitea.io/gitea/modules/storage"
|
2019-08-15 20:16:21 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2020-08-12 01:35:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2014-10-09 03:59:18 +05:30
|
|
|
)
|
|
|
|
|
2021-03-15 00:22:12 +05:30
|
|
|
// NoticeType describes the notice type
|
2014-10-09 03:59:18 +05:30
|
|
|
type NoticeType int
|
|
|
|
|
|
|
|
const (
|
2021-03-15 00:22:12 +05:30
|
|
|
// NoticeRepository type
|
2016-11-07 21:54:59 +05:30
|
|
|
NoticeRepository NoticeType = iota + 1
|
2020-05-17 05:01:38 +05:30
|
|
|
// NoticeTask type
|
|
|
|
NoticeTask
|
2014-10-09 03:59:18 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Notice represents a system notice for admin.
|
|
|
|
type Notice struct {
|
2015-12-05 11:39:14 +05:30
|
|
|
ID int64 `xorm:"pk autoincr"`
|
2014-10-09 03:59:18 +05:30
|
|
|
Type NoticeType
|
2019-08-15 20:16:21 +05:30
|
|
|
Description string `xorm:"TEXT"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
2014-10-09 03:59:18 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Notice))
|
|
|
|
}
|
|
|
|
|
2014-10-09 03:59:18 +05:30
|
|
|
// TrStr returns a translation format string.
|
|
|
|
func (n *Notice) TrStr() string {
|
2020-12-25 15:29:32 +05:30
|
|
|
return fmt.Sprintf("admin.notices.type_%d", n.Type)
|
2014-10-09 03:59:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// CreateNotice creates new system notice.
|
2021-11-18 23:12:27 +05:30
|
|
|
func CreateNotice(ctx context.Context, tp NoticeType, desc string, args ...interface{}) error {
|
2020-05-17 05:01:38 +05:30
|
|
|
if len(args) > 0 {
|
|
|
|
desc = fmt.Sprintf(desc, args...)
|
|
|
|
}
|
2014-10-09 03:59:18 +05:30
|
|
|
n := &Notice{
|
|
|
|
Type: tp,
|
|
|
|
Description: desc,
|
|
|
|
}
|
2021-11-18 11:28:42 +05:30
|
|
|
return db.Insert(ctx, n)
|
2014-10-09 03:59:18 +05:30
|
|
|
}
|
|
|
|
|
2016-11-07 21:54:59 +05:30
|
|
|
// CreateRepositoryNotice creates new system notice with type NoticeRepository.
|
2020-05-17 05:01:38 +05:30
|
|
|
func CreateRepositoryNotice(desc string, args ...interface{}) error {
|
2022-01-20 04:56:57 +05:30
|
|
|
// Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled
|
2021-11-18 23:12:27 +05:30
|
|
|
return CreateNotice(db.DefaultContext, NoticeRepository, desc, args...)
|
2014-10-09 03:59:18 +05:30
|
|
|
}
|
|
|
|
|
2016-02-06 00:41:53 +05:30
|
|
|
// RemoveAllWithNotice removes all directories in given path and
|
|
|
|
// creates a system notice when error occurs.
|
2021-11-18 23:12:27 +05:30
|
|
|
func RemoveAllWithNotice(ctx context.Context, title, path string) {
|
|
|
|
if err := util.RemoveAll(path); err != nil {
|
2020-08-18 09:53:45 +05:30
|
|
|
desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
|
|
|
|
log.Warn(title+" [%s]: %v", path, err)
|
2022-01-20 04:56:57 +05:30
|
|
|
// Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled
|
|
|
|
if err = CreateNotice(db.DefaultContext, NoticeRepository, desc); err != nil {
|
2020-08-18 09:53:45 +05:30
|
|
|
log.Error("CreateRepositoryNotice: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 23:12:27 +05:30
|
|
|
// RemoveStorageWithNotice removes a file from the storage and
|
2021-11-18 11:28:42 +05:30
|
|
|
// creates a system notice when error occurs.
|
2021-11-18 23:12:27 +05:30
|
|
|
func RemoveStorageWithNotice(ctx context.Context, bucket storage.ObjectStorage, title, path string) {
|
|
|
|
if err := bucket.Delete(path); err != nil {
|
2016-02-06 00:41:53 +05:30
|
|
|
desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
|
2019-06-16 03:50:29 +05:30
|
|
|
log.Warn(title+" [%s]: %v", path, err)
|
2022-01-20 04:56:57 +05:30
|
|
|
|
|
|
|
// Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled
|
|
|
|
if err = CreateNotice(db.DefaultContext, NoticeRepository, desc); err != nil {
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Error("CreateRepositoryNotice: %v", err)
|
2016-02-06 00:41:53 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 03:59:18 +05:30
|
|
|
// CountNotices returns number of notices.
|
|
|
|
func CountNotices() int64 {
|
2021-09-23 21:15:36 +05:30
|
|
|
count, _ := db.GetEngine(db.DefaultContext).Count(new(Notice))
|
2014-10-09 03:59:18 +05:30
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2017-01-09 23:56:05 +05:30
|
|
|
// Notices returns notices in given page.
|
2015-09-25 21:43:38 +05:30
|
|
|
func Notices(page, pageSize int) ([]*Notice, error) {
|
|
|
|
notices := make([]*Notice, 0, pageSize)
|
2021-09-23 21:15:36 +05:30
|
|
|
return notices, db.GetEngine(db.DefaultContext).
|
2016-11-10 20:46:32 +05:30
|
|
|
Limit(pageSize, (page-1)*pageSize).
|
2021-10-07 02:06:24 +05:30
|
|
|
Desc("created_unix").
|
2016-11-10 20:46:32 +05:30
|
|
|
Find(¬ices)
|
2014-10-09 03:59:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteNotice deletes a system notice by given ID.
|
|
|
|
func DeleteNotice(id int64) error {
|
2021-09-23 21:15:36 +05:30
|
|
|
_, err := db.GetEngine(db.DefaultContext).ID(id).Delete(new(Notice))
|
2014-10-09 03:59:18 +05:30
|
|
|
return err
|
|
|
|
}
|
2015-12-02 10:03:08 +05:30
|
|
|
|
|
|
|
// DeleteNotices deletes all notices with ID from start to end (inclusive).
|
|
|
|
func DeleteNotices(start, end int64) error {
|
2021-05-16 17:28:26 +05:30
|
|
|
if start == 0 && end == 0 {
|
2021-09-23 21:15:36 +05:30
|
|
|
_, err := db.GetEngine(db.DefaultContext).Exec("DELETE FROM notice")
|
2021-05-16 17:28:26 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-23 21:15:36 +05:30
|
|
|
sess := db.GetEngine(db.DefaultContext).Where("id >= ?", start)
|
2015-12-02 10:03:08 +05:30
|
|
|
if end > 0 {
|
|
|
|
sess.And("id <= ?", end)
|
|
|
|
}
|
|
|
|
_, err := sess.Delete(new(Notice))
|
|
|
|
return err
|
|
|
|
}
|
2015-12-05 11:39:14 +05:30
|
|
|
|
|
|
|
// DeleteNoticesByIDs deletes notices by given IDs.
|
|
|
|
func DeleteNoticesByIDs(ids []int64) error {
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-23 21:15:36 +05:30
|
|
|
_, err := db.GetEngine(db.DefaultContext).
|
2016-11-12 13:59:18 +05:30
|
|
|
In("id", ids).
|
2016-11-10 20:46:32 +05:30
|
|
|
Delete(new(Notice))
|
2015-12-05 11:39:14 +05:30
|
|
|
return err
|
|
|
|
}
|