refactor: simplify server & service manipulation (#993)

* refactor: simplify server & service manipulation

* update

* fix

* update for nat, ddns & notification

* chore

* update cron

* update dependencies

* use of function iterators

* update default dns servers
This commit is contained in:
UUBulb
2025-02-21 23:08:12 +08:00
committed by GitHub
parent 21eefde995
commit 91bef2882a
32 changed files with 987 additions and 1083 deletions

View File

@@ -1,13 +1,15 @@
package controller
import (
"slices"
"strconv"
"github.com/gin-gonic/gin"
"github.com/jinzhu/copier"
"gorm.io/gorm"
"github.com/nezhahq/nezha/model"
"github.com/nezhahq/nezha/service/singleton"
"gorm.io/gorm"
)
// List notification
@@ -21,11 +23,10 @@ import (
// @Success 200 {object} model.CommonResponse[[]model.Notification]
// @Router /notification [get]
func listNotification(c *gin.Context) ([]*model.Notification, error) {
singleton.NotificationSortedLock.RLock()
defer singleton.NotificationSortedLock.RUnlock()
slist := singleton.NotificationShared.GetSortedList()
var notifications []*model.Notification
if err := copier.Copy(&notifications, &singleton.NotificationListSorted); err != nil {
if err := copier.Copy(&notifications, &slist); err != nil {
return nil, err
}
return notifications, nil
@@ -75,8 +76,7 @@ func createNotification(c *gin.Context) (uint64, error) {
return 0, newGormError("%v", err)
}
singleton.OnRefreshOrAddNotification(&n)
singleton.UpdateNotificationList()
singleton.NotificationShared.Update(&n)
return n.ID, nil
}
@@ -137,8 +137,7 @@ func updateNotification(c *gin.Context) (any, error) {
return nil, newGormError("%v", err)
}
singleton.OnRefreshOrAddNotification(&n)
singleton.UpdateNotificationList()
singleton.NotificationShared.Update(&n)
return nil, nil
}
@@ -159,16 +158,9 @@ func batchDeleteNotification(c *gin.Context) (any, error) {
return nil, err
}
singleton.NotificationsLock.RLock()
for _, nid := range n {
if ns, ok := singleton.NotificationMap[nid]; ok {
if !ns.HasPermission(c) {
singleton.NotificationsLock.RUnlock()
return nil, singleton.Localizer.ErrorT("permission denied")
}
}
if !singleton.NotificationShared.CheckPermission(c, slices.Values(n)) {
return nil, singleton.Localizer.ErrorT("permission denied")
}
singleton.NotificationsLock.RUnlock()
err := singleton.DB.Transaction(func(tx *gorm.DB) error {
if err := tx.Unscoped().Delete(&model.Notification{}, "id in (?)", n).Error; err != nil {
@@ -184,7 +176,6 @@ func batchDeleteNotification(c *gin.Context) (any, error) {
return nil, newGormError("%v", err)
}
singleton.OnDeleteNotification(n)
singleton.UpdateNotificationList()
singleton.NotificationShared.Delete(n)
return nil, nil
}