2022-09-10 11:39:07 +00:00
|
|
|
package v2migrator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/internal/actors/counter"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/internal/actors/variables"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/internal/service/access"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/internal/service/timer"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/database"
|
2022-09-10 11:39:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s storageFile) migrateCoreKV(db database.Connector) (err error) {
|
2022-10-22 22:08:02 +00:00
|
|
|
as, err := access.New(db)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "creating access service")
|
|
|
|
}
|
2022-09-10 11:39:07 +00:00
|
|
|
|
2023-02-06 18:40:06 +00:00
|
|
|
//nolint:staticcheck // Use of deprecated function is fine for this purpose
|
2022-09-10 11:39:07 +00:00
|
|
|
if err = as.SetBotTwitchCredentials(s.BotAccessToken, s.BotRefreshToken); err != nil {
|
|
|
|
return errors.Wrap(err, "setting bot credentials")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.StoreEncryptedCoreMeta("event_sub_secret", s.EventSubSecret); err != nil {
|
|
|
|
return errors.Wrap(err, "storing bot eventsub token")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storageFile) migrateCounters(db database.Connector) (err error) {
|
2022-10-22 22:08:02 +00:00
|
|
|
for counterName, value := range s.Counters {
|
|
|
|
if err = counter.UpdateCounter(db, counterName, value, true); err != nil {
|
2022-09-10 11:39:07 +00:00
|
|
|
return errors.Wrap(err, "storing counter value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storageFile) migratePermissions(db database.Connector) (err error) {
|
2022-10-22 22:08:02 +00:00
|
|
|
as, err := access.New(db)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "creating access service")
|
|
|
|
}
|
2022-09-10 11:39:07 +00:00
|
|
|
|
|
|
|
for channel, perms := range s.ExtendedPermissions {
|
|
|
|
if err = as.SetExtendedTwitchCredentials(
|
|
|
|
channel,
|
|
|
|
perms.AccessToken,
|
|
|
|
perms.RefreshToken,
|
|
|
|
perms.Scopes,
|
|
|
|
); err != nil {
|
|
|
|
return errors.Wrapf(err, "storing channel %q credentials", channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storageFile) migrateTimers(db database.Connector) (err error) {
|
2023-06-24 12:50:45 +00:00
|
|
|
ts, err := timer.New(db, nil)
|
2022-10-22 22:08:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "creating timer service")
|
|
|
|
}
|
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
for id, expiry := range s.Timers {
|
2022-10-22 22:08:02 +00:00
|
|
|
if err := ts.SetTimer(id, expiry.Time); err != nil {
|
2022-09-10 11:39:07 +00:00
|
|
|
return errors.Wrap(err, "storing counter in database")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storageFile) migrateVariables(db database.Connector) (err error) {
|
|
|
|
for key, value := range s.Variables {
|
2022-10-22 22:08:02 +00:00
|
|
|
if err := variables.SetVariable(db, key, value); err != nil {
|
2022-09-10 11:39:07 +00:00
|
|
|
return errors.Wrap(err, "updating value in database")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|