mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-22 09:00:10 +00:00
invites: move code gen to function
code to generate an invite code w/ a non-integer first character was reused a bunch, so it's now function GenerateInviteCode().
This commit is contained in:
parent
7c861e5763
commit
77f6b1042e
@ -17,6 +17,18 @@ const (
|
|||||||
CAPTCHA_VALIDITY = 20 * 60 // Seconds
|
CAPTCHA_VALIDITY = 20 * 60 // Seconds
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GenerateInviteCode generates an invite code in the correct format.
|
||||||
|
func GenerateInviteCode() string {
|
||||||
|
// make sure code doesn't begin with number
|
||||||
|
inviteCode := shortuuid.New()
|
||||||
|
_, err := strconv.Atoi(string(inviteCode[0]))
|
||||||
|
for err == nil {
|
||||||
|
inviteCode = shortuuid.New()
|
||||||
|
_, err = strconv.Atoi(string(inviteCode[0]))
|
||||||
|
}
|
||||||
|
return inviteCode
|
||||||
|
}
|
||||||
|
|
||||||
func (app *appContext) checkInvites() {
|
func (app *appContext) checkInvites() {
|
||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
for _, data := range app.storage.GetInvites() {
|
for _, data := range app.storage.GetInvites() {
|
||||||
@ -150,14 +162,8 @@ func (app *appContext) GenerateInvite(gc *gin.Context) {
|
|||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
validTill := currentTime.AddDate(0, req.Months, req.Days)
|
validTill := currentTime.AddDate(0, req.Months, req.Days)
|
||||||
validTill = validTill.Add(time.Hour*time.Duration(req.Hours) + time.Minute*time.Duration(req.Minutes))
|
validTill = validTill.Add(time.Hour*time.Duration(req.Hours) + time.Minute*time.Duration(req.Minutes))
|
||||||
// make sure code doesn't begin with number
|
|
||||||
inviteCode := shortuuid.New()
|
|
||||||
_, err := strconv.Atoi(string(inviteCode[0]))
|
|
||||||
for err == nil {
|
|
||||||
inviteCode = shortuuid.New()
|
|
||||||
_, err = strconv.Atoi(string(inviteCode[0]))
|
|
||||||
}
|
|
||||||
var invite Invite
|
var invite Invite
|
||||||
|
invite.Code = GenerateInviteCode()
|
||||||
if req.Label != "" {
|
if req.Label != "" {
|
||||||
invite.Label = req.Label
|
invite.Label = req.Label
|
||||||
}
|
}
|
||||||
@ -185,7 +191,7 @@ func (app *appContext) GenerateInvite(gc *gin.Context) {
|
|||||||
if req.SendTo != "" && app.config.Section("invite_emails").Key("enabled").MustBool(false) {
|
if req.SendTo != "" && app.config.Section("invite_emails").Key("enabled").MustBool(false) {
|
||||||
addressValid := false
|
addressValid := false
|
||||||
discord := ""
|
discord := ""
|
||||||
app.debug.Printf("%s: Sending invite message", inviteCode)
|
app.debug.Printf("%s: Sending invite message", invite.Code)
|
||||||
if discordEnabled && !strings.Contains(req.SendTo, "@") {
|
if discordEnabled && !strings.Contains(req.SendTo, "@") {
|
||||||
users := app.discord.GetUsers(req.SendTo)
|
users := app.discord.GetUsers(req.SendTo)
|
||||||
if len(users) == 0 {
|
if len(users) == 0 {
|
||||||
@ -202,10 +208,10 @@ func (app *appContext) GenerateInvite(gc *gin.Context) {
|
|||||||
invite.SendTo = req.SendTo
|
invite.SendTo = req.SendTo
|
||||||
}
|
}
|
||||||
if addressValid {
|
if addressValid {
|
||||||
msg, err := app.email.constructInvite(inviteCode, invite, app, false)
|
msg, err := app.email.constructInvite(invite.Code, invite, app, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
invite.SendTo = fmt.Sprintf("Failed to send to %s", req.SendTo)
|
invite.SendTo = fmt.Sprintf("Failed to send to %s", req.SendTo)
|
||||||
app.err.Printf("%s: Failed to construct invite message: %v", inviteCode, err)
|
app.err.Printf("%s: Failed to construct invite message: %v", invite.Code, err)
|
||||||
} else {
|
} else {
|
||||||
var err error
|
var err error
|
||||||
if discord != "" {
|
if discord != "" {
|
||||||
@ -215,9 +221,9 @@ func (app *appContext) GenerateInvite(gc *gin.Context) {
|
|||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
invite.SendTo = fmt.Sprintf("Failed to send to %s", req.SendTo)
|
invite.SendTo = fmt.Sprintf("Failed to send to %s", req.SendTo)
|
||||||
app.err.Printf("%s: %s: %v", inviteCode, invite.SendTo, err)
|
app.err.Printf("%s: %s: %v", invite.Code, invite.SendTo, err)
|
||||||
} else {
|
} else {
|
||||||
app.info.Printf("%s: Sent invite email to \"%s\"", inviteCode, req.SendTo)
|
app.info.Printf("%s: Sent invite email to \"%s\"", invite.Code, req.SendTo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -229,7 +235,7 @@ func (app *appContext) GenerateInvite(gc *gin.Context) {
|
|||||||
invite.Profile = "Default"
|
invite.Profile = "Default"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
app.storage.SetInvitesKey(inviteCode, invite)
|
app.storage.SetInvitesKey(invite.Code, invite)
|
||||||
respondBool(200, true, gc)
|
respondBool(200, true, gc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/lithammer/shortuuid/v3"
|
|
||||||
"github.com/timshannon/badgerhold/v4"
|
"github.com/timshannon/badgerhold/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -151,13 +149,7 @@ func (app *appContext) EnableReferralForProfile(gc *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate new code for referral template
|
// Generate new code for referral template
|
||||||
inv.Code = shortuuid.New()
|
inv.Code = GenerateInviteCode()
|
||||||
// make sure code doesn't begin with number
|
|
||||||
_, err := strconv.Atoi(string(inv.Code[0]))
|
|
||||||
for err == nil {
|
|
||||||
inv.Code = shortuuid.New()
|
|
||||||
_, err = strconv.Atoi(string(inv.Code[0]))
|
|
||||||
}
|
|
||||||
inv.Created = time.Now()
|
inv.Created = time.Now()
|
||||||
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
|
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
|
||||||
inv.IsReferral = true
|
inv.IsReferral = true
|
||||||
|
@ -3,13 +3,11 @@ package main
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/golang-jwt/jwt"
|
"github.com/golang-jwt/jwt"
|
||||||
"github.com/lithammer/shortuuid/v3"
|
|
||||||
"github.com/timshannon/badgerhold/v4"
|
"github.com/timshannon/badgerhold/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -673,13 +671,7 @@ func (app *appContext) GetMyReferral(gc *gin.Context) {
|
|||||||
respondBool(400, false, gc)
|
respondBool(400, false, gc)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
inv.Code = shortuuid.New()
|
inv.Code = GenerateInviteCode()
|
||||||
// make sure code doesn't begin with number
|
|
||||||
_, err := strconv.Atoi(string(inv.Code[0]))
|
|
||||||
for err == nil {
|
|
||||||
inv.Code = shortuuid.New()
|
|
||||||
_, err = strconv.Atoi(string(inv.Code[0]))
|
|
||||||
}
|
|
||||||
inv.Created = time.Now()
|
inv.Created = time.Now()
|
||||||
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
|
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
|
||||||
inv.IsReferral = true
|
inv.IsReferral = true
|
||||||
@ -689,13 +681,7 @@ func (app *appContext) GetMyReferral(gc *gin.Context) {
|
|||||||
// 3. We found an invite for us, but it's expired.
|
// 3. We found an invite for us, but it's expired.
|
||||||
// We delete it from storage, and put it back with a fresh code and expiry.
|
// We delete it from storage, and put it back with a fresh code and expiry.
|
||||||
app.storage.DeleteInvitesKey(inv.Code)
|
app.storage.DeleteInvitesKey(inv.Code)
|
||||||
inv.Code = shortuuid.New()
|
inv.Code = GenerateInviteCode()
|
||||||
// make sure code doesn't begin with number
|
|
||||||
_, err := strconv.Atoi(string(inv.Code[0]))
|
|
||||||
for err == nil {
|
|
||||||
inv.Code = shortuuid.New()
|
|
||||||
_, err = strconv.Atoi(string(inv.Code[0]))
|
|
||||||
}
|
|
||||||
inv.Created = time.Now()
|
inv.Created = time.Now()
|
||||||
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
|
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
|
||||||
app.storage.SetInvitesKey(inv.Code, inv)
|
app.storage.SetInvitesKey(inv.Code, inv)
|
||||||
|
10
api-users.go
10
api-users.go
@ -3,14 +3,12 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/golang-jwt/jwt"
|
"github.com/golang-jwt/jwt"
|
||||||
"github.com/hrfee/mediabrowser"
|
"github.com/hrfee/mediabrowser"
|
||||||
"github.com/lithammer/shortuuid/v3"
|
|
||||||
"github.com/timshannon/badgerhold/v4"
|
"github.com/timshannon/badgerhold/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -707,13 +705,7 @@ func (app *appContext) EnableReferralForUsers(gc *gin.Context) {
|
|||||||
|
|
||||||
// 2. Generate referral invite.
|
// 2. Generate referral invite.
|
||||||
inv := baseInv
|
inv := baseInv
|
||||||
inv.Code = shortuuid.New()
|
inv.Code = GenerateInviteCode()
|
||||||
// make sure code doesn't begin with number
|
|
||||||
_, err := strconv.Atoi(string(inv.Code[0]))
|
|
||||||
for err == nil {
|
|
||||||
inv.Code = shortuuid.New()
|
|
||||||
_, err = strconv.Atoi(string(inv.Code[0]))
|
|
||||||
}
|
|
||||||
inv.Created = time.Now()
|
inv.Created = time.Now()
|
||||||
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
|
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
|
||||||
inv.IsReferral = true
|
inv.IsReferral = true
|
||||||
|
Loading…
Reference in New Issue
Block a user