repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

pico / tui
Antonio Mika · 08 Oct 24

util.go

 1package tui
 2
 3import (
 4	"errors"
 5	"fmt"
 6
 7	"github.com/picosh/pico/db"
 8	"github.com/picosh/pico/tui/common"
 9	"github.com/picosh/utils"
10)
11
12func findUser(shrd common.SharedModel) (*db.User, error) {
13	logger := shrd.Cfg.Logger
14	var user *db.User
15	usr := shrd.Session.User()
16
17	if shrd.Session.PublicKey() == nil {
18		return nil, fmt.Errorf("unable to find public key")
19	}
20
21	key := utils.KeyForKeyText(shrd.Session.PublicKey())
22
23	user, err := shrd.Dbpool.FindUserForKey(usr, key)
24	if err != nil {
25		logger.Error("no user found for public key", "err", err.Error())
26		// we only want to throw an error for specific cases
27		if errors.Is(err, &db.ErrMultiplePublicKeys{}) {
28			return nil, err
29		}
30		// no user and not error indicates we need to create an account
31		return nil, nil
32	}
33
34	return user, nil
35}
36
37func findPlusFeatureFlag(shrd common.SharedModel) (*db.FeatureFlag, error) {
38	if shrd.User == nil {
39		return nil, nil
40	}
41
42	ff, err := shrd.Dbpool.FindFeatureForUser(shrd.User.ID, "plus")
43	if err != nil {
44		return nil, err
45	}
46
47	return ff, nil
48}