2022-08-10 05:10:47 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2022-08-13 06:11:36 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2022-08-10 05:10:47 +00:00
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/config"
|
2022-08-11 03:59:59 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/log"
|
2022-08-10 05:10:47 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2022-08-13 06:11:36 +00:00
|
|
|
"gorm.io/gorm"
|
2022-08-10 05:10:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// User account table.
|
|
|
|
type User struct {
|
2022-08-11 03:59:59 +00:00
|
|
|
ID uint64 `gorm:"primaryKey"`
|
2022-08-10 05:10:47 +00:00
|
|
|
Username string `gorm:"uniqueIndex"`
|
|
|
|
Email string `gorm:"uniqueIndex"`
|
|
|
|
HashedPassword string
|
2022-08-11 03:59:59 +00:00
|
|
|
IsAdmin bool `gorm:"index"`
|
|
|
|
Status UserStatus `gorm:"index"` // active, disabled
|
|
|
|
Visibility string `gorm:"index"` // public, private
|
2022-08-10 05:10:47 +00:00
|
|
|
Name *string
|
2022-08-11 03:59:59 +00:00
|
|
|
Birthdate time.Time
|
2022-08-10 05:10:47 +00:00
|
|
|
Certified bool
|
2022-08-12 06:03:06 +00:00
|
|
|
Explicit bool // user has opted-in to see explicit content
|
2022-08-10 05:10:47 +00:00
|
|
|
CreatedAt time.Time `gorm:"index"`
|
|
|
|
UpdatedAt time.Time `gorm:"index"`
|
2022-08-11 03:59:59 +00:00
|
|
|
LastLoginAt time.Time `gorm:"index"`
|
|
|
|
|
|
|
|
// Relational tables.
|
2022-08-12 06:03:06 +00:00
|
|
|
ProfileField []ProfileField
|
|
|
|
ProfilePhotoID uint64
|
2022-08-13 06:11:36 +00:00
|
|
|
ProfilePhoto Photo `gorm:"foreignKey:profile_photo_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preload related tables for the user (classmethod).
|
|
|
|
func (u *User) Preload() *gorm.DB {
|
|
|
|
return DB.Preload("ProfileField").Preload("ProfilePhoto")
|
2022-08-10 05:10:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 03:59:59 +00:00
|
|
|
// UserStatus options.
|
|
|
|
type UserStatus string
|
|
|
|
|
|
|
|
const (
|
|
|
|
UserStatusActive = "active"
|
|
|
|
UserStatusDisabled = "disabled"
|
2022-08-14 23:27:57 +00:00
|
|
|
UserStatusBanned = "banned"
|
2022-08-11 03:59:59 +00:00
|
|
|
)
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
// CreateUser. It is assumed username and email are correctly formatted.
|
|
|
|
func CreateUser(username, email, password string) (*User, error) {
|
|
|
|
// Verify username and email are unique.
|
|
|
|
if _, err := FindUser(username); err == nil {
|
|
|
|
return nil, errors.New("That username already exists. Please try a different username.")
|
|
|
|
} else if _, err := FindUser(email); err == nil {
|
|
|
|
return nil, errors.New("That email address is already registered.")
|
|
|
|
}
|
|
|
|
|
|
|
|
u := &User{
|
|
|
|
Username: username,
|
|
|
|
Email: email,
|
2022-08-11 03:59:59 +00:00
|
|
|
Status: UserStatusActive,
|
2022-08-10 05:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := u.HashPassword(password); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := DB.Create(u)
|
|
|
|
return u, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUser by ID.
|
|
|
|
func GetUser(userId uint64) (*User, error) {
|
|
|
|
user := &User{}
|
2022-08-13 06:11:36 +00:00
|
|
|
result := user.Preload().First(&user, userId)
|
2022-08-10 05:10:47 +00:00
|
|
|
return user, result.Error
|
|
|
|
}
|
|
|
|
|
2022-08-14 05:44:57 +00:00
|
|
|
// GetUsers queries for multiple user IDs and returns users in the same order.
|
|
|
|
func GetUsers(userIDs []uint64) ([]*User, error) {
|
|
|
|
userMap, err := MapUsers(userIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-order them per the original sequence.
|
|
|
|
var users = []*User{}
|
|
|
|
for _, uid := range userIDs {
|
|
|
|
if user, ok := userMap[uid]; ok {
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
// FindUser by username or email.
|
|
|
|
func FindUser(username string) (*User, error) {
|
2022-08-11 03:59:59 +00:00
|
|
|
if username == "" {
|
|
|
|
return nil, errors.New("username is required")
|
|
|
|
}
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
u := &User{}
|
|
|
|
if strings.ContainsRune(username, '@') {
|
2022-08-13 06:11:36 +00:00
|
|
|
result := u.Preload().Where("email = ?", username).Limit(1).First(u)
|
2022-08-10 05:10:47 +00:00
|
|
|
return u, result.Error
|
|
|
|
}
|
2022-08-13 06:11:36 +00:00
|
|
|
result := u.Preload().Where("username = ?", username).Limit(1).First(u)
|
2022-08-10 05:10:47 +00:00
|
|
|
return u, result.Error
|
|
|
|
}
|
|
|
|
|
2022-08-14 05:44:57 +00:00
|
|
|
// UserSearch config.
|
|
|
|
type UserSearch struct {
|
|
|
|
EmailOrUsername string
|
|
|
|
Gender string
|
|
|
|
Orientation string
|
|
|
|
MaritalStatus string
|
|
|
|
Certified bool
|
2022-08-14 21:40:57 +00:00
|
|
|
AgeMin int
|
|
|
|
AgeMax int
|
2022-08-14 05:44:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 00:45:55 +00:00
|
|
|
// SearchUsers from the perspective of a given user.
|
|
|
|
func SearchUsers(userID uint64, search *UserSearch, pager *Pagination) ([]*User, error) {
|
2022-08-14 05:44:57 +00:00
|
|
|
if search == nil {
|
|
|
|
search = &UserSearch{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-08-15 00:45:55 +00:00
|
|
|
users = []*User{}
|
|
|
|
query *gorm.DB
|
|
|
|
wheres = []string{}
|
|
|
|
placeholders = []interface{}{}
|
|
|
|
blockedUserIDs = BlockedUserIDs(userID)
|
2022-08-14 05:44:57 +00:00
|
|
|
)
|
|
|
|
|
2022-08-15 00:45:55 +00:00
|
|
|
if len(blockedUserIDs) > 0 {
|
|
|
|
wheres = append(wheres, "id NOT IN ?")
|
|
|
|
placeholders = append(placeholders, blockedUserIDs)
|
|
|
|
}
|
|
|
|
|
2022-08-14 05:44:57 +00:00
|
|
|
if search.EmailOrUsername != "" {
|
|
|
|
ilike := "%" + strings.TrimSpace(strings.ToLower(search.EmailOrUsername)) + "%"
|
|
|
|
wheres = append(wheres, "(email LIKE ? OR username LIKE ?)")
|
|
|
|
placeholders = append(placeholders, ilike, ilike)
|
|
|
|
}
|
|
|
|
|
|
|
|
if search.Gender != "" {
|
|
|
|
wheres = append(wheres, `
|
|
|
|
EXISTS (
|
|
|
|
SELECT 1 FROM profile_fields
|
|
|
|
WHERE user_id = users.id AND name = ? AND value = ?
|
|
|
|
)
|
|
|
|
`)
|
|
|
|
placeholders = append(placeholders, "gender", search.Gender)
|
|
|
|
}
|
|
|
|
|
|
|
|
if search.Orientation != "" {
|
|
|
|
wheres = append(wheres, `
|
|
|
|
EXISTS (
|
|
|
|
SELECT 1 FROM profile_fields
|
|
|
|
WHERE user_id = users.id AND name = ? AND value = ?
|
|
|
|
)
|
|
|
|
`)
|
|
|
|
placeholders = append(placeholders, "orientation", search.Orientation)
|
|
|
|
}
|
|
|
|
|
|
|
|
if search.MaritalStatus != "" {
|
|
|
|
wheres = append(wheres, `
|
|
|
|
EXISTS (
|
|
|
|
SELECT 1 FROM profile_fields
|
|
|
|
WHERE user_id = users.id AND name = ? AND value = ?
|
|
|
|
)
|
|
|
|
`)
|
|
|
|
placeholders = append(placeholders, "marital_status", search.MaritalStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
if search.Certified {
|
|
|
|
wheres = append(wheres, "certified = ?")
|
|
|
|
placeholders = append(placeholders, search.Certified)
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
if search.AgeMin > 0 {
|
|
|
|
date := time.Now().AddDate(-search.AgeMin, 0, 0)
|
|
|
|
wheres = append(wheres, "birthdate <= ?")
|
|
|
|
placeholders = append(placeholders, date)
|
|
|
|
}
|
|
|
|
|
|
|
|
if search.AgeMax > 0 {
|
|
|
|
date := time.Now().AddDate(-search.AgeMax-1, 0, 0)
|
|
|
|
wheres = append(wheres, "birthdate >= ?")
|
|
|
|
placeholders = append(placeholders, date)
|
|
|
|
}
|
|
|
|
|
2022-08-14 05:44:57 +00:00
|
|
|
query = (&User{}).Preload().Where(
|
|
|
|
strings.Join(wheres, " AND "),
|
|
|
|
placeholders...,
|
2022-08-14 21:40:57 +00:00
|
|
|
).Order(pager.Sort)
|
2022-08-14 05:44:57 +00:00
|
|
|
query.Model(&User{}).Count(&pager.Total)
|
|
|
|
result := query.Offset(pager.GetOffset()).Limit(pager.PerPage).Find(&users)
|
|
|
|
return users, result.Error
|
|
|
|
}
|
|
|
|
|
2022-08-13 22:39:31 +00:00
|
|
|
// UserMap helps map a set of users to look up by ID.
|
|
|
|
type UserMap map[uint64]*User
|
|
|
|
|
|
|
|
// MapUsers looks up a set of user IDs in bulk and returns a UserMap suitable for templates.
|
|
|
|
// Useful to avoid circular reference issues with Photos especially; the Site Gallery queries
|
|
|
|
// photos of ALL users and MapUsers helps stitch them together for the frontend.
|
|
|
|
func MapUsers(userIDs []uint64) (UserMap, error) {
|
2022-08-14 00:42:42 +00:00
|
|
|
var (
|
|
|
|
usermap = UserMap{}
|
|
|
|
set = map[uint64]interface{}{}
|
|
|
|
distinct = []uint64{}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Uniqueify users.
|
|
|
|
for _, uid := range userIDs {
|
|
|
|
if _, ok := set[uid]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
set[uid] = nil
|
|
|
|
distinct = append(distinct, uid)
|
|
|
|
}
|
2022-08-13 22:39:31 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
users = []*User{}
|
2022-08-14 00:42:42 +00:00
|
|
|
result = (&User{}).Preload().Where("id IN ?", distinct).Find(&users)
|
2022-08-13 22:39:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if result.Error == nil {
|
|
|
|
for _, row := range users {
|
|
|
|
usermap[row.ID] = row
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return usermap, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has a user ID in the map?
|
|
|
|
func (um UserMap) Has(id uint64) bool {
|
|
|
|
_, ok := um[id]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a user from the UserMap.
|
|
|
|
func (um UserMap) Get(id uint64) *User {
|
|
|
|
if user, ok := um[id]; ok {
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-16 05:33:17 +00:00
|
|
|
// NameOrUsername returns the name (if not null or empty) or the username.
|
|
|
|
func (u *User) NameOrUsername() string {
|
|
|
|
if u.Name != nil && len(*u.Name) > 0 {
|
|
|
|
return *u.Name
|
|
|
|
} else {
|
|
|
|
return u.Username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
// HashPassword sets the user's hashed (bcrypt) password.
|
|
|
|
func (u *User) HashPassword(password string) error {
|
|
|
|
passwd, err := bcrypt.GenerateFromPassword([]byte(password), config.BcryptCost)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.HashedPassword = string(passwd)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckPassword verifies the password is correct. Returns nil on success.
|
|
|
|
func (u *User) CheckPassword(password string) error {
|
|
|
|
return bcrypt.CompareHashAndPassword([]byte(u.HashedPassword), []byte(password))
|
|
|
|
}
|
|
|
|
|
2022-08-11 03:59:59 +00:00
|
|
|
// SetProfileField sets or creates a named profile field.
|
|
|
|
func (u *User) SetProfileField(name, value string) {
|
|
|
|
// Check if it exists.
|
|
|
|
log.Debug("User(%s).SetProfileField(%s, %s)", u.Username, name, value)
|
|
|
|
var exists bool
|
|
|
|
for _, field := range u.ProfileField {
|
|
|
|
log.Debug("\tCheck existing field %s", field.Name)
|
|
|
|
if field.Name == name {
|
|
|
|
log.Debug("\tFound existing field!")
|
|
|
|
changed := field.Value != value
|
|
|
|
field.Value = value
|
|
|
|
exists = true
|
|
|
|
|
|
|
|
// Save it now. TODO: otherwise gorm doesn't know we changed
|
|
|
|
// it and it won't be inserted when the User is saved. But
|
|
|
|
// this is probably not performant to do!
|
|
|
|
if changed {
|
|
|
|
DB.Save(&field)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u.ProfileField = append(u.ProfileField, ProfileField{
|
|
|
|
Name: name,
|
|
|
|
Value: value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProfileField returns the value of a profile field or blank string.
|
|
|
|
func (u *User) GetProfileField(name string) string {
|
|
|
|
for _, field := range u.ProfileField {
|
|
|
|
if field.Name == name {
|
|
|
|
return field.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProfileFieldIn checks if a substring is IN a profile field. Currently
|
|
|
|
// does a naive strings.Contains(), intended for the "here_for" field.
|
|
|
|
func (u *User) ProfileFieldIn(field, substr string) bool {
|
|
|
|
value := u.GetProfileField(field)
|
|
|
|
return strings.Contains(value, substr)
|
|
|
|
}
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
// Save user.
|
|
|
|
func (u *User) Save() error {
|
|
|
|
result := DB.Save(u)
|
|
|
|
return result.Error
|
|
|
|
}
|
2022-08-13 06:11:36 +00:00
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
// Delete a user. NOTE: use the models/deletion/DeleteUser() function
|
|
|
|
// instead of this to do a deep scrub of all related data!
|
|
|
|
func (u *User) Delete() error {
|
|
|
|
return DB.Delete(u).Error
|
|
|
|
}
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
// Print user object as pretty JSON.
|
|
|
|
func (u *User) Print() string {
|
|
|
|
var (
|
|
|
|
buf bytes.Buffer
|
|
|
|
enc = json.NewEncoder(&buf)
|
|
|
|
)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
enc.Encode(u)
|
|
|
|
return buf.String()
|
|
|
|
}
|