2018-06-06 00:21:22 +00:00
package common
import (
"database/sql"
"strconv"
"strings"
2019-07-26 22:26:52 +00:00
qgen "github.com/Azareal/Gosora/query_gen"
2018-06-06 00:21:22 +00:00
)
type CustomPageStmts struct {
update * sql . Stmt
create * sql . Stmt
}
var customPageStmts CustomPageStmts
func init ( ) {
DbInits . Add ( func ( acc * qgen . Accumulator ) error {
customPageStmts = CustomPageStmts {
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
update : acc . Update ( "pages" ) . Set ( "name=?,title=?,body=?,allowedGroups=?,menuID=?" ) . Where ( "pid=?" ) . Prepare ( ) ,
create : acc . Insert ( "pages" ) . Columns ( "name,title,body,allowedGroups,menuID" ) . Fields ( "?,?,?,?,?" ) . Prepare ( ) ,
2018-06-06 00:21:22 +00:00
}
return acc . FirstError ( )
} )
}
type CustomPage struct {
ID int
Name string // TODO: Let admins put pages in "virtual subdirectories"
Title string
Body string
AllowedGroups [ ] int
MenuID int
}
func BlankCustomPage ( ) * CustomPage {
return new ( CustomPage )
}
2019-11-08 21:46:50 +00:00
func ( p * CustomPage ) AddAllowedGroup ( gid int ) {
p . AllowedGroups = append ( p . AllowedGroups , gid )
2018-06-06 00:21:22 +00:00
}
2019-11-08 21:46:50 +00:00
func ( p * CustomPage ) getRawAllowedGroups ( ) ( rawAllowedGroups string ) {
for _ , group := range p . AllowedGroups {
2018-06-06 00:21:22 +00:00
rawAllowedGroups += strconv . Itoa ( group ) + ","
}
if len ( rawAllowedGroups ) > 0 {
rawAllowedGroups = rawAllowedGroups [ : len ( rawAllowedGroups ) - 1 ]
}
return rawAllowedGroups
}
2019-11-08 21:46:50 +00:00
func ( p * CustomPage ) Commit ( ) error {
_ , err := customPageStmts . update . Exec ( p . Name , p . Title , p . Body , p . getRawAllowedGroups ( ) , p . MenuID , p . ID )
Pages . Reload ( p . ID )
2018-06-06 00:21:22 +00:00
return err
}
2019-11-08 21:46:50 +00:00
func ( p * CustomPage ) Create ( ) ( int , error ) {
res , err := customPageStmts . create . Exec ( p . Name , p . Title , p . Body , p . getRawAllowedGroups ( ) , p . MenuID )
2018-06-06 00:21:22 +00:00
if err != nil {
return 0 , err
}
pid64 , err := res . LastInsertId ( )
return int ( pid64 ) , err
}
var Pages PageStore
// Holds the custom pages, but doesn't include the template pages in /pages/ which are a lot more flexible yet harder to use and which are too risky security-wise to make editable in the Control Panel
type PageStore interface {
2019-06-01 12:31:48 +00:00
Count ( ) ( count int )
2018-06-06 00:21:22 +00:00
Get ( id int ) ( * CustomPage , error )
GetByName ( name string ) ( * CustomPage , 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
GetOffset ( offset , perPage int ) ( pages [ ] * CustomPage , err error )
2018-06-06 00:21:22 +00:00
Reload ( id int ) error
Delete ( id int ) error
}
// TODO: Add a cache to this to save on the queries
type DefaultPageStore struct {
get * sql . Stmt
getByName * sql . Stmt
getOffset * sql . Stmt
count * sql . Stmt
delete * sql . Stmt
}
func NewDefaultPageStore ( acc * qgen . Accumulator ) ( * DefaultPageStore , error ) {
2019-10-29 22:13:45 +00:00
pa := "pages"
2018-06-06 00:21:22 +00:00
return & DefaultPageStore {
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
get : acc . Select ( pa ) . Columns ( "name, title, body, allowedGroups, menuID" ) . Where ( "pid=?" ) . Prepare ( ) ,
getByName : acc . Select ( pa ) . Columns ( "pid, name, title, body, allowedGroups, menuID" ) . Where ( "name=?" ) . Prepare ( ) ,
2019-10-29 22:13:45 +00:00
getOffset : acc . Select ( pa ) . Columns ( "pid, name, title, body, allowedGroups, menuID" ) . Orderby ( "pid DESC" ) . Limit ( "?,?" ) . Prepare ( ) ,
count : acc . Count ( pa ) . 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
delete : acc . Delete ( pa ) . Where ( "pid=?" ) . Prepare ( ) ,
2018-06-06 00:21:22 +00:00
} , acc . FirstError ( )
}
2019-06-01 12:31:48 +00:00
func ( s * DefaultPageStore ) Count ( ) ( count int ) {
err := s . count . QueryRow ( ) . Scan ( & count )
2018-06-06 00:21:22 +00:00
if err != nil {
LogError ( err )
}
2019-06-01 12:31:48 +00:00
return count
2018-06-06 00:21:22 +00:00
}
2019-06-01 12:31:48 +00:00
func ( s * DefaultPageStore ) parseAllowedGroups ( raw string , page * CustomPage ) error {
2018-06-06 00:21:22 +00:00
if raw == "" {
return nil
}
for _ , sgroup := range strings . Split ( raw , "," ) {
group , err := strconv . Atoi ( sgroup )
if err != nil {
return err
}
page . AddAllowedGroup ( group )
}
return nil
}
2019-07-26 22:26:52 +00:00
func ( s * DefaultPageStore ) Get ( id int ) ( * CustomPage , error ) {
2019-10-29 22:13:45 +00:00
p := & CustomPage { ID : id }
2018-06-06 00:21:22 +00:00
rawAllowedGroups := ""
2019-10-29 22:13:45 +00:00
err := s . get . QueryRow ( id ) . Scan ( & p . Name , & p . Title , & p . Body , & rawAllowedGroups , & p . MenuID )
2018-06-06 00:21:22 +00:00
if err != nil {
return nil , err
}
2019-10-29 22:13:45 +00:00
return p , s . parseAllowedGroups ( rawAllowedGroups , p )
2018-06-06 00:21:22 +00:00
}
2019-07-26 22:26:52 +00:00
func ( s * DefaultPageStore ) GetByName ( name string ) ( * CustomPage , error ) {
2019-10-29 22:13:45 +00:00
p := BlankCustomPage ( )
2018-06-06 00:21:22 +00:00
rawAllowedGroups := ""
2019-10-29 22:13:45 +00:00
err := s . getByName . QueryRow ( name ) . Scan ( & p . ID , & p . Name , & p . Title , & p . Body , & rawAllowedGroups , & p . MenuID )
2018-06-06 00:21:22 +00:00
if err != nil {
return nil , err
}
2019-10-29 22:13:45 +00:00
return p , s . parseAllowedGroups ( rawAllowedGroups , p )
2018-06-06 00:21:22 +00:00
}
2020-01-14 05:07:00 +00:00
func ( s * DefaultPageStore ) GetOffset ( offset , perPage int ) ( pages [ ] * CustomPage , err error ) {
2019-07-26 22:26:52 +00:00
rows , err := s . getOffset . Query ( offset , perPage )
2018-06-06 00:21:22 +00:00
if err != nil {
return pages , err
}
defer rows . Close ( )
for rows . Next ( ) {
2019-10-29 22:13:45 +00:00
p := & CustomPage { ID : 0 }
2018-06-06 00:21:22 +00:00
rawAllowedGroups := ""
2019-10-29 22:13:45 +00:00
err := rows . Scan ( & p . ID , & p . Name , & p . Title , & p . Body , & rawAllowedGroups , & p . MenuID )
2018-06-06 00:21:22 +00:00
if err != nil {
return pages , err
}
2019-10-29 22:13:45 +00:00
err = s . parseAllowedGroups ( rawAllowedGroups , p )
2018-06-06 00:21:22 +00:00
if err != nil {
return pages , err
}
2019-10-29 22:13:45 +00:00
pages = append ( pages , p )
2018-06-06 00:21:22 +00:00
}
return pages , rows . Err ( )
}
// Always returns nil as there's currently no cache
2019-07-26 22:26:52 +00:00
func ( s * DefaultPageStore ) Reload ( id int ) error {
2018-06-06 00:21:22 +00:00
return nil
}
2019-07-26 22:26:52 +00:00
func ( s * DefaultPageStore ) Delete ( id int ) error {
_ , err := s . delete . Exec ( id )
2018-06-06 00:21:22 +00:00
return err
}