From 4424565da48c0281c79a99a81b87030541696e0f Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Sun, 8 Mar 2026 01:23:07 +0100 Subject: [PATCH] bulk edit-delete #636 #1035 --- service/client.go | 56 ++++++++++++++++++++++++++++++++++++++++++-- util/common/array.go | 1 + 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/service/client.go b/service/client.go index 7cb70b6..034cba4 100644 --- a/service/client.go +++ b/service/client.go @@ -62,7 +62,7 @@ func (s *ClientService) Save(tx *gorm.DB, act string, data json.RawMessage, host } if act == "edit" { // Find changed inbounds - inboundIds, err = s.findInboundsChanges(tx, client) + inboundIds, err = s.findInboundsChanges(tx, &client, false) if err != nil { return nil, err } @@ -94,6 +94,54 @@ func (s *ClientService) Save(tx *gorm.DB, act string, data json.RawMessage, host if err != nil { return nil, err } + case "editbulk": + var clients []*model.Client + err = json.Unmarshal(data, &clients) + if err != nil { + return nil, err + } + for _, client := range clients { + changedInboundIds, err := s.findInboundsChanges(tx, client, true) + if err != nil { + return nil, err + } + if len(changedInboundIds) > 0 { + inboundIds = common.UnionUintArray(inboundIds, changedInboundIds) + } + } + if len(inboundIds) > 0 { + err = s.updateLinksWithFixedInbounds(tx, clients, hostname) + if err != nil { + return nil, err + } + } + err = tx.Save(clients).Error + if err != nil { + return nil, err + } + case "delbulk": + var ids []uint + err = json.Unmarshal(data, &ids) + if err != nil { + return nil, err + } + for _, id := range ids { + var client model.Client + err = tx.Where("id = ?", id).First(&client).Error + if err != nil { + return nil, err + } + var clientInbounds []uint + err = json.Unmarshal(client.Inbounds, &clientInbounds) + if err != nil { + return nil, err + } + inboundIds = common.UnionUintArray(inboundIds, clientInbounds) + } + err = tx.Where("id in ?", ids).Delete(model.Client{}).Error + if err != nil { + return nil, err + } case "del": var id uint err = json.Unmarshal(data, &id) @@ -371,7 +419,7 @@ func (s *ClientService) DepleteClients() ([]uint, error) { return inboundIds, nil } -func (s *ClientService) findInboundsChanges(tx *gorm.DB, client model.Client) ([]uint, error) { +func (s *ClientService) findInboundsChanges(tx *gorm.DB, client *model.Client, fillOmitted bool) ([]uint, error) { var err error var oldClient model.Client var oldInboundIds, newInboundIds []uint @@ -379,6 +427,10 @@ func (s *ClientService) findInboundsChanges(tx *gorm.DB, client model.Client) ([ if err != nil { return nil, err } + if fillOmitted { + client.Links = oldClient.Links + client.Config = oldClient.Config + } err = json.Unmarshal(oldClient.Inbounds, &oldInboundIds) if err != nil { return nil, err diff --git a/util/common/array.go b/util/common/array.go index 6a69c1b..46a1716 100644 --- a/util/common/array.go +++ b/util/common/array.go @@ -1,5 +1,6 @@ package common +// UnionUintArray returns a new unique slice that contains all elements from both input slices func UnionUintArray(a []uint, b []uint) []uint { m := make(map[uint]bool) for _, v := range a {