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"
2020-02-07 12:27:33 +00:00
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
//"log"
2017-06-28 12:05:26 +00:00
2019-10-06 22:20:37 +00:00
qgen "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
2020-02-09 10:00:08 +00:00
var GuestUser = User { ID : 0 , Name : "Guest" , Link : "#" , Group : 6 , Perms : GuestPerms , CreatedAt : StartTime } // 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
2019-12-08 03:40:56 +00:00
// TODO: Implement something like this for profiles?
//URLPrefix string // Move this to another table? Create a user lite?
//URLName string
Tag string
Level int
Score int
Posts int
Liked int
2020-02-09 10:00:08 +00:00
CreatedAt time . Time
2019-12-08 03:40:56 +00:00
LastIP string // ! This part of the UserCache data might fall out of date
2020-03-08 04:15:27 +00:00
LastAgent int // ! Temporary hack for http push, don't use
2019-12-08 03:40:56 +00:00
TempGroup int
ParseSettings * ParseSettings
2020-07-14 21:50:29 +00:00
Privacy UserPrivacy
}
type UserPrivacy struct {
2020-07-15 06:59:47 +00:00
ShowComments int // 0 = default, 1 = public, 2 = registered, 3 = friends, 4 = self, 5 = disabled / unused
AllowMessage int // 0 = default, 1 = registered, 2 = friends, 3 = mods, 4 = disabled / unused
2016-12-02 07:38:54 +00:00
}
2019-08-31 22:59:00 +00:00
func ( u * User ) WebSockets ( ) * WsJSONUser {
groupID := u . Group
if u . TempGroup != 0 {
groupID = u . TempGroup
2018-06-24 13:49:29 +00:00
}
// 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-08-31 22:59:00 +00:00
return & WsJSONUser { u . ID , u . Link , u . Name , groupID , u . IsMod , u . Avatar , u . MicroAvatar , u . Level , u . Score , u . 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
}
2019-08-31 22:59:00 +00:00
func ( u * User ) Me ( ) * MeUser {
groupID := u . Group
if u . TempGroup != 0 {
groupID = u . TempGroup
2018-08-11 15:53:42 +00:00
}
2019-08-31 22:59:00 +00:00
return & MeUser { u . ID , u . Link , u . Name , groupID , u . Active , u . IsMod , u . IsSuperMod , u . IsAdmin , u . IsBanned , u . Session , u . Avatar , u . MicroAvatar , u . Tag , u . Level , u . Score , u . Liked }
2018-08-11 15:53:42 +00:00
}
// 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
2019-10-06 22:20:37 +00:00
S string // Session
2018-08-11 15:53:42 +00:00
Avatar string
MicroAvatar string
Tag string
Level int
Score int
Liked int
}
2017-11-11 04:06:16 +00:00
type UserStmts struct {
2019-10-06 22:20:37 +00:00
activate * sql . Stmt
changeGroup * sql . Stmt
delete * sql . Stmt
setAvatar * sql . Stmt
2019-12-08 03:40:56 +00:00
setName * sql . Stmt
2019-10-06 22:20:37 +00:00
update * sql . Stmt
2018-05-28 06:27:12 +00:00
// TODO: Split these into a sub-struct
2020-01-14 05:07:00 +00:00
incScore * sql . Stmt
incPosts * sql . Stmt
incBigposts * sql . Stmt
incMegaposts * sql . Stmt
incPostStats * sql . Stmt
incBigpostStats * sql . Stmt
incMegapostStats * sql . Stmt
incLiked * sql . Stmt
incTopics * sql . Stmt
updateLevel * sql . Stmt
resetStats * sql . Stmt
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
setStats * sql . Stmt
2018-05-28 06:27:12 +00:00
2019-12-08 03:40:56 +00:00
decLiked * sql . Stmt
updateLastIP * sql . Stmt
updatePrivacy * sql . Stmt
2017-11-11 04:06:16 +00:00
setPassword * sql . Stmt
2018-07-28 12:52:23 +00:00
scheduleAvatarResize * sql . Stmt
2020-01-14 05:07:00 +00:00
2020-01-18 07:24:51 +00:00
deletePosts * sql . Stmt
deleteProfilePosts * sql . Stmt
deleteReplyPosts * sql . Stmt
getLikedRepliesOfTopic * sql . Stmt
2020-01-23 06:17:50 +00:00
getAttachmentsOfTopic * sql . Stmt
getAttachmentsOfTopic2 * sql . Stmt
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
getRepliesOfTopic * 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-12-08 03:40:56 +00:00
u := "users"
w := "uid=?"
2017-11-11 04:06:16 +00:00
userStmts = UserStmts {
2019-12-08 03:40:56 +00:00
activate : acc . SimpleUpdate ( u , "active=1" , w ) ,
changeGroup : acc . SimpleUpdate ( u , "group=?" , w ) , // TODO: Implement user_count for users_groups here
delete : acc . Delete ( u ) . Where ( w ) . Prepare ( ) ,
setAvatar : acc . Update ( u ) . Set ( "avatar=?" ) . Where ( w ) . Prepare ( ) ,
setName : acc . Update ( u ) . Set ( "name=?" ) . Where ( w ) . Prepare ( ) ,
update : acc . Update ( u ) . Set ( "name=?,email=?,group=?" ) . Where ( w ) . Prepare ( ) , // TODO: Implement user_count for users_groups on things which use this
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
// Stat Statements
// TODO: Do +0 to avoid having as many statements?
2020-01-14 05:07:00 +00:00
incScore : acc . Update ( u ) . Set ( "score=score+?" ) . Where ( w ) . Prepare ( ) ,
incPosts : acc . Update ( u ) . Set ( "posts=posts+?" ) . Where ( w ) . Prepare ( ) ,
incBigposts : acc . Update ( u ) . Set ( "posts=posts+?,bigposts=bigposts+?" ) . Where ( w ) . Prepare ( ) ,
incMegaposts : acc . Update ( u ) . Set ( "posts=posts+?,bigposts=bigposts+?,megaposts=megaposts+?" ) . Where ( w ) . Prepare ( ) ,
incPostStats : acc . Update ( u ) . Set ( "posts=posts+?,score=score+?,level=?" ) . Where ( w ) . Prepare ( ) ,
incBigpostStats : acc . Update ( u ) . Set ( "posts=posts+?,bigposts=bigposts+?,score=score+?,level=?" ) . Where ( w ) . Prepare ( ) ,
incMegapostStats : acc . Update ( u ) . Set ( "posts=posts+?,bigposts=bigposts+?,megaposts=megaposts+?,score=score+?,level=?" ) . Where ( w ) . Prepare ( ) ,
incTopics : acc . SimpleUpdate ( u , "topics=topics+?" , w ) ,
updateLevel : acc . SimpleUpdate ( u , "level=?" , w ) ,
resetStats : acc . Update ( u ) . Set ( "score=0,posts=0,bigposts=0,megaposts=0,topics=0,level=0" ) . Where ( w ) . Prepare ( ) ,
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
setStats : acc . Update ( u ) . Set ( "score=?,posts=?,bigposts=?,megaposts=?,topics=?,level=?" ) . Where ( w ) . Prepare ( ) ,
2020-01-14 05:07:00 +00:00
incLiked : acc . Update ( u ) . Set ( "liked=liked+?,lastLiked=UTC_TIMESTAMP()" ) . Where ( w ) . Prepare ( ) ,
decLiked : acc . Update ( u ) . Set ( "liked=liked-?" ) . Where ( w ) . Prepare ( ) ,
2018-03-31 05:25:27 +00:00
//recalcLastLiked: acc...
2019-12-08 03:40:56 +00:00
updateLastIP : acc . SimpleUpdate ( u , "last_ip=?" , w ) ,
2020-07-14 21:50:29 +00:00
updatePrivacy : acc . Update ( u ) . Set ( "profile_comments=?,enable_embeds=?" ) . Where ( w ) . Prepare ( ) ,
2017-11-11 04:06:16 +00:00
2019-12-08 03:40:56 +00:00
setPassword : acc . Update ( u ) . Set ( "password=?,salt=?" ) . Where ( w ) . Prepare ( ) ,
2018-07-28 12:52:23 +00:00
scheduleAvatarResize : acc . Insert ( "users_avatar_queue" ) . Columns ( "uid" ) . Fields ( "?" ) . Prepare ( ) ,
2020-01-14 05:07:00 +00:00
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
// Delete All Posts Statements
deletePosts : acc . Select ( "topics" ) . Columns ( "tid,parentID,postCount,poll" ) . Where ( "createdBy=?" ) . Prepare ( ) ,
deleteProfilePosts : acc . Select ( "users_replies" ) . Columns ( "rid,uid" ) . Where ( "createdBy=?" ) . Prepare ( ) ,
2020-01-18 07:24:51 +00:00
deleteReplyPosts : acc . Select ( "replies" ) . Columns ( "rid,tid" ) . Where ( "createdBy=?" ) . Prepare ( ) ,
getLikedRepliesOfTopic : acc . Select ( "replies" ) . Columns ( "rid" ) . Where ( "tid=? AND likeCount>0" ) . Prepare ( ) ,
2020-01-23 06:17:50 +00:00
getAttachmentsOfTopic : acc . Select ( "attachments" ) . Columns ( "attachID" ) . Where ( "originID=? AND originTable='topics'" ) . Prepare ( ) ,
getAttachmentsOfTopic2 : acc . Select ( "attachments" ) . Columns ( "attachID" ) . Where ( "extra=? AND originTable='replies'" ) . Prepare ( ) ,
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
getRepliesOfTopic : acc . Select ( "replies" ) . Columns ( "words" ) . Where ( "createdBy!=? AND tid=?" ) . Prepare ( ) ,
2017-11-11 04:06:16 +00:00
}
return acc . FirstError ( )
} )
2017-01-03 07:47:31 +00:00
}
2019-08-31 22:59:00 +00:00
func ( u * User ) Init ( ) {
2020-07-14 21:50:29 +00:00
// TODO: Let admins configure the minimum default?
if u . Privacy . ShowComments < 1 {
u . Privacy . ShowComments = 1
}
2019-08-31 22:59:00 +00:00
u . Avatar , u . MicroAvatar = BuildAvatar ( u . ID , u . RawAvatar )
u . Link = BuildProfileURL ( NameToSlug ( u . Name ) , u . ID )
u . Tag = Groups . DirtyGet ( u . Group ) . Tag
u . InitPerms ( )
2017-11-11 04:06:16 +00:00
}
2017-11-23 05:37:08 +00:00
// TODO: Refactor this idiom into something shorter, maybe with a NullUserCache when one isn't set?
2019-08-31 22:59:00 +00:00
func ( u * User ) CacheRemove ( ) {
2019-10-19 10:33:59 +00:00
if uc := Users . GetCache ( ) ; uc != nil {
uc . Remove ( u . 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
}
2020-11-09 06:12:08 +00:00
func ( u * User ) Ban ( dur time . Duration , issuedBy int ) error {
return u . ScheduleGroupUpdate ( BanGroup , issuedBy , dur )
2017-08-27 09:33:45 +00:00
}
2019-08-31 22:59:00 +00:00
func ( u * User ) Unban ( ) error {
return u . RevertGroupUpdate ( )
2017-10-21 00:27:47 +00:00
}
2019-08-31 22:59:00 +00:00
func ( u * User ) deleteScheduleGroupTx ( tx * sql . Tx ) error {
2020-11-09 06:12:08 +00:00
deleteScheduleGroupStmt , e := qgen . Builder . SimpleDeleteTx ( tx , "users_groups_scheduler" , "uid=?" )
if e != nil {
return e
2017-10-21 00:27:47 +00:00
}
2020-11-09 06:12:08 +00:00
_ , e = deleteScheduleGroupStmt . Exec ( u . ID )
return e
2017-10-21 00:27:47 +00:00
}
2019-08-31 22:59:00 +00:00
func ( u * User ) setTempGroupTx ( tx * sql . Tx , tempGroup int ) error {
2020-11-09 06:12:08 +00:00
setTempGroupStmt , e := qgen . Builder . SimpleUpdateTx ( tx , "users" , "temp_group=?" , "uid=?" )
if e != nil {
return e
2017-08-27 09:33:45 +00:00
}
2020-11-09 06:12:08 +00:00
_ , e = setTempGroupStmt . Exec ( tempGroup , u . ID )
return e
2017-08-27 09:33:45 +00:00
}
// Make this more stateless?
2020-11-09 06:12:08 +00:00
func ( u * User ) ScheduleGroupUpdate ( gid , issuedBy int , dur time . Duration ) error {
2019-08-31 22:59:00 +00:00
var temp bool
2020-11-09 06:12:08 +00:00
if dur . Nanoseconds ( ) != 0 {
2019-08-31 22:59:00 +00:00
temp = true
2017-08-27 09:33:45 +00:00
}
2020-11-09 06:12:08 +00:00
revertAt := time . Now ( ) . Add ( dur )
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 ( )
2019-08-31 22:59:00 +00:00
err = u . deleteScheduleGroupTx ( tx )
2017-10-21 00:27:47 +00:00
if err != nil {
return err
}
2019-08-31 22:59:00 +00:00
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
}
2019-08-31 22:59:00 +00:00
_ , err = createScheduleGroupTx . Exec ( u . ID , gid , issuedBy , revertAt , temp )
2017-10-21 00:27:47 +00:00
if err != nil {
return err
}
2019-08-31 22:59:00 +00:00
err = u . setTempGroupTx ( tx , gid )
2017-10-21 00:27:47 +00:00
if err != nil {
return err
}
err = tx . Commit ( )
2019-08-31 22:59:00 +00:00
u . CacheRemove ( )
2017-09-22 02:21:17 +00:00
return err
2017-08-27 09:33:45 +00:00
}
2019-08-31 22:59:00 +00:00
func ( u * User ) RevertGroupUpdate ( ) error {
2020-11-09 06:12:08 +00:00
tx , e := qgen . Builder . Begin ( )
if e != nil {
return e
2017-10-21 00:27:47 +00:00
}
defer tx . Rollback ( )
2020-11-09 06:12:08 +00:00
e = u . deleteScheduleGroupTx ( tx )
if e != nil {
return e
2017-08-27 09:33:45 +00:00
}
2017-10-21 00:27:47 +00:00
2020-11-09 06:12:08 +00:00
e = u . setTempGroupTx ( tx , 0 )
if e != nil {
return e
2017-10-21 00:27:47 +00:00
}
2020-11-09 06:12:08 +00:00
e = tx . Commit ( )
2017-10-21 00:27:47 +00:00
2019-08-31 22:59:00 +00:00
u . CacheRemove ( )
2020-11-09 06:12:08 +00:00
return e
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?
2020-11-09 06:12:08 +00:00
func ( u * User ) Activate ( ) ( e error ) {
_ , e = userStmts . activate . Exec ( u . ID )
if e != nil {
return e
2016-12-02 07:38:54 +00:00
}
2020-11-09 06:12:08 +00:00
_ , e = userStmts . changeGroup . Exec ( Config . DefaultGroup , u . ID )
2019-08-31 22:59:00 +00:00
u . CacheRemove ( )
2020-11-09 06:12:08 +00:00
return e
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?
2019-08-31 22:59:00 +00:00
func ( u * User ) Delete ( ) error {
2020-11-09 06:12:08 +00:00
_ , e := userStmts . delete . Exec ( u . ID )
if e != nil {
return e
2017-10-16 07:32:58 +00:00
}
2019-08-31 22:59:00 +00:00
u . CacheRemove ( )
return nil
2017-10-16 07:32:58 +00:00
}
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
// TODO: dismiss-event
2020-01-14 05:07:00 +00:00
func ( u * User ) DeletePosts ( ) error {
rows , err := userStmts . deletePosts . Query ( u . ID )
if err != nil {
return err
}
defer rows . Close ( )
defer TopicListThaw . Thaw ( )
defer u . CacheRemove ( )
updatedForums := make ( map [ int ] int ) // forum[count]
tc := Topics . GetCache ( )
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
umap := make ( map [ int ] struct { } )
2020-01-14 05:07:00 +00:00
for rows . Next ( ) {
2020-02-07 12:27:33 +00:00
var tid , parentID , postCount , poll int
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
err := rows . Scan ( & tid , & parentID , & postCount , & poll )
2020-01-14 05:07:00 +00:00
if err != nil {
return err
}
// TODO: Clear reply cache too
_ , err = topicStmts . delete . Exec ( tid )
if tc != nil {
tc . Remove ( tid )
}
if err != nil {
return err
}
updatedForums [ parentID ] = updatedForums [ parentID ] + 1
2020-01-15 04:16:10 +00:00
_ , err = topicStmts . deleteLikesForTopic . Exec ( tid )
if err != nil {
return err
}
2020-01-23 06:17:50 +00:00
err = handleTopicAttachments ( tid )
if err != nil {
return err
}
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
if postCount > 1 {
err = handleLikedTopicReplies ( tid )
if err != nil {
return err
}
err = handleTopicReplies ( umap , u . ID , tid )
if err != nil {
return err
}
_ , err = topicStmts . deleteReplies . Exec ( tid )
if err != nil {
return err
}
}
2020-02-07 12:27:33 +00:00
err = Subscriptions . DeleteResource ( tid , "topic" )
2020-01-14 05:07:00 +00:00
if err != nil {
return err
}
_ , err = topicStmts . deleteActivity . Exec ( tid )
if err != nil {
return err
}
2020-01-20 06:58:22 +00:00
if poll > 0 {
2020-01-23 06:17:50 +00:00
err = ( & Poll { ID : poll } ) . Delete ( )
2020-01-20 06:58:22 +00:00
if err != nil {
return err
}
}
2020-01-14 05:07:00 +00:00
}
if err = rows . Err ( ) ; err != nil {
return err
}
err = u . ResetPostStats ( )
if err != nil {
return err
}
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
for uid , _ := range umap {
err = ( & User { ID : uid } ) . RecalcPostStats ( )
if err != nil {
return err
}
}
2020-01-14 05:07:00 +00:00
for fid , count := range updatedForums {
err := Forums . RemoveTopics ( fid , count )
if err != nil && err != ErrNoRows {
return err
}
}
rows , err = userStmts . deleteProfilePosts . Query ( u . ID )
if err != nil {
return err
}
defer rows . Close ( )
for rows . Next ( ) {
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
var rid , uid int
2020-02-07 12:27:33 +00:00
err := rows . Scan ( & rid , & uid )
2020-01-14 05:07:00 +00:00
if err != nil {
return err
}
_ , err = profileReplyStmts . delete . Exec ( rid )
if err != nil {
return err
}
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
// TODO: Optimise this
// TODO: dismiss-event
2020-02-07 12:27:33 +00:00
err = Activity . DeleteByParamsExtra ( "reply" , uid , "user" , strconv . Itoa ( rid ) )
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
if err != nil {
return err
}
2020-01-14 05:07:00 +00:00
}
2020-01-18 07:24:51 +00:00
if err = rows . Err ( ) ; err != nil {
2020-01-14 05:07:00 +00:00
return err
}
rows , err = userStmts . deleteReplyPosts . Query ( u . ID )
if err != nil {
return err
}
defer rows . Close ( )
rc := Rstore . GetCache ( )
for rows . Next ( ) {
var rid , tid int
2020-01-15 04:16:10 +00:00
err := rows . Scan ( & rid , & tid )
2020-01-14 05:07:00 +00:00
if err != nil {
return err
}
_ , err = replyStmts . delete . Exec ( rid )
if err != nil {
return err
}
// TODO: Move this bit to *Topic
_ , err = replyStmts . removeRepliesFromTopic . Exec ( 1 , tid )
2020-01-14 10:38:05 +00:00
if err != nil {
return err
}
_ , err = replyStmts . updateTopicReplies . Exec ( tid )
if err != nil {
return err
}
_ , err = replyStmts . updateTopicReplies2 . Exec ( tid )
2020-01-14 05:07:00 +00:00
if tc != nil {
tc . Remove ( tid )
}
_ = rc . Remove ( rid )
2020-01-14 10:38:05 +00:00
if err != nil {
return err
}
2020-01-15 04:16:10 +00:00
_ , err = replyStmts . deleteLikesForReply . Exec ( rid )
if err != nil {
return err
}
2020-02-07 12:27:33 +00:00
err = Activity . DeleteByParamsExtra ( "reply" , tid , "topic" , strconv . Itoa ( rid ) )
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
if err != nil {
return err
}
2020-01-15 04:16:10 +00:00
_ , err = replyStmts . deleteActivitySubs . Exec ( rid )
if err != nil {
return err
}
_ , err = replyStmts . deleteActivity . Exec ( rid )
if err != nil {
return err
}
// TODO: Restructure alerts so we can delete the "x replied to topic" ones too.
2020-01-14 05:07:00 +00:00
}
return rows . Err ( )
}
2020-11-09 06:12:08 +00:00
func ( u * User ) bindStmt ( stmt * sql . Stmt , params ... interface { } ) ( e error ) {
2019-08-31 22:59:00 +00:00
params = append ( params , u . ID )
2020-11-09 06:12:08 +00:00
_ , e = stmt . Exec ( params ... )
2019-08-31 22:59:00 +00:00
u . CacheRemove ( )
2020-11-09 06:12:08 +00:00
return e
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
}
2020-11-09 06:12:08 +00:00
func ( u * User ) ChangeName ( name string ) error {
2019-12-08 03:40:56 +00:00
return u . bindStmt ( userStmts . setName , name )
2018-05-11 05:41:51 +00:00
}
2020-11-09 06:12:08 +00:00
func ( u * User ) ChangeAvatar ( avatar string ) error {
2019-08-31 22:59:00 +00:00
return u . 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
2019-08-31 22:59:00 +00:00
func ( u * User ) ScheduleAvatarResize ( ) ( err error ) {
_ , err = userStmts . scheduleAvatarResize . Exec ( u . ID )
2018-07-28 12:52:23 +00:00
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
}
2020-11-09 06:12:08 +00:00
func ( u * User ) ChangeGroup ( group int ) error {
2019-08-31 22:59:00 +00:00
return u . bindStmt ( userStmts . changeGroup , group )
2017-11-11 04:06:16 +00:00
}
2019-12-31 21:57:54 +00:00
func ( u * User ) GetIP ( ) string {
2020-01-14 05:07:00 +00:00
spl := strings . Split ( u . LastIP , "-" )
2019-12-31 21:57:54 +00:00
return spl [ len ( spl ) - 1 ]
}
2017-11-11 04:06:16 +00:00
// ! Only updates the database not the *User for safety reasons
2019-12-31 21:57:54 +00:00
func ( u * User ) UpdateIP ( ip string ) error {
_ , err := userStmts . updateLastIP . Exec ( ip , u . ID )
2019-10-19 10:33:59 +00:00
if uc := Users . GetCache ( ) ; uc != nil {
uc . Remove ( u . ID )
2018-11-29 07:27:17 +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
return err
}
2020-07-14 21:50:29 +00:00
//var ErrMalformedInteger = errors.New("malformed integer")
var ErrProfileCommentsOutOfBounds = errors . New ( "profile_comments must be an integer between -1 and 4" )
var ErrEnableEmbedsOutOfBounds = errors . New ( "enable_embeds must be -1, 0 or 1" )
/ * func ( u * User ) UpdatePrivacyS ( sProfileComments , sEnableEmbeds string ) error {
return u . UpdatePrivacy ( profileComments , enableEmbeds )
} * /
func ( u * User ) UpdatePrivacy ( profileComments , enableEmbeds int ) error {
if profileComments < - 1 || profileComments > 4 {
return ErrProfileCommentsOutOfBounds
}
if enableEmbeds < - 1 || enableEmbeds > 1 {
return ErrEnableEmbedsOutOfBounds
}
_ , e := userStmts . updatePrivacy . Exec ( profileComments , enableEmbeds , u . ID )
2019-12-08 03:40:56 +00:00
if uc := Users . GetCache ( ) ; uc != nil {
uc . Remove ( u . ID )
}
2020-07-14 21:50:29 +00:00
return e
2019-12-08 03:40:56 +00:00
}
func ( u * User ) Update ( name , email string , group int ) ( err error ) {
2019-10-06 22:20:37 +00:00
return u . bindStmt ( userStmts . update , name , email , group )
2018-06-17 07:28:18 +00:00
}
2019-08-31 22:59:00 +00:00
func ( u * User ) IncreasePostStats ( wcount int , topic bool ) ( err error ) {
2017-09-03 04:50:31 +00:00
baseScore := 1
2017-01-12 02:55:08 +00:00
if topic {
2019-10-06 22:20:37 +00:00
_ , err = userStmts . incTopics . Exec ( 1 , u . 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 )
2020-01-14 05:07:00 +00:00
var mod , level int
2017-06-10 07:58:15 +00:00
if wcount >= settings [ "megapost_min_words" ] . ( int ) {
2017-01-12 02:55:08 +00:00
mod = 4
2020-01-14 05:07:00 +00:00
level = GetLevel ( u . Score + baseScore + mod )
_ , err = userStmts . incMegapostStats . Exec ( 1 , 1 , 1 , baseScore + mod , level , u . ID )
2017-06-10 07:58:15 +00:00
} else if wcount >= settings [ "bigpost_min_words" ] . ( int ) {
2017-01-12 02:55:08 +00:00
mod = 1
2020-01-14 05:07:00 +00:00
level = GetLevel ( u . Score + baseScore + mod )
_ , err = userStmts . incBigpostStats . Exec ( 1 , 1 , baseScore + mod , level , u . ID )
2017-01-12 02:55:08 +00:00
} else {
2020-01-14 05:07:00 +00:00
level = GetLevel ( u . Score + baseScore + mod )
_ , err = userStmts . incPostStats . Exec ( 1 , baseScore + mod , level , u . ID )
2017-01-12 02:55:08 +00:00
}
2019-09-29 04:56:39 +00:00
if err != nil {
return err
}
2020-02-09 10:00:08 +00:00
err = GroupPromotions . PromoteIfEligible ( u , level , u . Posts + 1 , u . CreatedAt )
2019-09-29 04:56:39 +00:00
u . CacheRemove ( )
2017-01-12 02:55:08 +00:00
return err
}
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
func ( u * User ) countf ( stmt * sql . Stmt ) ( count int ) {
2020-11-09 06:12:08 +00:00
e := stmt . QueryRow ( ) . Scan ( & count )
if e != nil {
LogError ( e )
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
}
return count
}
func ( u * User ) RecalcPostStats ( ) error {
var score int
tcount := Topics . CountUser ( u . ID )
rcount := Rstore . CountUser ( u . ID )
//log.Print("tcount:", tcount)
//log.Print("rcount:", rcount)
score += tcount * 2
score += rcount
var tmega , tbig , rmega , rbig int
if tcount > 0 {
tmega = Topics . CountMegaUser ( u . ID )
score += tmega * 3
tbig := Topics . CountBigUser ( u . ID )
score += tbig
}
if rcount > 0 {
rmega = Rstore . CountMegaUser ( u . ID )
score += rmega * 3
rbig = Rstore . CountBigUser ( u . ID )
score += rbig
}
_ , err := userStmts . setStats . Exec ( score , tcount + rcount , tbig + rbig , tmega + rmega , tcount , GetLevel ( score ) , u . ID )
u . CacheRemove ( )
return err
}
2019-08-31 22:59:00 +00:00
func ( u * User ) DecreasePostStats ( wcount int , topic bool ) ( err error ) {
2017-09-03 04:50:31 +00:00
baseScore := - 1
2017-01-12 02:55:08 +00:00
if topic {
2019-10-06 22:20:37 +00:00
_ , err = userStmts . incTopics . Exec ( - 1 , u . 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
2020-01-14 05:07:00 +00:00
// TODO: Use a transaction to prevent level desyncs?
var mod int
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-01-12 02:55:08 +00:00
mod = 4
2020-01-14 05:07:00 +00:00
_ , err = userStmts . incMegapostStats . Exec ( - 1 , - 1 , - 1 , baseScore - mod , GetLevel ( u . Score - baseScore - mod ) , u . ID )
2017-06-12 09:03:14 +00:00
} else if wcount >= settings [ "bigpost_min_words" ] . ( int ) {
2017-01-12 02:55:08 +00:00
mod = 1
2020-01-14 05:07:00 +00:00
_ , err = userStmts . incBigpostStats . Exec ( - 1 , - 1 , baseScore - mod , GetLevel ( u . Score - baseScore - mod ) , u . ID )
2017-01-12 02:55:08 +00:00
} else {
2020-01-14 05:07:00 +00:00
_ , err = userStmts . incPostStats . Exec ( - 1 , baseScore - mod , GetLevel ( u . Score - baseScore - mod ) , u . ID )
2017-01-12 02:55:08 +00:00
}
2020-01-14 05:07:00 +00:00
u . CacheRemove ( )
return err
}
2017-11-05 01:04:57 +00:00
2020-11-09 06:12:08 +00:00
func ( u * User ) ResetPostStats ( ) error {
_ , err := userStmts . resetStats . Exec ( u . ID )
2019-09-29 04:56:39 +00:00
u . CacheRemove ( )
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
2019-08-31 22:59:00 +00:00
func ( u * User ) Copy ( ) User {
return * u
2017-09-28 22:16:34 +00:00
}
2017-09-25 00:48:35 +00:00
// TODO: Write unit tests for this
2019-08-31 22:59:00 +00:00
func ( u * User ) InitPerms ( ) {
if u . TempGroup != 0 {
u . Group = u . TempGroup
2017-09-15 22:20:01 +00:00
}
2019-08-31 22:59:00 +00:00
group := Groups . DirtyGet ( u . Group )
if u . IsSuperAdmin {
u . Perms = AllPerms
u . PluginPerms = AllPluginPerms
2017-08-06 15:22:18 +00:00
} else {
2019-08-31 22:59:00 +00:00
u . Perms = group . Perms
u . 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
2019-08-31 22:59:00 +00:00
u . IsAdmin = u . IsSuperAdmin || group . IsAdmin
u . IsSuperMod = u . IsAdmin || group . IsMod
u . IsMod = u . IsSuperMod
u . IsBanned = group . IsBanned
if u . IsBanned && u . IsSuperMod {
u . IsBanned = false
2017-02-15 10:49:30 +00:00
}
2017-02-28 09:27:28 +00:00
}
2020-07-15 06:59:47 +00:00
// TODO: Write tests
// TODO: Implement and use this
// TODO: Implement friends
func PrivacyAllowMessage ( pu , u * User ) ( canMsg bool ) {
switch pu . Privacy . AllowMessage {
case 4 : // Unused
canMsg = false
case 3 : // mods
canMsg = u . IsSuperMod
//case 2: // friends
case 1 : // registered
canMsg = true
default : // 0
canMsg = true
}
return canMsg
}
// TODO: Implement friend system
func PrivacyCommentsShow ( pu , u * User ) ( showComments bool ) {
switch pu . Privacy . ShowComments {
case 5 : // Unused
showComments = false
case 4 : // Self
showComments = u . ID == pu . ID
case 3 : // friends
showComments = u . ID == pu . ID
case 2 : // registered
showComments = u . Loggedin
case 1 : // public
showComments = true
default : // 0
showComments = true
}
return showComments
}
2018-12-31 09:03:49 +00:00
var guestAvatar GuestAvatar
type GuestAvatar struct {
Normal string
Micro string
}
2019-12-08 03:40:56 +00:00
func buildNoavatar ( uid , 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
}
}
2020-11-09 06:18:03 +00:00
if ! Config . DisableDefaultNoavatar && uid < 11 {
2020-02-07 12:27:33 +00:00
if width == 200 {
return noavatarCache200 [ uid ]
} else if width == 48 {
return noavatarCache48 [ uid ]
}
2020-07-30 23:40:14 +00:00
return StaticFiles . Prefix + "n" + strconv . Itoa ( uid ) + "-" + strconv . Itoa ( width ) + ".png?i=0"
2019-05-03 08:34:18 +00:00
}
2020-07-31 12:45:20 +00:00
// ? - Add a prefix setting to make this faster?
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 )
}
2020-07-31 12:45:20 +00:00
// ? - Make this part of *User?
2018-07-28 12:52:23 +00:00
// TODO: Write tests for this
2020-02-07 12:27:33 +00:00
func BuildAvatar ( uid int , avatar string ) ( normalAvatar , 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 ] == '.' {
2020-11-09 06:12:08 +00:00
normalAvatar = Config . AvatarResBase + "avatar_" + strconv . Itoa ( uid ) + "_tmp" + avatar [ 1 : ]
2019-05-03 08:34:18 +00:00
return normalAvatar , normalAvatar
}
2020-11-09 06:12:08 +00:00
normalAvatar = Config . AvatarResBase + "avatar_" + strconv . Itoa ( uid ) + avatar
2019-05-03 08:34:18 +00:00
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
}
2020-07-14 21:50:29 +00:00
func BuildProfileURLSb ( sb * strings . Builder , slug string , uid int ) {
if slug == "" || ! Config . BuildSlugs {
sb . Grow ( 6 + 1 )
sb . WriteString ( "/user/" )
sb . WriteString ( strconv . Itoa ( uid ) )
return
}
sb . Grow ( 7 + 1 + len ( slug ) )
sb . WriteString ( "/user/" )
sb . WriteString ( slug )
sb . WriteRune ( '.' )
sb . WriteString ( strconv . Itoa ( uid ) )
}