(ctx context.Context, config *hctx.ClientConfig, db *gorm.DB, userSecret string)
| 644 | } |
| 645 | |
| 646 | func registerAndBootstrapDevice(ctx context.Context, config *hctx.ClientConfig, db *gorm.DB, userSecret string) error { |
| 647 | registerPath := "/api/v1/register?user_id=" + data.UserId(userSecret) + "&device_id=" + config.DeviceId |
| 648 | if isIntegrationTestDevice() { |
| 649 | registerPath += "&is_integration_test_device=true" |
| 650 | } |
| 651 | _, err := lib.ApiGet(ctx, registerPath) |
| 652 | if err != nil { |
| 653 | return fmt.Errorf("failed to register device with backend: %w", err) |
| 654 | } |
| 655 | |
| 656 | respBody, err := lib.ApiGet(ctx, "/api/v1/bootstrap?user_id="+data.UserId(userSecret)+"&device_id="+config.DeviceId) |
| 657 | if err != nil { |
| 658 | return fmt.Errorf("failed to bootstrap device from the backend: %w", err) |
| 659 | } |
| 660 | var retrievedEntries []*shared.EncHistoryEntry |
| 661 | err = json.Unmarshal(respBody, &retrievedEntries) |
| 662 | if err != nil { |
| 663 | return fmt.Errorf("failed to load JSON response: %w", err) |
| 664 | } |
| 665 | hctx.GetLogger().Infof("Bootstrapping new device: Found %d entries", len(retrievedEntries)) |
| 666 | for _, entry := range retrievedEntries { |
| 667 | decEntry, err := data.DecryptHistoryEntry(userSecret, *entry) |
| 668 | if err != nil { |
| 669 | return fmt.Errorf("failed to decrypt history entry from server: %w", err) |
| 670 | } |
| 671 | lib.AddToDbIfNew(db, decEntry) |
| 672 | } |
| 673 | |
| 674 | return nil |
| 675 | } |
| 676 | |
| 677 | func isIntegrationTestDevice() bool { |
| 678 | if os.Getenv("HISHTORY_TEST") != "" { |
no test coverage detected