use sync group

This commit is contained in:
Dot-Rar 2020-05-29 17:02:27 +01:00
parent 2a1bab393d
commit 04f3def6a0

View File

@ -1,11 +1,14 @@
package api
import (
"context"
"github.com/TicketsBot/GoPanel/database"
"github.com/TicketsBot/GoPanel/utils"
"github.com/TicketsBot/common/permission"
"github.com/gin-gonic/gin"
"github.com/rxdn/gdl/objects/guild"
"golang.org/x/sync/errgroup"
"sync"
)
type wrappedGuild struct {
@ -25,8 +28,13 @@ func GetGuilds(ctx *gin.Context) {
return
}
group, _ := errgroup.WithContext(context.Background())
adminGuilds := make([]wrappedGuild, 0)
var lock sync.Mutex
for _, g := range guilds {
group.Go(func() error {
fakeGuild := guild.Guild{
Id: g.GuildId,
Owner: g.Owner,
@ -38,11 +46,24 @@ func GetGuilds(ctx *gin.Context) {
}
if utils.GetPermissionLevel(g.GuildId, userId) >= permission.Admin {
lock.Lock()
adminGuilds = append(adminGuilds, wrappedGuild{
Id: g.GuildId,
Name: g.Name,
})
lock.Unlock()
}
return nil
})
}
// not possible anyway but eh
if err := group.Wait(); err != nil {
ctx.JSON(500, gin.H{
"success": false,
"error": err.Error(),
})
return
}
ctx.JSON(200, adminGuilds)