92 lines
2.0 KiB
Go
Raw Normal View History

2019-05-11 07:03:37 +00:00
package graphql
import (
"fmt"
"strings"
"github.com/aerogo/aero"
"github.com/aerogo/graphql"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2019-05-11 07:03:37 +00:00
)
var (
empty = struct{}{}
privateCollections = map[string]struct{}{
"PayPalPayment": empty,
"Purchase": empty,
"EmailToUser": empty,
"Session": empty,
"EditLogEntry": empty,
}
)
func Install(app *aero.Application) {
api := graphql.New(arn.DB)
// Block private collections
api.AddRootResolver(func(name string, arguments graphql.Map) (interface{}, error, bool) {
typeName := strings.TrimPrefix(name, "all")
typeName = strings.TrimPrefix(typeName, "like")
_, private := privateCollections[typeName]
if private {
return nil, fmt.Errorf("Type '%s' is private", typeName), true
}
return nil, nil, false
})
// Like objects
2019-10-21 05:02:09 +00:00
// api.AddRootResolver(func(name string, arguments graphql.Map) (interface{}, error, bool) {
// if !strings.HasPrefix(name, "like") {
// return nil, nil, false
// }
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// id, ok := arguments["ID"].(string)
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// if !ok {
// return nil, fmt.Errorf("'%s' needs to specify an ID", name), true
// }
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// typeName := strings.TrimPrefix(name, "like")
// obj, err := arn.DB.Get(typeName, id)
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// if err != nil {
// return nil, err, true
// }
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// field := reflect.ValueOf(obj).Elem().FieldByName("IsDraft")
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// if field.IsValid() && field.Bool() {
// return nil, errors.New("Drafts need to be published before they can be liked"), true
// }
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// likeable, ok := obj.(arn.Likeable)
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// if !ok {
// return nil, fmt.Errorf("'%s' does not implement the Likeable interface", name), true
// }
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// // TODO: Authentication
// // user := GetUserFromContext(ctx)
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// // if user == nil {
// // return errors.New("Not logged in")
// // }
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// // likeable.Like(user.ID)
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// // Call OnLike if the object implements it
// // receiver, ok := likeable.(LikeEventReceiver)
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// // if ok {
// // receiver.OnLike(user)
// // }
2019-05-11 07:03:37 +00:00
2019-10-21 05:02:09 +00:00
// likeable.Save()
// return obj, nil, true
// })
2019-05-11 07:03:37 +00:00
app.Post("/graphql", api.Handler())
}