2019-09-29 04:56:39 +00:00
package common
import (
2022-02-21 03:53:13 +00:00
"database/sql"
//"log"
"time"
2019-09-29 04:56:39 +00:00
2022-02-21 03:53:13 +00:00
qgen "git.tuxpa.in/a/gosora/query_gen"
2019-09-29 04:56:39 +00:00
)
2019-10-06 22:20:37 +00:00
var GroupPromotions GroupPromotionStore
2019-09-29 04:56:39 +00:00
type GroupPromotion struct {
2022-02-21 03:32:53 +00:00
ID int
From int
To int
TwoWay bool
Level int
Posts int
MinTime int
RegisteredFor int
2019-09-29 04:56:39 +00:00
}
type GroupPromotionStore interface {
2022-02-21 03:32:53 +00:00
GetByGroup ( gid int ) ( gps [ ] * GroupPromotion , err error )
Get ( id int ) ( * GroupPromotion , error )
PromoteIfEligible ( u * User , level , posts int , registeredAt time . Time ) error
Delete ( id int ) error
Create ( from , to int , twoWay bool , level , posts , registeredFor int ) ( int , error )
2019-09-29 04:56:39 +00:00
}
type DefaultGroupPromotionStore struct {
2022-02-21 03:32:53 +00:00
getByGroup * sql . Stmt
get * sql . Stmt
delete * sql . Stmt
create * sql . Stmt
getByUser * sql . Stmt
getByUserMins * sql . Stmt
updateUser * sql . Stmt
updateGeneric * sql . Stmt
2019-09-29 04:56:39 +00:00
}
func NewDefaultGroupPromotionStore ( acc * qgen . Accumulator ) ( * DefaultGroupPromotionStore , error ) {
2022-02-21 03:32:53 +00:00
ugp := "users_groups_promotions"
prs := & DefaultGroupPromotionStore {
getByGroup : acc . Select ( ugp ) . Columns ( "pid, from_gid, to_gid, two_way, level, posts, minTime, registeredFor" ) . Where ( "from_gid=? OR to_gid=?" ) . Prepare ( ) ,
get : acc . Select ( ugp ) . Columns ( "from_gid, to_gid, two_way, level, posts, minTime, registeredFor" ) . Where ( "pid=?" ) . Prepare ( ) ,
delete : acc . Delete ( ugp ) . Where ( "pid=?" ) . Prepare ( ) ,
create : acc . Insert ( ugp ) . Columns ( "from_gid, to_gid, two_way, level, posts, minTime, registeredFor" ) . Fields ( "?,?,?,?,?,?,?" ) . Prepare ( ) ,
getByUserMins : acc . Select ( ugp ) . Columns ( "pid, to_gid, two_way, level, posts, minTime, registeredFor" ) . Where ( "from_gid=? AND level<=? AND posts<=? AND registeredFor<=?" ) . Orderby ( "level DESC" ) . Limit ( "1" ) . Prepare ( ) ,
getByUser : acc . Select ( ugp ) . Columns ( "pid, to_gid, two_way, level, posts, minTime, registeredFor" ) . Where ( "from_gid=? AND level<=? AND posts<=?" ) . Orderby ( "level DESC" ) . Limit ( "1" ) . Prepare ( ) ,
updateUser : acc . Update ( "users" ) . Set ( "group=?" ) . Where ( "group=? AND uid=?" ) . Prepare ( ) ,
updateGeneric : acc . Update ( "users" ) . Set ( "group=?" ) . Where ( "group=? AND level>=? AND posts>=?" ) . Prepare ( ) ,
}
Tasks . FifteenMin . Add ( prs . Tick )
return prs , acc . FirstError ( )
2020-02-09 10:00:08 +00:00
}
func ( s * DefaultGroupPromotionStore ) Tick ( ) error {
2022-02-21 03:32:53 +00:00
return nil
2019-09-29 04:56:39 +00:00
}
func ( s * DefaultGroupPromotionStore ) GetByGroup ( gid int ) ( gps [ ] * GroupPromotion , err error ) {
2022-02-21 03:32:53 +00:00
rows , err := s . getByGroup . Query ( gid , gid )
if err != nil {
return nil , err
}
defer rows . Close ( )
for rows . Next ( ) {
g := & GroupPromotion { }
err := rows . Scan ( & g . ID , & g . From , & g . To , & g . TwoWay , & g . Level , & g . Posts , & g . MinTime , & g . RegisteredFor )
if err != nil {
return nil , err
}
gps = append ( gps , g )
}
return gps , rows . Err ( )
2019-09-29 04:56:39 +00:00
}
// TODO: Cache the group promotions to avoid hitting the database as much
func ( s * DefaultGroupPromotionStore ) Get ( id int ) ( * GroupPromotion , error ) {
2022-02-21 03:32:53 +00:00
/ * g , err := s . cache . Get ( id )
if err == nil {
return u , nil
} * /
g := & GroupPromotion { ID : id }
err := s . get . QueryRow ( id ) . Scan ( & g . From , & g . To , & g . TwoWay , & g . Level , & g . Posts , & g . MinTime , & g . RegisteredFor )
if err == nil {
//s.cache.Set(u)
}
return g , err
2019-09-29 04:56:39 +00:00
}
2020-02-09 10:00:08 +00:00
// TODO: Optimise this to avoid the query
func ( s * DefaultGroupPromotionStore ) PromoteIfEligible ( u * User , level , posts int , registeredAt time . Time ) error {
2022-02-21 03:32:53 +00:00
mins := time . Since ( registeredAt ) . Minutes ( )
g := & GroupPromotion { From : u . Group }
//log.Printf("pre getByUserMins: %+v\n", u)
err := s . getByUserMins . QueryRow ( u . Group , level , posts , mins ) . Scan ( & g . ID , & g . To , & g . TwoWay , & g . Level , & g . Posts , & g . MinTime , & g . RegisteredFor )
if err == sql . ErrNoRows {
//log.Print("no matches found")
return nil
} else if err != nil {
return err
}
//log.Printf("g: %+v\n", g)
if g . RegisteredFor == 0 {
_ , err = s . updateGeneric . Exec ( g . To , g . From , g . Level , g . Posts )
} else {
_ , err = s . updateUser . Exec ( g . To , g . From , u . ID )
}
return err
2019-09-29 04:56:39 +00:00
}
func ( s * DefaultGroupPromotionStore ) Delete ( id int ) error {
2022-02-21 03:32:53 +00:00
_ , err := s . delete . Exec ( id )
return err
2019-09-29 04:56:39 +00:00
}
2020-02-09 10:00:08 +00:00
func ( s * DefaultGroupPromotionStore ) Create ( from , to int , twoWay bool , level , posts , registeredFor int ) ( int , error ) {
2022-02-21 03:32:53 +00:00
res , err := s . create . Exec ( from , to , twoWay , level , posts , 0 , registeredFor )
if err != nil {
return 0 , err
}
lastID , err := res . LastInsertId ( )
return int ( lastID ) , err
2019-09-29 04:56:39 +00:00
}