2017-05-06 17:55:02 +02:00
package model
import (
2017-05-21 20:20:40 +02:00
"encoding/json"
2017-05-22 00:22:42 +02:00
"fmt"
2017-05-24 09:11:13 +02:00
"time"
2017-05-20 12:45:27 +02:00
"github.com/NyaaPantsu/nyaa/config"
2017-05-06 17:55:02 +02:00
)
2017-05-20 01:10:16 +02:00
const (
2017-05-26 12:12:52 +02:00
// UserStatusBanned : Int for User status banned
UserStatusBanned = - 1
// UserStatusMember : Int for User status member
UserStatusMember = 0
// UserStatusTrusted : Int for User status trusted
UserStatusTrusted = 1
// UserStatusModerator : Int for User status moderator
2017-05-20 01:10:16 +02:00
UserStatusModerator = 2
2017-06-07 03:14:57 +02:00
// UserStatusScraped : Int for User status scrapped
UserStatusScraped = 3
2017-05-20 01:10:16 +02:00
)
2017-05-26 12:12:52 +02:00
// User model
2017-05-06 17:55:02 +02:00
type User struct {
2017-05-20 01:10:16 +02:00
ID uint ` gorm:"column:user_id;primary_key" `
Username string ` gorm:"column:username" `
Password string ` gorm:"column:password" `
Email string ` gorm:"column:email" `
Status int ` gorm:"column:status" `
CreatedAt time . Time ` gorm:"column:created_at" `
UpdatedAt time . Time ` gorm:"column:updated_at" `
2017-05-26 12:12:52 +02:00
APIToken string ` gorm:"column:api_token" `
APITokenExpiry time . Time ` gorm:"column:api_token_expiry" `
2017-05-20 01:10:16 +02:00
Language string ` gorm:"column:language" `
2017-05-27 19:08:47 +02:00
Theme string ` gorm:"column:theme" `
2017-05-31 04:21:35 +02:00
Mascot string ` gorm:"column:mascot" `
2017-06-05 16:32:48 +02:00
MascotURL string ` gorm:"column:mascot_url" `
2017-05-21 18:13:28 +02:00
UserSettings string ` gorm:"column:settings" `
2017-05-09 12:14:12 +02:00
// TODO: move this to PublicUser
2017-05-27 00:45:18 +02:00
Followers [ ] User // Don't work `gorm:"foreignkey:user_id;associationforeignkey:follower_id;many2many:user_follows"`
Likings [ ] User // Don't work `gorm:"foreignkey:follower_id;associationforeignkey:user_id;many2many:user_follows"`
2017-05-09 12:14:12 +02:00
2017-05-24 09:11:13 +02:00
MD5 string ` json:"md5" gorm:"column:md5" ` // Hash of email address, used for Gravatar
Torrents [ ] Torrent ` gorm:"ForeignKey:UploaderID" `
2017-05-21 18:13:28 +02:00
Notifications [ ] Notification ` gorm:"ForeignKey:UserID" `
2017-05-20 20:53:05 +02:00
2017-05-24 09:11:13 +02:00
UnreadNotifications int ` gorm:"-" ` // We don't want to loop every notifications when accessing user unread notif
Settings UserSettings ` gorm:"-" ` // We don't want to load settings everytime, stock it as a string, parse it when needed
2017-05-10 10:27:17 +02:00
}
2017-05-26 12:12:52 +02:00
// UserJSON : User model conversion in JSON
2017-05-10 21:09:37 +02:00
type UserJSON struct {
ID uint ` json:"user_id" `
Username string ` json:"username" `
Status int ` json:"status" `
2017-06-13 08:01:57 +02:00
APIToken string ` json:"token" `
MD5 string ` json:"md5" `
2017-05-10 21:09:37 +02:00
CreatedAt string ` json:"created_at" `
LikingCount int ` json:"liking_count" `
LikedCount int ` json:"liked_count" `
}
2017-05-26 12:12:52 +02:00
// Size : Returns the total size of memory recursively allocated for this struct
2017-05-10 10:27:17 +02:00
func ( u User ) Size ( ) ( s int ) {
s += 4 + // ints
6 * 2 + // string pointers
4 * 3 + //time.Time
3 * 2 + // arrays
// string arrays
2017-05-27 19:08:47 +02:00
len ( u . Username ) + len ( u . Password ) + len ( u . Email ) + len ( u . APIToken ) + len ( u . MD5 ) + len ( u . Language ) + len ( u . Theme )
2017-05-10 10:27:17 +02:00
s *= 8
// Ignoring foreign key users. Fuck them.
return
2017-05-06 17:55:02 +02:00
}
2017-05-26 12:12:52 +02:00
// IsBanned : Return true if user is banned
func ( u * User ) IsBanned ( ) bool {
2017-05-20 01:10:16 +02:00
return u . Status == UserStatusBanned
}
2017-05-26 12:12:52 +02:00
// IsMember : Return true if user is member
func ( u * User ) IsMember ( ) bool {
2017-05-20 01:10:16 +02:00
return u . Status == UserStatusMember
}
2017-05-26 12:12:52 +02:00
// IsTrusted : Return true if user is tusted
func ( u * User ) IsTrusted ( ) bool {
2017-05-20 01:10:16 +02:00
return u . Status == UserStatusTrusted
}
2017-05-26 12:12:52 +02:00
// IsModerator : Return true if user is moderator
func ( u * User ) IsModerator ( ) bool {
2017-05-20 01:10:16 +02:00
return u . Status == UserStatusModerator
}
2017-06-07 03:14:57 +02:00
// IsScraped : Return true if user is a scrapped user
func ( u * User ) IsScraped ( ) bool {
return u . Status == UserStatusScraped
2017-06-07 02:59:46 +02:00
}
2017-05-26 12:12:52 +02:00
// GetUnreadNotifications : Get unread notifications from a user
func ( u * User ) GetUnreadNotifications ( ) int {
2017-05-24 09:11:13 +02:00
if u . UnreadNotifications == 0 {
2017-05-20 20:53:05 +02:00
for _ , notif := range u . Notifications {
if ! notif . Read {
u . UnreadNotifications ++
}
}
}
return u . UnreadNotifications
}
2017-05-26 12:12:52 +02:00
// PublicUser : Is it Deprecated?
2017-05-08 19:26:29 +02:00
type PublicUser struct {
2017-05-10 10:27:17 +02:00
User * User
2017-05-06 17:55:02 +02:00
}
2017-05-26 12:12:52 +02:00
// UserFollows association table : different users following eachother
2017-05-09 17:14:13 +02:00
type UserFollows struct {
2017-05-09 12:14:12 +02:00
UserID uint ` gorm:"column:user_id" `
FollowerID uint ` gorm:"column:following" `
2017-05-09 07:21:14 +02:00
}
2017-05-10 13:32:45 +02:00
2017-05-26 12:12:52 +02:00
// UserUploadsOld model : Is it deprecated?
2017-05-10 13:32:45 +02:00
type UserUploadsOld struct {
Username string ` gorm:"column:username" `
2017-05-26 12:12:52 +02:00
TorrentID uint ` gorm:"column:torrent_id" `
2017-05-10 13:32:45 +02:00
}
2017-05-26 12:12:52 +02:00
// UserSettings : Struct for user settings, not a model
2017-05-21 18:13:28 +02:00
type UserSettings struct {
2017-05-24 09:11:13 +02:00
Settings map [ string ] bool ` json:"settings" `
2017-05-21 18:13:28 +02:00
}
2017-05-26 12:12:52 +02:00
// TableName : Return the name of OldComment table
2017-05-10 13:32:45 +02:00
func ( c UserUploadsOld ) TableName ( ) string {
2017-05-20 12:45:27 +02:00
// is this needed here?
2017-05-31 04:21:57 +02:00
return config . Conf . Models . UploadsOldTableName
2017-05-10 13:32:45 +02:00
}
2017-05-10 21:09:37 +02:00
2017-05-26 12:12:52 +02:00
// ToJSON : Conversion of a user model to json
2017-05-10 21:09:37 +02:00
func ( u * User ) ToJSON ( ) UserJSON {
json := UserJSON {
ID : u . ID ,
Username : u . Username ,
2017-06-13 08:01:57 +02:00
APIToken : u . APIToken ,
MD5 : u . MD5 ,
2017-05-10 21:09:37 +02:00
Status : u . Status ,
CreatedAt : u . CreatedAt . Format ( time . RFC3339 ) ,
2017-05-27 00:45:18 +02:00
LikingCount : len ( u . Followers ) ,
LikedCount : len ( u . Likings ) ,
2017-05-10 21:09:37 +02:00
}
return json
}
2017-05-21 18:13:28 +02:00
/* User Settings */
2017-05-26 12:12:52 +02:00
// Get a user setting by keyname
2017-05-24 09:11:13 +02:00
func ( s * UserSettings ) Get ( key string ) bool {
if val , ok := s . Settings [ key ] ; ok {
return val
2017-05-21 19:38:39 +02:00
}
2017-05-31 04:21:57 +02:00
return config . Conf . Users . DefaultUserSettings [ key ]
2017-05-21 19:38:39 +02:00
}
2017-05-26 12:12:52 +02:00
// GetSettings : get all user settings
2017-05-22 00:22:42 +02:00
func ( s * UserSettings ) GetSettings ( ) map [ string ] bool {
return s . Settings
2017-05-21 18:13:28 +02:00
}
2017-05-26 12:12:52 +02:00
// Set a user setting by keyname
2017-05-22 00:22:42 +02:00
func ( s * UserSettings ) Set ( key string , val bool ) {
if s . Settings == nil {
s . Settings = make ( map [ string ] bool )
}
s . Settings [ key ] = val
}
2017-05-26 12:12:52 +02:00
// ToDefault : Set user settings to default
2017-05-22 00:22:42 +02:00
func ( s * UserSettings ) ToDefault ( ) {
2017-05-31 04:21:57 +02:00
s . Settings = config . Conf . Users . DefaultUserSettings
2017-05-21 18:13:28 +02:00
}
2017-05-26 12:12:52 +02:00
func ( s * UserSettings ) initialize ( ) {
2017-05-22 00:22:42 +02:00
s . Settings = make ( map [ string ] bool )
2017-05-21 18:13:28 +02:00
}
2017-05-26 12:12:52 +02:00
// SaveSettings : Format settings into a json string for preparing before user insertion
2017-05-22 00:22:42 +02:00
func ( u * User ) SaveSettings ( ) {
byteArray , err := json . Marshal ( u . Settings )
2017-05-24 09:11:13 +02:00
if err != nil {
2017-05-22 00:22:42 +02:00
fmt . Print ( err )
}
u . UserSettings = string ( byteArray )
2017-05-21 18:13:28 +02:00
}
2017-05-26 12:12:52 +02:00
// ParseSettings : Function to parse json string into usersettings struct, only parse if necessary
2017-05-22 00:22:42 +02:00
func ( u * User ) ParseSettings ( ) {
2017-05-21 19:38:39 +02:00
if len ( u . Settings . GetSettings ( ) ) == 0 && u . UserSettings != "" {
2017-05-26 12:12:52 +02:00
u . Settings . initialize ( )
2017-05-22 00:22:42 +02:00
json . Unmarshal ( [ ] byte ( u . UserSettings ) , & u . Settings )
} else if len ( u . Settings . GetSettings ( ) ) == 0 && u . UserSettings != "" {
2017-05-26 12:12:52 +02:00
u . Settings . initialize ( )
2017-05-22 00:22:42 +02:00
u . Settings . ToDefault ( )
2017-05-21 19:38:39 +02:00
}
2017-05-24 09:11:13 +02:00
}