track created_at column for invites

This commit is contained in:
Henry 2021-03-16 17:13:01 +01:00
parent d39d1ac6e9
commit 0f82c29f48
16 changed files with 84 additions and 19 deletions

5
go.mod
View File

@ -14,6 +14,7 @@ require (
github.com/gorilla/sessions v1.2.1
github.com/gorilla/websocket v1.4.2
github.com/keks/nocomment v0.0.0-20181007001506-30c6dcb4a472
github.com/kevinburke/go-bindata v3.21.0+incompatible // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/nicksnyder/go-i18n/v2 v2.1.2
github.com/pkg/errors v0.9.1
@ -22,8 +23,8 @@ require (
github.com/stretchr/testify v1.6.1
github.com/unrolled/secure v1.0.8
github.com/vcraescu/go-paginator/v2 v2.0.0
github.com/volatiletech/sqlboiler-sqlite3 v0.0.0-20200618013359-a93887c09a14 // indirect
github.com/volatiletech/sqlboiler/v4 v4.4.0
github.com/volatiletech/sqlboiler-sqlite3 v0.0.0-20210314195744-a1c697a68aef // indirect
github.com/volatiletech/sqlboiler/v4 v4.5.0
github.com/volatiletech/strmangle v0.0.1
go.cryptoscope.co/muxrpc/v2 v2.0.0-20210202162901-fe642d405dc6
go.cryptoscope.co/netwrap v0.1.1

5
go.sum
View File

@ -428,13 +428,18 @@ github.com/volatiletech/inflect v0.0.1 h1:2a6FcMQyhmPZcLa+uet3VJ8gLn/9svWhJxJYwv
github.com/volatiletech/inflect v0.0.1/go.mod h1:IBti31tG6phkHitLlr5j7shC5SOo//x0AjDzaJU1PLA=
github.com/volatiletech/null/v8 v8.1.0 h1:eAO3I31A5R04usY5SKMMfDcOCnEGyT/T4wRI0JVGp4U=
github.com/volatiletech/null/v8 v8.1.0/go.mod h1:98DbwNoKEpRrYtGjWFctievIfm4n4MxG0A6EBUcoS5g=
github.com/volatiletech/null/v8 v8.1.2/go.mod h1:98DbwNoKEpRrYtGjWFctievIfm4n4MxG0A6EBUcoS5g=
github.com/volatiletech/randomize v0.0.1 h1:eE5yajattWqTB2/eN8df4dw+8jwAzBtbdo5sbWC4nMk=
github.com/volatiletech/randomize v0.0.1/go.mod h1:GN3U0QYqfZ9FOJ67bzax1cqZ5q2xuj2mXrXBjWaRTlY=
github.com/volatiletech/sqlboiler-sqlite3 v0.0.0-20200618013359-a93887c09a14 h1:2PCMsnM/GVptZVyB8s0vTIFCPjl6f5rhz5Ry5MNShMQ=
github.com/volatiletech/sqlboiler-sqlite3 v0.0.0-20200618013359-a93887c09a14/go.mod h1:fmZQG/eGdD2vdjWZjrVq4v2sTQ+Alz/I09chjYWWUVw=
github.com/volatiletech/sqlboiler-sqlite3 v0.0.0-20210314195744-a1c697a68aef h1:XjoYLjR/XToxGQY9O6WKA8l6t0hIWjPwazVQcQEctFM=
github.com/volatiletech/sqlboiler-sqlite3 v0.0.0-20210314195744-a1c697a68aef/go.mod h1:fmZQG/eGdD2vdjWZjrVq4v2sTQ+Alz/I09chjYWWUVw=
github.com/volatiletech/sqlboiler/v4 v4.0.0/go.mod h1:U0Z5K4y+twWgHxh364G45QyzyNssSbBqNWtXGHVTlgM=
github.com/volatiletech/sqlboiler/v4 v4.4.0 h1:aSlvHidRBuxHHQZNX3ZLGgzNVPVPzWqsC3lhcLbV/b0=
github.com/volatiletech/sqlboiler/v4 v4.4.0/go.mod h1:h4RBAO6QbwMP3ezGmtfGljRms7S27cFIgF3rKgPKstE=
github.com/volatiletech/sqlboiler/v4 v4.5.0 h1:oJ3YXEvv0c48S9W/3TuPLxJxefIkewpub2qZioXXlUY=
github.com/volatiletech/sqlboiler/v4 v4.5.0/go.mod h1:tQgF5zxwqrjR6Wydc5rRylI6puDOO1WvBC70/5up+Hg=
github.com/volatiletech/strmangle v0.0.1 h1:UKQoHmY6be/R3tSvD2nQYrH41k43OJkidwEiC74KIzk=
github.com/volatiletech/strmangle v0.0.1/go.mod h1:F6RA6IkB5vq0yTG4GQ0UsbbRcl3ni9P76i+JrTBKFFg=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=

View File

@ -7,6 +7,7 @@ import (
"database/sql"
"encoding/base64"
"fmt"
"time"
"github.com/friendsofgo/errors"
"github.com/mattn/go-sqlite3"
@ -49,6 +50,9 @@ func (i Invites) Create(ctx context.Context, createdBy int64, aliasSuggestion st
// generate an invite code
rand.Read(tokenBytes)
// see comment on migrations/6-invite-createdAt.sql
newInvite.CreatedAt = time.Now()
// hash the binary of the token for storage
h := sha256.New()
h.Write(tokenBytes)
@ -115,6 +119,7 @@ func (i Invites) Consume(ctx context.Context, token string, newMember refs.FeedR
}
inv.ID = entry.ID
inv.CreatedAt = entry.CreatedAt
inv.AliasSuggestion = entry.AliasSuggestion
inv.CreatedBy.ID = entry.R.CreatedByAuthFallback.ID
inv.CreatedBy.Name = entry.R.CreatedByAuthFallback.Name
@ -162,6 +167,7 @@ func (i Invites) GetByToken(ctx context.Context, token string) (roomdb.Invite, e
}
inv.ID = entry.ID
inv.CreatedAt = entry.CreatedAt
inv.AliasSuggestion = entry.AliasSuggestion
inv.CreatedBy.ID = entry.R.CreatedByAuthFallback.ID
inv.CreatedBy.Name = entry.R.CreatedByAuthFallback.Name
@ -184,6 +190,7 @@ func (i Invites) GetByID(ctx context.Context, id int64) (roomdb.Invite, error) {
}
inv.ID = entry.ID
inv.CreatedAt = entry.CreatedAt
inv.AliasSuggestion = entry.AliasSuggestion
inv.CreatedBy.ID = entry.R.CreatedByAuthFallback.ID
inv.CreatedBy.Name = entry.R.CreatedByAuthFallback.Name
@ -208,6 +215,7 @@ func (i Invites) List(ctx context.Context) ([]roomdb.Invite, error) {
for idx, e := range entries {
var inv roomdb.Invite
inv.ID = e.ID
inv.CreatedAt = e.CreatedAt
inv.AliasSuggestion = e.AliasSuggestion
inv.CreatedBy.ID = e.R.CreatedByAuthFallback.ID
inv.CreatedBy.Name = e.R.CreatedByAuthFallback.Name

View File

@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
@ -56,6 +57,9 @@ func TestInvites(t *testing.T) {
t.Run("simple create and consume", func(t *testing.T) {
r := require.New(t)
// i really don't want to do a mocked time functions and rather solve the comment in migration 6 instead
before := time.Now()
tok, err := db.Invites.Create(ctx, uid, "bestie")
r.NoError(err, "failed to create invite token")
@ -68,6 +72,7 @@ func TestInvites(t *testing.T) {
r.Equal("bestie", lst[0].AliasSuggestion)
r.Equal(testUserName, lst[0].CreatedBy.Name)
r.True(lst[0].CreatedAt.After(before), "expected CreatedAt to be after the start marker")
nope := db.AllowList.HasFeed(ctx, newMember)
r.False(nope, "expected feed to not yet be on the allow list")
@ -76,6 +81,7 @@ func TestInvites(t *testing.T) {
r.NoError(err, "failed to consume the invite")
r.Equal(testUserName, inv.CreatedBy.Name)
r.NotEqualValues(0, inv.ID, "invite ID unset")
r.True(inv.CreatedAt.After(before), "expected CreatedAt to be after the start marker")
// consume also adds it to the allow list
yes := db.AllowList.HasFeed(ctx, newMember)

View File

@ -0,0 +1,10 @@
-- +migrate Up
ALTER TABLE invites ADD COLUMN created_at datetime NOT NULL DEFAULT 0;
-- because of the restrictions in 4. https://www.sqlite.org/lang_altertable.html
-- the actual default should be date('now') but can't be.
-- this needs to be fixed once we merge the migrations into a signle schema prior to the next point release.
-- at which point we dont need to insert the current time into the db from the application anymore.
UPDATE invites set created_at=date('now');
-- +migrate Down
ALTER TABLE invites DROP COLUMN created_at;

View File

@ -1,4 +1,4 @@
// Code generated by SQLBoiler 4.4.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// Code generated by SQLBoiler 4.5.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models

View File

@ -1,4 +1,4 @@
// Code generated by SQLBoiler 4.4.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// Code generated by SQLBoiler 4.5.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models

View File

@ -1,4 +1,4 @@
// Code generated by SQLBoiler 4.4.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// Code generated by SQLBoiler 4.5.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models

View File

@ -1,4 +1,4 @@
// Code generated by SQLBoiler 4.4.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// Code generated by SQLBoiler 4.5.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models

View File

@ -1,4 +1,4 @@
// Code generated by SQLBoiler 4.4.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// Code generated by SQLBoiler 4.5.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models

View File

@ -1,4 +1,4 @@
// Code generated by SQLBoiler 4.4.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// Code generated by SQLBoiler 4.5.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models

View File

@ -1,4 +1,4 @@
// Code generated by SQLBoiler 4.4.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// Code generated by SQLBoiler 4.5.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models
@ -22,11 +22,12 @@ import (
// Invite is an object representing the database table.
type Invite struct {
ID int64 `boil:"id" json:"id" toml:"id" yaml:"id"`
Token string `boil:"token" json:"token" toml:"token" yaml:"token"`
CreatedBy int64 `boil:"created_by" json:"created_by" toml:"created_by" yaml:"created_by"`
AliasSuggestion string `boil:"alias_suggestion" json:"alias_suggestion" toml:"alias_suggestion" yaml:"alias_suggestion"`
Active bool `boil:"active" json:"active" toml:"active" yaml:"active"`
ID int64 `boil:"id" json:"id" toml:"id" yaml:"id"`
Token string `boil:"token" json:"token" toml:"token" yaml:"token"`
CreatedBy int64 `boil:"created_by" json:"created_by" toml:"created_by" yaml:"created_by"`
AliasSuggestion string `boil:"alias_suggestion" json:"alias_suggestion" toml:"alias_suggestion" yaml:"alias_suggestion"`
Active bool `boil:"active" json:"active" toml:"active" yaml:"active"`
CreatedAt time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"`
R *inviteR `boil:"-" json:"-" toml:"-" yaml:"-"`
L inviteL `boil:"-" json:"-" toml:"-" yaml:"-"`
@ -38,12 +39,14 @@ var InviteColumns = struct {
CreatedBy string
AliasSuggestion string
Active string
CreatedAt string
}{
ID: "id",
Token: "token",
CreatedBy: "created_by",
AliasSuggestion: "alias_suggestion",
Active: "active",
CreatedAt: "created_at",
}
// Generated where
@ -57,18 +60,41 @@ func (w whereHelperbool) LTE(x bool) qm.QueryMod { return qmhelper.Where(w.field
func (w whereHelperbool) GT(x bool) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GT, x) }
func (w whereHelperbool) GTE(x bool) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GTE, x) }
type whereHelpertime_Time struct{ field string }
func (w whereHelpertime_Time) EQ(x time.Time) qm.QueryMod {
return qmhelper.Where(w.field, qmhelper.EQ, x)
}
func (w whereHelpertime_Time) NEQ(x time.Time) qm.QueryMod {
return qmhelper.Where(w.field, qmhelper.NEQ, x)
}
func (w whereHelpertime_Time) LT(x time.Time) qm.QueryMod {
return qmhelper.Where(w.field, qmhelper.LT, x)
}
func (w whereHelpertime_Time) LTE(x time.Time) qm.QueryMod {
return qmhelper.Where(w.field, qmhelper.LTE, x)
}
func (w whereHelpertime_Time) GT(x time.Time) qm.QueryMod {
return qmhelper.Where(w.field, qmhelper.GT, x)
}
func (w whereHelpertime_Time) GTE(x time.Time) qm.QueryMod {
return qmhelper.Where(w.field, qmhelper.GTE, x)
}
var InviteWhere = struct {
ID whereHelperint64
Token whereHelperstring
CreatedBy whereHelperint64
AliasSuggestion whereHelperstring
Active whereHelperbool
CreatedAt whereHelpertime_Time
}{
ID: whereHelperint64{field: "\"invites\".\"id\""},
Token: whereHelperstring{field: "\"invites\".\"token\""},
CreatedBy: whereHelperint64{field: "\"invites\".\"created_by\""},
AliasSuggestion: whereHelperstring{field: "\"invites\".\"alias_suggestion\""},
Active: whereHelperbool{field: "\"invites\".\"active\""},
CreatedAt: whereHelpertime_Time{field: "\"invites\".\"created_at\""},
}
// InviteRels is where relationship names are stored.
@ -92,9 +118,9 @@ func (*inviteR) NewStruct() *inviteR {
type inviteL struct{}
var (
inviteAllColumns = []string{"id", "token", "created_by", "alias_suggestion", "active"}
inviteAllColumns = []string{"id", "token", "created_by", "alias_suggestion", "active", "created_at"}
inviteColumnsWithoutDefault = []string{}
inviteColumnsWithDefault = []string{"id", "token", "created_by", "alias_suggestion", "active"}
inviteColumnsWithDefault = []string{"id", "token", "created_by", "alias_suggestion", "active", "created_at"}
invitePrimaryKeyColumns = []string{"id"}
)
@ -578,6 +604,13 @@ func (o *Invite) Insert(ctx context.Context, exec boil.ContextExecutor, columns
}
var err error
if !boil.TimestampsAreSkipped(ctx) {
currTime := time.Now().In(boil.GetLocation())
if o.CreatedAt.IsZero() {
o.CreatedAt = currTime
}
}
if err := o.doBeforeInsertHooks(ctx, exec); err != nil {
return err

View File

@ -1,4 +1,4 @@
// Code generated by SQLBoiler 4.4.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// Code generated by SQLBoiler 4.5.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models

View File

@ -1,4 +1,4 @@
// Code generated by SQLBoiler 4.4.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// Code generated by SQLBoiler 4.5.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models

View File

@ -4,8 +4,8 @@ dbname = "testrun/TestSimple/roomdb"
blacklist = ["gorp_migrations"]
# marshal pub_key strings ala @asdjjasd as feed references.
[[types]]
# marshal pub_key strings ala @asdjjasd as feed references.
[types.match]
type = "string"
#tables = ['fallback_auth']

View File

@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"sort"
"time"
refs "go.mindeco.de/ssb-refs"
)
@ -45,6 +46,7 @@ type Invite struct {
ID int64
CreatedBy User
CreatedAt time.Time
AliasSuggestion string
}