-
Notifications
You must be signed in to change notification settings - Fork 1
/
elementSets.go
146 lines (122 loc) · 3.92 KB
/
elementSets.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
package main
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/core/fileaccess"
protos "github.com/pixlise/core/v4/generated-protos"
"go.mongodb.org/mongo-driver/mongo"
)
type SrcElementLines struct {
AtomicNumber int8 `json:"Z"` // 118 still fits! Will we break past 127 any time soon? :)
K bool `json:"K"`
L bool `json:"L"`
M bool `json:"M"`
Esc bool `json:"Esc"`
}
type SrcElementSet struct {
Name string `json:"name"`
Lines []SrcElementLines `json:"lines"`
*SrcAPIObjectItem
}
type SrcElementSetLookup map[string]SrcElementSet
func migrateElementSets(userContentBucket string, userContentFiles []string, fs fileaccess.FileAccess, dest *mongo.Database, userGroups map[string]string) error {
coll := dest.Collection(dbCollections.ElementSetsName)
err := coll.Drop(context.TODO())
if err != nil {
return err
}
sharedItems := SrcElementSetLookup{}
for _, p := range userContentFiles {
if strings.HasSuffix(p, "ElementSets.json") && strings.HasPrefix(p, "UserContent/shared/") {
// Read this file
items := SrcElementSetLookup{}
err = fs.ReadJSON(userContentBucket, p, &items, false)
if err != nil {
return err
}
// Store these till we're finished here
sharedItems = items
}
}
destSets := []interface{}{}
allItems := SrcElementSetLookup{}
for _, p := range userContentFiles {
if strings.HasSuffix(p, "ElementSets.json") && !strings.HasPrefix(p, "UserContent/shared/") {
userIdFromPath := filepath.Base(filepath.Dir(p))
if shouldIgnoreUser(userIdFromPath) {
fmt.Printf(" SKIPPING import of element set from user: %v aka %v\n", userIdFromPath, usersIdsToIgnore[userIdFromPath])
continue
}
// Read this file
items := SrcElementSetLookup{}
err = fs.ReadJSON(userContentBucket, p, &items, false)
if err != nil {
return err
}
// Write these to DB and also remember them for later...
for id, item := range items {
if ex, ok := allItems[id]; ok {
fmt.Printf("Duplicate: %v - %v vs %v\n", id, item.Name, ex.Name)
continue
}
allItems[id] = item
destSet := protos.ElementSet{
Id: id,
Name: item.Name,
Lines: []*protos.ElementLine{},
}
for _, line := range item.Lines {
destSet.Lines = append(destSet.Lines, &protos.ElementLine{
Z: int32(line.AtomicNumber),
K: line.K,
L: line.L,
M: line.M,
Esc: line.Esc,
})
}
viewerGroupId := ""
if removeIfElementSet(item, sharedItems) {
viewerGroupId = userGroups["PIXL-FM"]
}
err = saveOwnershipItem(destSet.Id, protos.ObjectType_OT_ELEMENT_SET, item.Creator.UserID, "", viewerGroupId, uint32(item.CreatedUnixTimeSec), dest)
if err != nil {
return err
}
destSets = append(destSets, &destSet)
}
}
}
result, err := coll.InsertMany(context.TODO(), destSets)
if err != nil {
return err
}
fmt.Printf("Element sets inserted: %v\n", len(result.InsertedIDs))
// Report what was shared
for sharedId, sharedItem := range sharedItems {
found := false
for itemId, item := range allItems {
if item.Name == sharedItem.Name && item.Creator.UserID == sharedItem.Creator.UserID && len(sharedItem.Lines) == len(item.Lines) {
fmt.Printf("User %v item %v, named %v seems shared as %v\n", item.Creator.UserID, itemId, item.Name, sharedId)
found = true
break
}
}
if !found {
fmt.Printf("Shared %v item is orphaned:%+v\n", sharedId, sharedItem)
}
}
return err
}
func removeIfElementSet(elementSet SrcElementSet, sharedElementSets SrcElementSetLookup) bool {
for c, sharedItem := range sharedElementSets {
if elementSet.Name == sharedItem.Name && elementSet.Creator.UserID == sharedItem.Creator.UserID && len(sharedItem.Lines) == len(elementSet.Lines) {
// Remove this from the shared list
delete(sharedElementSets, c)
return true
}
}
return false
}