2019-01-21 12:27:59 +00:00
package common
import (
2022-02-21 03:32:53 +00:00
"database/sql"
"encoding/json"
"strconv"
"strings"
"sync/atomic"
2019-01-21 12:27:59 +00:00
2022-02-21 03:32:53 +00:00
qgen "github.com/Azareal/Gosora/query_gen"
2019-01-21 12:27:59 +00:00
)
type WidgetStmts struct {
2022-02-21 03:32:53 +00:00
//getList *sql.Stmt
getDockList * sql . Stmt
delete * sql . Stmt
create * sql . Stmt
update * 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
2022-02-21 03:32:53 +00:00
//qgen.SimpleModel
2019-01-21 12:27:59 +00:00
}
var widgetStmts WidgetStmts
func init ( ) {
2022-02-21 03:32:53 +00:00
DbInits . Add ( func ( acc * qgen . Accumulator ) error {
w := "widgets"
widgetStmts = WidgetStmts {
//getList: acc.Select(w).Columns("wid,position,side,type,active,location,data").Orderby("position ASC").Prepare(),
getDockList : acc . Select ( w ) . Columns ( "wid,position,type,active,location,data" ) . Where ( "side=?" ) . Orderby ( "position ASC" ) . Prepare ( ) ,
//model: acc.SimpleModel(w,"position,type,active,location,data","wid"),
delete : acc . Delete ( w ) . Where ( "wid=?" ) . Prepare ( ) ,
create : acc . Insert ( w ) . Columns ( "position,side,type,active,location,data" ) . Fields ( "?,?,?,?,?,?" ) . Prepare ( ) ,
update : acc . Update ( w ) . Set ( "position=?,side=?,type=?,active=?,location=?,data=?" ) . Where ( "wid=?" ) . Prepare ( ) ,
}
return acc . FirstError ( )
} )
2019-01-21 12:27:59 +00:00
}
// TODO: Shrink this struct for common uses in the templates? Would that really make things go faster?
type Widget struct {
2022-02-21 03:32:53 +00:00
ID int
Enabled bool
Location string // Coming Soon: overview, topics, topic / topic_view, forums, forum, global
Position int
RawBody string
Body string
Side string
Type string
Literal bool
TickMask atomic . Value
InitFunc func ( w * Widget , sched * WidgetScheduler ) error
ShutdownFunc func ( w * Widget ) error
BuildFunc func ( w * Widget , hvars interface { } ) ( string , error )
TickFunc func ( w * Widget ) error
2019-01-21 12:27:59 +00:00
}
2019-11-10 02:37:53 +00:00
func ( w * Widget ) Delete ( ) error {
2022-02-21 03:32:53 +00:00
_ , err := widgetStmts . delete . Exec ( w . ID )
if err != nil {
return err
}
// Reload the dock
// TODO: Better synchronisation
Widgets . delete ( w . ID )
widgets , err := getDockWidgets ( w . Side )
if err != nil {
return err
}
setDock ( w . Side , widgets )
return nil
2019-01-21 12:27:59 +00:00
}
2021-04-07 14:23:11 +00:00
func ( w * Widget ) Copy ( ) ( ow * Widget ) {
2022-02-21 03:32:53 +00:00
ow = & Widget { }
* ow = * w
return ow
2019-01-21 12:27:59 +00:00
}
// TODO: Test this
// TODO: Add support for zone:id. Perhaps, carry a ZoneID property around in *Header? It might allow some weirdness like frontend[5] which matches any zone with an ID of 5 but it would be a tad faster than verifying each zone, although it might be problematic if users end up relying on this behaviour for areas which don't pass IDs to the widgets system but *probably* should
2019-06-01 12:31:48 +00:00
// TODO: Add a selector which also matches topics inside a specific forum?
2019-11-10 02:37:53 +00:00
func ( w * Widget ) Allowed ( zone string , zoneid int ) bool {
2022-02-21 03:32:53 +00:00
for _ , loc := range strings . Split ( w . Location , "|" ) {
if len ( loc ) == 0 {
continue
}
sloc := strings . Split ( ":" , loc )
if len ( sloc ) > 1 {
iloc , _ := strconv . Atoi ( sloc [ 1 ] )
if zoneid != 0 && iloc != zoneid {
continue
}
}
if loc == "global" || loc == zone {
return true
} else if loc [ 0 ] == '!' {
loc = loc [ 1 : ]
if loc != "global" && loc != zone {
return true
}
}
}
return false
2019-01-21 12:27:59 +00:00
}
// TODO: Refactor
2019-11-10 02:37:53 +00:00
func ( w * Widget ) Build ( hvars interface { } ) ( string , error ) {
2022-02-21 03:32:53 +00:00
if w . Literal {
return w . Body , nil
}
if w . BuildFunc != nil {
return w . BuildFunc ( w , hvars )
}
header := hvars . ( * Header )
err := header . Theme . RunTmpl ( w . Body , hvars , header . Writer )
return "" , err
2019-01-21 12:27:59 +00:00
}
type WidgetEdit struct {
2022-02-21 03:32:53 +00:00
* Widget
Data map [ string ] string
2019-01-21 12:27:59 +00:00
}
2019-11-10 02:37:53 +00:00
func ( w * WidgetEdit ) Create ( ) ( int , error ) {
2022-02-21 03:32:53 +00:00
data , err := json . Marshal ( w . Data )
if err != nil {
return 0 , err
}
res , err := widgetStmts . create . Exec ( w . Position , w . Side , w . Type , w . Enabled , w . Location , data )
if err != nil {
return 0 , err
}
// Reload the dock
widgets , err := getDockWidgets ( w . Side )
if err != nil {
return 0 , err
}
setDock ( w . Side , widgets )
wid64 , err := res . LastInsertId ( )
return int ( wid64 ) , err
2019-01-21 12:27:59 +00:00
}
2019-11-10 02:37:53 +00:00
func ( w * WidgetEdit ) Commit ( ) error {
2022-02-21 03:32:53 +00:00
data , err := json . Marshal ( w . Data )
if err != nil {
return err
}
_ , err = widgetStmts . update . Exec ( w . Position , w . Side , w . Type , w . Enabled , w . Location , data , w . ID )
if err != nil {
return err
}
// Reload the dock
widgets , err := getDockWidgets ( w . Side )
if err != nil {
return err
}
setDock ( w . Side , widgets )
return nil
2019-01-21 12:27:59 +00:00
}