From e6b65e3d5d7001d07638426baf678d4ca1c13cc5 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 16 Mar 2021 17:49:26 +0100 Subject: [PATCH] fix AdminInvitesCount pluralization --- go.mod | 1 + go.sum | 1 + web/handlers/admin/app_test.go | 8 ++--- web/handlers/admin/invites_test.go | 49 ++++++++++++++++++++++++++++ web/i18n/defaults/active.en.toml | 9 +++-- web/templates/admin/invite-list.tmpl | 15 +++++---- web/utils.go | 14 +++++--- 7 files changed, 80 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index ccc699f..787cbef 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/BurntSushi/toml v0.3.1 github.com/PuerkitoBio/goquery v1.5.0 + github.com/dustin/go-humanize v1.0.0 // indirect github.com/friendsofgo/errors v0.9.2 github.com/go-kit/kit v0.10.0 github.com/gofrs/uuid v4.0.0+incompatible // indirect diff --git a/go.sum b/go.sum index 759fbd4..79aec89 100644 --- a/go.sum +++ b/go.sum @@ -84,6 +84,7 @@ github.com/dgryski/go-farm v0.0.0-20190104051053-3adb47b1fb0f/go.mod h1:SqUrOPUn github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= diff --git a/web/handlers/admin/app_test.go b/web/handlers/admin/app_test.go index 7d978f9..56d944a 100644 --- a/web/handlers/admin/app_test.go +++ b/web/handlers/admin/app_test.go @@ -7,7 +7,9 @@ import ( "math/rand" "net/http" "testing" + "time" + "github.com/dustin/go-humanize" "github.com/gorilla/mux" "github.com/pkg/errors" "go.mindeco.de/http/render" @@ -75,12 +77,10 @@ func newSession(t *testing.T) *testSession { } return msgID + "Plural" } - testFuncs["current_page_is"] = func(routeName string) bool { - return true - } - + testFuncs["current_page_is"] = func(routeName string) bool { return true } testFuncs["is_logged_in"] = func() *roomdb.User { return ts.User } testFuncs["urlToNotice"] = func(name string) string { return "" } + testFuncs["relative_time"] = func(when time.Time) string { return humanize.Time(when) } r, err := render.New(web.Templates, render.SetLogger(log), diff --git a/web/handlers/admin/invites_test.go b/web/handlers/admin/invites_test.go index a3a606a..693041c 100644 --- a/web/handlers/admin/invites_test.go +++ b/web/handlers/admin/invites_test.go @@ -9,11 +9,60 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/ssb-ngi-pointer/go-ssb-room/roomdb" "github.com/ssb-ngi-pointer/go-ssb-room/web" "github.com/ssb-ngi-pointer/go-ssb-room/web/router" "github.com/ssb-ngi-pointer/go-ssb-room/web/webassert" ) +func TestInvitesOverview(t *testing.T) { + ts := newSession(t) + a := assert.New(t) + + testUser := roomdb.User{ID: 23} + + lst := []roomdb.Invite{ + {ID: 1, CreatedBy: testUser, AliasSuggestion: "foo"}, + {ID: 2, CreatedBy: testUser, AliasSuggestion: "bar"}, + {ID: 3, CreatedBy: testUser, AliasSuggestion: "baz"}, + } + ts.InvitesDB.ListReturns(lst, nil) + + html, resp := ts.Client.GetHTML("/invites") + a.Equal(http.StatusOK, resp.Code, "wrong HTTP status code") + + webassert.Localized(t, html, []webassert.LocalizedElement{ + {"#welcome", "AdminInvitesWelcome"}, + {"title", "AdminInvitesTitle"}, + {"#invite-list-count", "AdminInvitesCountPlural"}, + }) + + trSelector := "#the-table-rows tr" + a.EqualValues(3, html.Find(trSelector).Length(), "wrong number of entries on the table (plural)") + + lst = []roomdb.Invite{ + {ID: 666, CreatedBy: testUser, AliasSuggestion: "single entry"}, + } + ts.InvitesDB.ListReturns(lst, nil) + + html, resp = ts.Client.GetHTML("/invites") + a.Equal(http.StatusOK, resp.Code, "wrong HTTP status code") + + webassert.Localized(t, html, []webassert.LocalizedElement{ + {"#welcome", "AdminInvitesWelcome"}, + {"title", "AdminInvitesTitle"}, + {"#invite-list-count", "AdminInvitesCountSingular"}, + }) + + elems := html.Find(trSelector) + a.EqualValues(1, elems.Length(), "wrong number of entries on the table (signular)") + + // check for link to remove confirm link + link, yes := elems.ContentsFiltered("a").Attr("href") + a.True(yes, "a-tag has href attribute") + a.Equal("/admin/invites/revoke/confirm?id=666", link) +} + func TestInvitesCreateForm(t *testing.T) { ts := newSession(t) a := assert.New(t) diff --git a/web/i18n/defaults/active.en.toml b/web/i18n/defaults/active.en.toml index 5d0b5cb..9ba6bbd 100644 --- a/web/i18n/defaults/active.en.toml +++ b/web/i18n/defaults/active.en.toml @@ -32,8 +32,7 @@ AdminAllowListRemoveConfirmTitle = "Confirm member removal" AdminInvitesTitle = "Invites" AdminInvitesWelcome = "Create invite tokens for people who are not yet members of this room. At the same time as you create an invite, you can also (optionally) attach an alias to it so that the person who receives the invite can automatically use that claim that alias for themselves." AdminInvitesAliasSuggestion = "(Optional) Alias suggestion" -AdminInvitesCount = "{{.Count}} invites still unclaimed" -AdminInvitesIDColumn = "ID" +AdminInvitesCreatedAtColumn = "Created at" AdminInvitesCreatorColumn = "Created by" AdminInvitesAliasColumn = "Alias suggestion" AdminInvitesSummaryFrom = "From" @@ -42,6 +41,7 @@ AdminInviteRevoke = "Revoke" AdminInviteRevokeConfirmTitle = "Confirm invite revocation" AdminInviteRevokeConfirmWelcome = "Are you sure you want to remove this invite? If you already sent it out, they will not be able to use it." + # TODO: add placeholder support to the template helpers (https://github.com/ssb-ngi-pointer/go-ssb-room/issues/60) AdminInviteCreatedBy = "Created by:" AdminInviteSuggestedAliasIs = "The suggested alias is:" @@ -89,3 +89,8 @@ other = "There are {{.Count}} items on the List" description = "The number of people in a room" one = "There is one person in the Room" other = "There are {{.Count}} people in the Room" + +[AdminInvitesCount] +description = "the number of invites that are not yet claimed" +one = "1 invite still unclaimed" +other = "{{.Count}} invites still unclaimed" \ No newline at end of file diff --git a/web/templates/admin/invite-list.tmpl b/web/templates/admin/invite-list.tmpl index a19b1c9..7dfaeab 100644 --- a/web/templates/admin/invite-list.tmpl +++ b/web/templates/admin/invite-list.tmpl @@ -10,7 +10,10 @@ -
{{i18npl "AdminInvitesCount" .Count}}
+
{{i18npl "AdminInvitesCount" .Count}}
- {{i18n "AdminInvitesCreatedAtColumn"}} - {{i18n "AdminInvitesCreatorColumn"}} - {{i18n "AdminInvitesAliasColumn"}} + {{i18n "AdminInvitesCreatedAtColumn"}} + {{i18n "AdminInvitesCreatorColumn"}} + {{i18n "AdminInvitesAliasColumn"}} - + {{range .Entries}} - {{.CreatedAt.Format "2006-01-02 15:04"}} + {{human_time .CreatedAt}} {{.CreatedBy.Name}} {{if ne .AliasSuggestion ""}}{{.AliasSuggestion}}{{end}} diff --git a/web/utils.go b/web/utils.go index 78a9f08..08654bc 100644 --- a/web/utils.go +++ b/web/utils.go @@ -11,20 +11,24 @@ import ( "path/filepath" "strconv" "strings" + "time" - refs "go.mindeco.de/ssb-refs" - - "github.com/gorilla/securecookie" - + "github.com/dustin/go-humanize" "github.com/go-kit/kit/log/level" "github.com/gorilla/mux" - "github.com/ssb-ngi-pointer/go-ssb-room/internal/repo" + "github.com/gorilla/securecookie" "go.mindeco.de/logging" + + "github.com/ssb-ngi-pointer/go-ssb-room/internal/repo" + refs "go.mindeco.de/ssb-refs" ) // TemplateFuncs returns a map of template functions func TemplateFuncs(m *mux.Router) template.FuncMap { return template.FuncMap{ + "human_time": func(when time.Time) string { + return humanize.Time(when) + }, "urlTo": NewURLTo(m), "inc": func(i int) int { return i + 1 }, }