repos / pico

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

commit
84f8549
parent
b93f4f4
author
Eric Bower
date
2024-05-15 02:43:42 +0000 UTC
refactor(plus): make plus a single feature (#128)

8 files changed,  +26, -38
M auth/api.go
+7, -3
 1@@ -274,7 +274,12 @@ func keyHandler(w http.ResponseWriter, r *http.Request) {
 2 		return
 3 	}
 4 
 5-	if !client.Dbpool.HasFeatureForUser(user.ID, space) {
 6+	if space == "tuns" {
 7+		if !client.Dbpool.HasFeatureForUser(user.ID, "plus") {
 8+			w.WriteHeader(http.StatusUnauthorized)
 9+			return
10+		}
11+	} else if !client.Dbpool.HasFeatureForUser(user.ID, space) {
12 		w.WriteHeader(http.StatusUnauthorized)
13 		return
14 	}
15@@ -341,8 +346,7 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
16 	var feedItems []*feeds.Item
17 
18 	now := time.Now()
19-	// using pgs as the signal
20-	ff, err := client.Dbpool.FindFeatureForUser(user.ID, "pgs")
21+	ff, err := client.Dbpool.FindFeatureForUser(user.ID, "plus")
22 	if err != nil {
23 		// still want to send an empty feed
24 	} else {
M db/postgres/storage.go
+4, -28
 1@@ -1899,34 +1899,10 @@ func (me *PsqlDB) AddPicoPlusUser(username, paymentType, txId string) error {
 2 		}
 3 	}
 4 
 5-	pgs := me.createFeatureExpiresAt(user.ID, "pgs")
 6-	pgsQuery := `INSERT INTO feature_flags (user_id, name, data, expires_at)
 7-	VALUES ($1, 'pgs', '{"storage_max":10000000000, "file_max":50000000}'::jsonb, $2);`
 8-	_, err = tx.Exec(pgsQuery, user.ID, pgs)
 9-	if err != nil {
10-		return err
11-	}
12-
13-	imgs := me.createFeatureExpiresAt(user.ID, "imgs")
14-	imgsQuery := `INSERT INTO feature_flags (user_id, name, data, expires_at)
15-	VALUES ($1, 'imgs', '{"storage_max":5000000000}'::jsonb, $2);`
16-	_, err = tx.Exec(imgsQuery, user.ID, imgs)
17-	if err != nil {
18-		return err
19-	}
20-
21-	prose := me.createFeatureExpiresAt(user.ID, "prose")
22-	proseQuery := `INSERT INTO feature_flags (user_id, name, data, expires_at)
23-	VALUES ($1, 'prose', '{"storage_max":1000000000, "file_max":50000000}'::jsonb, $2);`
24-	_, err = tx.Exec(proseQuery, user.ID, prose)
25-	if err != nil {
26-		return err
27-	}
28-
29-	tuns := me.createFeatureExpiresAt(user.ID, "tuns")
30-	tunsQuery := `INSERT INTO feature_flags (user_id, name, expires_at)
31-	VALUES ($1, 'tuns', $2);`
32-	_, err = tx.Exec(tunsQuery, user.ID, tuns)
33+	plus := me.createFeatureExpiresAt(user.ID, "plus")
34+	plusQuery := `INSERT INTO feature_flags (user_id, name, data, expires_at)
35+	VALUES ($1, 'plus', '{"storage_max":20000000000, "file_max":50000000}'::jsonb, $2);`
36+	_, err = tx.Exec(plusQuery, user.ID, plus)
37 	if err != nil {
38 		return err
39 	}
M filehandlers/assets/handler.go
+2, -2
 1@@ -208,13 +208,13 @@ func (h *UploadAssetHandler) Validate(s ssh.Session) error {
 2 		return fmt.Errorf("must have username set")
 3 	}
 4 
 5-	ff, err := h.DBPool.FindFeatureForUser(user.ID, "pgs")
 6+	ff, err := h.DBPool.FindFeatureForUser(user.ID, "plus")
 7 	// pgs.sh has a free tier so users might not have a feature flag
 8 	// in which case we set sane defaults
 9 	if err != nil {
10 		ff = db.NewFeatureFlag(
11 			user.ID,
12-			"pgs",
13+			"plus",
14 			h.Cfg.MaxSize,
15 			h.Cfg.MaxAssetSize,
16 		)
M filehandlers/router_handler.go
+2, -2
 1@@ -149,13 +149,13 @@ func (r *FileHandlerRouter) Validate(s ssh.Session) error {
 2 		return fmt.Errorf("must have username set")
 3 	}
 4 
 5-	ff, _ := r.DBPool.FindFeatureForUser(user.ID, r.Cfg.Space)
 6+	ff, _ := r.DBPool.FindFeatureForUser(user.ID, "plus")
 7 	// we have free tiers so users might not have a feature flag
 8 	// in which case we set sane defaults
 9 	if ff == nil {
10 		ff = db.NewFeatureFlag(
11 			user.ID,
12-			r.Cfg.Space,
13+			"plus",
14 			r.Cfg.MaxSize,
15 			r.Cfg.MaxAssetSize,
16 		)
M imgs/ssh.go
+5, -0
 1@@ -60,6 +60,11 @@ func AuthHandler(dbh db.DB, log *slog.Logger) func(ssh.Context, ssh.PublicKey) b
 2 			return true
 3 		}
 4 
 5+		if !dbh.HasFeatureForUser(user.ID, "plus") {
 6+			log.Error("not a pico+ user", "user", user.Name)
 7+			return false
 8+		}
 9+
10 		return false
11 	}
12 }
M pgs/cli.go
+2, -2
 1@@ -188,9 +188,9 @@ func (c *Cmd) help() {
 2 }
 3 
 4 func (c *Cmd) stats(cfgMaxSize uint64) error {
 5-	ff, err := c.Dbpool.FindFeatureForUser(c.User.ID, "pgs")
 6+	ff, err := c.Dbpool.FindFeatureForUser(c.User.ID, "plus")
 7 	if err != nil {
 8-		ff = db.NewFeatureFlag(c.User.ID, "pgs", cfgMaxSize, 0)
 9+		ff = db.NewFeatureFlag(c.User.ID, "plus", cfgMaxSize, 0)
10 	}
11 	// this is jank
12 	ff.Data.StorageMax = ff.FindStorageMax(cfgMaxSize)
A sql/features_migration.sql
+3, -0
1@@ -0,0 +1,3 @@
2+UPDATE feature_flags SET name = 'plus' WHERE name = 'pgs';
3+
4+-- DELETE FROM feature_flags WHERE name = 'imgs' OR name = 'prose' OR name = 'tuns';
M tui/cms.go
+1, -1
1@@ -188,7 +188,7 @@ func (m model) findPlusFeatureFlag() (*db.FeatureFlag, error) {
2 		return nil, nil
3 	}
4 
5-	ff, err := m.dbpool.FindFeatureForUser(m.user.ID, "pgs")
6+	ff, err := m.dbpool.FindFeatureForUser(m.user.ID, "plus")
7 	if err != nil {
8 		return nil, err
9 	}