package resolver import ( "context" "time" "git.aiterp.net/rpdata/api/internal/session" "git.aiterp.net/rpdata/api/model/file" ) // FileResolver for the File graphql type type FileResolver struct{ F file.File } // FileArgs is an arg type FileArgs struct { ID string } // File implements the file query func (r *QueryResolver) File(ctx context.Context, args *FileArgs) (*FileResolver, error) { file, err := file.FindID(args.ID) if err != nil { return nil, err } 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 Name *string Public *bool } // EditFile resolves the editFile mutation func (r *MutationResolver) EditFile(ctx context.Context, args *struct{ Input FileEditInput }) (*FileResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member") { return nil, ErrUnauthorized } file, err := file.FindID(args.Input.ID) if err != nil { return nil, err } if file.Author != user.ID && !user.Permitted("file.edit") { return nil, ErrUnauthorized } err = file.Edit(args.Input.Name, args.Input.Public) if err != nil { return nil, err } return &FileResolver{F: file}, nil } // RemoveFile resolves the removeFIle mutation func (r *MutationResolver) RemoveFile(ctx context.Context, args *FileArgs) (*FileResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member") { return nil, ErrUnauthorized } file, err := file.FindID(args.ID) if err != nil { return nil, err } if file.Author != user.ID && !user.Permitted("file.remove") { return nil, ErrUnauthorized } err = file.Delete() if err != nil { return nil, err } return &FileResolver{F: file}, nil } // ID resolves File.id func (f *FileResolver) ID() string { return f.F.ID } // Author resolves File.author func (f *FileResolver) Author() string { return f.F.Author } // Kind resolves File.kind func (f *FileResolver) Kind() string { return f.F.Kind } // Time resolves File.time func (f *FileResolver) Time() string { return f.F.Time.Format(time.RFC3339Nano) } // Public resolves File.public func (f *FileResolver) Public() bool { return f.F.Public } // Name resolves File.name func (f *FileResolver) Name() string { return f.F.Name } // MimeType resolves File.mimeType func (f *FileResolver) MimeType() string { return f.F.MimeType } // Size resolves File.size func (f *FileResolver) Size() int32 { return int32(f.F.Size) } // URL resolves File.url func (f *FileResolver) URL() *string { if f.F.URL == "" { return nil } return &f.F.URL }