mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 16:34:52 +03:00
146 lines
3.9 KiB
Go
146 lines
3.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
type groupClientIdentity struct {
|
|
ID int
|
|
Email string
|
|
}
|
|
|
|
type GroupBulkScope struct {
|
|
group string
|
|
emails []string
|
|
ids map[string]int
|
|
// Only the adjustment's serialized writer publishes completed inbound mutations.
|
|
committedAdjustments map[string]bool
|
|
}
|
|
|
|
func groupClientEmails(rows []groupClientIdentity) []string {
|
|
emails := make([]string, 0, len(rows))
|
|
for _, row := range rows {
|
|
emails = append(emails, row.Email)
|
|
}
|
|
return emails
|
|
}
|
|
|
|
func ResolveGroupBulkScope(ctx context.Context, group, token string) (*GroupBulkScope, error) {
|
|
db := database.GetDB().WithContext(ctx)
|
|
scope, err := findGroupClientScope(db, group, "remove")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
q := scope.candidates(db)
|
|
var rows []groupClientIdentity
|
|
if token != "" {
|
|
rows, err = scope.frozenClients(q, GroupClientSelection{ScopeToken: token})
|
|
} else {
|
|
err = q.Select("c.id, c.email").Order("c.id").Scan(&rows).Error
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
targets := &GroupBulkScope{group: scope.name, emails: groupClientEmails(rows), ids: make(map[string]int, len(rows))}
|
|
for _, row := range rows {
|
|
targets.ids[row.Email] = row.ID
|
|
}
|
|
return targets, nil
|
|
}
|
|
|
|
func (scope *GroupBulkScope) records(db *gorm.DB, emails []string, lock bool) (map[string]*model.ClientRecord, error) {
|
|
if scope == nil {
|
|
return clientRecordsByEmail(db, emails)
|
|
}
|
|
ids := make([]int, 0, len(emails))
|
|
for _, email := range emails {
|
|
if id, ok := scope.ids[email]; ok {
|
|
ids = append(ids, id)
|
|
}
|
|
}
|
|
slices.Sort(ids)
|
|
matches := make(map[string]*model.ClientRecord, len(ids))
|
|
for _, batch := range chunkInts(ids, sqlInChunk) {
|
|
q := db.Where("id IN ?", batch).Order("id")
|
|
if scope.committedAdjustments == nil {
|
|
q = q.Where("group_name = ?", scope.group)
|
|
}
|
|
if lock && db.Name() == database.DialectPostgres {
|
|
q = q.Clauses(clause.Locking{Strength: "UPDATE"})
|
|
}
|
|
var rows []model.ClientRecord
|
|
if err := q.Find(&rows).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
for i := range rows {
|
|
if scope.ids[rows[i].Email] == rows[i].Id && (rows[i].Group == scope.group || scope.committedAdjustments[rows[i].Email]) {
|
|
matches[rows[i].Email] = &rows[i]
|
|
}
|
|
}
|
|
}
|
|
return matches, nil
|
|
}
|
|
|
|
// Lock the original records and check the link used to plan this inbound write.
|
|
func (scope *GroupBulkScope) inboundRecords(tx *gorm.DB, inboundID int, emails []string) (map[string]*model.ClientRecord, error) {
|
|
if scope == nil {
|
|
return nil, nil
|
|
}
|
|
records, err := scope.records(tx, emails, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(records) != len(emails) {
|
|
return nil, errGroupSelectionStale
|
|
}
|
|
ids := make([]int, 0, len(records))
|
|
for _, rec := range records {
|
|
ids = append(ids, rec.Id)
|
|
}
|
|
slices.Sort(ids)
|
|
for _, batch := range chunkInts(ids, sqlInChunk) {
|
|
var linked []int
|
|
q := tx.Model(&model.ClientInbound{}).
|
|
Where("inbound_id = ? AND client_id IN ?", inboundID, batch).Order("client_id")
|
|
if tx.Name() == database.DialectPostgres {
|
|
q = q.Clauses(clause.Locking{Strength: "UPDATE"})
|
|
}
|
|
if err := q.Pluck("client_id", &linked).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if len(linked) != len(batch) {
|
|
return nil, errGroupSelectionStale
|
|
}
|
|
}
|
|
return records, nil
|
|
}
|
|
|
|
func (scope *GroupBulkScope) skipReason() string {
|
|
if scope == nil {
|
|
return "client not found"
|
|
}
|
|
return errGroupSelectionStale.Error()
|
|
}
|
|
|
|
func linkedGroupBulkClients(tx *gorm.DB, ids []int) (map[int]bool, error) {
|
|
matches := make(map[int]bool)
|
|
for _, batch := range chunkInts(ids, sqlInChunk) {
|
|
var linked []int
|
|
err := tx.Model(&model.ClientInbound{}).Where("client_id IN ?", batch).Distinct().Pluck("client_id", &linked).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, id := range linked {
|
|
matches[id] = true
|
|
}
|
|
}
|
|
return matches, nil
|
|
}
|