multiple plan
This commit is contained in:
parent
3a98202a1c
commit
e6899832f7
12
Chat.go
12
Chat.go
@ -46,7 +46,8 @@ func LoadChatHandler(c *fiber.Ctx) error {
|
|||||||
deleteLLMtoDelete(c)
|
deleteLLMtoDelete(c)
|
||||||
|
|
||||||
if checkIfLogin(c) {
|
if checkIfLogin(c) {
|
||||||
if IsCurrentUserLimiteReached(c) && !IsCurrentUserSubscribed(c) {
|
isPremium, isBasic := IsCurrentUserSubscribed(c)
|
||||||
|
if IsCurrentUserLimiteReached(c, isBasic) && !isPremium {
|
||||||
return c.SendString(generateLimitReachedChatHTML(c))
|
return c.SendString(generateLimitReachedChatHTML(c))
|
||||||
} else if !checkIfHaveKey(c) {
|
} else if !checkIfHaveKey(c) {
|
||||||
return c.SendString(generateEnterKeyChatHTML())
|
return c.SendString(generateEnterKeyChatHTML())
|
||||||
@ -66,7 +67,8 @@ func LoadChatInputHandler(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
return c.SendString(out)
|
return c.SendString(out)
|
||||||
} else {
|
} else {
|
||||||
out, err := chatInputTmpl.Execute(pongo2.Context{"IsLogin": checkIfLogin(c), "HaveKey": checkIfHaveKey(c), "IsSubscribed": IsCurrentUserSubscribed(c), "IsLimiteReached": IsCurrentUserLimiteReached(c)})
|
isPremium, isBasic := IsCurrentUserSubscribed(c)
|
||||||
|
out, err := chatInputTmpl.Execute(pongo2.Context{"IsLogin": checkIfLogin(c), "HaveKey": checkIfHaveKey(c), "IsSubscribed": isPremium, "IsLimiteReached": IsCurrentUserLimiteReached(c, isBasic)})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error executing chat input template")
|
fmt.Println("Error executing chat input template")
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -697,6 +699,7 @@ func GenerateModelPopoverHTML(refresh bool, c *fiber.Ctx) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
modelInfos := GetAvailableModels(c)
|
modelInfos := GetAvailableModels(c)
|
||||||
|
isPremium, isBasic := IsCurrentUserSubscribed(c)
|
||||||
|
|
||||||
out, err := modelPopoverTmpl.Execute(pongo2.Context{
|
out, err := modelPopoverTmpl.Execute(pongo2.Context{
|
||||||
"IsLogin": checkIfLogin(c),
|
"IsLogin": checkIfLogin(c),
|
||||||
@ -710,7 +713,7 @@ func GenerateModelPopoverHTML(refresh bool, c *fiber.Ctx) string {
|
|||||||
"LLMs": llms,
|
"LLMs": llms,
|
||||||
"ModelInfos": modelInfos,
|
"ModelInfos": modelInfos,
|
||||||
"DeleteUpdate": refresh,
|
"DeleteUpdate": refresh,
|
||||||
"IsSub": IsCurrentUserSubscribed(c),
|
"IsSub": isPremium || isBasic,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error generating model popover")
|
fmt.Println("Error generating model popover")
|
||||||
@ -798,6 +801,7 @@ func LoadSettingsHandler(c *fiber.Ctx) error {
|
|||||||
stripeSubLink := "https://billing.stripe.com/p/login/test_eVa5kC1q7dogaaIcMM?prefilled_email=" + user.Email
|
stripeSubLink := "https://billing.stripe.com/p/login/test_eVa5kC1q7dogaaIcMM?prefilled_email=" + user.Email
|
||||||
|
|
||||||
openaiExists, anthropicExists, mistralExists, groqExists, gooseaiExists, googleExists := getExistingKeys(c)
|
openaiExists, anthropicExists, mistralExists, groqExists, gooseaiExists, googleExists := getExistingKeys(c)
|
||||||
|
isPremium, isBasic := IsCurrentUserSubscribed(c)
|
||||||
|
|
||||||
out, err := settingPopoverTmpl.Execute(pongo2.Context{
|
out, err := settingPopoverTmpl.Execute(pongo2.Context{
|
||||||
"IsLogin": checkIfLogin(c),
|
"IsLogin": checkIfLogin(c),
|
||||||
@ -808,7 +812,7 @@ func LoadSettingsHandler(c *fiber.Ctx) error {
|
|||||||
"GooseaiExists": gooseaiExists,
|
"GooseaiExists": gooseaiExists,
|
||||||
"GoogleExists": googleExists,
|
"GoogleExists": googleExists,
|
||||||
"AnyExists": openaiExists || anthropicExists || mistralExists || groqExists || gooseaiExists || googleExists,
|
"AnyExists": openaiExists || anthropicExists || mistralExists || groqExists || gooseaiExists || googleExists,
|
||||||
"IsSub": IsCurrentUserSubscribed(c),
|
"IsSub": isPremium || isBasic,
|
||||||
"StripeSubLink": stripeSubLink,
|
"StripeSubLink": stripeSubLink,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
38
Stripe.go
38
Stripe.go
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/flosch/pongo2"
|
"github.com/flosch/pongo2"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
@ -83,7 +84,8 @@ func CreateNewStripeCustomer(name string, email string) string {
|
|||||||
return result.ID
|
return result.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsCurrentUserSubscribed(c *fiber.Ctx) bool {
|
func IsCurrentUserSubscribed(c *fiber.Ctx) (bool, bool) {
|
||||||
|
// TODO Change to add both plan
|
||||||
var user User
|
var user User
|
||||||
err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, "SELECT global currentUser { stripe_id } LIMIT 1;", &user)
|
err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, "SELECT global currentUser { stripe_id } LIMIT 1;", &user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -96,29 +98,53 @@ func IsCurrentUserSubscribed(c *fiber.Ctx) bool {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if result.Subscriptions.TotalCount == 0 {
|
if result.Subscriptions.TotalCount == 0 {
|
||||||
return false
|
return false, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.Subscriptions.Data[0].Plan.Product.ID == "prod_PnDjBBwvQN36cQ" && result.Subscriptions.Data[0].Plan.Active
|
// Check if an active product "prod_PnDjBBwvQN36cQ" is in the subscription. It is the premium plan
|
||||||
|
isPremium := false
|
||||||
|
for _, sub := range result.Subscriptions.Data {
|
||||||
|
if sub.Plan.Product.ID == "prod_Q9TpVos0t8QtX3" && sub.Plan.Active {
|
||||||
|
isPremium = true
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsCurrentUserLimiteReached(c *fiber.Ctx) bool {
|
isBasic := false
|
||||||
|
for _, sub := range result.Subscriptions.Data {
|
||||||
|
if sub.Plan.Product.ID == "prod_QDHakyHUeHLMET" && sub.Plan.Active {
|
||||||
|
isBasic = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return isPremium, isBasic
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsCurrentUserLimiteReached(c *fiber.Ctx, isBasic bool) bool {
|
||||||
|
// 30 days ago
|
||||||
|
date := time.Now().AddDate(0, 0, -30)
|
||||||
// Count the number of Usage for the current user
|
// Count the number of Usage for the current user
|
||||||
var count int64
|
var count int64
|
||||||
err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, `
|
err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, `
|
||||||
WITH
|
WITH
|
||||||
U := (
|
U := (
|
||||||
SELECT Usage
|
SELECT Usage
|
||||||
FILTER .user = global currentUser
|
FILTER .user = global currentUser AND .date >= <datetime>$0
|
||||||
)
|
)
|
||||||
SELECT count(U)
|
SELECT count(U)
|
||||||
`, &count)
|
`, &count, date)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error counting Usage")
|
fmt.Println("Error counting Usage")
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
// 500 if isBasic is false, otherwise 10000
|
||||||
|
if isBasic {
|
||||||
|
return count >= 10000
|
||||||
|
} else {
|
||||||
return count >= 500
|
return count >= 500
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func CreateClientSecretSession(c *fiber.Ctx) string {
|
func CreateClientSecretSession(c *fiber.Ctx) string {
|
||||||
var user User
|
var user User
|
||||||
|
Loading…
x
Reference in New Issue
Block a user