From d701c5f27d9159642890e2860657d7e6a74da007 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Thu, 8 Apr 2021 20:43:01 +0100 Subject: [PATCH] add months field to invites & expiry --- api.go | 13 ++-- config/config-base.json | 4 +- html/admin.html | 156 +++++++++++++++++++++++++++------------- html/form-base.html | 1 + lang/admin/en-us.json | 1 + lang/admin/fr-fr.json | 1 + models.go | 5 ++ storage.go | 1 + ts/form.ts | 2 + ts/modules/accounts.ts | 4 +- ts/modules/invites.ts | 32 +++++++-- views.go | 1 + 12 files changed, 158 insertions(+), 63 deletions(-) diff --git a/api.go b/api.go index ebbde07..85f66b9 100644 --- a/api.go +++ b/api.go @@ -438,7 +438,7 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc if invite.UserExpiry { app.storage.usersLock.Lock() defer app.storage.usersLock.Unlock() - expiry = time.Now().Add(time.Duration(60*(invite.UserDays*24+invite.UserHours)+invite.UserMinutes) * time.Minute) + expiry = time.Now().AddDate(0, invite.UserMonths, invite.UserDays).Add(time.Duration((60*invite.UserHours)+invite.UserMinutes) * time.Minute) app.storage.users[id] = expiry if err := app.storage.storeUsers(); err != nil { app.err.Printf("Failed to store user duration: %v", err) @@ -472,7 +472,7 @@ func (app *appContext) ExtendExpiry(gc *gin.Context) { var req extendExpiryDTO gc.BindJSON(&req) app.info.Printf("Expiry extension requested for %d user(s)", len(req.Users)) - if req.Days <= 0 && req.Hours <= 0 && req.Minutes <= 0 { + if req.Months <= 0 && req.Days <= 0 && req.Hours <= 0 && req.Minutes <= 0 { respondBool(400, false, gc) return } @@ -480,7 +480,7 @@ func (app *appContext) ExtendExpiry(gc *gin.Context) { defer app.storage.usersLock.Unlock() for _, id := range req.Users { if expiry, ok := app.storage.users[id]; ok { - app.storage.users[id] = expiry.Add(time.Duration(60*(req.Days*24+req.Hours)+req.Minutes) * time.Minute) + app.storage.users[id] = expiry.AddDate(0, req.Months, req.Days).Add(time.Duration(((60 * req.Hours) + req.Minutes)) * time.Minute) app.debug.Printf("Expiry extended for \"%s\"", id) } } @@ -654,7 +654,7 @@ func (app *appContext) GenerateInvite(gc *gin.Context) { app.storage.loadInvites() gc.BindJSON(&req) currentTime := time.Now() - validTill := currentTime.AddDate(0, 0, 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)) // make sure code doesn't begin with number inviteCode := shortuuid.New() @@ -679,6 +679,7 @@ func (app *appContext) GenerateInvite(gc *gin.Context) { } invite.UserExpiry = req.UserExpiry if invite.UserExpiry { + invite.UserMonths = req.UserMonths invite.UserDays = req.UserDays invite.UserHours = req.UserHours invite.UserMinutes = req.UserMinutes @@ -861,13 +862,15 @@ func (app *appContext) GetInvites(gc *gin.Context) { app.checkInvites() var invites []inviteDTO for code, inv := range app.storage.invites { - _, _, days, hours, minutes, _ := timeDiff(inv.ValidTill, currentTime) + _, months, days, hours, minutes, _ := timeDiff(inv.ValidTill, currentTime) invite := inviteDTO{ Code: code, + Months: months, Days: days, Hours: hours, Minutes: minutes, UserExpiry: inv.UserExpiry, + UserMonths: inv.UserMonths, UserDays: inv.UserDays, UserHours: inv.UserHours, UserMinutes: inv.UserMinutes, diff --git a/config/config-base.json b/config/config-base.json index 9a63ebd..87b9283 100644 --- a/config/config-base.json +++ b/config/config-base.json @@ -124,7 +124,7 @@ ["en-us", "English (US)"] ], "value": "en-us", - "description": "Default Account Form Language. See issue #12 on Github if you'd like to translate." + "description": "Default Account Form Language. Visit weblate.hrfee.dev if you'd like to translate." }, "language-admin": { "name": "Default Admin Language", @@ -135,7 +135,7 @@ ["en-us", "English (US)"] ], "value": "en-us", - "description": "Default Admin page Language. Settings has not been translated. Submit a PR on github if you'd like to translate." + "description": "Default Admin page Language. Settings has not been translated. Visit weblate.hrfee.dev if you'd like to translate." }, "theme": { "name": "Default Look", diff --git a/html/admin.html b/html/admin.html index 73e813e..37a07d4 100644 --- a/html/admin.html +++ b/html/admin.html @@ -99,23 +99,41 @@