2023-07-21 15:16:35 -06:00
|
|
|
package graphvent
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
)
|
|
|
|
|
2023-07-21 19:16:30 -06:00
|
|
|
func PrepResolve(p graphql.ResolveParams) (*ResolveContext, error) {
|
2023-07-21 18:51:42 -06:00
|
|
|
resolve_context, ok := p.Context.Value("resolve").(*ResolveContext)
|
2023-07-21 15:16:35 -06:00
|
|
|
if ok == false {
|
2023-07-21 18:51:42 -06:00
|
|
|
return nil, fmt.Errorf("Bad resolve in params context")
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
2023-07-21 18:51:42 -06:00
|
|
|
return resolve_context, nil
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExtractParam[K interface{}](p graphql.ResolveParams, name string) (K, error) {
|
|
|
|
var zero K
|
|
|
|
arg_if, ok := p.Args[name]
|
|
|
|
if ok == false {
|
|
|
|
return zero, fmt.Errorf("No Arg of name %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg, ok := arg_if.(K)
|
|
|
|
if ok == false {
|
|
|
|
return zero, fmt.Errorf("Failed to cast arg %s(%+v) to %+v", name, arg_if, reflect.TypeOf(zero))
|
|
|
|
}
|
|
|
|
|
|
|
|
return arg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExtractID(p graphql.ResolveParams, name string) (NodeID, error) {
|
|
|
|
id_str, err := ExtractParam[string](p, name)
|
|
|
|
if err != nil {
|
|
|
|
return ZeroID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := ParseID(id_str)
|
|
|
|
if err != nil {
|
|
|
|
return ZeroID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLNodeID(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:16:35 -06:00
|
|
|
node, ok := p.Source.(Node)
|
|
|
|
if ok == false || node == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to cast source to Node")
|
|
|
|
}
|
2023-07-21 19:16:30 -06:00
|
|
|
|
|
|
|
err = UseStates(ctx.Context, []Node{node, ctx.User}, func(nodes NodeMap) error {
|
|
|
|
return node.Allowed("read", "id", ctx.User)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:16:35 -06:00
|
|
|
return node.ID(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLThreadListen(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:16:35 -06:00
|
|
|
node, ok := p.Source.(*GQLThread)
|
|
|
|
if ok == false || node == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to cast source to GQLThread")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
listen := ""
|
2023-07-21 19:16:30 -06:00
|
|
|
err = UseStates(ctx.Context, []Node{node, ctx.User}, func(nodes NodeMap) error {
|
2023-07-21 15:16:35 -06:00
|
|
|
listen = node.Listen
|
2023-07-21 19:16:30 -06:00
|
|
|
return node.Allowed("read", "listen", ctx.User)
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return listen, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLThreadParent(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:16:35 -06:00
|
|
|
node, ok := p.Source.(Thread)
|
|
|
|
if ok == false || node == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to cast source to Thread")
|
|
|
|
}
|
|
|
|
|
|
|
|
var parent Thread = nil
|
2023-07-21 19:16:30 -06:00
|
|
|
err = UseStates(ctx.Context, []Node{node, ctx.User}, func(nodes NodeMap) (error) {
|
2023-07-21 15:16:35 -06:00
|
|
|
parent = node.Parent()
|
2023-07-21 19:16:30 -06:00
|
|
|
return node.Allowed("read", "parent", ctx.User)
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLThreadState(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:16:35 -06:00
|
|
|
node, ok := p.Source.(Thread)
|
|
|
|
if ok == false || node == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to cast source to Thread")
|
|
|
|
}
|
|
|
|
|
|
|
|
var state string
|
2023-07-21 19:16:30 -06:00
|
|
|
err = UseStates(ctx.Context, []Node{node, ctx.User}, func(nodes NodeMap) (error) {
|
2023-07-21 15:16:35 -06:00
|
|
|
state = node.State()
|
2023-07-21 19:16:30 -06:00
|
|
|
return node.Allowed("read", "state", ctx.User)
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLThreadChildren(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:16:35 -06:00
|
|
|
node, ok := p.Source.(Thread)
|
|
|
|
if ok == false || node == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to cast source to Thread")
|
|
|
|
}
|
|
|
|
|
|
|
|
var children []Thread = nil
|
2023-07-21 19:16:30 -06:00
|
|
|
err = UseStates(ctx.Context, []Node{node, ctx.User}, func(nodes NodeMap) (error) {
|
2023-07-21 15:16:35 -06:00
|
|
|
children = node.Children()
|
2023-07-21 19:16:30 -06:00
|
|
|
return node.Allowed("read", "children", ctx.User)
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return children, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLLockableName(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 19:16:30 -06:00
|
|
|
node, ok := p.Source.(Lockable)
|
2023-07-21 15:16:35 -06:00
|
|
|
if ok == false || node == nil {
|
2023-07-21 19:16:30 -06:00
|
|
|
return nil, fmt.Errorf("Failed to cast source to Lockable")
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
name := ""
|
2023-07-21 19:16:30 -06:00
|
|
|
err = UseStates(ctx.Context, []Node{node, ctx.User}, func(nodes NodeMap) error {
|
2023-07-21 15:16:35 -06:00
|
|
|
name = node.Name()
|
2023-07-21 19:16:30 -06:00
|
|
|
return node.Allowed("read", "name", ctx.User)
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLLockableRequirements(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:16:35 -06:00
|
|
|
node, ok := p.Source.(Lockable)
|
|
|
|
if ok == false || node == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to cast source to Lockable")
|
|
|
|
}
|
|
|
|
|
|
|
|
var requirements []Lockable = nil
|
2023-07-21 19:16:30 -06:00
|
|
|
err = UseStates(ctx.Context, []Node{node, ctx.User}, func(nodes NodeMap) (error) {
|
2023-07-21 15:16:35 -06:00
|
|
|
requirements = node.Requirements()
|
2023-07-21 19:16:30 -06:00
|
|
|
return node.Allowed("read", "requirements", ctx.User)
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return requirements, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLLockableDependencies(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:16:35 -06:00
|
|
|
node, ok := p.Source.(Lockable)
|
|
|
|
if ok == false || node == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to cast source to Lockable")
|
|
|
|
}
|
|
|
|
|
|
|
|
var dependencies []Lockable = nil
|
2023-07-21 19:16:30 -06:00
|
|
|
err = UseStates(ctx.Context, []Node{node, ctx.User}, func(nodes NodeMap) (error) {
|
2023-07-21 15:16:35 -06:00
|
|
|
dependencies = node.Dependencies()
|
2023-07-21 19:16:30 -06:00
|
|
|
return node.Allowed("read", "dependencies", ctx.User)
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dependencies, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLLockableOwner(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:16:35 -06:00
|
|
|
node, ok := p.Source.(Lockable)
|
|
|
|
if ok == false || node == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to cast source to Lockable")
|
|
|
|
}
|
|
|
|
|
|
|
|
var owner Node = nil
|
2023-07-21 19:16:30 -06:00
|
|
|
err = UseStates(ctx.Context, []Node{node, ctx.User}, func(nodes NodeMap) (error) {
|
2023-07-21 15:16:35 -06:00
|
|
|
owner = node.Owner()
|
2023-07-21 19:16:30 -06:00
|
|
|
return node.Allowed("read", "owner", ctx.User)
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return owner, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLThreadUsers(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:16:35 -06:00
|
|
|
node, ok := p.Source.(*GQLThread)
|
|
|
|
if ok == false || node == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to cast source to GQLThread")
|
|
|
|
}
|
|
|
|
|
|
|
|
var users []*User
|
2023-07-21 19:16:30 -06:00
|
|
|
err = UseStates(ctx.Context, []Node{node, ctx.User}, func(nodes NodeMap) error {
|
2023-07-21 15:16:35 -06:00
|
|
|
users = make([]*User, len(node.Users))
|
|
|
|
i := 0
|
|
|
|
for _, user := range(node.Users) {
|
|
|
|
users[i] = user
|
|
|
|
i += 1
|
|
|
|
}
|
2023-07-21 19:16:30 -06:00
|
|
|
return node.Allowed("read", "users", ctx.User)
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLSignalFn(p graphql.ResolveParams, fn func(GraphSignal, graphql.ResolveParams)(interface{}, error))(interface{}, error) {
|
|
|
|
if signal, ok := p.Source.(GraphSignal); ok {
|
|
|
|
return fn(signal, p)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Failed to cast source to event")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLSignalType(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return GQLSignalFn(p, func(signal GraphSignal, p graphql.ResolveParams)(interface{}, error){
|
|
|
|
return signal.Type(), nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLSignalSource(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return GQLSignalFn(p, func(signal GraphSignal, p graphql.ResolveParams)(interface{}, error){
|
|
|
|
return signal.Source(), nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLSignalDirection(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return GQLSignalFn(p, func(signal GraphSignal, p graphql.ResolveParams)(interface{}, error){
|
|
|
|
direction := signal.Direction()
|
|
|
|
if direction == Up {
|
|
|
|
return "up", nil
|
|
|
|
} else if direction == Down {
|
|
|
|
return "down", nil
|
|
|
|
} else if direction == Direct {
|
|
|
|
return "direct", nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Invalid direction: %+v", direction)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GQLSignalString(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return GQLSignalFn(p, func(signal GraphSignal, p graphql.ResolveParams)(interface{}, error){
|
|
|
|
return signal.String(), nil
|
|
|
|
})
|
|
|
|
}
|