2017-09-13 15:09:13 +00:00
/ *
*
* Gosora User File
2019-05-03 08:34:18 +00:00
* Copyright Azareal 2017 - 2020
2017-09-13 15:09:13 +00:00
*
* /
2017-11-10 03:33:11 +00:00
package common
2017-05-11 13:04:43 +00:00
2017-06-05 11:57:27 +00:00
import (
2017-10-21 00:27:47 +00:00
"database/sql"
"errors"
2017-09-03 04:50:31 +00:00
"strconv"
2017-11-02 13:35:19 +00:00
"strings"
2017-09-03 04:50:31 +00:00
"time"
2017-06-28 12:05:26 +00:00
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/query_gen"
2018-07-28 12:52:23 +00:00
"github.com/go-sql-driver/mysql"
2017-06-05 11:57:27 +00:00
)
2016-12-02 07:38:54 +00:00
2017-10-16 07:32:58 +00:00
// TODO: Replace any literals with this
2017-11-10 03:33:11 +00:00
var BanGroup = 4
2017-10-16 07:32:58 +00:00
2018-05-27 09:36:35 +00:00
// TODO: Use something else as the guest avatar, maybe a question mark of some sort?
2017-11-11 04:06:16 +00:00
// GuestUser is an instance of user which holds guest data to avoid having to initialise a guest every time
2018-05-27 09:36:35 +00:00
var GuestUser = User { ID : 0 , Name : "Guest" , Link : "#" , Group : 6 , Perms : GuestPerms } // BuildAvatar is done in site.go to make sure it's done after init
2017-10-21 00:27:47 +00:00
var ErrNoTempGroup = errors . New ( "We couldn't find a temporary group for this user" )
2017-09-03 04:50:31 +00:00
type User struct {
ID int
Link string
Name string
Email string
Group int
Active bool
IsMod bool
IsSuperMod bool
IsAdmin bool
IsSuperAdmin bool
IsBanned bool
Perms Perms
PluginPerms map [ string ] bool
Session string
2018-01-08 08:53:51 +00:00
//AuthToken string
2018-07-28 12:52:23 +00:00
Loggedin bool
RawAvatar string
Avatar string
MicroAvatar string
Message string
URLPrefix string // Move this to another table? Create a user lite?
URLName string
Tag string
Level int
Score int
Liked int
LastIP string // ! This part of the UserCache data might fall out of date
2019-04-02 07:43:11 +00:00
LastAgent string // ! Temporary hack, don't use
2018-07-28 12:52:23 +00:00
TempGroup int
2016-12-02 07:38:54 +00:00
}
2018-06-24 13:49:29 +00:00
func ( user * User ) WebSockets ( ) * WsJSONUser {
var groupID = user . Group
if user . TempGroup != 0 {
groupID = user . TempGroup
}
// TODO: Do we want to leak the user's permissions? Users will probably be able to see their status from the group tags, but still
2019-02-10 05:52:26 +00:00
return & WsJSONUser { user . ID , user . Link , user . Name , groupID , user . IsMod , user . Avatar , user . MicroAvatar , user . Level , user . Score , user . Liked }
2018-06-24 13:49:29 +00:00
}
// Use struct tags to avoid having to define this? It really depends on the circumstances, sometimes we want the whole thing, sometimes... not.
type WsJSONUser struct {
2019-02-10 05:52:26 +00:00
ID int
Link string
Name string
Group int // Be sure to mask with TempGroup
IsMod bool
Avatar string
MicroAvatar string
Level int
Score int
Liked int
2018-06-24 13:49:29 +00:00
}
2018-08-11 15:53:42 +00:00
func ( user * User ) Me ( ) * MeUser {
var groupID = user . Group
if user . TempGroup != 0 {
groupID = user . TempGroup
}
return & MeUser { user . ID , user . Link , user . Name , groupID , user . Active , user . IsMod , user . IsSuperMod , user . IsAdmin , user . IsBanned , user . Session , user . Avatar , user . MicroAvatar , user . Tag , user . Level , user . Score , user . Liked }
}
// For when users need to see their own data, I've omitted some redundancies and less useful items, so we don't wind up sending them on every request
type MeUser struct {
ID int
Link string
Name string
Group int
Active bool
IsMod bool
IsSuperMod bool
IsAdmin bool
IsBanned bool
// TODO: Implement these as copies (might already be the case for Perms, but we'll want to look at it's definition anyway)
//Perms Perms
//PluginPerms map[string]bool
Session string
Avatar string
MicroAvatar string
Tag string
Level int
Score int
Liked int
}
2017-11-11 04:06:16 +00:00
type UserStmts struct {
2018-05-28 06:27:12 +00:00
activate * sql . Stmt
changeGroup * sql . Stmt
delete * sql . Stmt
setAvatar * sql . Stmt
setUsername * sql . Stmt
incrementTopics * sql . Stmt
updateLevel * sql . Stmt
2018-06-17 07:28:18 +00:00
update * sql . Stmt
2018-05-28 06:27:12 +00:00
// TODO: Split these into a sub-struct
2017-11-11 04:06:16 +00:00
incrementScore * sql . Stmt
incrementPosts * sql . Stmt
incrementBigposts * sql . Stmt
incrementMegaposts * sql . Stmt
2018-03-31 05:25:27 +00:00
incrementLiked * sql . Stmt
2018-05-28 06:27:12 +00:00
decrementLiked * sql . Stmt
updateLastIP * sql . Stmt
2017-11-11 04:06:16 +00:00
setPassword * sql . Stmt
2018-07-28 12:52:23 +00:00
scheduleAvatarResize * sql . Stmt
2017-11-11 04:06:16 +00:00
}
var userStmts UserStmts
func init ( ) {
2017-11-12 03:29:05 +00:00
DbInits . Add ( func ( acc * qgen . Accumulator ) error {
2019-07-28 23:19:46 +00:00
var w = "uid = ?"
2017-11-11 04:06:16 +00:00
userStmts = UserStmts {
2019-07-28 23:19:46 +00:00
activate : acc . SimpleUpdate ( "users" , "active = 1" , w ) ,
changeGroup : acc . SimpleUpdate ( "users" , "group = ?" , w ) , // TODO: Implement user_count for users_groups here
delete : acc . SimpleDelete ( "users" , w ) ,
setAvatar : acc . Update ( "users" ) . Set ( "avatar = ?" ) . Where ( w ) . Prepare ( ) ,
setUsername : acc . Update ( "users" ) . Set ( "name = ?" ) . Where ( w ) . Prepare ( ) ,
incrementTopics : acc . SimpleUpdate ( "users" , "topics = topics + ?" , w ) ,
updateLevel : acc . SimpleUpdate ( "users" , "level = ?" , w ) ,
update : acc . Update ( "users" ) . Set ( "name = ?, email = ?, group = ?" ) . Where ( w ) . Prepare ( ) , // TODO: Implement user_count for users_groups on things which use this
incrementScore : acc . SimpleUpdate ( "users" , "score = score + ?" , w ) ,
incrementPosts : acc . SimpleUpdate ( "users" , "posts = posts + ?" , w ) ,
incrementBigposts : acc . SimpleUpdate ( "users" , "posts = posts + ?, bigposts = bigposts + ?" , w ) ,
incrementMegaposts : acc . SimpleUpdate ( "users" , "posts = posts + ?, bigposts = bigposts + ?, megaposts = megaposts + ?" , w ) ,
incrementLiked : acc . SimpleUpdate ( "users" , "liked = liked + ?, lastLiked = UTC_TIMESTAMP()" , w ) ,
decrementLiked : acc . SimpleUpdate ( "users" , "liked = liked - ?" , w ) ,
2018-03-31 05:25:27 +00:00
//recalcLastLiked: acc...
2019-07-28 23:19:46 +00:00
updateLastIP : acc . SimpleUpdate ( "users" , "last_ip = ?" , w ) ,
2017-11-11 04:06:16 +00:00
2019-07-28 23:19:46 +00:00
setPassword : acc . Update ( "users" ) . Set ( "password = ?, salt = ?" ) . Where ( w ) . Prepare ( ) ,
2018-07-28 12:52:23 +00:00
scheduleAvatarResize : acc . Insert ( "users_avatar_queue" ) . Columns ( "uid" ) . Fields ( "?" ) . Prepare ( ) ,
2017-11-11 04:06:16 +00:00
}
return acc . FirstError ( )
} )
2017-01-03 07:47:31 +00:00
}
2017-11-02 13:35:19 +00:00
func ( user * User ) Init ( ) {
2018-07-28 12:52:23 +00:00
user . Avatar , user . MicroAvatar = BuildAvatar ( user . ID , user . RawAvatar )
2017-11-11 04:06:16 +00:00
user . Link = BuildProfileURL ( NameToSlug ( user . Name ) , user . ID )
2017-11-23 05:37:08 +00:00
user . Tag = Groups . DirtyGet ( user . Group ) . Tag
2017-11-11 04:06:16 +00:00
user . InitPerms ( )
}
2017-11-23 05:37:08 +00:00
// TODO: Refactor this idiom into something shorter, maybe with a NullUserCache when one isn't set?
2017-11-11 04:06:16 +00:00
func ( user * User ) CacheRemove ( ) {
2017-11-23 05:37:08 +00:00
ucache := Users . GetCache ( )
if ucache != nil {
ucache . Remove ( user . ID )
2017-11-02 13:35:19 +00:00
}
2018-11-19 23:06:15 +00:00
TopicListThaw . Thaw ( )
2017-11-02 13:35:19 +00:00
}
2017-08-27 09:33:45 +00:00
func ( user * User ) Ban ( duration time . Duration , issuedBy int ) error {
2017-11-11 04:06:16 +00:00
return user . ScheduleGroupUpdate ( BanGroup , issuedBy , duration )
2017-08-27 09:33:45 +00:00
}
func ( user * User ) Unban ( ) error {
2017-10-21 00:27:47 +00:00
return user . RevertGroupUpdate ( )
}
func ( user * User ) deleteScheduleGroupTx ( tx * sql . Tx ) error {
deleteScheduleGroupStmt , err := qgen . Builder . SimpleDeleteTx ( tx , "users_groups_scheduler" , "uid = ?" )
if err != nil {
return err
}
_ , err = deleteScheduleGroupStmt . Exec ( user . ID )
return err
}
func ( user * User ) setTempGroupTx ( tx * sql . Tx , tempGroup int ) error {
setTempGroupStmt , err := qgen . Builder . SimpleUpdateTx ( tx , "users" , "temp_group = ?" , "uid = ?" )
if err != nil {
return err
2017-08-27 09:33:45 +00:00
}
2017-10-21 00:27:47 +00:00
_ , err = setTempGroupStmt . Exec ( tempGroup , user . ID )
2017-09-22 02:21:17 +00:00
return err
2017-08-27 09:33:45 +00:00
}
// Make this more stateless?
func ( user * User ) ScheduleGroupUpdate ( gid int , issuedBy int , duration time . Duration ) error {
var temporary bool
if duration . Nanoseconds ( ) != 0 {
temporary = true
}
revertAt := time . Now ( ) . Add ( duration )
2017-10-21 00:27:47 +00:00
2017-11-11 04:06:16 +00:00
tx , err := qgen . Builder . Begin ( )
2017-10-21 00:27:47 +00:00
if err != nil {
return err
}
defer tx . Rollback ( )
err = user . deleteScheduleGroupTx ( tx )
if err != nil {
return err
}
createScheduleGroupTx , err := qgen . Builder . SimpleInsertTx ( tx , "users_groups_scheduler" , "uid, set_group, issued_by, issued_at, revert_at, temporary" , "?,?,?,UTC_TIMESTAMP(),?,?" )
2017-08-27 09:33:45 +00:00
if err != nil {
return err
}
2017-10-21 00:27:47 +00:00
_ , err = createScheduleGroupTx . Exec ( user . ID , gid , issuedBy , revertAt , temporary )
if err != nil {
return err
}
err = user . setTempGroupTx ( tx , gid )
if err != nil {
return err
}
err = tx . Commit ( )
2017-11-11 04:06:16 +00:00
user . CacheRemove ( )
2017-09-22 02:21:17 +00:00
return err
2017-08-27 09:33:45 +00:00
}
func ( user * User ) RevertGroupUpdate ( ) error {
2017-11-11 04:06:16 +00:00
tx , err := qgen . Builder . Begin ( )
2017-10-21 00:27:47 +00:00
if err != nil {
return err
}
defer tx . Rollback ( )
err = user . deleteScheduleGroupTx ( tx )
2017-08-27 09:33:45 +00:00
if err != nil {
return err
}
2017-10-21 00:27:47 +00:00
err = user . setTempGroupTx ( tx , 0 )
if err != nil {
return err
}
err = tx . Commit ( )
2017-11-11 04:06:16 +00:00
user . CacheRemove ( )
2017-09-22 02:21:17 +00:00
return err
2017-07-12 11:05:18 +00:00
}
2017-06-06 14:41:06 +00:00
2017-09-22 02:21:17 +00:00
// TODO: Use a transaction here
2017-09-25 00:48:35 +00:00
// ? - Add a Deactivate method? Not really needed, if someone's been bad you could do a ban, I guess it might be useful, if someone says that email x isn't actually owned by the user in question?
2017-09-22 02:21:17 +00:00
func ( user * User ) Activate ( ) ( err error ) {
2017-11-11 04:06:16 +00:00
_ , err = userStmts . activate . Exec ( user . ID )
2016-12-02 07:38:54 +00:00
if err != nil {
return err
}
2017-11-11 04:06:16 +00:00
_ , err = userStmts . changeGroup . Exec ( Config . DefaultGroup , user . ID )
user . CacheRemove ( )
2017-09-22 02:21:17 +00:00
return err
2017-02-10 13:39:13 +00:00
}
2017-10-16 07:32:58 +00:00
// TODO: Write tests for this
// TODO: Delete this user's content too?
// TODO: Expose this to the admin?
func ( user * User ) Delete ( ) error {
2017-11-11 04:06:16 +00:00
_ , err := userStmts . delete . Exec ( user . ID )
2017-10-16 07:32:58 +00:00
if err != nil {
return err
}
2017-11-11 04:06:16 +00:00
user . CacheRemove ( )
2017-10-16 07:32:58 +00:00
return err
}
2018-05-11 05:41:51 +00:00
func ( user * User ) bindStmt ( stmt * sql . Stmt , params ... interface { } ) ( err error ) {
params = append ( params , user . ID )
_ , err = stmt . Exec ( params ... )
2017-11-11 04:06:16 +00:00
user . CacheRemove ( )
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
return err
}
2018-05-11 05:41:51 +00:00
func ( user * User ) ChangeName ( username string ) ( err error ) {
return user . bindStmt ( userStmts . setUsername , username )
}
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
func ( user * User ) ChangeAvatar ( avatar string ) ( err error ) {
2018-05-13 06:56:59 +00:00
return user . bindStmt ( userStmts . setAvatar , avatar )
2017-10-21 00:27:47 +00:00
}
2018-07-28 12:52:23 +00:00
// TODO: Abstract this with an interface so we can scale this with an actual dedicated queue in a real cluster
func ( user * User ) ScheduleAvatarResize ( ) ( err error ) {
_ , err = userStmts . scheduleAvatarResize . Exec ( user . ID )
if err != nil {
// TODO: Do a more generic check so that we're not as tied to MySQL
me , ok := err . ( * mysql . MySQLError )
if ! ok {
return err
}
// If it's just telling us that the item already exists in the database, then we can ignore it, as it doesn't matter if it's this call or another which schedules the item in the queue
if me . Number != 1062 {
return err
}
}
return nil
}
2017-10-21 00:27:47 +00:00
func ( user * User ) ChangeGroup ( group int ) ( err error ) {
2018-05-28 06:27:12 +00:00
return user . bindStmt ( userStmts . changeGroup , group )
2017-11-11 04:06:16 +00:00
}
// ! Only updates the database not the *User for safety reasons
func ( user * User ) UpdateIP ( host string ) error {
_ , err := userStmts . updateLastIP . Exec ( host , user . ID )
2018-11-29 07:27:17 +00:00
ucache := Users . GetCache ( )
if ucache != nil {
ucache . Remove ( user . ID )
}
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
return err
}
2018-06-17 07:28:18 +00:00
func ( user * User ) Update ( newname string , newemail string , newgroup int ) ( err error ) {
return user . bindStmt ( userStmts . update , newname , newemail , newgroup )
}
2017-11-11 04:06:16 +00:00
func ( user * User ) IncreasePostStats ( wcount int , topic bool ) ( err error ) {
2017-01-12 02:55:08 +00:00
var mod int
2017-09-03 04:50:31 +00:00
baseScore := 1
2017-01-12 02:55:08 +00:00
if topic {
2017-11-11 04:06:16 +00:00
_ , err = userStmts . incrementTopics . Exec ( 1 , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
2017-09-03 04:50:31 +00:00
baseScore = 2
2017-01-12 02:55:08 +00:00
}
2017-06-06 14:41:06 +00:00
2017-11-11 04:06:16 +00:00
settings := SettingBox . Load ( ) . ( SettingMap )
2017-06-10 07:58:15 +00:00
if wcount >= settings [ "megapost_min_words" ] . ( int ) {
2017-11-11 04:06:16 +00:00
_ , err = userStmts . incrementMegaposts . Exec ( 1 , 1 , 1 , user . ID )
2017-01-12 02:55:08 +00:00
mod = 4
2017-06-10 07:58:15 +00:00
} else if wcount >= settings [ "bigpost_min_words" ] . ( int ) {
2017-11-11 04:06:16 +00:00
_ , err = userStmts . incrementBigposts . Exec ( 1 , 1 , user . ID )
2017-01-12 02:55:08 +00:00
mod = 1
} else {
2017-11-11 04:06:16 +00:00
_ , err = userStmts . incrementPosts . Exec ( 1 , user . ID )
2017-11-05 01:04:57 +00:00
}
if err != nil {
return err
2017-01-12 02:55:08 +00:00
}
2017-11-05 01:04:57 +00:00
2017-11-11 04:06:16 +00:00
_ , err = userStmts . incrementScore . Exec ( baseScore + mod , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
2017-08-13 11:22:34 +00:00
//log.Print(user.Score + base_score + mod)
//log.Print(getLevel(user.Score + base_score + mod))
2017-09-10 16:57:22 +00:00
// TODO: Use a transaction to prevent level desyncs?
2017-11-11 04:06:16 +00:00
_ , err = userStmts . updateLevel . Exec ( GetLevel ( user . Score + baseScore + mod ) , user . ID )
2017-01-12 02:55:08 +00:00
return err
}
2017-11-11 04:06:16 +00:00
func ( user * User ) DecreasePostStats ( wcount int , topic bool ) ( err error ) {
2017-01-12 02:55:08 +00:00
var mod int
2017-09-03 04:50:31 +00:00
baseScore := - 1
2017-01-12 02:55:08 +00:00
if topic {
2017-11-11 04:06:16 +00:00
_ , err = userStmts . incrementTopics . Exec ( - 1 , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
2017-09-03 04:50:31 +00:00
baseScore = - 2
2017-01-12 02:55:08 +00:00
}
2017-06-06 14:41:06 +00:00
2017-11-11 04:06:16 +00:00
settings := SettingBox . Load ( ) . ( SettingMap )
2017-06-12 09:03:14 +00:00
if wcount >= settings [ "megapost_min_words" ] . ( int ) {
2017-11-11 04:06:16 +00:00
_ , err = userStmts . incrementMegaposts . Exec ( - 1 , - 1 , - 1 , user . ID )
2017-01-12 02:55:08 +00:00
mod = 4
2017-06-12 09:03:14 +00:00
} else if wcount >= settings [ "bigpost_min_words" ] . ( int ) {
2017-11-11 04:06:16 +00:00
_ , err = userStmts . incrementBigposts . Exec ( - 1 , - 1 , user . ID )
2017-01-12 02:55:08 +00:00
mod = 1
} else {
2017-11-11 04:06:16 +00:00
_ , err = userStmts . incrementPosts . Exec ( - 1 , user . ID )
2017-11-05 01:04:57 +00:00
}
if err != nil {
return err
2017-01-12 02:55:08 +00:00
}
2017-11-05 01:04:57 +00:00
2017-11-11 04:06:16 +00:00
_ , err = userStmts . incrementScore . Exec ( baseScore - mod , user . ID )
2017-01-12 02:55:08 +00:00
if err != nil {
return err
}
2017-09-10 16:57:22 +00:00
// TODO: Use a transaction to prevent level desyncs?
2017-11-11 04:06:16 +00:00
_ , err = userStmts . updateLevel . Exec ( GetLevel ( user . Score - baseScore - mod ) , user . ID )
2017-01-12 02:55:08 +00:00
return err
}
2017-02-15 10:49:30 +00:00
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
// Copy gives you a non-pointer concurrency safe copy of the user
2017-09-28 22:16:34 +00:00
func ( user * User ) Copy ( ) User {
return * user
}
2017-09-25 00:48:35 +00:00
// TODO: Write unit tests for this
2017-11-11 04:06:16 +00:00
func ( user * User ) InitPerms ( ) {
2017-09-15 22:20:01 +00:00
if user . TempGroup != 0 {
user . Group = user . TempGroup
}
2017-11-23 05:37:08 +00:00
group := Groups . DirtyGet ( user . Group )
2017-09-03 04:50:31 +00:00
if user . IsSuperAdmin {
2017-08-06 15:22:18 +00:00
user . Perms = AllPerms
user . PluginPerms = AllPluginPerms
} else {
2017-09-15 22:20:01 +00:00
user . Perms = group . Perms
user . PluginPerms = group . PluginPerms
2017-08-27 09:33:45 +00:00
}
2018-11-29 07:27:17 +00:00
/ * if len ( group . CanSee ) == 0 {
panic ( "should not be zero" )
} * /
2017-08-27 09:33:45 +00:00
2017-09-15 22:20:01 +00:00
user . IsAdmin = user . IsSuperAdmin || group . IsAdmin
user . IsSuperMod = user . IsAdmin || group . IsMod
2017-09-03 04:50:31 +00:00
user . IsMod = user . IsSuperMod
2017-09-15 22:20:01 +00:00
user . IsBanned = group . IsBanned
2017-09-03 04:50:31 +00:00
if user . IsBanned && user . IsSuperMod {
user . IsBanned = false
2017-02-15 10:49:30 +00:00
}
2017-02-28 09:27:28 +00:00
}
2018-12-31 09:03:49 +00:00
var guestAvatar GuestAvatar
type GuestAvatar struct {
Normal string
Micro string
}
2018-07-28 12:52:23 +00:00
func buildNoavatar ( uid int , width int ) string {
2019-05-03 08:34:18 +00:00
if ! Config . DisableNoavatarRange {
// TODO: Find a faster algorithm
if uid > 50000 {
uid -= 50000
}
if uid > 5000 {
uid -= 5000
}
if uid > 500 {
uid -= 500
}
for uid > 50 {
uid -= 50
}
}
if ! Config . DisableDefaultNoavatar && uid < 5 {
return "/static/n" + strconv . Itoa ( uid ) + "-" + strconv . Itoa ( width ) + ".png?i=0"
}
2018-07-28 12:52:23 +00:00
return strings . Replace ( strings . Replace ( Config . Noavatar , "{id}" , strconv . Itoa ( uid ) , 1 ) , "{width}" , strconv . Itoa ( width ) , 1 )
}
2018-02-03 05:47:14 +00:00
// ? Make this part of *User?
2018-07-28 12:52:23 +00:00
// TODO: Write tests for this
func BuildAvatar ( uid int , avatar string ) ( normalAvatar string , microAvatar string ) {
2019-05-03 08:34:18 +00:00
if avatar == "" {
if uid == 0 {
return guestAvatar . Normal , guestAvatar . Micro
2018-02-03 05:47:14 +00:00
}
2019-05-03 08:34:18 +00:00
return buildNoavatar ( uid , 200 ) , buildNoavatar ( uid , 48 )
2018-02-03 05:47:14 +00:00
}
2019-05-03 08:34:18 +00:00
if avatar [ 0 ] == '.' {
if avatar [ 1 ] == '.' {
normalAvatar = "/uploads/avatar_" + strconv . Itoa ( uid ) + "_tmp" + avatar [ 1 : ]
return normalAvatar , normalAvatar
}
normalAvatar = "/uploads/avatar_" + strconv . Itoa ( uid ) + avatar
return normalAvatar , normalAvatar
2018-12-31 09:03:49 +00:00
}
2019-05-03 08:34:18 +00:00
return avatar , avatar
2018-02-03 05:47:14 +00:00
}
2017-11-11 04:06:16 +00:00
// TODO: Move this to *User
2017-09-22 02:21:17 +00:00
func SetPassword ( uid int , password string ) error {
hashedPassword , salt , err := GeneratePassword ( password )
if err != nil {
return err
}
2017-11-11 04:06:16 +00:00
_ , err = userStmts . setPassword . Exec ( hashedPassword , salt , uid )
2017-09-22 02:21:17 +00:00
return err
}
2017-09-25 00:48:35 +00:00
// TODO: Write units tests for this
2017-09-22 02:21:17 +00:00
func wordsToScore ( wcount int , topic bool ) ( score int ) {
if topic {
score = 2
} else {
score = 1
}
2017-11-11 04:06:16 +00:00
settings := SettingBox . Load ( ) . ( SettingMap )
2017-09-22 02:21:17 +00:00
if wcount >= settings [ "megapost_min_words" ] . ( int ) {
score += 4
} else if wcount >= settings [ "bigpost_min_words" ] . ( int ) {
score ++
}
return score
}
2017-09-28 22:16:34 +00:00
// For use in tests and to help generate dummy users for forums which don't have last posters
2017-11-11 04:06:16 +00:00
func BlankUser ( ) * User {
2018-06-06 06:13:55 +00:00
return new ( User )
2017-09-28 22:16:34 +00:00
}
2017-09-25 00:48:35 +00:00
// TODO: Write unit tests for this
2017-11-11 04:06:16 +00:00
func BuildProfileURL ( slug string , uid int ) string {
2018-04-03 04:34:07 +00:00
if slug == "" || ! Config . BuildSlugs {
2017-06-28 12:05:26 +00:00
return "/user/" + strconv . Itoa ( uid )
}
return "/user/" + slug + "." + strconv . Itoa ( uid )
2017-02-28 09:27:28 +00:00
}