2017-06-19 08:06:54 +00:00
/* Copyright Azareal 2017 - 2018 */
2017-11-11 04:06:16 +00:00
package common
2017-06-19 08:06:54 +00:00
2017-11-11 23:34:27 +00:00
import (
"bytes"
"database/sql"
"encoding/json"
2017-11-29 02:34:02 +00:00
"html/template"
"strings"
2017-11-11 23:34:27 +00:00
"sync"
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/query_gen"
2017-11-11 23:34:27 +00:00
)
2017-09-03 04:50:31 +00:00
2017-11-11 04:06:16 +00:00
var Docks WidgetDocks
2017-09-03 04:50:31 +00:00
var widgetUpdateMutex sync . RWMutex
2017-06-19 08:06:54 +00:00
2017-09-03 04:50:31 +00:00
type WidgetDocks struct {
2018-03-17 08:16:43 +00:00
LeftOfNav [ ] * Widget
RightOfNav [ ] * Widget
2017-11-29 02:34:02 +00:00
LeftSidebar [ ] * Widget
RightSidebar [ ] * Widget
2017-06-19 08:06:54 +00:00
//PanelLeft []Menus
2017-11-29 02:34:02 +00:00
Footer [ ] * Widget
2017-06-19 08:06:54 +00:00
}
2017-09-03 04:50:31 +00:00
type Widget struct {
Enabled bool
2017-06-19 08:06:54 +00:00
Location string // Coming Soon: overview, topics, topic / topic_view, forums, forum, global
Position int
2017-09-03 04:50:31 +00:00
Body string
2017-11-29 02:34:02 +00:00
Side string
Type string
Literal bool
2017-06-19 08:06:54 +00:00
}
2017-09-03 04:50:31 +00:00
type WidgetMenu struct {
Name string
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
MenuList [ ] WidgetMenuItem
}
2017-09-03 04:50:31 +00:00
type WidgetMenuItem struct {
Text string
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
Location string
2017-09-03 04:50:31 +00:00
Compact bool
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
}
2017-09-03 04:50:31 +00:00
type NameTextPair struct {
2017-06-19 08:06:54 +00:00
Name string
2017-11-29 02:34:02 +00:00
Text template . HTML
2017-06-19 08:06:54 +00:00
}
2017-11-11 23:34:27 +00:00
type WidgetStmts struct {
getWidgets * sql . Stmt
}
var widgetStmts WidgetStmts
func init ( ) {
2017-11-12 03:29:05 +00:00
DbInits . Add ( func ( acc * qgen . Accumulator ) error {
2017-11-11 23:34:27 +00:00
widgetStmts = WidgetStmts {
getWidgets : acc . Select ( "widgets" ) . Columns ( "position, side, type, active, location, data" ) . Orderby ( "position ASC" ) . Prepare ( ) ,
}
return acc . FirstError ( )
} )
}
2017-11-29 02:34:02 +00:00
func preparseWidget ( widget * Widget , wdata string ) ( err error ) {
prebuildWidget := func ( name string , data interface { } ) ( string , error ) {
var b bytes . Buffer
err := Templates . ExecuteTemplate ( & b , name + ".html" , data )
return string ( b . Bytes ( ) ) , err
}
sbytes := [ ] byte ( wdata )
switch widget . Type {
case "simple" :
var tmp NameTextPair
err = json . Unmarshal ( sbytes , & tmp )
if err != nil {
return err
}
widget . Body , err = prebuildWidget ( "widget_simple" , tmp )
case "about" :
var tmp NameTextPair
err = json . Unmarshal ( sbytes , & tmp )
if err != nil {
return err
}
widget . Body , err = prebuildWidget ( "widget_about" , tmp )
default :
widget . Body = wdata
}
2018-04-22 12:33:56 +00:00
widget . Literal = true
2017-11-29 02:34:02 +00:00
// TODO: Test this
// TODO: Should we toss this through a proper parser rather than crudely replacing it?
widget . Location = strings . Replace ( widget . Location , " " , "" , - 1 )
widget . Location = strings . Replace ( widget . Location , "frontend" , "!panel" , - 1 )
widget . Location = strings . Replace ( widget . Location , "!!" , "" , - 1 )
// Skip blank zones
var locs = strings . Split ( widget . Location , "|" )
if len ( locs ) > 0 {
widget . Location = ""
for _ , loc := range locs {
if loc == "" {
continue
}
widget . Location += loc + "|"
}
widget . Location = widget . Location [ : len ( widget . Location ) - 1 ]
}
return err
}
2018-04-22 12:33:56 +00:00
func BuildWidget ( dock string , header * Header ) ( sbody string ) {
2017-11-29 02:34:02 +00:00
var widgets [ ] * Widget
2018-04-22 12:33:56 +00:00
if ! header . Theme . HasDock ( dock ) {
2017-11-29 02:34:02 +00:00
return ""
}
2018-03-17 08:16:43 +00:00
// Let themes forcibly override this slot
2018-04-22 12:33:56 +00:00
sbody = header . Theme . BuildDock ( dock )
2018-03-17 08:16:43 +00:00
if sbody != "" {
return sbody
}
2017-11-29 02:34:02 +00:00
switch dock {
2018-03-17 08:16:43 +00:00
case "leftOfNav" :
widgets = Docks . LeftOfNav
case "rightOfNav" :
widgets = Docks . RightOfNav
2018-04-22 12:33:56 +00:00
case "topMenu" :
// 1 = id for the default menu
2018-05-11 05:41:51 +00:00
mhold , err := Menus . Get ( 1 )
if err == nil {
2018-10-14 05:08:44 +00:00
err := mhold . Build ( header . Writer , & header . CurrentUser , header . Path )
2018-04-22 12:33:56 +00:00
if err != nil {
LogError ( err )
}
}
return ""
2017-11-29 02:34:02 +00:00
case "rightSidebar" :
widgets = Docks . RightSidebar
case "footer" :
widgets = Docks . Footer
}
for _ , widget := range widgets {
if ! widget . Enabled {
continue
}
2018-04-22 12:33:56 +00:00
if widget . Allowed ( header . Zone ) {
item , err := widget . Build ( header )
2017-11-29 02:34:02 +00:00
if err != nil {
LogError ( err )
}
sbody += item
}
}
return sbody
}
// TODO: Test this
2018-04-22 12:33:56 +00:00
// 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
2017-11-29 02:34:02 +00:00
func ( widget * Widget ) Allowed ( zone string ) bool {
for _ , loc := range strings . Split ( widget . Location , "|" ) {
if loc == "global" || loc == zone {
return true
} else if len ( loc ) > 0 && loc [ 0 ] == '!' {
loc = loc [ 1 : ]
if loc != "global" && loc != zone {
return true
}
}
}
return false
}
// TODO: Refactor
func ( widget * Widget ) Build ( hvars interface { } ) ( string , error ) {
if widget . Literal {
return widget . Body , nil
}
2018-04-22 12:33:56 +00:00
var header = hvars . ( * Header )
2018-12-08 00:45:27 +00:00
err := header . Theme . RunTmpl ( widget . Body , hvars , header . Writer )
2017-12-10 03:43:30 +00:00
return "" , err
2017-11-29 02:34:02 +00:00
}
2017-11-05 09:55:34 +00:00
// TODO: Make a store for this?
2017-11-11 04:06:16 +00:00
func InitWidgets ( ) error {
2017-11-11 23:34:27 +00:00
rows , err := widgetStmts . getWidgets . Query ( )
2017-06-19 08:06:54 +00:00
if err != nil {
return err
}
defer rows . Close ( )
2017-11-29 02:34:02 +00:00
var data string
2018-03-17 08:16:43 +00:00
var leftOfNavWidgets [ ] * Widget
var rightOfNavWidgets [ ] * Widget
var leftSidebarWidgets [ ] * Widget
var rightSidebarWidgets [ ] * Widget
2017-11-29 02:34:02 +00:00
var footerWidgets [ ] * Widget
2017-06-19 08:06:54 +00:00
for rows . Next ( ) {
2017-11-29 02:34:02 +00:00
var widget = & Widget { Position : 0 }
err = rows . Scan ( & widget . Position , & widget . Side , & widget . Type , & widget . Enabled , & widget . Location , & data )
2017-06-19 08:06:54 +00:00
if err != nil {
return err
}
2017-11-29 02:34:02 +00:00
err = preparseWidget ( widget , data )
if err != nil {
return err
2017-06-19 08:06:54 +00:00
}
2017-11-29 02:34:02 +00:00
switch widget . Side {
2018-03-17 08:16:43 +00:00
case "leftOfNav" :
leftOfNavWidgets = append ( leftOfNavWidgets , widget )
case "rightOfNav" :
rightOfNavWidgets = append ( rightOfNavWidgets , widget )
2017-11-29 02:34:02 +00:00
case "left" :
2018-03-17 08:16:43 +00:00
leftSidebarWidgets = append ( leftSidebarWidgets , widget )
2017-11-29 02:34:02 +00:00
case "right" :
2018-03-17 08:16:43 +00:00
rightSidebarWidgets = append ( rightSidebarWidgets , widget )
2017-11-29 02:34:02 +00:00
case "footer" :
footerWidgets = append ( footerWidgets , widget )
2017-06-19 08:06:54 +00:00
}
}
err = rows . Err ( )
if err != nil {
return err
}
2018-03-17 08:16:43 +00:00
// TODO: Let themes set default values for widget docks, and let them lock in particular places with their stuff, e.g. leftOfNav and rightOfNav
2017-09-03 04:50:31 +00:00
widgetUpdateMutex . Lock ( )
2018-03-17 08:16:43 +00:00
Docks . LeftOfNav = leftOfNavWidgets
Docks . RightOfNav = rightOfNavWidgets
Docks . LeftSidebar = leftSidebarWidgets
Docks . RightSidebar = rightSidebarWidgets
2017-11-29 02:34:02 +00:00
Docks . Footer = footerWidgets
2017-09-03 04:50:31 +00:00
widgetUpdateMutex . Unlock ( )
2017-06-19 08:06:54 +00:00
2018-03-17 08:16:43 +00:00
DebugLog ( "Docks.LeftOfNav" , Docks . LeftOfNav )
DebugLog ( "Docks.RightOfNav" , Docks . RightOfNav )
2018-02-19 04:26:01 +00:00
DebugLog ( "Docks.LeftSidebar" , Docks . LeftSidebar )
DebugLog ( "Docks.RightSidebar" , Docks . RightSidebar )
DebugLog ( "Docks.Footer" , Docks . Footer )
2017-06-19 08:06:54 +00:00
return nil
}