2017-11-10 03:33:11 +00:00
|
|
|
package common
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-10-14 07:39:22 +00:00
|
|
|
import (
|
|
|
|
"html/template"
|
2018-04-22 12:33:56 +00:00
|
|
|
"io"
|
2017-10-14 07:39:22 +00:00
|
|
|
"log"
|
2018-10-27 03:21:02 +00:00
|
|
|
"math"
|
2018-03-21 05:56:33 +00:00
|
|
|
"path/filepath"
|
2018-03-12 04:52:47 +00:00
|
|
|
"strconv"
|
2018-03-21 05:56:33 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-10-14 07:39:22 +00:00
|
|
|
"time"
|
2017-11-11 04:06:16 +00:00
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/common/alerts"
|
2018-11-01 06:43:56 +00:00
|
|
|
"github.com/Azareal/Gosora/common/phrases"
|
2018-11-17 02:36:02 +00:00
|
|
|
"github.com/Azareal/Gosora/common/templates"
|
2017-10-14 07:39:22 +00:00
|
|
|
)
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-11-11 06:33:08 +00:00
|
|
|
var Ctemplates []string
|
2017-11-11 04:06:16 +00:00
|
|
|
var Templates = template.New("")
|
2018-04-22 12:33:56 +00:00
|
|
|
var PrebuildTmplList []func(User, *Header) CTmpl
|
2017-11-10 03:33:11 +00:00
|
|
|
|
|
|
|
type CTmpl struct {
|
|
|
|
Name string
|
|
|
|
Filename string
|
|
|
|
Path string
|
|
|
|
StructName string
|
|
|
|
Data interface{}
|
2017-11-11 06:33:08 +00:00
|
|
|
Imports []string
|
2017-11-10 03:33:11 +00:00
|
|
|
}
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
// TODO: Refactor the template trees to not need these
|
2018-03-08 03:59:47 +00:00
|
|
|
// TODO: Stop duplicating these bits of code
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-04-22 12:33:56 +00:00
|
|
|
func interpretedTopicTemplate(pi TopicPage, w io.Writer) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["topic"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "topic"
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-04-22 12:33:56 +00:00
|
|
|
var Template_topic_handle = interpretedTopicTemplate
|
|
|
|
var Template_topic_alt_handle = interpretedTopicTemplate
|
2018-11-26 05:08:10 +00:00
|
|
|
var Template_topic_alt_guest_handle = interpretedTopicTemplate
|
|
|
|
var Template_topic_alt_member_handle = interpretedTopicTemplate
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-04-22 12:33:56 +00:00
|
|
|
var Template_topics_handle = func(pi TopicListPage, w io.Writer) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["topics"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "topics"
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2018-11-28 21:46:53 +00:00
|
|
|
var Template_topics_guest_handle = Template_topics_handle
|
|
|
|
var Template_topics_member_handle = Template_topics_handle
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-04-22 12:33:56 +00:00
|
|
|
var Template_forum_handle = func(pi ForumPage, w io.Writer) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["forum"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "forum"
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-04-22 12:33:56 +00:00
|
|
|
var Template_forums_handle = func(pi ForumsPage, w io.Writer) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["forums"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "forums"
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-04-22 12:33:56 +00:00
|
|
|
var Template_profile_handle = func(pi ProfilePage, w io.Writer) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["profile"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "profile"
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-04-22 12:33:56 +00:00
|
|
|
var Template_create_topic_handle = func(pi CreateTopicPage, w io.Writer) error {
|
2018-01-10 03:32:48 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["create_topic"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
2018-01-10 03:32:48 +00:00
|
|
|
mapping = "create_topic"
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 03:59:47 +00:00
|
|
|
// nolint
|
2018-04-22 12:33:56 +00:00
|
|
|
var Template_login_handle = func(pi Page, w io.Writer) error {
|
2018-03-08 03:59:47 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["login"]
|
|
|
|
if !ok {
|
|
|
|
mapping = "login"
|
|
|
|
}
|
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint
|
2018-04-22 12:33:56 +00:00
|
|
|
var Template_register_handle = func(pi Page, w io.Writer) error {
|
2018-03-08 03:59:47 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["register"]
|
|
|
|
if !ok {
|
|
|
|
mapping = "register"
|
|
|
|
}
|
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint
|
2018-06-06 00:21:22 +00:00
|
|
|
var Template_error_handle = func(pi ErrorPage, w io.Writer) error {
|
2018-03-08 03:59:47 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["error"]
|
|
|
|
if !ok {
|
|
|
|
mapping = "error"
|
|
|
|
}
|
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint
|
2018-04-22 12:33:56 +00:00
|
|
|
var Template_ip_search_handle = func(pi IPSearchPage, w io.Writer) error {
|
2018-03-08 03:59:47 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["ip_search"]
|
|
|
|
if !ok {
|
|
|
|
mapping = "ip_search"
|
|
|
|
}
|
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
|
|
|
}
|
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
// nolint
|
|
|
|
var Template_account_handle = func(pi AccountDashPage, w io.Writer) error {
|
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["account"]
|
|
|
|
if !ok {
|
|
|
|
mapping = "account"
|
|
|
|
}
|
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
|
|
|
}
|
|
|
|
|
2018-06-24 13:49:29 +00:00
|
|
|
func tmplInitUsers() (User, User, User) {
|
2018-07-28 12:52:23 +00:00
|
|
|
avatar, microAvatar := BuildAvatar(62, "")
|
|
|
|
user := User{62, BuildProfileURL("fake-user", 62), "Fake User", "compiler@localhost", 0, false, false, false, false, false, false, GuestPerms, make(map[string]bool), "", false, "", avatar, microAvatar, "", "", "", "", 0, 0, 0, "0.0.0.0.0", 0}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Do a more accurate level calculation for this?
|
2018-07-28 12:52:23 +00:00
|
|
|
avatar, microAvatar = BuildAvatar(1, "")
|
|
|
|
user2 := User{1, BuildProfileURL("admin-alice", 1), "Admin Alice", "alice@localhost", 1, true, true, true, true, false, false, AllPerms, make(map[string]bool), "", true, "", avatar, microAvatar, "", "", "", "", 58, 1000, 0, "127.0.0.1", 0}
|
|
|
|
|
|
|
|
avatar, microAvatar = BuildAvatar(2, "")
|
|
|
|
user3 := User{2, BuildProfileURL("admin-fred", 62), "Admin Fred", "fred@localhost", 1, true, true, true, true, false, false, AllPerms, make(map[string]bool), "", true, "", avatar, microAvatar, "", "", "", "", 42, 900, 0, "::1", 0}
|
2018-06-24 13:49:29 +00:00
|
|
|
return user, user2, user3
|
|
|
|
}
|
|
|
|
|
|
|
|
func tmplInitHeaders(user User, user2 User, user3 User) (*Header, *Header, *Header) {
|
2018-04-22 12:33:56 +00:00
|
|
|
header := &Header{
|
2017-11-11 04:06:16 +00:00
|
|
|
Site: Site,
|
|
|
|
Settings: SettingBox.Load().(SettingMap),
|
|
|
|
Themes: Themes,
|
2017-11-23 05:37:08 +00:00
|
|
|
Theme: Themes[DefaultThemeBox.Load().(string)],
|
2018-04-22 12:33:56 +00:00
|
|
|
CurrentUser: user,
|
2017-09-03 04:50:31 +00:00
|
|
|
NoticeList: []string{"test"},
|
2017-08-27 09:33:45 +00:00
|
|
|
Stylesheets: []string{"panel"},
|
2017-09-03 04:50:31 +00:00
|
|
|
Scripts: []string{"whatever"},
|
2017-08-27 09:33:45 +00:00
|
|
|
Widgets: PageWidgets{
|
|
|
|
LeftSidebar: template.HTML("lalala"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-11-17 02:36:02 +00:00
|
|
|
buildHeader := func(user User) *Header {
|
|
|
|
var head = &Header{Site: Site}
|
|
|
|
*head = *header
|
|
|
|
head.CurrentUser = user
|
|
|
|
return head
|
|
|
|
}
|
2018-05-27 09:36:35 +00:00
|
|
|
|
2018-11-17 02:36:02 +00:00
|
|
|
return header, buildHeader(user2), buildHeader(user3)
|
2018-06-24 13:49:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 05:08:10 +00:00
|
|
|
type TmplLoggedin struct {
|
|
|
|
Stub string
|
|
|
|
Guest string
|
|
|
|
Member string
|
|
|
|
}
|
|
|
|
|
2018-06-24 13:49:29 +00:00
|
|
|
// ? - Add template hooks?
|
|
|
|
func CompileTemplates() error {
|
|
|
|
var config tmpl.CTemplateConfig
|
|
|
|
config.Minify = Config.MinifyTemplates
|
|
|
|
config.Debug = Dev.DebugMode
|
|
|
|
config.SuperDebug = Dev.TemplateDebug
|
|
|
|
|
|
|
|
c := tmpl.NewCTemplateSet()
|
|
|
|
c.SetConfig(config)
|
|
|
|
c.SetBaseImportMap(map[string]string{
|
2018-10-27 03:21:02 +00:00
|
|
|
"io": "io",
|
|
|
|
"github.com/Azareal/Gosora/common": "github.com/Azareal/Gosora/common",
|
2018-06-24 13:49:29 +00:00
|
|
|
})
|
|
|
|
c.SetBuildTags("!no_templategen")
|
|
|
|
|
|
|
|
// Schemas to train the template compiler on what to expect
|
|
|
|
// TODO: Add support for interface{}s
|
|
|
|
user, user2, user3 := tmplInitUsers()
|
|
|
|
header, header2, _ := tmplInitHeaders(user, user2, user3)
|
|
|
|
now := time.Now()
|
|
|
|
|
2017-08-27 09:33:45 +00:00
|
|
|
log.Print("Compiling the templates")
|
|
|
|
|
2018-01-26 05:53:34 +00:00
|
|
|
poll := Poll{ID: 1, Type: 0, Options: map[int]string{0: "Nothing", 1: "Something"}, Results: map[int]int{0: 5, 1: 2}, QuickOptions: []PollOption{
|
|
|
|
PollOption{0, "Nothing"},
|
|
|
|
PollOption{1, "Something"},
|
|
|
|
}, VoteCount: 7}
|
2018-07-28 12:52:23 +00:00
|
|
|
avatar, microAvatar := BuildAvatar(62, "")
|
2018-08-30 10:31:21 +00:00
|
|
|
topic := TopicUser{1, "blah", "Blah", "Hey there!", 0, false, false, now, RelativeTime(now), now, RelativeTime(now), 0, "", "127.0.0.1", 1, 0, 1, "classname", poll.ID, "weird-data", BuildProfileURL("fake-user", 62), "Fake User", Config.DefaultGroup, avatar, microAvatar, 0, "", "", "", "", "", 58, false}
|
2017-09-28 22:16:34 +00:00
|
|
|
var replyList []ReplyUser
|
2018-07-28 12:52:23 +00:00
|
|
|
// TODO: Do we want the UID on this to be 0?
|
|
|
|
avatar, microAvatar = BuildAvatar(0, "")
|
|
|
|
replyList = append(replyList, ReplyUser{0, 0, "Yo!", "Yo!", 0, "alice", "Alice", Config.DefaultGroup, now, RelativeTime(now), 0, 0, avatar, microAvatar, "", 0, "", "", "", "", 0, "127.0.0.1", false, 1, "", ""})
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
var varList = make(map[string]tmpl.VarItem)
|
2018-09-24 07:52:06 +00:00
|
|
|
var compile = func(name string, expects string, expectsInt interface{}) (tmpl string, err error) {
|
|
|
|
return c.Compile(name+".html", "templates/", expects, expectsInt, varList)
|
|
|
|
}
|
2018-11-26 05:08:10 +00:00
|
|
|
var compileByLoggedin = func(name string, expects string, expectsInt interface{}) (tmpl TmplLoggedin, err error) {
|
|
|
|
stub, guest, member, err := c.CompileByLoggedin(name+".html", "templates/", expects, expectsInt, varList)
|
|
|
|
return TmplLoggedin{stub, guest, member}, err
|
|
|
|
}
|
2018-09-24 07:52:06 +00:00
|
|
|
|
2018-05-27 09:36:35 +00:00
|
|
|
header.Title = "Topic Name"
|
2018-08-30 10:31:21 +00:00
|
|
|
tpage := TopicPage{header, replyList, topic, &Forum{ID: 1, Name: "Hahaha"}, poll, 1, 1}
|
|
|
|
tpage.Forum.Link = BuildForumURL(NameToSlug(tpage.Forum.Name), tpage.Forum.ID)
|
2018-11-26 05:08:10 +00:00
|
|
|
topicTmpl, err := compile("topic", "common.TopicPage", tpage)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-26 05:08:10 +00:00
|
|
|
/*topicAltTmpl, err := compile("topic_alt", "common.TopicPage", tpage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}*/
|
|
|
|
|
|
|
|
topicAltTmpl, err := compileByLoggedin("topic_alt", "common.TopicPage", tpage)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
varList = make(map[string]tmpl.VarItem)
|
2018-06-01 05:02:29 +00:00
|
|
|
header.Title = "User 526"
|
2018-10-08 05:34:25 +00:00
|
|
|
ppage := ProfilePage{header, replyList, user, 0, 0} // TODO: Use the score from user to generate the currentScore and nextScore
|
2018-09-24 07:52:06 +00:00
|
|
|
profileTmpl, err := compile("profile", "common.ProfilePage", ppage)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
// TODO: Use a dummy forum list to avoid o(n) problems
|
2017-08-27 09:33:45 +00:00
|
|
|
var forumList []Forum
|
2017-11-23 05:37:08 +00:00
|
|
|
forums, err := Forums.GetAll()
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, forum := range forums {
|
2017-09-10 16:57:22 +00:00
|
|
|
forumList = append(forumList, *forum)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2018-09-24 07:52:06 +00:00
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
varList = make(map[string]tmpl.VarItem)
|
2018-06-01 05:02:29 +00:00
|
|
|
header.Title = "Forum List"
|
|
|
|
forumsPage := ForumsPage{header, forumList}
|
2018-09-24 07:52:06 +00:00
|
|
|
forumsTmpl, err := compile("forums", "common.ForumsPage", forumsPage)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var topicsList []*TopicsRow
|
2018-08-30 10:31:21 +00:00
|
|
|
topicsList = append(topicsList, &TopicsRow{1, "topic-title", "Topic Title", "The topic content.", 1, false, false, now, now, "Date", user3.ID, 1, "", "127.0.0.1", 1, 0, 1, "classname", "", &user2, "", 0, &user3, "General", "/forum/general.2"})
|
2018-05-27 09:36:35 +00:00
|
|
|
header2.Title = "Topic List"
|
2018-09-20 08:56:50 +00:00
|
|
|
topicListPage := TopicListPage{header, topicsList, forumList, Config.DefaultForum, TopicListSort{"lastupdated", false}, Paginator{[]int{1}, 1, 1}}
|
2018-11-28 21:46:53 +00:00
|
|
|
/*topicListTmpl, err := compile("topics", "common.TopicListPage", topicListPage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}*/
|
|
|
|
topicListTmpl, err := compileByLoggedin("topics", "common.TopicListPage", topicListPage)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-11 05:22:33 +00:00
|
|
|
forumItem := BlankForum(1, "general-forum.1", "General Forum", "Where the general stuff happens", true, "all", 0, "", 0)
|
2018-05-27 09:36:35 +00:00
|
|
|
header.Title = "General Forum"
|
|
|
|
forumPage := ForumPage{header, topicsList, forumItem, Paginator{[]int{1}, 1, 1}}
|
2018-09-24 07:52:06 +00:00
|
|
|
forumTmpl, err := compile("forum", "common.ForumPage", forumPage)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-06 00:21:22 +00:00
|
|
|
header.Title = "Login Page"
|
|
|
|
loginPage := Page{header, tList, nil}
|
2018-09-24 07:52:06 +00:00
|
|
|
loginTmpl, err := compile("login", "common.Page", loginPage)
|
2018-03-08 03:59:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-06 00:21:22 +00:00
|
|
|
header.Title = "Registration Page"
|
|
|
|
registerPage := Page{header, tList, "nananana"}
|
2018-09-24 07:52:06 +00:00
|
|
|
registerTmpl, err := compile("register", "common.Page", registerPage)
|
2018-03-08 03:59:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-11-11 05:22:33 +00:00
|
|
|
}
|
2017-11-11 06:33:08 +00:00
|
|
|
|
2018-06-06 00:21:22 +00:00
|
|
|
header.Title = "Error"
|
|
|
|
errorPage := ErrorPage{header, "A problem has occurred in the system."}
|
2018-09-24 07:52:06 +00:00
|
|
|
errorTmpl, err := compile("error", "common.ErrorPage", errorPage)
|
2018-03-08 03:59:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ipUserList = make(map[int]*User)
|
|
|
|
ipUserList[1] = &user2
|
2018-06-01 05:02:29 +00:00
|
|
|
header.Title = "IP Search"
|
|
|
|
ipSearchPage := IPSearchPage{header2, ipUserList, "::1"}
|
2018-09-24 07:52:06 +00:00
|
|
|
ipSearchTmpl, err := compile("ip_search", "common.IPSearchPage", ipSearchPage)
|
2018-03-08 03:59:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
mfaSetup := false
|
|
|
|
prevScore := GetLevelScore(header.CurrentUser.Level)
|
|
|
|
currentScore := header.CurrentUser.Score - prevScore
|
|
|
|
nextScore := GetLevelScore(header.CurrentUser.Level+1) - prevScore
|
|
|
|
perc := int(math.Ceil((float64(nextScore) / float64(currentScore)) * 100))
|
|
|
|
|
|
|
|
accountPage := AccountDashPage{header, "dashboard", "account_own_edit", mfaSetup, currentScore, nextScore, user.Level + 1, perc * 2}
|
|
|
|
accountTmpl, err := compile("account", "common.AccountDashPage", accountPage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-21 05:56:33 +00:00
|
|
|
var wg sync.WaitGroup
|
2018-11-26 05:08:10 +00:00
|
|
|
var writeTemplate = func(name string, content interface{}) {
|
2018-03-21 05:56:33 +00:00
|
|
|
log.Print("Writing template '" + name + "'")
|
|
|
|
|
2018-11-26 05:08:10 +00:00
|
|
|
var writeTmpl = func(name string, content string) {
|
|
|
|
if content == "" {
|
|
|
|
log.Fatal("No content body for " + name)
|
|
|
|
}
|
2018-03-21 05:56:33 +00:00
|
|
|
err := writeFile("./template_"+name+".go", content)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-11-26 05:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
switch content := content.(type) {
|
|
|
|
case string:
|
|
|
|
writeTmpl(name, content)
|
|
|
|
case TmplLoggedin:
|
|
|
|
writeTmpl(name, content.Stub)
|
|
|
|
writeTmpl(name+"_guest", content.Guest)
|
|
|
|
writeTmpl(name+"_member", content.Member)
|
|
|
|
}
|
2018-03-21 05:56:33 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-03-08 03:59:47 +00:00
|
|
|
// Let plugins register their own templates
|
|
|
|
DebugLog("Registering the templates for the plugins")
|
|
|
|
config = c.GetConfig()
|
|
|
|
config.SkipHandles = true
|
|
|
|
c.SetConfig(config)
|
2017-11-11 04:06:16 +00:00
|
|
|
for _, tmplfunc := range PrebuildTmplList {
|
2018-04-22 12:33:56 +00:00
|
|
|
tmplItem := tmplfunc(user, header)
|
2017-11-11 04:06:16 +00:00
|
|
|
varList = make(map[string]tmpl.VarItem)
|
2017-11-11 06:33:08 +00:00
|
|
|
compiledTmpl, err := c.Compile(tmplItem.Filename, tmplItem.Path, tmplItem.StructName, tmplItem.Data, varList, tmplItem.Imports...)
|
2017-11-10 03:33:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-21 05:56:33 +00:00
|
|
|
writeTemplate(tmplItem.Name, compiledTmpl)
|
2017-11-10 03:33:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 09:33:45 +00:00
|
|
|
log.Print("Writing the templates")
|
2018-11-26 05:08:10 +00:00
|
|
|
writeTemplate("topic", topicTmpl)
|
|
|
|
writeTemplate("topic_alt", topicAltTmpl)
|
2018-03-21 05:56:33 +00:00
|
|
|
writeTemplate("profile", profileTmpl)
|
|
|
|
writeTemplate("forums", forumsTmpl)
|
2018-04-22 12:33:56 +00:00
|
|
|
writeTemplate("topics", topicListTmpl)
|
2018-03-21 05:56:33 +00:00
|
|
|
writeTemplate("forum", forumTmpl)
|
|
|
|
writeTemplate("login", loginTmpl)
|
|
|
|
writeTemplate("register", registerTmpl)
|
|
|
|
writeTemplate("ip_search", ipSearchTmpl)
|
2018-10-27 03:21:02 +00:00
|
|
|
writeTemplate("account", accountTmpl)
|
2018-03-21 05:56:33 +00:00
|
|
|
writeTemplate("error", errorTmpl)
|
2018-04-22 12:33:56 +00:00
|
|
|
writeTemplateList(c, &wg, "./")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CompileJSTemplates() error {
|
|
|
|
log.Print("Compiling the JS templates")
|
|
|
|
var config tmpl.CTemplateConfig
|
|
|
|
config.Minify = Config.MinifyTemplates
|
2018-06-24 13:49:29 +00:00
|
|
|
config.Debug = Dev.DebugMode
|
2018-04-22 12:33:56 +00:00
|
|
|
config.SuperDebug = Dev.TemplateDebug
|
|
|
|
config.SkipHandles = true
|
2018-06-24 13:49:29 +00:00
|
|
|
config.SkipTmplPtrMap = true
|
|
|
|
config.SkipInitBlock = false
|
2018-04-22 12:33:56 +00:00
|
|
|
config.PackageName = "tmpl"
|
|
|
|
|
|
|
|
c := tmpl.NewCTemplateSet()
|
|
|
|
c.SetConfig(config)
|
|
|
|
c.SetBaseImportMap(map[string]string{
|
2018-10-27 03:21:02 +00:00
|
|
|
"io": "io",
|
|
|
|
"github.com/Azareal/Gosora/common/alerts": "github.com/Azareal/Gosora/common/alerts",
|
2018-04-22 12:33:56 +00:00
|
|
|
})
|
2018-06-24 13:49:29 +00:00
|
|
|
c.SetBuildTags("!no_templategen")
|
|
|
|
|
|
|
|
user, user2, user3 := tmplInitUsers()
|
|
|
|
header, _, _ := tmplInitHeaders(user, user2, user3)
|
|
|
|
now := time.Now()
|
2018-04-22 12:33:56 +00:00
|
|
|
var varList = make(map[string]tmpl.VarItem)
|
|
|
|
|
|
|
|
// TODO: Check what sort of path is sent exactly and use it here
|
2018-05-14 08:56:56 +00:00
|
|
|
alertItem := alerts.AlertItem{Avatar: "", ASID: 1, Path: "/", Message: "uh oh, something happened"}
|
|
|
|
alertTmpl, err := c.Compile("alert.html", "templates/", "alerts.AlertItem", alertItem, varList)
|
2018-04-22 12:33:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-24 13:49:29 +00:00
|
|
|
c.SetBaseImportMap(map[string]string{
|
2018-10-27 03:21:02 +00:00
|
|
|
"io": "io",
|
|
|
|
"github.com/Azareal/Gosora/common": "github.com/Azareal/Gosora/common",
|
2018-06-24 13:49:29 +00:00
|
|
|
})
|
|
|
|
// TODO: Fix the import loop so we don't have to use this hack anymore
|
|
|
|
c.SetBuildTags("!no_templategen,tmplgentopic")
|
|
|
|
|
2018-08-30 10:31:21 +00:00
|
|
|
var topicsRow = &TopicsRow{1, "topic-title", "Topic Title", "The topic content.", 1, false, false, now, now, "Date", user3.ID, 1, "", "127.0.0.1", 1, 0, 1, "classname", "", &user2, "", 0, &user3, "General", "/forum/general.2"}
|
2018-06-24 13:49:29 +00:00
|
|
|
topicListItemTmpl, err := c.Compile("topics_topic.html", "templates/", "*common.TopicsRow", topicsRow, varList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
poll := Poll{ID: 1, Type: 0, Options: map[int]string{0: "Nothing", 1: "Something"}, Results: map[int]int{0: 5, 1: 2}, QuickOptions: []PollOption{
|
|
|
|
PollOption{0, "Nothing"},
|
|
|
|
PollOption{1, "Something"},
|
|
|
|
}, VoteCount: 7}
|
2018-07-28 12:52:23 +00:00
|
|
|
avatar, microAvatar := BuildAvatar(62, "")
|
2018-08-30 10:31:21 +00:00
|
|
|
topic := TopicUser{1, "blah", "Blah", "Hey there!", 62, false, false, now, RelativeTime(now), now, RelativeTime(now), 0, "", "127.0.0.1", 1, 0, 1, "classname", poll.ID, "weird-data", BuildProfileURL("fake-user", 62), "Fake User", Config.DefaultGroup, avatar, microAvatar, 0, "", "", "", "", "", 58, false}
|
2018-06-24 13:49:29 +00:00
|
|
|
var replyList []ReplyUser
|
2018-07-28 12:52:23 +00:00
|
|
|
// TODO: Do we really want the UID here to be zero?
|
|
|
|
avatar, microAvatar = BuildAvatar(0, "")
|
|
|
|
replyList = append(replyList, ReplyUser{0, 0, "Yo!", "Yo!", 0, "alice", "Alice", Config.DefaultGroup, now, RelativeTime(now), 0, 0, avatar, microAvatar, "", 0, "", "", "", "", 0, "127.0.0.1", false, 1, "", ""})
|
2018-06-24 13:49:29 +00:00
|
|
|
|
|
|
|
varList = make(map[string]tmpl.VarItem)
|
|
|
|
header.Title = "Topic Name"
|
2018-08-30 10:31:21 +00:00
|
|
|
tpage := TopicPage{header, replyList, topic, &Forum{ID: 1, Name: "Hahaha"}, poll, 1, 1}
|
|
|
|
tpage.Forum.Link = BuildForumURL(NameToSlug(tpage.Forum.Name), tpage.Forum.ID)
|
2018-11-26 05:08:10 +00:00
|
|
|
topicPostsTmpl, err := c.Compile("topic_posts.html", "templates/", "common.TopicPage", tpage, varList)
|
2018-06-24 13:49:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-26 05:08:10 +00:00
|
|
|
topicAltPostsTmpl, err := c.Compile("topic_alt_posts.html", "templates/", "common.TopicPage", tpage, varList)
|
2018-06-24 13:49:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-14 10:21:18 +00:00
|
|
|
var dirPrefix = "./tmpl_client/"
|
2018-04-22 12:33:56 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
var writeTemplate = func(name string, content string) {
|
|
|
|
log.Print("Writing template '" + name + "'")
|
|
|
|
if content == "" {
|
|
|
|
log.Fatal("No content body")
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
err := writeFile(dirPrefix+"template_"+name+".go", content)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
writeTemplate("alert", alertTmpl)
|
2018-06-24 13:49:29 +00:00
|
|
|
writeTemplate("topics_topic", topicListItemTmpl)
|
2018-11-26 05:08:10 +00:00
|
|
|
writeTemplate("topic_posts", topicPostsTmpl)
|
|
|
|
writeTemplate("topic_alt_posts", topicAltPostsTmpl)
|
2018-04-22 12:33:56 +00:00
|
|
|
writeTemplateList(c, &wg, dirPrefix)
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-21 05:56:33 +00:00
|
|
|
|
2018-04-22 12:33:56 +00:00
|
|
|
func writeTemplateList(c *tmpl.CTemplateSet, wg *sync.WaitGroup, prefix string) {
|
|
|
|
log.Print("Writing template list")
|
2018-03-21 05:56:33 +00:00
|
|
|
wg.Add(1)
|
2017-08-27 09:33:45 +00:00
|
|
|
go func() {
|
2018-04-22 12:33:56 +00:00
|
|
|
out := "package " + c.GetConfig().PackageName + "\n\n"
|
2018-05-14 10:59:18 +00:00
|
|
|
var getterstr = "\n// nolint\nGetFrag = func(name string) [][]byte {\nswitch(name) {\n"
|
2018-03-12 04:52:47 +00:00
|
|
|
for templateName, count := range c.TemplateFragmentCount {
|
|
|
|
out += "var " + templateName + "_frags = make([][]byte," + strconv.Itoa(count) + ")\n"
|
2018-05-14 10:59:18 +00:00
|
|
|
getterstr += "\tcase \"" + templateName + "\":\n"
|
|
|
|
getterstr += "\treturn " + templateName + "_frags\n"
|
2018-03-12 04:52:47 +00:00
|
|
|
}
|
2018-05-14 10:59:18 +00:00
|
|
|
getterstr += "}\nreturn nil\n}\n"
|
|
|
|
out += "\n// nolint\nfunc init() {\n" + c.FragOut + "\n" + getterstr + "}\n"
|
2018-04-22 12:33:56 +00:00
|
|
|
err := writeFile(prefix+"template_list.go", out)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-03-21 05:56:33 +00:00
|
|
|
wg.Done()
|
2017-08-27 09:33:45 +00:00
|
|
|
}()
|
2018-03-21 05:56:33 +00:00
|
|
|
wg.Wait()
|
2018-04-22 12:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func arithToInt64(in interface{}) (out int64) {
|
|
|
|
switch in := in.(type) {
|
|
|
|
case int64:
|
|
|
|
out = in
|
|
|
|
case int32:
|
|
|
|
out = int64(in)
|
|
|
|
case int:
|
|
|
|
out = int64(in)
|
|
|
|
case uint32:
|
|
|
|
out = int64(in)
|
|
|
|
case uint16:
|
|
|
|
out = int64(in)
|
|
|
|
case uint8:
|
|
|
|
out = int64(in)
|
|
|
|
case uint:
|
|
|
|
out = int64(in)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func arithDuoToInt64(left interface{}, right interface{}) (leftInt int64, rightInt int64) {
|
|
|
|
return arithToInt64(left), arithToInt64(right)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 05:22:33 +00:00
|
|
|
func InitTemplates() error {
|
2018-03-21 05:56:33 +00:00
|
|
|
DebugLog("Initialising the template system")
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Add support for floats
|
2017-08-27 09:33:45 +00:00
|
|
|
fmap := make(map[string]interface{})
|
2017-09-03 04:50:31 +00:00
|
|
|
fmap["add"] = func(left interface{}, right interface{}) interface{} {
|
2018-04-22 12:33:56 +00:00
|
|
|
leftInt, rightInt := arithDuoToInt64(left, right)
|
2017-09-10 16:57:22 +00:00
|
|
|
return leftInt + rightInt
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
fmap["subtract"] = func(left interface{}, right interface{}) interface{} {
|
2018-04-22 12:33:56 +00:00
|
|
|
leftInt, rightInt := arithDuoToInt64(left, right)
|
2017-09-10 16:57:22 +00:00
|
|
|
return leftInt - rightInt
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
fmap["multiply"] = func(left interface{}, right interface{}) interface{} {
|
2018-04-22 12:33:56 +00:00
|
|
|
leftInt, rightInt := arithDuoToInt64(left, right)
|
2017-09-10 16:57:22 +00:00
|
|
|
return leftInt * rightInt
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
fmap["divide"] = func(left interface{}, right interface{}) interface{} {
|
2018-04-22 12:33:56 +00:00
|
|
|
leftInt, rightInt := arithDuoToInt64(left, right)
|
2017-09-10 16:57:22 +00:00
|
|
|
if leftInt == 0 || rightInt == 0 {
|
2017-08-27 09:33:45 +00:00
|
|
|
return 0
|
|
|
|
}
|
2017-09-10 16:57:22 +00:00
|
|
|
return leftInt / rightInt
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 12:33:56 +00:00
|
|
|
fmap["dock"] = func(dock interface{}, headerInt interface{}) interface{} {
|
|
|
|
return template.HTML(BuildWidget(dock.(string), headerInt.(*Header)))
|
2017-11-29 02:34:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-17 02:36:02 +00:00
|
|
|
fmap["elapsed"] = func(startedAtInt interface{}) interface{} {
|
|
|
|
return time.Since(startedAtInt.(time.Time)).String()
|
|
|
|
}
|
|
|
|
|
2018-03-08 03:59:47 +00:00
|
|
|
fmap["lang"] = func(phraseNameInt interface{}) interface{} {
|
|
|
|
phraseName, ok := phraseNameInt.(string)
|
|
|
|
if !ok {
|
|
|
|
panic("phraseNameInt is not a string")
|
|
|
|
}
|
2018-06-17 07:28:18 +00:00
|
|
|
// TODO: Log non-existent phrases?
|
2018-11-01 06:43:56 +00:00
|
|
|
return template.HTML(phrases.GetTmplPhrase(phraseName))
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2018-03-08 03:59:47 +00:00
|
|
|
|
2018-10-10 07:33:51 +00:00
|
|
|
fmap["level"] = func(levelInt interface{}) interface{} {
|
|
|
|
level, ok := levelInt.(int)
|
|
|
|
if !ok {
|
|
|
|
panic("levelInt is not an integer")
|
|
|
|
}
|
2018-11-01 06:43:56 +00:00
|
|
|
return template.HTML(phrases.GetLevelPhrase(level))
|
2018-10-10 07:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 05:25:27 +00:00
|
|
|
fmap["scope"] = func(name interface{}) interface{} {
|
|
|
|
return ""
|
2018-07-13 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmap["dyntmpl"] = func(nameInt interface{}, pageInt interface{}, headerInt interface{}) interface{} {
|
|
|
|
header := headerInt.(*Header)
|
|
|
|
err := RunThemeTemplate(header.Theme.Name, nameInt.(string), pageInt, header.Writer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ""
|
2018-03-31 05:25:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 03:59:47 +00:00
|
|
|
// The interpreted templates...
|
|
|
|
DebugLog("Loading the template files...")
|
2017-11-11 04:06:16 +00:00
|
|
|
Templates.Funcs(fmap)
|
2018-03-21 05:56:33 +00:00
|
|
|
templateFiles, err := filepath.Glob("templates/*.html")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var templateFileMap = make(map[string]int)
|
|
|
|
for index, path := range templateFiles {
|
|
|
|
path = strings.Replace(path, "\\", "/", -1)
|
|
|
|
log.Print("templateFile: ", path)
|
|
|
|
templateFileMap[path] = index
|
|
|
|
}
|
|
|
|
|
|
|
|
overrideFiles, err := filepath.Glob("templates/overrides/*.html")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, path := range overrideFiles {
|
|
|
|
path = strings.Replace(path, "\\", "/", -1)
|
|
|
|
log.Print("overrideFile: ", path)
|
|
|
|
index, ok := templateFileMap["templates/"+strings.TrimPrefix(path, "templates/overrides/")]
|
|
|
|
if !ok {
|
|
|
|
log.Print("not ok: templates/" + strings.TrimPrefix(path, "templates/overrides/"))
|
|
|
|
templateFiles = append(templateFiles, path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
templateFiles[index] = path
|
|
|
|
}
|
|
|
|
template.Must(Templates.ParseFiles(templateFiles...))
|
2017-11-11 04:06:16 +00:00
|
|
|
template.Must(Templates.ParseGlob("pages/*"))
|
2017-11-11 05:22:33 +00:00
|
|
|
|
|
|
|
return nil
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|