You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.4 KiB
115 lines
2.4 KiB
package resolver
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.aiterp.net/rpdata/api/internal/session"
|
|
"git.aiterp.net/rpdata/api/model/file"
|
|
"git.aiterp.net/rpdata/api/resolver/types"
|
|
)
|
|
|
|
// FileArgs is an arg
|
|
type FileArgs struct {
|
|
ID string
|
|
}
|
|
|
|
// File implements the file query
|
|
func (r *QueryResolver) File(ctx context.Context, args *FileArgs) (*types.FileResolver, error) {
|
|
file, err := file.FindID(args.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &types.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) ([]*types.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([]*types.FileResolver, len(files))
|
|
for i := range files {
|
|
resolvers[i] = &types.FileResolver{F: files[i]}
|
|
}
|
|
|
|
return resolvers, nil
|
|
}
|
|
|
|
// FileEditArgs is args for the editFile mutation
|
|
type FileEditArgs struct {
|
|
Input *struct {
|
|
ID string
|
|
Name *string
|
|
Public *bool
|
|
}
|
|
}
|
|
|
|
// EditFile resolves the editFile mutation
|
|
func (r *MutationResolver) EditFile(ctx context.Context, args *FileEditArgs) (*types.FileResolver, error) {
|
|
input := args.Input
|
|
|
|
user := session.FromContext(ctx).User()
|
|
if user == nil || !user.Permitted("member") {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
|
|
file, err := file.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if file.Author != user.ID && !user.Permitted("file.edit") {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
|
|
err = file.Edit(input.Name, input.Public)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &types.FileResolver{F: file}, nil
|
|
}
|
|
|
|
// RemoveFile resolves the removeFIle mutation
|
|
func (r *MutationResolver) RemoveFile(ctx context.Context, args *FileArgs) (*types.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 &types.FileResolver{F: file}, nil
|
|
}
|