package controller import ( "net/http" "github.com/QuantumNous/new-api/common" "github.com/QuantumNous/new-api/model" "github.com/QuantumNous/new-api/service" "github.com/QuantumNous/new-api/setting" "github.com/QuantumNous/new-api/setting/ratio_setting" "github.com/gin-gonic/gin" ) func GetGroups(c *gin.Context) { groupNames := make([]string, 0) for groupName := range ratio_setting.GetGroupRatioCopy() { groupNames = append(groupNames, groupName) } c.JSON(http.StatusOK, gin.H{ "success": true, "message": "", "data": groupNames, }) } func GetUserGroups(c *gin.Context) { usableGroups := make(map[string]map[string]any) enabledGroups, err := model.GetEnabledGroupSet() if err != nil { common.ApiError(c, err) return } userGroup := "" userId := c.GetInt("id") userGroup, _ = model.GetUserGroup(userId, false) if err := model.LoadPersonalGroupRatios(c, userId); err != nil { common.ApiError(c, err) return } metadata, err := model.GetGroupMetadata(c.Request.Context()) if err != nil { common.ApiError(c, err) return } recent, err := service.RecentDegradedGroups(c.Request.Context()) if err != nil { common.ApiError(c, err) return } userUsableGroups := service.GetUserUsableGroups(userGroup) for groupName, _ := range ratio_setting.GetGroupRatioCopy() { if _, ok := enabledGroups[groupName]; !ok { continue } // UserUsableGroups contains the groups that the user can use if desc, ok := userUsableGroups[groupName]; ok { usableGroups[groupName] = map[string]any{ "ratio": model.EffectiveGroupRatio(c, userGroup, groupName).GroupRatio, "base_ratio": ratio_setting.GetGroupRatio(groupName), "has_personal_ratio": model.HasPersonalGroupRatio(c, groupName), "tags": metadata[groupName].Tags, "recent_degraded": recent[groupName] && !metadata[groupName].HideRecentDegraded, "desc": desc, } } } if len(usableGroups) > 0 { if _, ok := userUsableGroups["auto"]; ok { usableGroups["auto"] = map[string]any{ "ratio": "自动", "desc": setting.GetUsableGroupDescription("auto"), } } } c.JSON(http.StatusOK, gin.H{ "success": true, "message": "", "data": usableGroups, }) }