github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/common/pages.go (about)

     1  package common
     2  
     3  import (
     4  	"html/template"
     5  	"net/http"
     6  	"runtime"
     7  	"sync"
     8  	"time"
     9  
    10  	p "github.com/Azareal/Gosora/common/phrases"
    11  )
    12  
    13  /*type HResource struct {
    14  	Name string
    15  	Hash string
    16  }*/
    17  
    18  // TODO: Allow resources in spots other than /s/ and possibly even external domains (e.g. CDNs)
    19  // TODO: Preload Trumboyg on Cosora on the forum list
    20  type Header struct {
    21  	Title string
    22  	//Title      []byte // Experimenting with []byte for increased efficiency, let's avoid converting too many things to []byte, as it involves a lot of extra boilerplate
    23  	NoticeList      []string
    24  	Scripts         []HScript
    25  	PreScriptsAsync []HScript
    26  	ScriptsAsync    []HScript
    27  	//Preload []string
    28  	Stylesheets []HScript
    29  	Widgets     PageWidgets
    30  	Site        *site
    31  	Settings    SettingMap
    32  	//Themes      map[string]*Theme // TODO: Use a slice containing every theme instead of the main map for speed?
    33  	ThemesSlice []*Theme
    34  	Theme       *Theme
    35  	//TemplateName string // TODO: Use this to move template calls to the router rather than duplicating them over and over and over?
    36  	CurrentUser *User // TODO: Deprecate CurrentUser on the page structs and use a pointer here
    37  	Hooks       *HookTable
    38  	Zone        string
    39  	ZoneID      int
    40  	ZoneData    interface{}
    41  	Path        string
    42  	MetaDesc    string
    43  	//OGImage string
    44  	OGDesc         string
    45  	GoogSiteVerify string
    46  	IsoCode        string
    47  	LooseCSP       bool
    48  	ExternalMedia  bool
    49  	//StartedAt      time.Time
    50  	StartedAt int64
    51  	Elapsed1  string
    52  	Writer    http.ResponseWriter
    53  	ExtData   ExtData
    54  }
    55  
    56  type HScript struct {
    57  	Name string
    58  	Hash string
    59  }
    60  
    61  func (h *Header) getScript(name string) HScript {
    62  	if name[0] == '/' && name[1] == '/' {
    63  	} else {
    64  		file, ok := StaticFiles.GetShort(name)
    65  		if ok {
    66  			return HScript{file.OName, file.Sha256I}
    67  		}
    68  	}
    69  	return HScript{name, ""}
    70  }
    71  
    72  func (h *Header) AddScript(name string) {
    73  	//log.Print("name:", name)
    74  	h.Scripts = append(h.Scripts, h.getScript(name))
    75  }
    76  
    77  func (h *Header) AddPreScriptAsync(name string) {
    78  	h.PreScriptsAsync = append(h.PreScriptsAsync, h.getScript(name))
    79  }
    80  
    81  func (h *Header) AddScriptAsync(name string) {
    82  	h.ScriptsAsync = append(h.ScriptsAsync, h.getScript(name))
    83  }
    84  
    85  /*func (h *Header) Preload(name string) {
    86  	h.Preload = append(h.Preload, name)
    87  }*/
    88  
    89  func (h *Header) AddSheet(name string) {
    90  	h.Stylesheets = append(h.Stylesheets, h.getScript(name))
    91  }
    92  
    93  // ! Experimental
    94  func (h *Header) AddXRes(names ...string) {
    95  	var o string
    96  	for i, name := range names {
    97  		if name[0] == '/' && name[1] == '/' {
    98  		} else {
    99  			file, ok := StaticFiles.GetShort(name)
   100  			if ok {
   101  				name = file.OName
   102  			}
   103  		}
   104  		if i != 0 {
   105  			o += "," + name
   106  		} else {
   107  			o += name
   108  		}
   109  	}
   110  	h.Writer.Header().Set("X-Res", o)
   111  }
   112  
   113  func (h *Header) AddNotice(name string) {
   114  	h.NoticeList = append(h.NoticeList, p.GetNoticePhrase(name))
   115  }
   116  
   117  // TODO: Add this to routes which don't use templates. E.g. Json APIs.
   118  type HeaderLite struct {
   119  	Site     *site
   120  	Settings SettingMap
   121  	Hooks    *HookTable
   122  	ExtData  ExtData
   123  }
   124  
   125  type PageWidgets struct {
   126  	LeftSidebar  template.HTML
   127  	RightSidebar template.HTML
   128  }
   129  
   130  // TODO: Add a ExtDataHolder interface with methods for manipulating the contents?
   131  // ? - Could we use a sync.Map instead?
   132  type ExtData struct {
   133  	Items map[string]interface{} // Key: pluginname
   134  	sync.RWMutex
   135  }
   136  
   137  type Page struct {
   138  	*Header
   139  	ItemList  []interface{}
   140  	Something interface{}
   141  }
   142  
   143  type SimplePage struct {
   144  	*Header
   145  }
   146  
   147  type ErrorPage struct {
   148  	*Header
   149  	Message string
   150  }
   151  
   152  type Paginator struct {
   153  	PageList []int
   154  	Page     int
   155  	LastPage int
   156  }
   157  
   158  type PaginatorMod struct {
   159  	Params   template.URL
   160  	PageList []int
   161  	Page     int
   162  	LastPage int
   163  }
   164  
   165  type CustomPagePage struct {
   166  	*Header
   167  	Page *CustomPage
   168  }
   169  
   170  type TopicCEditPost struct {
   171  	ID     int
   172  	Source string
   173  	Ref    string
   174  }
   175  type TopicCAttachItem struct {
   176  	ID       int
   177  	ImgSrc   string
   178  	Path     string
   179  	FullPath string
   180  }
   181  type TopicCPollInput struct {
   182  	Index int
   183  	Place string
   184  }
   185  
   186  type TopicPage struct {
   187  	*Header
   188  	ItemList []*ReplyUser
   189  	Topic    TopicUser
   190  	Forum    *Forum
   191  	Poll     *Poll
   192  	Paginator
   193  }
   194  
   195  type TopicListSort struct {
   196  	SortBy    string // lastupdate, mostviewed, mostviewedtoday, mostviewedthisweek, mostviewedthismonth
   197  	Ascending bool
   198  }
   199  
   200  type QuickTools struct {
   201  	CanDelete bool
   202  	CanLock   bool
   203  	CanMove   bool
   204  }
   205  
   206  type TopicListPage struct {
   207  	*Header
   208  	TopicList    []TopicsRowMut
   209  	ForumList    []Forum
   210  	DefaultForum int
   211  	Sort         TopicListSort
   212  	SelectedFids []int
   213  	QuickTools
   214  	Paginator
   215  }
   216  
   217  type ForumPage struct {
   218  	*Header
   219  	ItemList []TopicsRowMut
   220  	Forum    *Forum
   221  	CanLock  bool
   222  	CanMove  bool
   223  	Paginator
   224  }
   225  
   226  type ForumsPage struct {
   227  	*Header
   228  	ItemList []Forum
   229  }
   230  
   231  type ProfilePage struct {
   232  	*Header
   233  	ItemList     []*ReplyUser
   234  	ProfileOwner User
   235  	CurrentScore int
   236  	NextScore    int
   237  	Blocked      bool
   238  	CanMessage   bool
   239  	CanComment   bool
   240  	ShowComments bool
   241  }
   242  
   243  type CreateTopicPage struct {
   244  	*Header
   245  	ItemList []Forum
   246  	FID      int
   247  }
   248  
   249  type IPSearchPage struct {
   250  	*Header
   251  	ItemList map[int]*User
   252  	IP       string
   253  }
   254  
   255  // WIP: Optional anti-bot methods
   256  type RegisterVerifyImageGridImage struct {
   257  	Src string
   258  }
   259  type RegisterVerifyImageGrid struct {
   260  	Question string
   261  	Items    []RegisterVerifyImageGridImage
   262  }
   263  type RegisterVerify struct {
   264  	NoScript bool
   265  
   266  	Image *RegisterVerifyImageGrid
   267  }
   268  
   269  type RegisterPage struct {
   270  	*Header
   271  	RequireEmail bool
   272  	Token        string
   273  	Verify       []RegisterVerify
   274  }
   275  
   276  type Account struct {
   277  	*Header
   278  	HTMLID   string
   279  	TmplName string
   280  	Inner    nobreak
   281  }
   282  
   283  type EmailListPage struct {
   284  	*Header
   285  	ItemList []Email
   286  }
   287  
   288  type AccountLoginsPage struct {
   289  	*Header
   290  	ItemList []LoginLogItem
   291  	Paginator
   292  }
   293  
   294  type AccountBlocksPage struct {
   295  	*Header
   296  	Users []*User
   297  	Paginator
   298  }
   299  
   300  type AccountPrivacyPage struct {
   301  	*Header
   302  	ProfileComments int
   303  	ReceiveConvos   int
   304  	EnableEmbeds    bool
   305  }
   306  
   307  type AccountDashPage struct {
   308  	*Header
   309  	MFASetup     bool
   310  	CurrentScore int
   311  	NextScore    int
   312  	NextLevel    int
   313  	Percentage   int
   314  }
   315  
   316  type LevelListItem struct {
   317  	Level      int
   318  	Score      int
   319  	Status     string
   320  	Percentage int // 0 to 200 to fit with the CSS logic
   321  }
   322  
   323  type LevelListPage struct {
   324  	*Header
   325  	Levels []LevelListItem
   326  }
   327  
   328  type ResetPage struct {
   329  	*Header
   330  	UID   int
   331  	Token string
   332  	MFA   bool
   333  }
   334  
   335  type ConvoListRow struct {
   336  	*ConversationExtra
   337  	ShortUsers []*User
   338  	OneOnOne   bool
   339  }
   340  
   341  type ConvoListPage struct {
   342  	*Header
   343  	Convos []ConvoListRow
   344  	Paginator
   345  }
   346  
   347  type ConvoViewRow struct {
   348  	*ConversationPost
   349  	User         *User
   350  	ClassName    string
   351  	ContentLines int
   352  
   353  	CanModify bool
   354  }
   355  
   356  type ConvoViewPage struct {
   357  	*Header
   358  	Convo    *Conversation
   359  	Posts    []ConvoViewRow
   360  	Users    []*User
   361  	CanReply bool
   362  	Paginator
   363  }
   364  
   365  type ConvoCreatePage struct {
   366  	*Header
   367  	RecpName string
   368  }
   369  
   370  /* WIP for dyntmpl */
   371  type Panel struct {
   372  	*BasePanelPage
   373  	HTMLID     string
   374  	ClassNames string
   375  	TmplName   string
   376  	Inner      nobreak
   377  }
   378  type PanelAnalytics struct {
   379  	*BasePanelPage
   380  	FormAction string
   381  	TmplName   string
   382  	Inner      nobreak
   383  }
   384  type PanelAnalyticsStd struct {
   385  	Graph     PanelTimeGraph
   386  	ViewItems []PanelAnalyticsItem
   387  	TimeRange string
   388  	Unit      string
   389  	TimeType  string
   390  }
   391  type PanelAnalyticsStdUnit struct {
   392  	Graph     PanelTimeGraph
   393  	ViewItems []PanelAnalyticsItemUnit
   394  	TimeRange string
   395  	Unit      string
   396  	TimeType  string
   397  }
   398  type PanelAnalyticsActiveMemory struct {
   399  	Graph     PanelTimeGraph
   400  	ViewItems []PanelAnalyticsItemUnit
   401  	TimeRange string
   402  	Unit      string
   403  	TimeType  string
   404  	MemType   int
   405  }
   406  type PanelAnalyticsPerf struct {
   407  	Graph     PanelTimeGraph
   408  	ViewItems []PanelAnalyticsItemUnit
   409  	TimeRange string
   410  	Unit      string
   411  	TimeType  string
   412  	PerfType  int
   413  }
   414  
   415  type PanelStats struct {
   416  	Users       int
   417  	Groups      int
   418  	Forums      int
   419  	Pages       int
   420  	Settings    int
   421  	WordFilters int
   422  	Themes      int
   423  	Reports     int
   424  }
   425  type BasePanelPage struct {
   426  	*Header
   427  	Stats         PanelStats
   428  	Zone          string
   429  	ReportForumID int
   430  	DebugAdmin    bool
   431  }
   432  type PanelPage struct {
   433  	*BasePanelPage
   434  	ItemList  []interface{}
   435  	Something interface{}
   436  }
   437  
   438  type GridElement struct {
   439  	ID         string
   440  	Href       string
   441  	Body       string
   442  	Order      int // For future use
   443  	Class      string
   444  	Background string
   445  	TextColour string
   446  	Note       string
   447  }
   448  type DashGrids struct {
   449  	Grid1 []GridElement
   450  	Grid2 []GridElement
   451  }
   452  type PanelDashboardPage struct {
   453  	*BasePanelPage
   454  	Grids DashGrids
   455  }
   456  
   457  type PanelSetting struct {
   458  	*Setting
   459  	FriendlyName string
   460  }
   461  type PanelSettingPage struct {
   462  	*BasePanelPage
   463  	ItemList []OptionLabel
   464  	Setting  *PanelSetting
   465  }
   466  
   467  type PanelUserEditPage struct {
   468  	*BasePanelPage
   469  	Groups    []*Group
   470  	User      *User
   471  	ShowEmail bool
   472  }
   473  
   474  type PanelCustomPagesPage struct {
   475  	*BasePanelPage
   476  	ItemList []*CustomPage
   477  	Paginator
   478  }
   479  type PanelCustomPageEditPage struct {
   480  	*BasePanelPage
   481  	Page *CustomPage
   482  }
   483  
   484  /*type PanelTimeGraph struct {
   485  	Series []int64 // The counts on the left
   486  	Labels []int64 // unixtimes for the bottom, gets converted into 1:00, 2:00, etc. with JS
   487  }*/
   488  type PanelTimeGraph struct {
   489  	Series  [][]int64 // The counts on the left
   490  	Labels  []int64   // unixtimes for the bottom, gets converted into 1:00, 2:00, etc. with JS
   491  	Legends []string
   492  }
   493  
   494  type PanelAnalyticsItem struct {
   495  	Time  int64
   496  	Count int64
   497  }
   498  type PanelAnalyticsItemUnit struct {
   499  	Time  int64
   500  	Count int64
   501  	Unit  string
   502  }
   503  
   504  type PanelAnalyticsPage struct {
   505  	*BasePanelPage
   506  	Graph     PanelTimeGraph
   507  	ViewItems []PanelAnalyticsItem
   508  	TimeRange string
   509  	Unit      string
   510  	TimeType  string
   511  }
   512  
   513  type PanelAnalyticsRoutesItem struct {
   514  	Route string
   515  	Count int
   516  }
   517  
   518  type PanelAnalyticsRoutesPage struct {
   519  	*BasePanelPage
   520  	ItemList  []PanelAnalyticsRoutesItem
   521  	Graph     PanelTimeGraph
   522  	TimeRange string
   523  }
   524  
   525  type PanelAnalyticsRoutesPerfItem struct {
   526  	Route string
   527  	Count int
   528  	Unit  string
   529  }
   530  
   531  type PanelAnalyticsRoutesPerfPage struct {
   532  	*BasePanelPage
   533  	ItemList  []PanelAnalyticsRoutesPerfItem
   534  	Graph     PanelTimeGraph
   535  	TimeRange string
   536  }
   537  
   538  // TODO: Rename the fields as this structure is being used in a generic way now
   539  type PanelAnalyticsAgentsItem struct {
   540  	Agent         string
   541  	FriendlyAgent string
   542  	Count         int
   543  }
   544  
   545  type PanelAnalyticsAgentsPage struct {
   546  	*BasePanelPage
   547  	ItemList  []PanelAnalyticsAgentsItem
   548  	TimeRange string
   549  }
   550  
   551  type PanelAnalyticsReferrersPage struct {
   552  	*BasePanelPage
   553  	ItemList  []PanelAnalyticsAgentsItem
   554  	TimeRange string
   555  	ShowSpam  bool
   556  }
   557  
   558  type PanelAnalyticsRoutePage struct {
   559  	*BasePanelPage
   560  	Route     string
   561  	Graph     PanelTimeGraph
   562  	ViewItems []PanelAnalyticsItem
   563  	TimeRange string
   564  }
   565  
   566  type PanelAnalyticsAgentPage struct {
   567  	*BasePanelPage
   568  	Agent         string
   569  	FriendlyAgent string
   570  	Graph         PanelTimeGraph
   571  	TimeRange     string
   572  }
   573  
   574  type PanelAnalyticsDuoPage struct {
   575  	*BasePanelPage
   576  	ItemList  []PanelAnalyticsAgentsItem
   577  	Graph     PanelTimeGraph
   578  	TimeRange string
   579  }
   580  
   581  type PanelThemesPage struct {
   582  	*BasePanelPage
   583  	PrimaryThemes []*Theme
   584  	VariantThemes []*Theme
   585  }
   586  
   587  type PanelMenuListItem struct {
   588  	Name      string
   589  	ID        int
   590  	ItemCount int
   591  }
   592  
   593  type PanelMenuListPage struct {
   594  	*BasePanelPage
   595  	ItemList []PanelMenuListItem
   596  }
   597  
   598  type PanelWidgetListPage struct {
   599  	*BasePanelPage
   600  	Docks       map[string][]WidgetEdit
   601  	BlankWidget WidgetEdit
   602  }
   603  
   604  type PanelMenuPage struct {
   605  	*BasePanelPage
   606  	MenuID   int
   607  	ItemList []MenuItem
   608  }
   609  
   610  type PanelMenuItemPage struct {
   611  	*BasePanelPage
   612  	Item MenuItem
   613  }
   614  
   615  type PanelUserPageSearch struct {
   616  	Name  string
   617  	Email string
   618  	Group int
   619  
   620  	Any bool
   621  }
   622  type PanelUserPage struct {
   623  	*BasePanelPage
   624  	ItemList []*User
   625  	Groups   []*Group
   626  	Search   PanelUserPageSearch
   627  	PaginatorMod
   628  }
   629  
   630  type PanelGroupPage struct {
   631  	*BasePanelPage
   632  	ItemList []GroupAdmin
   633  	Paginator
   634  }
   635  
   636  type PanelEditGroupPage struct {
   637  	*BasePanelPage
   638  	ID          int
   639  	Name        string
   640  	Tag         string
   641  	Rank        string
   642  	DisableRank bool
   643  }
   644  
   645  type GroupForumPermPreset struct {
   646  	Group         *Group
   647  	Preset        string
   648  	DefaultPreset bool
   649  }
   650  
   651  type PanelEditForumPage struct {
   652  	*BasePanelPage
   653  	ID      int
   654  	Name    string
   655  	Desc    string
   656  	Active  bool
   657  	Preset  string
   658  	Groups  []GroupForumPermPreset
   659  	Actions []*ForumActionAction
   660  }
   661  
   662  type ForumActionAction struct {
   663  	*ForumAction
   664  	ActionName string
   665  }
   666  
   667  type NameLangToggle struct {
   668  	Name    string
   669  	LangStr string
   670  	Toggle  bool
   671  }
   672  
   673  type PanelEditForumGroupPage struct {
   674  	*BasePanelPage
   675  	ForumID int
   676  	GroupID int
   677  	Name    string
   678  	Desc    string
   679  	Active  bool
   680  	Preset  string
   681  	Perms   []NameLangToggle
   682  }
   683  
   684  type PanelEditGroupPermsPage struct {
   685  	*BasePanelPage
   686  	ID          int
   687  	Name        string
   688  	LocalPerms  []NameLangToggle
   689  	GlobalPerms []NameLangToggle
   690  	ModPerms    []NameLangToggle
   691  }
   692  
   693  type GroupPromotionExtend struct {
   694  	*GroupPromotion
   695  	FromGroup *Group
   696  	ToGroup   *Group
   697  }
   698  
   699  type PanelEditGroupPromotionsPage struct {
   700  	*BasePanelPage
   701  	ID         int
   702  	Name       string
   703  	Promotions []*GroupPromotionExtend
   704  	Groups     []*Group
   705  }
   706  
   707  type BackupItem struct {
   708  	SQLURL string
   709  
   710  	// TODO: Add an easier to parse format here for Gosora to be able to more easily reimport portions of the dump and to strip unnecessary data (e.g. table defs and parsed post data)
   711  
   712  	Timestamp time.Time
   713  }
   714  
   715  type PanelBackupPage struct {
   716  	*BasePanelPage
   717  	Backups []BackupItem
   718  }
   719  
   720  type PageLogItem struct {
   721  	Action template.HTML
   722  	IP     string
   723  	DoneAt string
   724  }
   725  
   726  type PanelLogsPage struct {
   727  	*BasePanelPage
   728  	Logs []PageLogItem
   729  	Paginator
   730  }
   731  
   732  type PageRegLogItem struct {
   733  	RegLogItem
   734  	ParsedReason string
   735  }
   736  
   737  type PanelRegLogsPage struct {
   738  	*BasePanelPage
   739  	Logs []PageRegLogItem
   740  	Paginator
   741  }
   742  
   743  type DebugPageTasks struct {
   744  	HalfSecond    int
   745  	Second        int
   746  	FifteenMinute int
   747  	Hour          int
   748  	Day           int
   749  	Shutdown      int
   750  }
   751  
   752  type DebugPageCache struct {
   753  	Topics  int
   754  	Users   int
   755  	Replies int
   756  
   757  	TCap int
   758  	UCap int
   759  	RCap int
   760  
   761  	TopicListThaw bool
   762  }
   763  
   764  type DebugPageDatabase struct {
   765  	Topics         int
   766  	Users          int
   767  	Replies        int
   768  	ProfileReplies int
   769  	ActivityStream int
   770  	Likes          int
   771  	Attachments    int
   772  	Polls          int
   773  
   774  	LoginLogs int
   775  	RegLogs   int
   776  	ModLogs   int
   777  	AdminLogs int
   778  
   779  	Views          int
   780  	ViewsAgents    int
   781  	ViewsForums    int
   782  	ViewsLangs     int
   783  	ViewsReferrers int
   784  	ViewsSystems   int
   785  	PostChunks     int
   786  	TopicChunks    int
   787  }
   788  
   789  type DebugPageDisk struct {
   790  	Static      int
   791  	Attachments int
   792  	Avatars     int
   793  	Logs        int
   794  	Backups     int
   795  	Git         int
   796  }
   797  
   798  type PanelDebugPage struct {
   799  	*BasePanelPage
   800  	GoVersion string
   801  	DBVersion string
   802  	Uptime    string
   803  
   804  	DBConns   int
   805  	DBAdapter string
   806  
   807  	Goroutines int
   808  	CPUs       int
   809  	HttpConns  int
   810  
   811  	Tasks    DebugPageTasks
   812  	MemStats runtime.MemStats
   813  	Cache    DebugPageCache
   814  	Database DebugPageDatabase
   815  	Disk     DebugPageDisk
   816  }
   817  
   818  type PanelTaskTask struct {
   819  	Name string
   820  	Type int // 0 = halfsec, 1 = sec, 2 = fifteenmin, 3 = hour, 4 = shutdown
   821  }
   822  type PanelTaskType struct {
   823  	Name string
   824  	FAvg string
   825  }
   826  type PanelTaskPage struct {
   827  	*BasePanelPage
   828  	Tasks []PanelTaskTask
   829  	Types []PanelTaskType
   830  }
   831  
   832  type PageSimple struct {
   833  	Title     string
   834  	Something interface{}
   835  }
   836  
   837  type AreYouSure struct {
   838  	URL     string
   839  	Message string
   840  }
   841  
   842  // TODO: Write a test for this
   843  func DefaultHeader(w http.ResponseWriter, u *User) *Header {
   844  	return &Header{Site: Site, Theme: Themes[fallbackTheme], CurrentUser: u, Writer: w}
   845  }
   846  func SimpleDefaultHeader(w http.ResponseWriter) *Header {
   847  	return &Header{Site: Site, Theme: Themes[fallbackTheme], CurrentUser: &GuestUser, Writer: w}
   848  }