diff --git a/go.sum b/go.sum index 88b96b69..942e2894 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/api/gql/v3/resolvers/query/query.users.go b/internal/api/gql/v3/resolvers/query/query.users.go index 66326227..be05488a 100644 --- a/internal/api/gql/v3/resolvers/query/query.users.go +++ b/internal/api/gql/v3/resolvers/query/query.users.go @@ -2,6 +2,7 @@ package query import ( "context" + "strings" "time" "github.com/hashicorp/go-multierror" @@ -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") diff --git a/internal/api/gql/v3/resolvers/user/user.go b/internal/api/gql/v3/resolvers/user/user.go index a9ad146a..794581c1 100644 --- a/internal/api/gql/v3/resolvers/user/user.go +++ b/internal/api/gql/v3/resolvers/user/user.go @@ -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)) diff --git a/internal/api/gql/v3/schema/users.gql b/internal/api/gql/v3/schema/users.gql index e2f43469..3fc0de0e 100644 --- a/internal/api/gql/v3/schema/users.gql +++ b/internal/api/gql/v3/schema/users.gql @@ -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]!