2017-05-06 17:55:02 +02:00
package model
import (
"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 (
UserStatusBanned = - 1
UserStatusMember = 0
UserStatusTrusted = 1
UserStatusModerator = 2
)
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" `
ApiToken string ` gorm:"column:api_token" `
ApiTokenExpiry time . Time ` gorm:"column:api_token_expiry" `
Language string ` gorm:"column:language" `
2017-05-09 12:14:12 +02:00
// TODO: move this to PublicUser
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
Likings [ ] User // Don't work `gorm:"foreignkey:user_id;associationforeignkey:follower_id;many2many:user_follows"`
Liked [ ] User // Don't work `gorm:"foreignkey:follower_id;associationforeignkey:user_id;many2many:user_follows"`
2017-05-09 12:14:12 +02:00
2017-05-10 13:57:31 +02:00
MD5 string ` json:"md5" gorm:"column:md5" ` // Hash of email address, used for Gravatar
2017-05-10 07:17:11 +02:00
Torrents [ ] Torrent ` gorm:"ForeignKey:UploaderID" `
2017-05-20 20:53:05 +02:00
2017-05-21 00:02:57 +02:00
UnreadNotifications int ` gorm:"-" ` // We don't want to loop every notifications when accessing user unread notif
2017-05-20 20:53:05 +02:00
Notifications [ ] Notification ` gorm:"ForeignKey:UserID" `
2017-05-10 10:27:17 +02:00
}
2017-05-10 21:09:37 +02:00
type UserJSON struct {
ID uint ` json:"user_id" `
Username string ` json:"username" `
Status int ` json:"status" `
CreatedAt string ` json:"created_at" `
LikingCount int ` json:"liking_count" `
LikedCount int ` json:"liked_count" `
}
2017-05-10 10:27:17 +02:00
// Returns the total size of memory recursively allocated for this struct
func ( u User ) Size ( ) ( s int ) {
s += 4 + // ints
6 * 2 + // string pointers
4 * 3 + //time.Time
3 * 2 + // arrays
// string arrays
2017-05-12 12:40:31 +02:00
len ( u . Username ) + len ( u . Password ) + len ( u . Email ) + len ( u . ApiToken ) + len ( u . MD5 ) + len ( u . Language )
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-20 01:10:16 +02:00
func ( u User ) IsBanned ( ) bool {
return u . Status == UserStatusBanned
}
func ( u User ) IsMember ( ) bool {
return u . Status == UserStatusMember
}
func ( u User ) IsTrusted ( ) bool {
return u . Status == UserStatusTrusted
}
func ( u User ) IsModerator ( ) bool {
return u . Status == UserStatusModerator
}
2017-05-20 20:53:05 +02:00
func ( u User ) GetUnreadNotifications ( ) int {
if u . UnreadNotifications == 0 {
for _ , notif := range u . Notifications {
if ! notif . Read {
u . UnreadNotifications ++
}
}
}
return u . UnreadNotifications
}
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-09 17:14:13 +02:00
// different users following eachother
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
type UserUploadsOld struct {
Username string ` gorm:"column:username" `
TorrentId uint ` gorm:"column:torrent_id" `
}
func ( c UserUploadsOld ) TableName ( ) string {
2017-05-20 12:45:27 +02:00
// is this needed here?
return config . UploadsOldTableName
2017-05-10 13:32:45 +02:00
}
2017-05-10 21:09:37 +02:00
func ( u * User ) ToJSON ( ) UserJSON {
json := UserJSON {
ID : u . ID ,
Username : u . Username ,
Status : u . Status ,
CreatedAt : u . CreatedAt . Format ( time . RFC3339 ) ,
2017-05-20 20:53:05 +02:00
LikingCount : len ( u . Likings ) ,
LikedCount : len ( u . Liked ) ,
2017-05-10 21:09:37 +02:00
}
return json
}