woodpecker/shared/model/user.go

57 lines
1.8 KiB
Go
Raw Normal View History

package model
2014-06-04 21:25:38 +00:00
import (
"time"
)
type User struct {
2015-01-27 22:42:20 +00:00
ID int64 `meddler:"user_id,pk" json:"-"`
Remote string `meddler:"user_remote" json:"remote"`
Login string `meddler:"user_login" json:"login"`
Access string `meddler:"user_access" json:"-"`
Secret string `meddler:"user_secret" json:"-"`
Name string `meddler:"user_name" json:"name"`
Email string `meddler:"user_email" json:"email,omitempty"`
Gravatar string `meddler:"user_gravatar" json:"gravatar"`
Token string `meddler:"user_token" json:"-"`
Admin bool `meddler:"user_admin" json:"admin"`
Active bool `meddler:"user_active" json:"active"`
Syncing bool `meddler:"user_syncing" json:"syncing"`
Created int64 `meddler:"user_created" json:"created_at"`
Updated int64 `meddler:"user_updated" json:"updated_at"`
Synced int64 `meddler:"user_synced" json:"synced_at"`
TokenExpiry int64 `meddler:"user_access_expires,zeroisnull" json:"-"`
2014-06-04 21:25:38 +00:00
}
func NewUser(remote, login, email string) *User {
2014-06-04 21:25:38 +00:00
user := User{}
2014-10-22 07:13:47 +00:00
user.Token = GenerateToken()
2014-06-04 21:25:38 +00:00
user.Login = login
user.Remote = remote
user.Active = true
user.SetEmail(email)
return &user
}
// SetEmail sets the email address and calculate the Gravatar hash.
func (u *User) SetEmail(email string) {
u.Email = email
2014-10-22 07:13:47 +00:00
u.Gravatar = CreateGravatar(email)
2014-06-04 21:25:38 +00:00
}
func (u *User) IsStale() bool {
2014-06-04 21:25:38 +00:00
switch {
case u.Synced == 0:
return true
// refresh every 24 hours
case u.Synced+DefaultExpires < time.Now().Unix():
2014-06-04 21:25:38 +00:00
return true
default:
return false
}
}
// by default, let's expire the user
// cache after 72 hours
var DefaultExpires = int64(time.Hour.Seconds() * 72)