repos / pico

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

pico / tui
Eric Bower · 17 May 24

util.go

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