Skip to content

Commit

Permalink
entitled sets param for gql user emote sets (#251)
Browse files Browse the repository at this point in the history
* entitled sets param for gql user emote sets

* disable user search

* user search: exact search as temporary filler
  • Loading branch information
AnatoleAM authored Dec 15, 2023
1 parent c270344 commit c47b8c8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,6 @@ github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1Avp
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/seventv/common v0.0.0-20231212092216-2658fa1b97ba h1:byOf2HOo7LZyyfg8j7LePLtODt1vBjOk4D6dBGNETo8=
github.com/seventv/common v0.0.0-20231212092216-2658fa1b97ba/go.mod h1:jHHFe3uNMyzb/ReDqMvaI/A1euvV/PW/G+2PhcWQqWw=
github.com/seventv/common v0.0.0-20231212140741-9412bffd7525 h1:3LdJj533sxENKoUkJLx3p7WHj0YuLko+CyitAYvC5pk=
github.com/seventv/common v0.0.0-20231212140741-9412bffd7525/go.mod h1:jHHFe3uNMyzb/ReDqMvaI/A1euvV/PW/G+2PhcWQqWw=
github.com/seventv/common v0.0.0-20231212143655-048a247f3aa4 h1:TNDOnWqZfRJSdnjD4jq8RueLQD8rL52+6HgPXg9W5s0=
github.com/seventv/common v0.0.0-20231212143655-048a247f3aa4/go.mod h1:jHHFe3uNMyzb/ReDqMvaI/A1euvV/PW/G+2PhcWQqWw=
github.com/seventv/compactdisc v0.0.0-20221006190906-ccfe99954e48 h1:IqWrtt/yob45YnOQ5Wwkbf8qP22eKVtg0WzfyEkGnFg=
Expand Down
14 changes: 11 additions & 3 deletions internal/api/gql/v3/resolvers/query/query.users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package query

import (
"context"
"strings"
"time"

"github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -44,12 +45,19 @@ func (r *Resolver) Users(ctx context.Context, queryArg string, pageArg *int, lim
}

actor := auth.For(ctx)
if actor.ID.IsZero() {
return nil, errors.ErrUnauthorized()
}

isManager := actor.HasPermission(structures.RolePermissionManageUsers)

// Temporary measure until search is optimized
if !isManager {
user, err := r.Ctx.Inst().Loaders.UserByUsername().Load(strings.ToLower((queryArg)))
if err != nil {
return nil, err
}

return []*model.UserPartial{modelgql.UserPartialModel(r.Ctx.Inst().Modelizer.User(user).ToPartial())}, nil
}

// Unprivileged users must provide a query
if !isManager && len(queryArg) < 2 {
return nil, errors.ErrInvalidRequest().SetDetail("query must be at least 2 characters long")
Expand Down
31 changes: 30 additions & 1 deletion internal/api/gql/v3/resolvers/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,41 @@ func New(r types.Resolver) generated.UserResolver {
return &Resolver{r}
}

func (r *Resolver) EmoteSets(ctx context.Context, obj *model.User) ([]*model.EmoteSet, error) {
func (r *Resolver) EmoteSets(ctx context.Context, obj *model.User, entitled *bool) ([]*model.EmoteSet, error) {
sets, err := r.Ctx.Inst().Loaders.EmoteSetByUserID().Load(obj.ID)
if err != nil {
return nil, err
}

if entitled != nil && *entitled {
ownedSets := make(utils.Set[primitive.ObjectID])
for _, set := range sets {
if set.OwnerID == obj.ID {
continue
}

ownedSets.Add(set.ID)
}

res, err := r.Ctx.Inst().Loaders.EntitlementsLoader().Load(obj.ID)
if err == nil {
ids := make(utils.Set[primitive.ObjectID])
ids.Fill(utils.Map(res.EmoteSets, func(x structures.Entitlement[structures.EntitlementDataEmoteSet]) primitive.ObjectID {
return x.Data.RefID
})...)

entitledSets, _ := r.Ctx.Inst().Loaders.EmoteSetByID().LoadAll(ids.Values())

for _, set := range entitledSets {
if set.OwnerID == obj.ID {
continue
}

sets = append(sets, set)
}
}
}

result := make([]*model.EmoteSet, len(sets))
for i, v := range sets {
result[i] = modelgql.EmoteSetModel(r.Ctx.Inst().Modelizer.EmoteSet(v))
Expand Down
2 changes: 1 addition & 1 deletion internal/api/gql/v3/schema/users.gql
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type User {

roles: [ObjectID!]!

emote_sets: [EmoteSet!]! @goField(forceResolver: true)
emote_sets(entitled: Boolean): [EmoteSet!]! @goField(forceResolver: true)
owned_emotes: [Emote!]! @goField(forceResolver: true)
activity(limit: Int): [AuditLog!]! @goField(forceResolver: true)
connections(type: [ConnectionPlatform!]): [UserConnection]!
Expand Down

0 comments on commit c47b8c8

Please sign in to comment.