2017-11-10 03:33:11 +00:00
package common
2017-06-13 07:12:58 +00:00
2017-08-06 15:22:18 +00:00
import (
2017-09-03 04:50:31 +00:00
"database/sql"
2017-08-06 15:22:18 +00:00
"errors"
"strconv"
2019-12-08 03:40:56 +00:00
qgen "github.com/Azareal/Gosora/query_gen"
2017-08-06 15:22:18 +00:00
"golang.org/x/crypto/bcrypt"
)
2017-06-13 07:12:58 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Add the watchdog goroutine
2017-09-15 22:20:01 +00:00
// TODO: Add some sort of update method
2017-11-11 04:06:16 +00:00
var Users UserStore
var ErrAccountExists = errors . New ( "this username is already in use" )
2018-03-17 08:16:43 +00:00
var ErrLongUsername = errors . New ( "this username is too long" )
2021-02-26 21:22:36 +00:00
var ErrSomeUsersNotFound = errors . New ( "Unable to find some users" )
2017-06-13 07:12:58 +00:00
type UserStore interface {
2017-11-11 04:06:16 +00:00
DirtyGet ( id int ) * User
2017-06-13 07:12:58 +00:00
Get ( id int ) ( * User , error )
2020-04-27 12:41:55 +00:00
Getn ( id int ) * User
2018-09-20 06:31:09 +00:00
GetByName ( name string ) ( * User , error )
2021-02-26 21:22:36 +00:00
BulkGetByName ( names [ ] string ) ( list [ ] * User , err error )
RawBulkGetByNameForConvo ( f func ( int , string , int , bool , int , int ) error , names [ ] string ) error
2017-09-15 22:20:01 +00:00
Exists ( id int ) bool
2021-01-19 23:47:08 +00:00
SearchOffset ( name , email string , gid , offset , perPage int ) ( users [ ] * User , err error )
2019-12-08 03:40:56 +00:00
GetOffset ( offset , perPage int ) ( [ ] * User , error )
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
Each ( f func ( * User ) error ) error
2017-09-15 22:20:01 +00:00
//BulkGet(ids []int) ([]*User, error)
BulkGetMap ( ids [ ] int ) ( map [ int ] * User , error )
2017-06-13 07:12:58 +00:00
BypassGet ( id int ) ( * User , error )
2020-02-05 02:48:35 +00:00
Create ( name , password , email string , group int , active bool ) ( int , error )
2017-11-23 05:37:08 +00:00
Reload ( id int ) error
2019-06-01 12:31:48 +00:00
Count ( ) int
2021-01-19 23:47:08 +00:00
CountSearch ( name , email string , gid int ) int
2017-09-15 22:20:01 +00:00
2017-11-23 05:37:08 +00:00
SetCache ( cache UserCache )
GetCache ( ) UserCache
2017-06-13 07:12:58 +00:00
}
2017-11-23 05:37:08 +00:00
type DefaultUserStore struct {
cache UserCache
2021-01-18 22:11:30 +00:00
get * sql . Stmt
getByName * sql . Stmt
searchOffset * sql . Stmt
getOffset * sql . Stmt
getAll * sql . Stmt
exists * sql . Stmt
register * sql . Stmt
nameExists * sql . Stmt
count * sql . Stmt
countSearch * sql . Stmt
2017-06-13 07:12:58 +00:00
}
2017-11-23 05:37:08 +00:00
// NewDefaultUserStore gives you a new instance of DefaultUserStore
func NewDefaultUserStore ( cache UserCache ) ( * DefaultUserStore , error ) {
2018-08-04 11:46:36 +00:00
acc := qgen . NewAcc ( )
2017-11-23 05:37:08 +00:00
if cache == nil {
cache = NewNullUserCache ( )
}
2019-12-08 03:40:56 +00:00
u := "users"
2020-07-14 21:50:29 +00:00
allCols := "uid,name,group,active,is_super_admin,session,email,avatar,message,level,score,posts,liked,last_ip,temp_group,createdAt,enable_embeds,profile_comments,who_can_convo"
2017-11-06 07:23:32 +00:00
// TODO: Add an admin version of registerStmt with more flexibility?
2017-11-23 05:37:08 +00:00
return & DefaultUserStore {
2021-01-18 22:11:30 +00:00
cache : cache ,
get : acc . Select ( u ) . Columns ( "name,group,active,is_super_admin,session,email,avatar,message,level,score,posts,liked,last_ip,temp_group,createdAt,enable_embeds,profile_comments,who_can_convo" ) . Where ( "uid=?" ) . Prepare ( ) ,
getByName : acc . Select ( u ) . Columns ( allCols ) . Where ( "name=?" ) . Prepare ( ) ,
2021-02-26 21:22:36 +00:00
searchOffset : acc . Select ( u ) . Columns ( allCols ) . Where ( "(name=? OR ?='') AND (email=? OR ?='') AND (group=? OR ?=0)" ) . Orderby ( "uid ASC" ) . Limit ( "?,?" ) . Prepare ( ) ,
2021-01-18 22:11:30 +00:00
getOffset : acc . Select ( u ) . Columns ( allCols ) . Orderby ( "uid ASC" ) . Limit ( "?,?" ) . Prepare ( ) ,
getAll : acc . Select ( u ) . Columns ( allCols ) . Prepare ( ) ,
2019-12-08 03:40:56 +00:00
exists : acc . Exists ( u , "uid" ) . Prepare ( ) ,
2020-07-14 21:50:29 +00:00
register : acc . Insert ( u ) . Columns ( "name,email,password,salt,group,is_super_admin,session,active,message,createdAt,lastActiveAt,lastLiked,oldestItemLikedCreatedAt" ) . Fields ( "?,?,?,?,?,0,'',?,'',UTC_TIMESTAMP(),UTC_TIMESTAMP(),UTC_TIMESTAMP(),UTC_TIMESTAMP()" ) . Prepare ( ) , // TODO: Implement user_count on users_groups here
2019-12-08 03:40:56 +00:00
nameExists : acc . Exists ( u , "name" ) . Prepare ( ) ,
2021-01-18 22:11:30 +00:00
count : acc . Count ( u ) . Prepare ( ) ,
2021-03-02 07:22:32 +00:00
countSearch : acc . Count ( u ) . Where ( "(name=? OR ?='') AND (email=? OR ?='') AND (group=? OR ?=0)" ) . Prepare ( ) ,
2017-11-06 07:23:32 +00:00
} , acc . FirstError ( )
2017-06-13 07:12:58 +00:00
}
2019-08-28 06:47:54 +00:00
func ( s * DefaultUserStore ) DirtyGet ( id int ) * User {
user , err := s . Get ( id )
2017-11-23 05:37:08 +00:00
if err == nil {
2017-11-11 04:06:16 +00:00
return user
}
2019-08-28 06:47:54 +00:00
/ * if s . OutOfBounds ( id ) {
2018-09-13 07:41:01 +00:00
return BlankUser ( )
} * /
2017-11-11 04:06:16 +00:00
return BlankUser ( )
}
2021-02-26 21:22:36 +00:00
func ( s * DefaultUserStore ) scanUser ( r * sql . Row , u * User ) ( embeds int , err error ) {
e := r . Scan ( & u . Name , & u . Group , & u . Active , & u . IsSuperAdmin , & u . Session , & u . Email , & u . RawAvatar , & u . Message , & u . Level , & u . Score , & u . Posts , & u . Liked , & u . LastIP , & u . TempGroup , & u . CreatedAt , & embeds , & u . Privacy . ShowComments , & u . Privacy . AllowMessage )
return embeds , e
}
2017-11-23 05:37:08 +00:00
// TODO: Log weird cache errors? Not just here but in every *Cache?
2019-08-28 06:47:54 +00:00
func ( s * DefaultUserStore ) Get ( id int ) ( * User , error ) {
u , err := s . cache . Get ( id )
2017-11-23 05:37:08 +00:00
if err == nil {
2018-12-06 11:54:20 +00:00
//log.Print("cached user")
//log.Print(string(debug.Stack()))
//log.Println("")
2019-08-28 06:47:54 +00:00
return u , nil
2017-06-13 07:12:58 +00:00
}
2018-12-06 11:54:20 +00:00
//log.Print("uncached user")
2017-06-13 07:12:58 +00:00
2019-08-28 06:47:54 +00:00
u = & User { ID : id , Loggedin : true }
2021-02-26 21:22:36 +00:00
embeds , err := s . scanUser ( s . get . QueryRow ( id ) , u )
2018-09-20 06:31:09 +00:00
if err == nil {
2019-12-08 03:40:56 +00:00
if embeds != - 1 {
u . ParseSettings = DefaultParseSettings . CopyPtr ( )
u . ParseSettings . NoEmbed = embeds == 0
}
2019-08-28 06:47:54 +00:00
u . Init ( )
s . cache . Set ( u )
2018-09-20 06:31:09 +00:00
}
2019-08-28 06:47:54 +00:00
return u , err
2018-09-20 06:31:09 +00:00
}
2020-04-27 12:41:55 +00:00
func ( s * DefaultUserStore ) Getn ( id int ) * User {
u := s . cache . Getn ( id )
if u != nil {
return u
}
u = & User { ID : id , Loggedin : true }
2021-02-26 21:22:36 +00:00
embeds , err := s . scanUser ( s . get . QueryRow ( id ) , u )
2020-04-27 12:41:55 +00:00
if err != nil {
return nil
}
if embeds != - 1 {
u . ParseSettings = DefaultParseSettings . CopyPtr ( )
u . ParseSettings . NoEmbed = embeds == 0
}
u . Init ( )
s . cache . Set ( u )
return u
}
2018-09-20 06:31:09 +00:00
// TODO: Log weird cache errors? Not just here but in every *Cache?
// ! This bypasses the cache, use frugally
2019-08-28 06:47:54 +00:00
func ( s * DefaultUserStore ) GetByName ( name string ) ( * User , error ) {
u := & User { Loggedin : true }
2019-12-08 03:40:56 +00:00
var embeds int
2020-07-14 21:50:29 +00:00
err := s . getByName . QueryRow ( name ) . Scan ( & u . ID , & u . Name , & u . Group , & u . Active , & u . IsSuperAdmin , & u . Session , & u . Email , & u . RawAvatar , & u . Message , & u . Level , & u . Score , & u . Posts , & u . Liked , & u . LastIP , & u . TempGroup , & u . CreatedAt , & embeds , & u . Privacy . ShowComments , & u . Privacy . AllowMessage )
2020-04-27 12:41:55 +00:00
if err != nil {
return nil , err
2017-06-13 07:12:58 +00:00
}
2020-04-27 12:41:55 +00:00
if embeds != - 1 {
u . ParseSettings = DefaultParseSettings . CopyPtr ( )
u . ParseSettings . NoEmbed = embeds == 0
}
u . Init ( )
s . cache . Set ( u )
return u , nil
2017-06-13 07:12:58 +00:00
}
2021-02-26 21:22:36 +00:00
// TODO: Optimise the query to avoid preparing it on the spot? Maybe, use knowledge of the most common IN() parameter counts?
// ! This bypasses the cache, use frugally
func ( s * DefaultUserStore ) BulkGetByName ( names [ ] string ) ( list [ ] * User , err error ) {
if len ( names ) == 0 {
return list , nil
} else if len ( names ) == 1 {
user , err := s . GetByName ( names [ 0 ] )
if err != nil {
return list , err
}
return [ ] * User { user } , nil
}
idList , q := inqbuildstr ( names )
rows , err := qgen . NewAcc ( ) . Select ( "users" ) . Columns ( "uid,name,group,active,is_super_admin,session,email,avatar,message,level,score,posts,liked,last_ip,temp_group,createdAt,enable_embeds,profile_comments,who_can_convo" ) . Where ( "name IN(" + q + ")" ) . Query ( idList ... )
if err != nil {
return list , err
}
defer rows . Close ( )
var embeds int
for rows . Next ( ) {
u := & User { Loggedin : true }
err := rows . Scan ( & u . ID , & u . Name , & u . Group , & u . Active , & u . IsSuperAdmin , & u . Session , & u . Email , & u . RawAvatar , & u . Message , & u . Level , & u . Score , & u . Posts , & u . Liked , & u . LastIP , & u . TempGroup , & u . CreatedAt , & embeds , & u . Privacy . ShowComments , & u . Privacy . AllowMessage )
if err != nil {
return list , err
}
if embeds != - 1 {
u . ParseSettings = DefaultParseSettings . CopyPtr ( )
u . ParseSettings . NoEmbed = embeds == 0
}
u . Init ( )
s . cache . Set ( u )
2021-03-02 07:22:32 +00:00
list = append ( list , u )
2021-02-26 21:22:36 +00:00
}
if err = rows . Err ( ) ; err != nil {
return list , err
}
// Did we miss any users?
if len ( names ) > len ( list ) {
return list , ErrSomeUsersNotFound
}
return list , err
}
// Special case function for efficiency
func ( s * DefaultUserStore ) RawBulkGetByNameForConvo ( f func ( int , string , int , bool , int , int ) error , names [ ] string ) error {
idList , q := inqbuildstr ( names )
rows , e := qgen . NewAcc ( ) . Select ( "users" ) . Columns ( "uid,name,group,is_super_admin,temp_group,who_can_convo" ) . Where ( "name IN(" + q + ")" ) . Query ( idList ... )
if e != nil {
return e
}
defer rows . Close ( )
for rows . Next ( ) {
var name string
var id , group , temp_group , who_can_convo int
var super_admin bool
if e = rows . Scan ( & id , & name , & group , & super_admin , & temp_group , & who_can_convo ) ; e != nil {
return e
}
if e = f ( id , name , group , super_admin , temp_group , who_can_convo ) ; e != nil {
return e
}
}
return rows . Err ( )
}
2018-05-27 09:36:35 +00:00
// TODO: Optimise this, so we don't wind up hitting the database every-time for small gaps
// TODO: Make this a little more consistent with DefaultGroupStore's GetRange method
2020-01-14 05:07:00 +00:00
func ( s * DefaultUserStore ) GetOffset ( offset , perPage int ) ( users [ ] * User , err error ) {
2019-08-28 06:47:54 +00:00
rows , err := s . getOffset . Query ( offset , perPage )
2018-05-27 09:36:35 +00:00
if err != nil {
return users , err
}
defer rows . Close ( )
2019-12-08 03:40:56 +00:00
var embeds int
2018-05-27 09:36:35 +00:00
for rows . Next ( ) {
2019-08-28 06:47:54 +00:00
u := & User { Loggedin : true }
2020-07-14 21:50:29 +00:00
err := rows . Scan ( & u . ID , & u . Name , & u . Group , & u . Active , & u . IsSuperAdmin , & u . Session , & u . Email , & u . RawAvatar , & u . Message , & u . Level , & u . Score , & u . Posts , & u . Liked , & u . LastIP , & u . TempGroup , & u . CreatedAt , & embeds , & u . Privacy . ShowComments , & u . Privacy . AllowMessage )
2018-05-27 09:36:35 +00:00
if err != nil {
return nil , err
}
2019-12-08 03:40:56 +00:00
if embeds != - 1 {
u . ParseSettings = DefaultParseSettings . CopyPtr ( )
u . ParseSettings . NoEmbed = embeds == 0
}
2019-08-28 06:47:54 +00:00
u . Init ( )
s . cache . Set ( u )
users = append ( users , u )
2018-05-27 09:36:35 +00:00
}
return users , rows . Err ( )
}
2021-01-19 23:47:08 +00:00
func ( s * DefaultUserStore ) SearchOffset ( name , email string , gid , offset , perPage int ) ( users [ ] * User , err error ) {
rows , err := s . searchOffset . Query ( name , name , email , email , gid , gid , offset , perPage )
2021-01-18 22:11:30 +00:00
if err != nil {
return users , err
}
defer rows . Close ( )
var embeds int
for rows . Next ( ) {
u := & User { Loggedin : true }
err := rows . Scan ( & u . ID , & u . Name , & u . Group , & u . Active , & u . IsSuperAdmin , & u . Session , & u . Email , & u . RawAvatar , & u . Message , & u . Level , & u . Score , & u . Posts , & u . Liked , & u . LastIP , & u . TempGroup , & u . CreatedAt , & embeds , & u . Privacy . ShowComments , & u . Privacy . AllowMessage )
if err != nil {
return nil , err
}
if embeds != - 1 {
u . ParseSettings = DefaultParseSettings . CopyPtr ( )
u . ParseSettings . NoEmbed = embeds == 0
}
u . Init ( )
s . cache . Set ( u )
users = append ( users , u )
}
return users , rows . 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 ( s * DefaultUserStore ) Each ( f func ( * User ) error ) error {
2020-07-14 21:50:29 +00:00
rows , e := s . getAll . Query ( )
if e != nil {
return 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
}
defer rows . Close ( )
var embeds int
for rows . Next ( ) {
u := new ( User )
2020-07-14 21:50:29 +00:00
if e := rows . Scan ( & u . ID , & u . Name , & u . Group , & u . Active , & u . IsSuperAdmin , & u . Session , & u . Email , & u . RawAvatar , & u . Message , & u . Level , & u . Score , & u . Posts , & u . Liked , & u . LastIP , & u . TempGroup , & u . CreatedAt , & embeds , & u . Privacy . ShowComments , & u . Privacy . AllowMessage ) ; e != nil {
return 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
}
if embeds != - 1 {
u . ParseSettings = DefaultParseSettings . CopyPtr ( )
u . ParseSettings . NoEmbed = embeds == 0
}
u . Init ( )
2020-07-14 21:50:29 +00:00
if e := f ( u ) ; e != nil {
return 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 rows . Err ( )
}
2018-05-27 09:36:35 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Optimise the query to avoid preparing it on the spot? Maybe, use knowledge of the most common IN() parameter counts?
// TODO: ID of 0 should always error?
2019-07-25 07:00:49 +00:00
func ( s * DefaultUserStore ) BulkGetMap ( ids [ ] int ) ( list map [ int ] * User , err error ) {
2019-08-28 06:47:54 +00:00
idCount := len ( ids )
2017-08-06 15:22:18 +00:00
list = make ( map [ int ] * User )
2017-09-03 04:50:31 +00:00
if idCount == 0 {
2017-08-06 15:22:18 +00:00
return list , nil
}
2017-09-03 04:50:31 +00:00
var stillHere [ ] int
2019-07-25 07:00:49 +00:00
sliceList := s . cache . BulkGet ( ids )
2018-06-06 06:13:55 +00:00
if len ( sliceList ) > 0 {
for i , sliceItem := range sliceList {
if sliceItem != nil {
list [ sliceItem . ID ] = sliceItem
} else {
stillHere = append ( stillHere , ids [ i ] )
}
2017-08-06 15:22:18 +00:00
}
2018-06-06 06:13:55 +00:00
ids = stillHere
2017-08-06 15:22:18 +00:00
}
// If every user is in the cache, then return immediately
if len ( ids ) == 0 {
return list , nil
2018-09-13 07:41:01 +00:00
} else if len ( ids ) == 1 {
2019-07-25 07:00:49 +00:00
user , err := s . Get ( ids [ 0 ] )
2018-09-13 07:41:01 +00:00
if err != nil {
return list , err
}
2019-07-25 07:00:49 +00:00
list [ user . ID ] = user
2018-09-13 07:41:01 +00:00
return list , nil
2017-08-06 15:22:18 +00:00
}
2021-02-26 21:22:36 +00:00
idList , q := inqbuild ( ids )
2020-07-14 21:50:29 +00:00
rows , err := qgen . NewAcc ( ) . Select ( "users" ) . Columns ( "uid,name,group,active,is_super_admin,session,email,avatar,message,level,score,posts,liked,last_ip,temp_group,createdAt,enable_embeds,profile_comments,who_can_convo" ) . Where ( "uid IN(" + q + ")" ) . Query ( idList ... )
2017-08-06 15:22:18 +00:00
if err != nil {
2018-02-15 13:15:27 +00:00
return list , err
2017-08-06 15:22:18 +00:00
}
2019-03-11 08:47:45 +00:00
defer rows . Close ( )
2019-12-08 03:40:56 +00:00
var embeds int
2017-08-06 15:22:18 +00:00
for rows . Next ( ) {
2019-07-25 07:00:49 +00:00
u := & User { Loggedin : true }
2020-07-14 21:50:29 +00:00
err := rows . Scan ( & u . ID , & u . Name , & u . Group , & u . Active , & u . IsSuperAdmin , & u . Session , & u . Email , & u . RawAvatar , & u . Message , & u . Level , & u . Score , & u . Posts , & u . Liked , & u . LastIP , & u . TempGroup , & u . CreatedAt , & embeds , & u . Privacy . ShowComments , & u . Privacy . AllowMessage )
2017-08-06 15:22:18 +00:00
if err != nil {
2018-02-15 13:15:27 +00:00
return list , err
2017-08-06 15:22:18 +00:00
}
2019-12-08 03:40:56 +00:00
if embeds != - 1 {
u . ParseSettings = DefaultParseSettings . CopyPtr ( )
u . ParseSettings . NoEmbed = embeds == 0
}
2019-07-25 07:00:49 +00:00
u . Init ( )
s . cache . Set ( u )
list [ u . ID ] = u
2017-08-06 15:22:18 +00:00
}
2019-12-08 03:40:56 +00:00
if err = rows . Err ( ) ; err != nil {
2019-03-11 08:47:45 +00:00
return list , err
}
2017-08-06 15:22:18 +00:00
// Did we miss any users?
2017-09-03 04:50:31 +00:00
if idCount > len ( list ) {
var sidList string
2017-08-06 15:22:18 +00:00
for _ , id := range ids {
_ , ok := list [ id ]
if ! ok {
2017-09-03 04:50:31 +00:00
sidList += strconv . Itoa ( id ) + ","
2017-08-06 15:22:18 +00:00
}
}
2018-12-06 11:54:20 +00:00
if sidList != "" {
sidList = sidList [ 0 : len ( sidList ) - 1 ]
2019-02-23 06:29:19 +00:00
err = errors . New ( "Unable to find users with the following IDs: " + sidList )
2017-08-06 15:22:18 +00:00
}
}
2017-11-11 23:34:27 +00:00
return list , err
2017-08-06 15:22:18 +00:00
}
2019-08-28 06:47:54 +00:00
func ( s * DefaultUserStore ) BypassGet ( id int ) ( * User , error ) {
u := & User { ID : id , Loggedin : true }
2021-02-26 21:22:36 +00:00
embeds , err := s . scanUser ( s . get . QueryRow ( id ) , u )
2019-10-06 22:20:37 +00:00
if err == nil {
2019-12-08 03:40:56 +00:00
if embeds != - 1 {
u . ParseSettings = DefaultParseSettings . CopyPtr ( )
u . ParseSettings . NoEmbed = embeds == 0
}
2019-10-06 22:20:37 +00:00
u . Init ( )
}
2019-08-28 06:47:54 +00:00
return u , err
2017-06-13 07:12:58 +00:00
}
2019-08-28 06:47:54 +00:00
func ( s * DefaultUserStore ) Reload ( id int ) error {
2019-10-06 22:20:37 +00:00
u , err := s . BypassGet ( id )
2017-06-13 07:12:58 +00:00
if err != nil {
2019-08-28 06:47:54 +00:00
s . cache . Remove ( id )
2017-06-13 07:12:58 +00:00
return err
}
2019-08-28 06:47:54 +00:00
_ = s . cache . Set ( u )
2018-11-19 23:06:15 +00:00
TopicListThaw . Thaw ( )
2017-06-13 07:12:58 +00:00
return nil
}
2019-08-28 06:47:54 +00:00
func ( s * DefaultUserStore ) Exists ( id int ) bool {
err := s . exists . QueryRow ( id ) . Scan ( & id )
2017-10-16 07:32:58 +00:00
if err != nil && err != ErrNoRows {
LogError ( err )
}
return err != ErrNoRows
2017-09-15 22:20:01 +00:00
}
2017-10-16 07:32:58 +00:00
// TODO: Change active to a bool?
2018-05-28 06:27:12 +00:00
// TODO: Use unique keys for the usernames
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 ( s * DefaultUserStore ) Create ( name , password , email string , group int , active bool ) ( int , error ) {
2018-03-17 08:16:43 +00:00
// TODO: Strip spaces?
// ? This number might be a little screwy with Unicode, but it's the only consistent thing we have, as Unicode characters can be any number of bytes in theory?
2019-12-08 03:40:56 +00:00
if len ( name ) > Config . MaxUsernameLength {
2018-03-17 08:16:43 +00:00
return 0 , ErrLongUsername
}
2019-12-08 03:40:56 +00:00
// Is this name already taken..?
err := s . nameExists . QueryRow ( name ) . Scan ( & name )
2017-06-28 12:05:26 +00:00
if err != ErrNoRows {
2017-11-11 04:06:16 +00:00
return 0 , ErrAccountExists
2017-06-25 09:56:39 +00:00
}
2017-11-11 04:06:16 +00:00
salt , err := GenerateSafeString ( SaltLength )
2017-06-25 09:56:39 +00:00
if err != nil {
return 0 , err
}
2017-09-03 04:50:31 +00:00
hashedPassword , err := bcrypt . GenerateFromPassword ( [ ] byte ( password + salt ) , bcrypt . DefaultCost )
2017-06-25 09:56:39 +00:00
if err != nil {
return 0 , err
}
2019-12-08 03:40:56 +00:00
res , err := s . register . Exec ( name , email , string ( hashedPassword ) , salt , group , active )
2017-06-25 09:56:39 +00:00
if err != nil {
return 0 , err
}
2017-09-03 04:50:31 +00:00
lastID , err := res . LastInsertId ( )
return int ( lastID ) , err
2017-06-25 09:56:39 +00:00
}
2019-06-01 12:31:48 +00:00
// Count returns the total number of users registered on the forums
func ( s * DefaultUserStore ) Count ( ) ( count 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
return Countf ( s . count )
2017-08-15 13:47:56 +00:00
}
2021-01-19 23:47:08 +00:00
func ( s * DefaultUserStore ) CountSearch ( name , email string , gid int ) ( count int ) {
return Countf ( s . countSearch , name , name , email , email , gid , gid )
2021-01-18 22:11:30 +00:00
}
2019-06-01 12:31:48 +00:00
func ( s * DefaultUserStore ) SetCache ( cache UserCache ) {
s . cache = cache
2017-06-13 07:12:58 +00:00
}
2017-11-23 05:37:08 +00:00
// TODO: We're temporarily doing this so that you can do ucache != nil in getTopicUser. Refactor it.
2019-06-01 12:31:48 +00:00
func ( s * DefaultUserStore ) GetCache ( ) UserCache {
_ , ok := s . cache . ( * NullUserCache )
2017-11-23 05:37:08 +00:00
if ok {
return nil
2017-08-15 13:47:56 +00:00
}
2019-06-01 12:31:48 +00:00
return s . cache
2017-10-21 00:27:47 +00:00
}