MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / UpdateAbilities

Method UpdateAbilities

model/ability.go:223–291  ·  view source on GitHub ↗

UpdateAbilities updates abilities of this channel. Make sure the channel is completed before calling this function.

(tx *gorm.DB)

Source from the content-addressed store, hash-verified

221// UpdateAbilities updates abilities of this channel.
222// Make sure the channel is completed before calling this function.
223func (channel *Channel) UpdateAbilities(tx *gorm.DB) error {
224 isNewTx := false
225 // 如果没有传入事务,创建新的事务
226 if tx == nil {
227 tx = DB.Begin()
228 if tx.Error != nil {
229 return tx.Error
230 }
231 isNewTx = true
232 defer func() {
233 if r := recover(); r != nil {
234 tx.Rollback()
235 }
236 }()
237 }
238
239 // First delete all abilities of this channel
240 err := tx.Where("channel_id = ?", channel.Id).Delete(&Ability{}).Error
241 if err != nil {
242 if isNewTx {
243 tx.Rollback()
244 }
245 return err
246 }
247
248 // Then add new abilities
249 models_ := strings.Split(channel.Models, ",")
250 groups_ := strings.Split(channel.Group, ",")
251 abilitySet := make(map[string]struct{})
252 abilities := make([]Ability, 0, len(models_))
253 for _, model := range models_ {
254 for _, group := range groups_ {
255 key := group + "|" + model
256 if _, exists := abilitySet[key]; exists {
257 continue
258 }
259 abilitySet[key] = struct{}{}
260 ability := Ability{
261 Group: group,
262 Model: model,
263 ChannelId: channel.Id,
264 Enabled: channel.Status == common.ChannelStatusEnabled,
265 Priority: channel.Priority,
266 Weight: uint(channel.GetWeight()),
267 Tag: channel.Tag,
268 }
269 abilities = append(abilities, ability)
270 }
271 }
272
273 if len(abilities) > 0 {
274 for _, chunk := range lo.Chunk(abilities, 50) {
275 err = tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&chunk).Error
276 if err != nil {
277 if isNewTx {
278 tx.Rollback()
279 }
280 return err

Callers 3

UpdateMethod · 0.95
EditChannelByTagFunction · 0.80
BatchSetChannelTagFunction · 0.80

Calls 2

GetWeightMethod · 0.95
DeleteMethod · 0.45

Tested by

no test coverage detected