-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.go
161 lines (142 loc) · 3.68 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package lrs
import (
"crypto/rand"
"net"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/gosimple/slug"
"github.com/jbenet/go-base58"
"github.com/zoonman/gravatar"
"golang.org/x/crypto/bcrypt"
)
// User represents user entity
type User struct {
Model
Email string `validator:"email" gorm:"unique_index" json:"email,omitempty"`
EmailVerified bool `gorm:"default:false" json:"email_verified,omitempty"`
Password string `json:"-"`
Hash string `json:"-"`
Name string `json:"name"`
Slug string `json:"slug" gorm:"unique_index"`
Picture string `json:"picture"`
About string `json:"about,omitempty"`
Gender string `json:"gender,omitempty"`
Rank float32 `json:"rank"`
Online bool `json:"online"`
Roles []Role `json:"roles,omitempty"`
Profiles []SocialProfile `json:"profiles,omitempty" gorm:"[]"`
Devices []Device `json:"devices,omitempty"`
Settings *Settings `json:"settings,omitempty"`
// Status?
// Available
// Busy
// Offline
}
// UserList for list of the users
type UserList []User
// Device describes the device used by the user
type Device struct {
UserID uint
DeviceID string
Type string // browser, phone
UserAgent string
LastIP net.Addr
AccessAt time.Time
Subscribed bool
PushEndpoint string
PushKeyP256 string
PushAuth string
}
// Settings keep user settings under control
type Settings struct {
// Offline Notifications:
// 0. No
// 1. Push only
// 2. Immediate to email
// 3. Daily email digests
// 4. Weekly email digests
UserID uint
Notifications uint `json:"notifications"`
Timezone time.Location
}
// SocialProfile represents user profile in social networks
type SocialProfile struct {
Model
NetworkID string `json:"networkId"`
Network string `json:"name"`
Token string `json:"-"`
ExpiresAt time.Time `json:"-"`
UserID uint
}
// Role of the user
type Role struct {
Role string `json:"role"`
}
// UserAuthData packet
type UserAuthData struct {
Email string `json:"email"`
Password string `json:"password"`
RememberMe bool `json:"rememberMe"`
}
// returns a string for a given length
func randomString(l uint8) string {
b := make([]byte, l)
rand.Read(b)
return base58.Encode(b)[:l]
}
// SetPassword for the User
func (u *User) SetPassword(password string) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err == nil {
u.Password = string(hashedPassword)
u.Hash = randomString(16)
} else {
logrus.Error(err)
}
}
// MakeNameFromEmail method
func (u *User) MakeNameFromEmail() string {
emails := strings.Split(u.Email, "@")
name := emails[0]
for _, v := range []string{".", "_", "-"} {
name = strings.Replace(name, v, " ", -1)
}
return strings.Title(name)
}
// MakeSlug method
func (u *User) MakeSlug() {
slug.MaxLength = 12
u.Slug = strings.Replace(
slug.Make(u.Name),
"-",
"",
-1,
)
}
// MakeGravatarPicture method
func (u *User) MakeGravatarPicture() string {
return gravatar.Avatar(u.Email, 100)
}
// SafePluck returns sanitized version of User object
func (u *User) SafePluck() User {
var ru User
ru.Name = u.Name
ru.Picture = u.Picture
ru.Slug = u.Slug
ru.Rank = u.Rank
ru.Online = u.Online
ru.Gender = u.Gender
ru.ID = u.ID
ru.CreatedAt = u.CreatedAt
ru.UpdatedAt = u.UpdatedAt
return ru
}
// Map applies given function to list of users and
func (ul UserList) Map(f func(User) User) UserList {
nl := make(UserList, len(ul))
for i, v := range ul {
nl[i] = f(v)
}
return nl
}