use sync group
This commit is contained in:
parent
2a1bab393d
commit
04f3def6a0
@ -1,11 +1,14 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/TicketsBot/GoPanel/database"
|
"github.com/TicketsBot/GoPanel/database"
|
||||||
"github.com/TicketsBot/GoPanel/utils"
|
"github.com/TicketsBot/GoPanel/utils"
|
||||||
"github.com/TicketsBot/common/permission"
|
"github.com/TicketsBot/common/permission"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/rxdn/gdl/objects/guild"
|
"github.com/rxdn/gdl/objects/guild"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type wrappedGuild struct {
|
type wrappedGuild struct {
|
||||||
@ -25,24 +28,42 @@ func GetGuilds(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
group, _ := errgroup.WithContext(context.Background())
|
||||||
|
|
||||||
adminGuilds := make([]wrappedGuild, 0)
|
adminGuilds := make([]wrappedGuild, 0)
|
||||||
|
var lock sync.Mutex
|
||||||
|
|
||||||
for _, g := range guilds {
|
for _, g := range guilds {
|
||||||
fakeGuild := guild.Guild{
|
group.Go(func() error {
|
||||||
Id: g.GuildId,
|
fakeGuild := guild.Guild{
|
||||||
Owner: g.Owner,
|
Id: g.GuildId,
|
||||||
Permissions: int(g.UserPermissions),
|
Owner: g.Owner,
|
||||||
}
|
Permissions: int(g.UserPermissions),
|
||||||
|
}
|
||||||
|
|
||||||
if g.Owner {
|
if g.Owner {
|
||||||
fakeGuild.OwnerId = userId
|
fakeGuild.OwnerId = userId
|
||||||
}
|
}
|
||||||
|
|
||||||
if utils.GetPermissionLevel(g.GuildId, userId) >= permission.Admin {
|
if utils.GetPermissionLevel(g.GuildId, userId) >= permission.Admin {
|
||||||
adminGuilds = append(adminGuilds, wrappedGuild{
|
lock.Lock()
|
||||||
Id: g.GuildId,
|
adminGuilds = append(adminGuilds, wrappedGuild{
|
||||||
Name: g.Name,
|
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)
|
ctx.JSON(200, adminGuilds)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user