sync/app.go
2025-05-19 18:26:52 -04:00

117 lines
3.8 KiB
Go

package main
import (
"FanslySync/handlers"
"FanslySync/structs"
"FanslySync/utils"
"context"
"fmt"
"github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
ConfigManager handlers.ConfigManager
AppConfig *structs.Config
Logger logger.Logger
}
// NewApp creates a new App application struct
func NewApp() *App {
// Return an empty app
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context, logger logger.Logger) {
a.ctx = ctx
// Setup config manager
configPath, cfgErr := handlers.GetConfigPathForRuntime()
if cfgErr != nil {
// Show message box and quit
utils.ShowMessageBox(a.ctx, "FanslySync | Initialization Error", "Could not get config path.\n\nError: "+cfgErr.Error(), utils.WithDialogType(runtime.ErrorDialog))
runtime.Quit(a.ctx)
return
}
// Create our config manager
a.ConfigManager = handlers.NewFileConfigManager(configPath, logger)
a.Logger = logger
logger.Info("[startup] initializing FanslySync...")
// Check our config path to see if it was set correctly. Will not contain FailedConfigPathFetch
// Do we have an old config file?
shouldMigrate, err := a.ConfigManager.ShouldMigrateOldAppConfig()
if err != nil {
// Show the error in a message box
utils.ShowMessageBox(a.ctx, "FanslySync | Initialization Error", "Could not check for old config file.\n\nError: "+err.Error(), utils.WithDialogType(runtime.ErrorDialog))
runtime.Quit(a.ctx)
return
}
if shouldMigrate {
logger.Info("[startup] migrating old config file...")
// Migrate the old config file
err := a.ConfigManager.MigrateOldAppConfig()
if err != nil {
// Show the error in a message box
utils.ShowMessageBox(a.ctx, "FanslySync | Initialization Error", "Could not migrate old config file.\n\nError: "+err.Error(), utils.WithDialogType(runtime.ErrorDialog))
runtime.Quit(a.ctx)
return
} else {
// Show success message
logger.Info("[startup] old config file migrate ok")
utils.ShowMessageBox(a.ctx, "FanslySync | Notice", "We've detected an old config file (app version < 2.x and below).\n\nThe old config file has been migrated to the new format for you automatically, and the old config file has been deleted.\n\nPlease check your settings to ensure everything is correct.", utils.WithDialogType(runtime.InfoDialog))
// Now grab the new config
cfg, err := a.ConfigManager.GetConfig(false)
if err != nil {
// Show the error in a message box
utils.ShowMessageBox(a.ctx, "FanslySync | Initialization Error", "Could not load config file.\n\nError: "+err.Error(), utils.WithDialogType(runtime.ErrorDialog))
runtime.Quit(a.ctx)
}
// Set the config
a.AppConfig = cfg
}
} else {
// Load config as normal
logger.Info("[startup] loading config file...")
cfg, err := a.ConfigManager.LoadConfigOrCreate()
if err != nil {
// Show the error in a message box
utils.ShowMessageBox(a.ctx, "FanslySync | Initialization Error", "Could not load config file.\n\nError: "+err.Error(), utils.WithDialogType(runtime.ErrorDialog))
runtime.Quit(a.ctx)
}
// Set the config
logger.Info("[startup] config file loaded ok")
a.AppConfig = cfg
}
}
// Greet returns a greeting for the given name
func (a *App) Greet(token string) string {
// Create fansly API instance
fanslyAPI := handlers.NewFanslyAPIController(token, a.Logger)
// Get the user info
account, accountErr := fanslyAPI.GetMe()
if accountErr != nil {
return "Failed to get account info: " + accountErr.Error()
}
// Print the response we got
a.Logger.Info(fmt.Sprintf("[Greet] Account info: %+v", account))
// Return the greeting
return fmt.Sprintf("Hello %s! You have %d fans and %d posts likes.", account.Username, account.FollowCount, account.PostLikes)
}