1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// Copyright 2023 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package db
import (
"context"
"fmt"
"os"
"strconv"
"time"
"gorm.io/gorm"
log "unknwon.dev/clog/v2"
)
// NoticesStore is the persistent interface for system notices.
type NoticesStore interface {
// Create creates a system notice with the given type and description.
Create(ctx context.Context, typ NoticeType, desc string) error
// DeleteByIDs deletes system notices by given IDs.
DeleteByIDs(ctx context.Context, ids ...int64) error
// DeleteAll deletes all system notices.
DeleteAll(ctx context.Context) error
// List returns a list of system notices. Results are paginated by given page
// and page size, and sorted by primary key (id) in descending order.
List(ctx context.Context, page, pageSize int) ([]*Notice, error)
// Count returns the total number of system notices.
Count(ctx context.Context) int64
}
var Notices NoticesStore
var _ NoticesStore = (*notices)(nil)
type notices struct {
*gorm.DB
}
// NewNoticesStore returns a persistent interface for system notices with given
// database connection.
func NewNoticesStore(db *gorm.DB) NoticesStore {
return ¬ices{DB: db}
}
func (db *notices) Create(ctx context.Context, typ NoticeType, desc string) error {
return db.WithContext(ctx).Create(
&Notice{
Type: typ,
Description: desc,
},
).Error
}
func (db *notices) DeleteByIDs(ctx context.Context, ids ...int64) error {
return db.WithContext(ctx).Where("id IN (?)", ids).Delete(&Notice{}).Error
}
func (db *notices) DeleteAll(ctx context.Context) error {
return db.WithContext(ctx).Where("TRUE").Delete(&Notice{}).Error
}
func (db *notices) List(ctx context.Context, page, pageSize int) ([]*Notice, error) {
notices := make([]*Notice, 0, pageSize)
return notices, db.WithContext(ctx).
Limit(pageSize).Offset((page - 1) * pageSize).
Order("id DESC").
Find(¬ices).
Error
}
func (db *notices) Count(ctx context.Context) int64 {
var count int64
db.WithContext(ctx).Model(&Notice{}).Count(&count)
return count
}
type NoticeType int
const (
NoticeTypeRepository NoticeType = iota + 1
)
// TrStr returns a translation format string.
func (t NoticeType) TrStr() string {
return "admin.notices.type_" + strconv.Itoa(int(t))
}
// Notice represents a system notice for admin.
type Notice struct {
ID int64 `gorm:"primarykey"`
Type NoticeType
Description string `xorm:"TEXT" gorm:"type:TEXT"`
Created time.Time `xorm:"-" gorm:"-" json:"-"`
CreatedUnix int64
}
// BeforeCreate implements the GORM create hook.
func (n *Notice) BeforeCreate(tx *gorm.DB) error {
if n.CreatedUnix == 0 {
n.CreatedUnix = tx.NowFunc().Unix()
}
return nil
}
// AfterFind implements the GORM query hook.
func (n *Notice) AfterFind(_ *gorm.DB) error {
n.Created = time.Unix(n.CreatedUnix, 0).Local()
return nil
}
// RemoveAllWithNotice is a helper function to remove all directories in given
// path and creates a system notice in case of an error.
func RemoveAllWithNotice(title, path string) {
if err := os.RemoveAll(path); err != nil {
desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
if err = Notices.Create(context.Background(), NoticeTypeRepository, desc); err != nil {
log.Error("Failed to create repository notice: %v", err)
}
}
}
|