account

package
v0.0.0-...-e647751 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2022 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Modify action.Action = func(ctx context.Context) error {
	l := logger.WithField("func", "Modify")

	metricsCollector, err := statsd.New(
		viper.GetString(config.Keys.MetricsStatsDAddress),
		viper.GetString(config.Keys.MetricsStatsDPrefix),
	)
	if err != nil {
		l.Errorf("metrics: %s", err.Error())

		return err
	}
	defer func() {
		err := metricsCollector.Close()
		if err != nil {
			l.Errorf("closing metrics: %s", err.Error())
		}
	}()

	l.Info("running database migration")
	dbClient, err := bun.New(ctx, metricsCollector)
	if err != nil {
		l.Errorf("db: %s", err.Error())

		return err
	}
	defer func() {
		err := dbClient.Close(ctx)
		if err != nil {
			l.Errorf("closing db: %s", err.Error())
		}
	}()

	accountString := viper.GetString(config.Keys.AccountAccount)

	username, domain, err := lib.SplitAccount(accountString)
	if err != nil {
		l.Errorf("invalid account %s: %s", accountString, err.Error())

		return err
	}

	instance, err := dbClient.ReadFediInstanceByDomain(ctx, domain)
	if err != nil {
		l.Errorf("db read %s: %s", domain, err.Error())

		return err
	}
	if instance == nil {
		l.Infof("can't find instance %s", domain)

		return nil
	}
	l.Debugf("found instance %d: %+v", instance.ID, instance)

	account, err := dbClient.ReadFediAccountByUsername(ctx, instance.ID, username)
	if err != nil {
		l.Errorf("db read %s: %s", username, err.Error())

		return err
	}
	if instance == nil {
		l.Infof("can't find user %s", username)

		return nil
	}
	l.Debugf("found account %d: %+v", account.ID, account)

	for _, addGroup := range viper.GetStringSlice(config.Keys.AccountAddGroup) {
		switch addGroup {
		case "admin":
			account.Admin = true
		default:
			l.Warnf("unknown group %s, skipping", addGroup)
		}
	}

	err = dbClient.UpdateFediAccount(ctx, account)
	if err != nil {
		l.Errorf("db update: %s", err.Error())

		return err
	}

	return nil
}

Modify runs database migrations.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL