From 7c861e576349014ce0f8e5f08e767b88261243fd Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Tue, 10 Oct 2023 10:36:57 +0100 Subject: [PATCH 1/2] lang: fix the usual mistakes someone directly translating "English (US)", and lowercasing lang files. --- lang/admin/{ar-AA.json => ar-aa.json} | 0 lang/admin/hu-hu.json | 2 +- lang/admin/{it-IT.json => it-it.json} | 0 lang/common/hu-hu.json | 2 +- lang/email/{ar-AA.json => ar-aa.json} | 0 lang/email/{hu-HU.json => hu-hu.json} | 2 +- lang/form/hu-hu.json | 2 +- lang/pwreset/{hu-HU.json => hu-hu.json} | 2 +- lang/pwreset/{it-IT.json => it-it.json} | 0 lang/setup/{ar-AA.json => ar-aa.json} | 0 lang/setup/hu-hu.json | 2 +- lang/setup/{it-IT.json => it-it.json} | 0 lang/telegram/{hu-HU.json => hu-hu.json} | 2 +- 13 files changed, 7 insertions(+), 7 deletions(-) rename lang/admin/{ar-AA.json => ar-aa.json} (100%) rename lang/admin/{it-IT.json => it-it.json} (100%) rename lang/email/{ar-AA.json => ar-aa.json} (100%) rename lang/email/{hu-HU.json => hu-hu.json} (98%) rename lang/pwreset/{hu-HU.json => hu-hu.json} (96%) rename lang/pwreset/{it-IT.json => it-it.json} (100%) rename lang/setup/{ar-AA.json => ar-aa.json} (100%) rename lang/setup/{it-IT.json => it-it.json} (100%) rename lang/telegram/{hu-HU.json => hu-hu.json} (96%) diff --git a/lang/admin/ar-AA.json b/lang/admin/ar-aa.json similarity index 100% rename from lang/admin/ar-AA.json rename to lang/admin/ar-aa.json diff --git a/lang/admin/hu-hu.json b/lang/admin/hu-hu.json index 1e886c5..18e7caa 100644 --- a/lang/admin/hu-hu.json +++ b/lang/admin/hu-hu.json @@ -1,6 +1,6 @@ { "meta": { - "name": "Angol (US)" + "name": "Magyar (HU)" }, "strings": { "invites": "Meghívások", diff --git a/lang/admin/it-IT.json b/lang/admin/it-it.json similarity index 100% rename from lang/admin/it-IT.json rename to lang/admin/it-it.json diff --git a/lang/common/hu-hu.json b/lang/common/hu-hu.json index 2c7af7a..7a91336 100644 --- a/lang/common/hu-hu.json +++ b/lang/common/hu-hu.json @@ -1,6 +1,6 @@ { "meta": { - "name": "Angol (US)" + "name": "Magyar (HU)" }, "strings": { "login": "Belépés", diff --git a/lang/email/ar-AA.json b/lang/email/ar-aa.json similarity index 100% rename from lang/email/ar-AA.json rename to lang/email/ar-aa.json diff --git a/lang/email/hu-HU.json b/lang/email/hu-hu.json similarity index 98% rename from lang/email/hu-HU.json rename to lang/email/hu-hu.json index 55461b5..04ee4d3 100644 --- a/lang/email/hu-HU.json +++ b/lang/email/hu-hu.json @@ -1,6 +1,6 @@ { "meta": { - "name": "Angol (US)" + "name": "Magyar (HU)" }, "strings": { "ifItWasNotYou": "", diff --git a/lang/form/hu-hu.json b/lang/form/hu-hu.json index a43fe8e..2ddaedb 100644 --- a/lang/form/hu-hu.json +++ b/lang/form/hu-hu.json @@ -1,6 +1,6 @@ { "meta": { - "name": "Angol (US)" + "name": "Magyar (HU)" }, "strings": { "pageTitle": "Jellyfin fiók létrehozása", diff --git a/lang/pwreset/hu-HU.json b/lang/pwreset/hu-hu.json similarity index 96% rename from lang/pwreset/hu-HU.json rename to lang/pwreset/hu-hu.json index 23d70ba..68a0d44 100644 --- a/lang/pwreset/hu-HU.json +++ b/lang/pwreset/hu-hu.json @@ -1,6 +1,6 @@ { "meta": { - "name": "Angol (US)" + "name": "Magyar (HU)" }, "strings": { "passwordReset": "Jelszó visszaállítás", diff --git a/lang/pwreset/it-IT.json b/lang/pwreset/it-it.json similarity index 100% rename from lang/pwreset/it-IT.json rename to lang/pwreset/it-it.json diff --git a/lang/setup/ar-AA.json b/lang/setup/ar-aa.json similarity index 100% rename from lang/setup/ar-AA.json rename to lang/setup/ar-aa.json diff --git a/lang/setup/hu-hu.json b/lang/setup/hu-hu.json index 767a37a..e41f8f6 100644 --- a/lang/setup/hu-hu.json +++ b/lang/setup/hu-hu.json @@ -1,6 +1,6 @@ { "meta": { - "name": "Angol (US)" + "name": "Magyar (HU)" }, "strings": { "pageTitle": "Telepítés - jfa-go", diff --git a/lang/setup/it-IT.json b/lang/setup/it-it.json similarity index 100% rename from lang/setup/it-IT.json rename to lang/setup/it-it.json diff --git a/lang/telegram/hu-HU.json b/lang/telegram/hu-hu.json similarity index 96% rename from lang/telegram/hu-HU.json rename to lang/telegram/hu-hu.json index b85deb5..673ab2a 100644 --- a/lang/telegram/hu-HU.json +++ b/lang/telegram/hu-hu.json @@ -1,6 +1,6 @@ { "meta": { - "name": "Angol (US)" + "name": "Magyar (HU)" }, "strings": { "startMessage": "Helló!\nAdd meg a Jellyfin PIN kódodat itt, hogy megerősítsd a fiókodat.", From 77f6b1042e86aa49b4b9a8b06d99829dd318f79d Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Wed, 11 Oct 2023 11:30:28 +0100 Subject: [PATCH 2/2] 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(). --- api-invites.go | 32 +++++++++++++++++++------------- api-profiles.go | 10 +--------- api-userpage.go | 18 ++---------------- api-users.go | 10 +--------- 4 files changed, 23 insertions(+), 47 deletions(-) diff --git a/api-invites.go b/api-invites.go index 0fda9fd..14c5f45 100644 --- a/api-invites.go +++ b/api-invites.go @@ -17,6 +17,18 @@ const ( 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() { currentTime := time.Now() for _, data := range app.storage.GetInvites() { @@ -150,14 +162,8 @@ func (app *appContext) GenerateInvite(gc *gin.Context) { currentTime := time.Now() validTill := currentTime.AddDate(0, req.Months, req.Days) 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 + invite.Code = GenerateInviteCode() if 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) { addressValid := false discord := "" - app.debug.Printf("%s: Sending invite message", inviteCode) + app.debug.Printf("%s: Sending invite message", invite.Code) if discordEnabled && !strings.Contains(req.SendTo, "@") { users := app.discord.GetUsers(req.SendTo) if len(users) == 0 { @@ -202,10 +208,10 @@ func (app *appContext) GenerateInvite(gc *gin.Context) { invite.SendTo = req.SendTo } if addressValid { - msg, err := app.email.constructInvite(inviteCode, invite, app, false) + msg, err := app.email.constructInvite(invite.Code, invite, app, false) if err != nil { 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 { var err error if discord != "" { @@ -215,9 +221,9 @@ func (app *appContext) GenerateInvite(gc *gin.Context) { } if err != nil { 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 { - 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" } } - app.storage.SetInvitesKey(inviteCode, invite) + app.storage.SetInvitesKey(invite.Code, invite) respondBool(200, true, gc) } diff --git a/api-profiles.go b/api-profiles.go index 3623d33..ce58cbb 100644 --- a/api-profiles.go +++ b/api-profiles.go @@ -1,11 +1,9 @@ package main import ( - "strconv" "time" "github.com/gin-gonic/gin" - "github.com/lithammer/shortuuid/v3" "github.com/timshannon/badgerhold/v4" ) @@ -151,13 +149,7 @@ func (app *appContext) EnableReferralForProfile(gc *gin.Context) { } // Generate new code for referral template - inv.Code = shortuuid.New() - // 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.Code = GenerateInviteCode() inv.Created = time.Now() inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour) inv.IsReferral = true diff --git a/api-userpage.go b/api-userpage.go index dce320f..0df5653 100644 --- a/api-userpage.go +++ b/api-userpage.go @@ -3,13 +3,11 @@ package main import ( "net/http" "os" - "strconv" "strings" "time" "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt" - "github.com/lithammer/shortuuid/v3" "github.com/timshannon/badgerhold/v4" ) @@ -673,13 +671,7 @@ func (app *appContext) GetMyReferral(gc *gin.Context) { respondBool(400, false, gc) return } - inv.Code = shortuuid.New() - // 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.Code = GenerateInviteCode() inv.Created = time.Now() inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour) 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. // We delete it from storage, and put it back with a fresh code and expiry. app.storage.DeleteInvitesKey(inv.Code) - inv.Code = shortuuid.New() - // 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.Code = GenerateInviteCode() inv.Created = time.Now() inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour) app.storage.SetInvitesKey(inv.Code, inv) diff --git a/api-users.go b/api-users.go index ff2a0bd..f735bda 100644 --- a/api-users.go +++ b/api-users.go @@ -3,14 +3,12 @@ package main import ( "fmt" "os" - "strconv" "strings" "time" "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt" "github.com/hrfee/mediabrowser" - "github.com/lithammer/shortuuid/v3" "github.com/timshannon/badgerhold/v4" ) @@ -707,13 +705,7 @@ func (app *appContext) EnableReferralForUsers(gc *gin.Context) { // 2. Generate referral invite. inv := baseInv - inv.Code = shortuuid.New() - // 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.Code = GenerateInviteCode() inv.Created = time.Now() inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour) inv.IsReferral = true