Skip to content

Commit

Permalink
Only showing managed servers in the list of servers on public cc page (
Browse files Browse the repository at this point in the history
…#1639)

Co-authored-by: Ashish Jhanwar <[email protected]>
  • Loading branch information
ashishjh-bst and ashishjh-bst authored Apr 17, 2024
1 parent dff2b02 commit c5ca054
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion customcommands/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ func PublicCommandMW(inner http.Handler) http.Handler {
templateData["PublicAccess"] = true

if _, ok := ctx.Value(common.ContextKeyUser).(*discordgo.User); ok {
guilds, _ := web.GetUserGuilds(r.Context())
guilds, _ := web.GetUserManagedGuilds(r.Context())
templateData["UserGuilds"] = guilds
}
// retrieve the cc's page
Expand Down
65 changes: 33 additions & 32 deletions web/handlers_general.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,42 +495,12 @@ func HandleGetManagedGuilds(w http.ResponseWriter, r *http.Request) (TemplateDat
ctx := r.Context()
_, templateData := GetBaseCPContextData(ctx)

user := ContextUser(ctx)

// retrieve guilds this user is part of
// i really wish there was a easy to to invalidate this cache, but since there's not it just expires after 10 seconds
wrapped, err := GetUserGuilds(ctx)
managedGuilds, err := GetUserManagedGuilds(ctx)
if err != nil {
return templateData, err
}

nilled := make([]*common.GuildWithConnected, len(wrapped))
var wg sync.WaitGroup
wg.Add(len(wrapped))

// the servers the user is on and the user has manage server perms
for i, g := range wrapped {
go func(j int, gwc *common.GuildWithConnected) {
conf := common.GetCoreServerConfCached(gwc.ID)
if HasAccesstoGuildSettings(user.ID, gwc, conf, basicRoleProvider, false) {
nilled[j] = gwc
}

wg.Done()
}(i, g)
}

wg.Wait()

accessibleGuilds := make([]*common.GuildWithConnected, 0, len(wrapped))
for _, v := range nilled {
if v != nil {
accessibleGuilds = append(accessibleGuilds, v)
}
}

templateData["ManagedGuilds"] = accessibleGuilds

templateData["ManagedGuilds"] = managedGuilds
return templateData, nil
}

Expand All @@ -550,6 +520,37 @@ func basicRoleProvider(guildID, userID int64) []int64 {
return members[0].Roles
}

func GetUserManagedGuilds(ctx context.Context) ([]*common.GuildWithConnected, error) {
guilds, err := GetUserGuilds(ctx)
if err != nil {
return nil, err
}

user := ContextUser(ctx)
nilled := make([]*common.GuildWithConnected, len(guilds))
var wg sync.WaitGroup
wg.Add(len(guilds))
// the servers the user is on and the user has manage server perms
for i, g := range guilds {
go func(j int, gwc *common.GuildWithConnected) {
conf := common.GetCoreServerConfCached(gwc.ID)
if HasAccesstoGuildSettings(user.ID, gwc, conf, basicRoleProvider, false) {
nilled[j] = gwc
}
wg.Done()
}(i, g)
}

wg.Wait()
accessibleGuilds := make([]*common.GuildWithConnected, 0, len(guilds))
for _, v := range nilled {
if v != nil {
accessibleGuilds = append(accessibleGuilds, v)
}
}
return accessibleGuilds, nil
}

func GetUserGuilds(ctx context.Context) ([]*common.GuildWithConnected, error) {
session := DiscordSessionFromContext(ctx)
user := ContextUser(ctx)
Expand Down

0 comments on commit c5ca054

Please sign in to comment.