-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphql.go
166 lines (130 loc) · 3.31 KB
/
graphql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"context"
"fmt"
"path/filepath"
"github.com/shurcooL/graphql"
)
type Tag struct {
ID graphql.ID `graphql:"id"`
Name graphql.String `graphql:"name"`
}
type Scene struct {
ID graphql.ID
Title *graphql.String
Path graphql.String
Details *graphql.String
Tags []Tag
}
func (s Scene) getTagIds() []graphql.ID {
ret := []graphql.ID{}
for _, t := range s.Tags {
ret = append(ret, t.ID)
}
return ret
}
type ConfigGeneralResult struct {
GeneratedPath graphql.String `graphql:"generatedPath"`
}
type ConfigResult struct {
General ConfigGeneralResult `graphql:"general"`
}
func getSpriteDir(client *graphql.Client) (string, error) {
var m struct {
Configuration *ConfigResult `graphql:"configuration"`
}
err := client.Query(context.Background(), &m, nil)
if err != nil {
return "", fmt.Errorf("Error getting sprite directory from configuration: %s", err.Error())
}
ret := filepath.Join(string(m.Configuration.General.GeneratedPath), "vtt")
return ret, nil
}
func addTagId(tagIds []graphql.ID, tagId graphql.ID) []graphql.ID {
for _, t := range tagIds {
if t == tagId {
return tagIds
}
}
tagIds = append(tagIds, tagId)
return tagIds
}
func findSceneFromChecksum(client *graphql.Client, checksum string) (*Scene, error) {
var m struct {
FindScene *Scene `graphql:"findScene(checksum: $c)"`
}
vars := map[string]interface{}{
"c": graphql.String(checksum),
}
err := client.Query(context.Background(), &m, vars)
if err != nil {
return nil, err
}
return m.FindScene, nil
}
type SceneHashInput struct {
Oshash *graphql.String `graphql:"oshash" json:"oshash"`
}
func findSceneFromOshash(client *graphql.Client, oshash string) (*Scene, error) {
var m struct {
FindScene *Scene `graphql:"findSceneByHash(input: $i)"`
}
input := SceneHashInput{
Oshash: graphql.NewString(graphql.String(oshash)),
}
vars := map[string]interface{}{
"i": input,
}
err := client.Query(context.Background(), &m, vars)
if err != nil {
return nil, err
}
return m.FindScene, nil
}
type SceneUpdate struct {
ID graphql.ID `graphql:"id"`
}
type BulkUpdateIds struct {
IDs []graphql.ID `graphql:"ids" json:"ids"`
Mode graphql.String `graphql:"mode" json:"mode"`
}
func updateScene(client *graphql.Client, s Scene, details string, duplicateTagID *graphql.ID) error {
// use BulkSceneUpdateInput since sceneUpdate requires performers, etc.
var m struct {
SceneUpdate []SceneUpdate `graphql:"bulkSceneUpdate(input: {ids: $ids, details: $details, tag_ids: $tag_ids})"`
}
ids := []graphql.ID{s.ID}
detailsInput := graphql.String(details)
tagIds := &BulkUpdateIds{}
if duplicateTagID != nil {
tagIds.Mode = "ADD"
tagIds.IDs = addTagId(tagIds.IDs, *duplicateTagID)
}
vars := map[string]interface{}{
"ids": ids,
"details": detailsInput,
"tag_ids": tagIds,
}
err := client.Mutate(context.Background(), &m, vars)
if err != nil {
return err
}
return nil
}
func getDuplicateTagId(client *graphql.Client, tagName string) (*graphql.ID, error) {
var m struct {
AllTags []Tag `graphql:"allTags"`
}
err := client.Query(context.Background(), &m, nil)
if err != nil {
fmt.Printf("Error getting tags: %s\n", err.Error())
return nil, err
}
for _, t := range m.AllTags {
if string(t.Name) == tagName {
id := t.ID
return &id, nil
}
}
return nil, err
}