in functions/events-all-firebase/all_firebase.go [40:127]
func allFirebaseFunction(ctx context.Context, e event.Event) error {
// Parse the JSON into a generic map
var event map[string]interface{}
if err := json.Unmarshal([]byte(e.Data()), &event); err != nil {
return fmt.Errorf("error parsing JSON: %v", err)
}
// Expect events to have a base64 encoded 'data' field. This doesn't
// make for great messages. Decode and replace before using.
// Stat by checking for 'message'
message, ok := event["message"].(map[string]interface{})
if !ok {
return fmt.Errorf("message field not found")
}
// Extract and decode the 'data' field from 'message'
if dataEncoded, ok := message["data"].(string); ok {
dataDecoded, err := base64.StdEncoding.DecodeString(dataEncoded)
if err != nil {
return fmt.Errorf("error decoding base64: %v", err)
}
// Parse the JSON into a generic map, so we get a complete
// JSON doc after we add it back to the message a re-serialize
var data map[string]interface{}
if err := json.Unmarshal([]byte(dataDecoded), &data); err != nil {
return fmt.Errorf("error parsing JSON from 'data' field: %v", err)
}
message["data"] = data
} else {
return fmt.Errorf("data field not found within message or not a string")
}
// Move all attributes to the message itself (for ease of use in the web app)
var attributes = message["attributes"].(map[string]interface{})
for key, value := range attributes {
// log.Printf("attribute: %v : %v\n", key, value)
message[key] = value
}
delete(message, "attributes")
// Time to publish to Firebase
app, err := firebase.NewApp(context.Background(), nil)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
// Check if event is starting a game and if so, clear out the previous events
if message["PinballEventType"] == "GameStarted" {
log.Println("GameStarted")
var buff bytes.Buffer
deleteCollection(&buff, ctx, *client, "LiveGameEvents", 40)
log.Print(buff)
}
currentTimeUTC := time.Now().UTC()
timestampUnix := currentTimeUTC.UnixMilli()
// Add the utcTimestamp field to the message data
message["utcTimestamp"] = timestampUnix
publishTime, ok := message["publishTime"].(string)
if !ok {
return fmt.Errorf("messageId field not found")
}
_, err = client.Collection("LiveGameEvents").Doc(publishTime).Set(ctx, message)
if err != nil {
// If that doesn't work, not much we can do. Log and exit.
log.Fatalf("An error has occurred: %s", err)
}
// Write to the AllGameEvents collection
_, err = client.Collection("AllGameEvents").Doc(publishTime).Set(ctx, message)
if err != nil {
log.Fatalf("Failed to write to AllGameEvents: %v", err)
}
return nil
}