From a4fea77047f84f19ce633667ac48cbb8d3379b96 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Wed, 13 Jun 2018 21:11:41 +0200 Subject: [PATCH] Added files API, fixed file.List returning all files if both author and public is unset --- model/file/file.go | 28 ++++++++++++++++++++-------- resolver/file.go | 34 ++++++++++++++++++++++++++++++++++ schema/root.graphql | 3 +++ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/model/file/file.go b/model/file/file.go index 63debcf..026603b 100644 --- a/model/file/file.go +++ b/model/file/file.go @@ -4,6 +4,7 @@ import ( "context" "crypto/rand" "encoding/binary" + "errors" "io" "strconv" "time" @@ -125,19 +126,27 @@ func FindID(id string) (File, error) { return file, nil } -// ListFiles lists files according to the standard lookup. By default it's just the author's own files, +// List lists files according to the standard lookup. By default it's just the author's own files, // but if `public` is true it will alos include files made public by other authors. If `mimeTypes` contains -// any, it will limit the results to that. -func ListFiles(author string, public bool, mimeTypes []string) ([]File, error) { +// any, it will limit the results to that. If `author` is empty, it will only list public files +func List(author string, public bool, mimeTypes []string) ([]File, error) { query := bson.M{} - if public { - query["$or"] = []bson.M{ - bson.M{"author": author}, - bson.M{"public": true}, + if author != "" { + if public { + query["$or"] = []bson.M{ + bson.M{"author": author}, + bson.M{"public": true}, + } + } else { + query["author"] = author } } else { - query["author"] = author + if !public { + return nil, errors.New("No author specified, and public is unset") + } + + query["public"] = true } if len(mimeTypes) > 0 { @@ -183,5 +192,8 @@ func init() { fileCollection = db.C("file.headers") fileCollection.EnsureIndexKey("author") + fileCollection.EnsureIndexKey("public") + fileCollection.EnsureIndexKey("author", "public") + fileCollection.EnsureIndexKey("kind") }) } diff --git a/resolver/file.go b/resolver/file.go index e78e349..410923a 100644 --- a/resolver/file.go +++ b/resolver/file.go @@ -26,6 +26,40 @@ func (r *QueryResolver) File(ctx context.Context, args *FileArgs) (*FileResolver return &FileResolver{F: file}, nil } +// FilesArgs is an arg +type FilesArgs struct { + IncludePublic *bool + MimeTypes *[]string +} + +// Files implements the file query +func (r *QueryResolver) Files(ctx context.Context, args *FilesArgs) ([]*FileResolver, error) { + user := session.FromContext(ctx).User() + author := "" + if user != nil { + author = user.ID + } + + public := args.IncludePublic != nil && *args.IncludePublic == true + + mimeTypes := []string(nil) + if args.MimeTypes != nil { + mimeTypes = *args.MimeTypes + } + + files, err := file.List(author, public, mimeTypes) + if err != nil { + return nil, err + } + + resolvers := make([]*FileResolver, len(files)) + for i := range files { + resolvers[i] = &FileResolver{F: files[i]} + } + + return resolvers, nil +} + // FileEditInput is an input for the editFile mutation type FileEditInput struct { ID string diff --git a/schema/root.graphql b/schema/root.graphql index 77df271..0b84200 100644 --- a/schema/root.graphql +++ b/schema/root.graphql @@ -25,6 +25,9 @@ type Query { # Find file by ID file(id: String!): File + # Files get all available files. If includePublic is set, it will return public files as well + files(includePublic: Boolean, mimeTypes: [String!]): [File!]! + # Find current session session: Session!