Nuestro burocrata preferido: Sam Lowry https://lowry.sindominio.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

75 lines
1.7 KiB

package db
import (
"encoding/json"
"log"
"time"
"go.etcd.io/bbolt"
)
var (
inviteBucket = []byte("invite")
)
// Invite holds the data of an invite
type Invite struct {
Code string
CreationDate time.Time
}
type inviteValue struct {
User string
CreationDate time.Time
}
// AddInvite stores in the db the inviteValue code generated by user
func (db *DB) AddInvite(code string, user string) error {
return db.put(inviteBucket, code, inviteValue{user, time.Now()})
}
// IsInviteValid checks if the inviteValue code is in the database
func (db *DB) IsInviteValid(code string) bool {
var inv inviteValue
err := db.get(inviteBucket, code, &inv)
return err == nil
}
// InviteHost returns the user name that created the inviteValue
func (db *DB) InviteHost(code string) (string, error) {
var inv inviteValue
err := db.get(inviteBucket, code, &inv)
return inv.User, err
}
// DelInvite code from the database
func (db *DB) DelInvite(code string) error {
return db.del(inviteBucket, code)
}
// ExpireInvites older than duration
func (db *DB) ExpireInvites(duration time.Duration) error {
return db.expire(inviteBucket, duration)
}
// ListUserInvites returns all the inviteValue codes generated by a user
func (db *DB) ListUserInvites(user string) ([]Invite, error) {
var invites []Invite
err := db.bolt.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(inviteBucket)
return b.ForEach(func(k, v []byte) error {
var inv inviteValue
err := json.Unmarshal(v, &inv)
if err != nil {
log.Printf("Error unmarshalling invite %s: %v", string(k), err)
return nil
}
if inv.User == user {
invites = append(invites, Invite{string(k), inv.CreationDate})
}
return nil
})
})
return invites, err
}