mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-14 10:57:54 +02:00
refactor: remove instance notice level handling and related localization
This commit is contained in:
parent
b92f648555
commit
d82c0ee3fb
@ -54,41 +54,16 @@ type RepositoryStruct struct {
|
|||||||
GitGuideRemoteName *config.Value[string]
|
GitGuideRemoteName *config.Value[string]
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
InstanceNoticeLevelInfo = "info"
|
|
||||||
InstanceNoticeLevelSuccess = "success"
|
|
||||||
InstanceNoticeLevelWarning = "warning"
|
|
||||||
InstanceNoticeLevelDanger = "danger"
|
|
||||||
)
|
|
||||||
|
|
||||||
type InstanceNotice struct {
|
type InstanceNotice struct {
|
||||||
Enabled bool
|
Enabled bool
|
||||||
Message string
|
Message string
|
||||||
Level string
|
|
||||||
|
|
||||||
StartTime int64
|
StartTime int64
|
||||||
EndTime int64
|
EndTime int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultInstanceNotice() InstanceNotice {
|
func DefaultInstanceNotice() InstanceNotice {
|
||||||
return InstanceNotice{
|
return InstanceNotice{}
|
||||||
Level: InstanceNoticeLevelInfo,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsValidInstanceNoticeLevel(level string) bool {
|
|
||||||
switch level {
|
|
||||||
case InstanceNoticeLevelInfo, InstanceNoticeLevelSuccess, InstanceNoticeLevelWarning, InstanceNoticeLevelDanger:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *InstanceNotice) Normalize() {
|
|
||||||
if !IsValidInstanceNoticeLevel(n.Level) {
|
|
||||||
n.Level = InstanceNoticeLevelInfo
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *InstanceNotice) IsActive(now int64) bool {
|
func (n *InstanceNotice) IsActive(now int64) bool {
|
||||||
@ -105,9 +80,7 @@ func (n *InstanceNotice) IsActive(now int64) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetInstanceNotice(ctx context.Context) InstanceNotice {
|
func GetInstanceNotice(ctx context.Context) InstanceNotice {
|
||||||
notice := Config().InstanceNotice.Banner.Value(ctx)
|
return Config().InstanceNotice.Banner.Value(ctx)
|
||||||
notice.Normalize()
|
|
||||||
return notice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type InstanceNoticeStruct struct {
|
type InstanceNoticeStruct struct {
|
||||||
|
|||||||
@ -3283,11 +3283,6 @@
|
|||||||
"admin.config.instance_notice.message": "Message",
|
"admin.config.instance_notice.message": "Message",
|
||||||
"admin.config.instance_notice.message_placeholder": "This message supports Markdown.",
|
"admin.config.instance_notice.message_placeholder": "This message supports Markdown.",
|
||||||
"admin.config.instance_notice.message_required": "Message is required when the banner is enabled.",
|
"admin.config.instance_notice.message_required": "Message is required when the banner is enabled.",
|
||||||
"admin.config.instance_notice.level": "Level",
|
|
||||||
"admin.config.instance_notice.level.info": "Info",
|
|
||||||
"admin.config.instance_notice.level.success": "Success",
|
|
||||||
"admin.config.instance_notice.level.warning": "Warning",
|
|
||||||
"admin.config.instance_notice.level.danger": "Danger",
|
|
||||||
"admin.config.instance_notice.message_too_long": "Message must be at most %d characters.",
|
"admin.config.instance_notice.message_too_long": "Message must be at most %d characters.",
|
||||||
"admin.config.instance_notice.start_time": "Start time",
|
"admin.config.instance_notice.start_time": "Start time",
|
||||||
"admin.config.instance_notice.end_time": "End time",
|
"admin.config.instance_notice.end_time": "End time",
|
||||||
|
|||||||
@ -213,7 +213,6 @@ func parseDatetimeLocalValue(raw string) (int64, error) {
|
|||||||
|
|
||||||
func SetInstanceNotice(ctx *context.Context) {
|
func SetInstanceNotice(ctx *context.Context) {
|
||||||
saveInstanceNotice := func(instanceNotice setting.InstanceNotice) {
|
saveInstanceNotice := func(instanceNotice setting.InstanceNotice) {
|
||||||
instanceNotice.Normalize()
|
|
||||||
marshaled, err := json.Marshal(instanceNotice)
|
marshaled, err := json.Marshal(instanceNotice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("Marshal", err)
|
ctx.ServerError("Marshal", err)
|
||||||
@ -240,7 +239,6 @@ func SetInstanceNotice(ctx *context.Context) {
|
|||||||
|
|
||||||
enabled := ctx.FormBool("enabled")
|
enabled := ctx.FormBool("enabled")
|
||||||
message := strings.TrimSpace(ctx.FormString("message"))
|
message := strings.TrimSpace(ctx.FormString("message"))
|
||||||
level := strings.TrimSpace(ctx.FormString("level"))
|
|
||||||
startTime, err := parseDatetimeLocalValue(strings.TrimSpace(ctx.FormString("start_time")))
|
startTime, err := parseDatetimeLocalValue(strings.TrimSpace(ctx.FormString("start_time")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Flash.Error(ctx.Tr("admin.config.instance_notice.invalid_time"))
|
ctx.Flash.Error(ctx.Tr("admin.config.instance_notice.invalid_time"))
|
||||||
@ -253,9 +251,6 @@ func SetInstanceNotice(ctx *context.Context) {
|
|||||||
ctx.Redirect(setting.AppSubURL + "/-/admin/config#instance-notice")
|
ctx.Redirect(setting.AppSubURL + "/-/admin/config#instance-notice")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !setting.IsValidInstanceNoticeLevel(level) {
|
|
||||||
level = setting.InstanceNoticeLevelInfo
|
|
||||||
}
|
|
||||||
if enabled && message == "" {
|
if enabled && message == "" {
|
||||||
ctx.Flash.Error(ctx.Tr("admin.config.instance_notice.message_required"))
|
ctx.Flash.Error(ctx.Tr("admin.config.instance_notice.message_required"))
|
||||||
ctx.Redirect(setting.AppSubURL + "/-/admin/config#instance-notice")
|
ctx.Redirect(setting.AppSubURL + "/-/admin/config#instance-notice")
|
||||||
@ -275,7 +270,6 @@ func SetInstanceNotice(ctx *context.Context) {
|
|||||||
instanceNotice := setting.InstanceNotice{
|
instanceNotice := setting.InstanceNotice{
|
||||||
Enabled: enabled,
|
Enabled: enabled,
|
||||||
Message: message,
|
Message: message,
|
||||||
Level: level,
|
|
||||||
StartTime: startTime,
|
StartTime: startTime,
|
||||||
EndTime: endTime,
|
EndTime: endTime,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,7 +26,6 @@ func TestInstanceNoticeVisibility(t *testing.T) {
|
|||||||
setInstanceNoticeForTest(t, setting.InstanceNotice{
|
setInstanceNoticeForTest(t, setting.InstanceNotice{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
Message: "Planned **upgrade** in progress.",
|
Message: "Planned **upgrade** in progress.",
|
||||||
Level: setting.InstanceNoticeLevelWarning,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("AnonymousUserSeesBanner", func(t *testing.T) {
|
t.Run("AnonymousUserSeesBanner", func(t *testing.T) {
|
||||||
@ -60,7 +59,6 @@ func TestInstanceNoticeTimeWindow(t *testing.T) {
|
|||||||
setInstanceNoticeForTest(t, setting.InstanceNotice{
|
setInstanceNoticeForTest(t, setting.InstanceNotice{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
Message: "Future banner",
|
Message: "Future banner",
|
||||||
Level: setting.InstanceNoticeLevelInfo,
|
|
||||||
StartTime: now + 3600,
|
StartTime: now + 3600,
|
||||||
EndTime: now + 7200,
|
EndTime: now + 7200,
|
||||||
})
|
})
|
||||||
@ -71,7 +69,6 @@ func TestInstanceNoticeTimeWindow(t *testing.T) {
|
|||||||
setInstanceNoticeForTest(t, setting.InstanceNotice{
|
setInstanceNoticeForTest(t, setting.InstanceNotice{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
Message: "Expired banner",
|
Message: "Expired banner",
|
||||||
Level: setting.InstanceNoticeLevelInfo,
|
|
||||||
StartTime: now - 7200,
|
StartTime: now - 7200,
|
||||||
EndTime: now - 3600,
|
EndTime: now - 3600,
|
||||||
})
|
})
|
||||||
@ -88,25 +85,21 @@ func TestInstanceNoticeAdminCRUD(t *testing.T) {
|
|||||||
req := NewRequestWithValues(t, "POST", "/-/admin/config/instance_notice", map[string]string{
|
req := NewRequestWithValues(t, "POST", "/-/admin/config/instance_notice", map[string]string{
|
||||||
"enabled": "true",
|
"enabled": "true",
|
||||||
"message": "Admin set banner",
|
"message": "Admin set banner",
|
||||||
"level": "danger",
|
|
||||||
})
|
})
|
||||||
adminSession.MakeRequest(t, req, http.StatusSeeOther)
|
adminSession.MakeRequest(t, req, http.StatusSeeOther)
|
||||||
|
|
||||||
notice := setting.GetInstanceNotice(t.Context())
|
notice := setting.GetInstanceNotice(t.Context())
|
||||||
assert.True(t, notice.Enabled)
|
assert.True(t, notice.Enabled)
|
||||||
assert.Equal(t, "Admin set banner", notice.Message)
|
assert.Equal(t, "Admin set banner", notice.Message)
|
||||||
assert.Equal(t, setting.InstanceNoticeLevelDanger, notice.Level)
|
|
||||||
|
|
||||||
req = NewRequestWithValues(t, "POST", "/-/admin/config/instance_notice", map[string]string{
|
req = NewRequestWithValues(t, "POST", "/-/admin/config/instance_notice", map[string]string{
|
||||||
"enabled": "true",
|
"enabled": "true",
|
||||||
"message": strings.Repeat("a", 2001),
|
"message": strings.Repeat("a", 2001),
|
||||||
"level": "warning",
|
|
||||||
})
|
})
|
||||||
adminSession.MakeRequest(t, req, http.StatusSeeOther)
|
adminSession.MakeRequest(t, req, http.StatusSeeOther)
|
||||||
|
|
||||||
notice = setting.GetInstanceNotice(t.Context())
|
notice = setting.GetInstanceNotice(t.Context())
|
||||||
assert.Equal(t, "Admin set banner", notice.Message)
|
assert.Equal(t, "Admin set banner", notice.Message)
|
||||||
assert.Equal(t, setting.InstanceNoticeLevelDanger, notice.Level)
|
|
||||||
|
|
||||||
req = NewRequestWithValues(t, "POST", "/-/admin/config/instance_notice", map[string]string{
|
req = NewRequestWithValues(t, "POST", "/-/admin/config/instance_notice", map[string]string{
|
||||||
"action": "delete",
|
"action": "delete",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user