Compare commits

..

No commits in common. "master" and "conversations" have entirely different histories.

785 changed files with 40782 additions and 70011 deletions

View File

@ -7,13 +7,13 @@
/public/EQCSS.js /public/EQCSS.js
/schema/** /schema/**
tmpl_list.go template_list.go
tmpl_forum.go template_forum.go
tmpl_forums.go template_forums.go
tmpl_topic.go template_topic.go
tmpl_topic_alt.go template_topic_alt.go
tmpl_topics.go template_topics.go
tmpl_profile.go template_profile.go
gen_mysql.go gen_mysql.go
gen_mssql.go gen_mssql.go

9
.gitignore vendored
View File

@ -1,5 +1,4 @@
tmp/* tmp/*
!tmp/filler.txt
tmp2/* tmp2/*
cert_test/* cert_test/*
tmp.txt tmp.txt
@ -14,8 +13,6 @@ backups/*.sql
logs/*.log logs/*.log
config/config.json config/config.json
node_modules/* node_modules/*
samples/vue/node_modules/*
samples/vue/*
bin/* bin/*
out/* out/*
*.exe *.exe
@ -24,11 +21,11 @@ out/*
*.log *.log
.DS_Store .DS_Store
.vscode/launch.json .vscode/launch.json
schema/lastSchema.json
config/config.go config/config.go
QueryGen QueryGen
RouterGen RouterGen
Patcher Patcher
Gosora Gosora
Installer Install
tmpl_*.go template_*.go
tmpl_*.jgo

3
.htaccess Normal file
View File

@ -0,0 +1,3 @@
# Gosora doesn't use Apache, this file is just here to stop Apache from blindly serving our config files, etc. when this program isn't intended to be served in such a manner at all
deny from all

26
.travis.yml Normal file
View File

@ -0,0 +1,26 @@
env:
global:
- CC_TEST_REPORTER_ID=38ad11f95f28b1d33c9c31467f9eb5eb4e336e4e0e36046c86a4f7ff6e4c3977
language: go
go:
- "1.10"
- "1.11"
- master
before_install:
- cd $HOME
- git clone https://github.com/Azareal/Gosora
- cd Gosora
- chmod -R 0777 .
- mv ./config/config_example.json ./config/config.json
- ./update-deps-linux
- ./dev-update-travis
install: true
before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
script: ./run-linux-tests
after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
addons:
mariadb: '10.0'

View File

@ -1,11 +1,4 @@
// Place your settings in this file to overwrite default and user settings. // Place your settings in this file to overwrite default and user settings.
{ {
"editor.tabSize": 4, "editor.insertSpaces": false
// Insert spaces when pressing Tab. This setting is overriden
// based on the file contents when `editor.detectIndentation` is true.
"editor.insertSpaces": true,
// When opening a file, `editor.tabSize` and `editor.insertSpaces`
// will be detected based on the file contents. Set to false to keep
// the values you've explicitly set, above.
"editor.detectIndentation": false
} }

51
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,51 @@
If you want to add a contribution, you'll have to open a pull request and to sign the CLA (contributor level agreement).
It's mainly there to deal with any legal issues which come our way and to switch licenses without having to chase down contributors who have long stopped using the internet or are deceased or incapacitated.
Other uses may arise in the future, e.g. commercial licensing for companies which might not be authorised to use open source licensing and what-not, although that's currently uncertain as I'm not knowledgable about the ins and outs of the law.
Try to prefix commits which introduce a lot of bugs or otherwise has a large impact on the usability of Gosora with UNSTABLE.
If anything seems suspect, then feel free to bring up an alternative, although I'd rather not get hung up on the little details, if it's something which is purely a matter of opinion.
Also, please don't push new features, particularly ones which will require a great effort from other maintainers in the long term, particularly if it has fairly minor benefits to the ecosystem as a whole, unless you are willing to maintain it.
# Coding Standards
All code must be unit tested where ever possible with the exception of JavaScript which is untestable with our current technologies, tread with caution there.
Use tabs not spaces for indentation.
# Golang
Use the standard linter and listen to what it tells you to do.
The route assignments in main.go are *legacy code*, add new routes to `router_gen/routes.go` instead.
Try to use the single responsibility principle where ever possible, with the exception for if doing so will cause a large performance drop. In other words, don't give your interfaces / structs too many responsibilities, keep them simple.
Avoid hand-rolling queries. Use the builders, a ready built statement or a datastore structure instead. Preferably a datastore.
Commits which require the patcher / update script to be run should be prefixed with "Database Changes: "
More coming up.
# JavaScript
Use semicolons at the end of statements. If you don't, you might wind up breaking a minifier or two.
Always use strict mode.
Don't worry about ES5, we're targetting modern browsers. If we decide to backport code to older browsers, then we'll transpile the files.
Please don't use await. It incurs too much of a cognitive overhead as to where and when you can use it.
To keep consistency with Go code, variables must be camelCase.
# JSON
To keep consistency with Go code, map keys must be camelCase.
# Phrases
Try to keep the name of the phrase close to the actual phrase in english to make it easier for localisers to reason about which phrase is which.

View File

@ -1,21 +0,0 @@
tidy:
go mod tidy
build:
mkdir -p bin
go build -ldflags="-s -w" -o ./bin/gosora.exe .
clean:
echo "Deleting artifacts from previous builds"
rm -f template_*.go
rm -f tmpl_*.go
rm -f gen_*.go
rm -f tmpl_client/template_*
rm -f tmpl_client/tmpl_*
rm -f ./bin/*
rm -f ./common/gen_extend.go

189
README.md Normal file
View File

@ -0,0 +1,189 @@
# Gosora ![Build Status](https://travis-ci.org/Azareal/Gosora.svg?branch=master) [![Azareal's Discord Chat](https://img.shields.io/badge/style-Invite-7289DA.svg?style=flat&label=Discord)](https://discord.gg/eyYvtTf)
A super fast forum software written in Go. You can talk to us on our Discord chat!
The initial code-base was forked from one of my side projects, but has now gone far beyond that. We've moved along in a development and the software should be somewhat stable for general use.
Features may break from time to time, however I will generally try to warn of the biggest offenders in advance, so that you can tread with caution around certain commits, the upcoming v0.1 will undergo even more rigorous testing.
File an issue or open a topic on the forum, if there's something you want and you very well might find it landing in the software fairly quickly.
For plugin and theme developers, things are a little dicier, as the internal APIs and ways of writing themes are in constant flux, however some stability in that area should be coming fairly soon.
If you like this software, please give it a star and give us some feedback :)
If you dislike it, please give us some feedback on how to make it better! We're always looking for feedback. We love hearing your opinions. If there's something missing or something doesn't look quite right, don't worry! We plan to add many, many things in the run up to v0.1!
# Features
Standard Forum Functionality. All of the little things you would expect of any forum software. E.g. Common Moderation features, modlogs, theme system, avatars, bbcode parser, markdown parser, report system, per-forum permissions, group permissions and so on.
Custom Pages. There are some rough edges
Emojis. Allow your users to express themselves without resorting to serving tons upon tons of image files.
In-memory static file, forum and group caches. We have a slightly more dynamic cache for users and topics.
A profile system, including profile comments and moderation tools for the profile owner.
A template engine which compiles templates down to machine code. Over forty times faster than the standard template library `html/templates`, although it does remove some of the hand holding to achieve this. Compatible with templates written for `html/templates`, so you don't need to learn any new templating language.
A plugin system. We have a number of APIs and hooks for plugins, however they're currently subject to change and don't cover as much of the software as we'd like yet.
A responsive design. Looks great on mobile phones, tablets, laptops, desktops and more!
Other modern features like alerts, likes, advanced dashboard with live stats (CPU, RAM, online user count, and so on), etc.
# Requirements
Go 1.10 or newer - You will need to install this. Pick the .msi, if you want everything sorted out for you rather than having to go around updating the environment settings. https://golang.org/doc/install
For Ubuntu, you can consult: https://tecadmin.net/install-go-on-ubuntu/
You will also want to run `ln -s /usr/local/go/bin/go` (replace /usr/local with where ever you put Go), so that go becomes visible to other users.
If you followed the instructions above, you can update to the latest version of Go simply by deleting the `/go/` folder and replacing it with a `/go/` folder for the latest version of Go.
Git - You may need this for downloading updates via the updater. You might already have this installed on your server, if the `git` commands don't work, then install this. https://git-scm.com/downloads
MySQL Database - You will need to setup a MySQL Database somewhere. A MariaDB Database works equally well and is much faster than MySQL. You could use something like WNMP / XAMPP which have a little PHP script called PhpMyAdmin for managing MySQL databases or you could install MariaDB directly.
Download the .msi installer from [MariaDB](https://mariadb.com/downloads) and run that. You may want to set it up as a service to avoid running it every-time the computer starts up.
Instructions on how to set MariaDB up on Linux: https://downloads.mariadb.org/mariadb/repositories/
We recommend changing the root password (that is the password for the user 'root'). Remember that password, you will need it for the installation process. Of course, we would advise using a user other than root for maximum security, although that adds additional steps to the process of getting everything setup.
You might also want to run `mysql_secure_installation` to further harden (aka make it more secure) MySQL / MariaDB.
If you're using Ubuntu, you might want to look at: https://www.itzgeek.com/how-tos/linux/ubuntu-how-tos/install-mariadb-on-ubuntu-16-04.html
It's entirely possible that your host already has MySQL installed and ready to go, so you might be able to skip this step, particularly if it's a managed VPS or a shared host. Or they might have a quicker and easier method of setting up MySQL.
# How to download
For Linux, you can skip down to the Installation section as it covers this.
On Windows, you might want to try the [GosoraBootstrapper](https://github.com/Azareal/GosoraBootstrapper), if you can't find the command prompt or otherwise can't follow those instructions. It's just a matter of double-clicking on the bat file there and it'll download the rest of the files for you.
# Installation
Consult [installation](https://github.com/Azareal/Gosora/blob/master/docs/installation.md) for instructions on how to install Gosora.
# Updating
Consult [updating](https://github.com/Azareal/Gosora/blob/master/docs/updating.md) for instructions on how to update Gosora.
# Running the program
*Linux*
If you have setup a service, you can run:
`./pre-run-linux`
`service gosora start`
You can then, check Gosora's current status (to see if it started up properly) with:
`service gosora status`
And you can stop it with:
`service gosora stop`
If you haven't setup a service, you can run `./run-linux`, although you will be responsible for finding a way to run it in the background, so that it doesn't close when the terminal does.
One method might be to use: https://serverfault.com/questions/34750/is-it-possible-to-detach-a-process-from-its-terminal-or-i-should-have-used-s
*Windows*
Run `run.bat`, e.g. double-clicking on it.
# How do I install plugins?
For the default plugins like Markdown and Helloworld, you can find them in the Plugin Manager of your Control Panel. For ones which aren't included by default, you will need to drag them from your `/extend/` directory and into the `/` directory (the root directory of your Gosora installation, where the executable and most of the main Go files are).
You will then need to recompile Gosora in order to link the plugin code with Gosora's code. For plugins not written in Gosora (e.g. JavaScript), you do not need to move them from the `/extend/` directory, they will automatically show up in your Control Panel ready to be installed.
Experimental plugins aka the ones in the `/experimental/` folder (e.g. plugin_sendmail) are similar but different. You will have to move native plugins (ones written in Go) to the root directory of your installation and will have to move experimental plugins written in other languages into the `/extend/` directory.
We're looking for ways to clean-up the plugin system so that all of them (except the experimental ones) are housed in `/extend/`, however we've encountered some problems with Go's packaging system. We hope to fix this issue in the future.
# Images
![Shadow Theme](https://github.com/Azareal/Gosora/blob/master/images/shadow.png)
![Shadow Quick Topic](https://github.com/Azareal/Gosora/blob/master/images/quick-topics.png)
![Tempra Simple Theme](https://github.com/Azareal/Gosora/blob/master/images/tempra-simple.png)
![Tempra Simple Topic List](https://github.com/Azareal/Gosora/blob/master/images/topic-list.png)
![Tempra Simple Mobile](https://github.com/Azareal/Gosora/blob/master/images/tempra-simple-mobile-375px.png)
![Cosora Prototype WIP](https://github.com/Azareal/Gosora/blob/master/images/cosora-wip.png)
More images in the /images/ folder. Beware though, some of them are *really* outdated. Also, keep in mind that a new theme is in the works.
# Dependencies
These are the libraries and pieces of software which Gosora relies on to function, an "ingredients" list so to speak.
A few of these like Rez aren't currently in use, but are things we think we'll need in the very near future and want to have those things ready, so that we can quickly slot them in.
* Go 1.10+
* MariaDB (or any other MySQL compatible database engine). We'll allow other database engines in the future.
* github.com/go-sql-driver/mysql For interfacing with MariaDB.
* golang.org/x/crypto/bcrypt For hashing passwords.
* golang.org/x/crypto/argon2 For hashing passwords.
* github.com/Azareal/gopsutil For pulling information on CPU and memory usage. I've temporarily forked this, as we were having stability issues with the latest build.
* github.com/StackExchange/wmi Dependency for gopsutil on Windows.
* golang.org/x/sys/windows Also a dependency for gopsutil on Windows. This isn't needed at the moment, as I've rolled things back to an older more stable build.
* github.com/gorilla/websocket Needed for Gosora's Optional WebSockets Module.
* github.com/robertkrimen/otto Needed for the upcoming JS plugin type.
* gopkg.in/sourcemap.v1 Dependency for Otto.
* github.com/lib/pq For interfacing with PostgreSQL. You will be able to pick this instead of MariaDB soon.
* ithub.com/denisenkom/go-mssqldb For interfacing with MSSQL. You will be able to pick this instead of MSSQL soon.
* github.com/bamiaux/rez An image resizer (e.g. for spitting out thumbnails)
* github.com/esimov/caire The other image resizer, slower but may be useful for covering cases Rez does not. A third faster one we might point to at some point is probably Discord's Lilliput, however it requires a C Compiler and we don't want to add that as a dependency at this time.
* github.com/fsnotify/fsnotify A library for watching events on the file system.
* github.com/pkg/errors Some helpers to make it easier for us to track down bugs.
* More items to come here, our dependencies are going through a lot of changes, and I'll be documenting those soon ;)
# Bundled Plugins
There are several plugins which are bundled with the software by default. These cover various common tasks which aren't common enough to clutter the core with or which have competing implementation methods (E.g. plugin_markdown vs plugin_bbcode for post mark-up).
* Hey There / Skeleton / Hey There (JS Version) - Example plugins for helping you learn how to develop plugins.
* BBCode - A plugin in early development for converting BBCode Tags into HTML.
* Markdown - An extremely simple plugin for converting Markdown into HTML.
* Social Groups - An extremely unstable WIP plugin which lets users create their own little discussion areas which they can administrate / moderate on their own.
# Developers
There are a few things you'll need to know before running the more developer oriented features like the tests or the benchmarks.
The benchmarks are currently being rewritten as they're currently extremely serial which can lead to severe slow-downs when run on a home computer due to the benchmarks being run on the one core everything else is being run on (Browser, OS, etc.) and the tests not taking parallelism into account.

38
TODO.md Normal file
View File

@ -0,0 +1,38 @@
# TO-DO
Oh my, you caught me right at the start of this project. There's nothing to see here yet, asides from the absolute basics. You might want to look again later!
The various little features which somehow got stuck in the net. Don't worry, I'll get to them!
More moderation features. E.g. Move, Approval Queue (Posts made by users in certain usergroups will need to be approved by a moderator before they're publically visible), etc.
Add a simple anti-spam measure. I have quite a few ideas in mind, but it'll take a while to implement the more advanced ones, so I'd like to put off some of those to a later date and focus on the basics. E.g. CAPTCHAs, hidden fields, etc.
Add more granular permissions management to the Forum Manager.
Add a *better* plugin system. E.g. Allow for plugins written in Javascript and ones written in Go. Also, we need to add many, many, many more plugin hooks.
I will need to ponder over implementing an even faster router. We don't need one immediately, although it would be nice if we could get one in the near future. It really depends. Ideally, it would be one which can easily integrate with the current structure without much work, although I'm not beyond making some alterations to faciliate it, assuming that we don't get too tightly bound to that specific router.
Allow themes to define their own templates and to override core templates with their own.
Add a friend system.
Improve profile customisability.
Implement all the common BBCode tags in plugin_bbcode
Implement all the common Markdown codes in plugin_markdown
Add more administration features.
Add more features for improving user engagement. E.g. A like system. I have a few of these in mind, but I've been pre-occupied with implementing other features.
Add a widget system.
Add support for multi-factor authentication.
Add support for secondary emails for users.
Improve the shell scripts and possibly add support for Make? A make.go might be a good solution?

1
attachs/filler.txt Normal file
View File

@ -0,0 +1 @@
This file is here so that Git will include this folder in the repository.

1
backups/filler.txt Normal file
View File

@ -0,0 +1 @@
This file is here so that Git will include this folder in the repository.

View File

@ -1,24 +1,31 @@
echo "Deleting artifacts from previous builds"
rm -f template_*.go
rm -f gen_*.go
rm -f tmpl_client/template_*.go
rm -f ./Gosora
echo "Building the router generator" echo "Building the router generator"
go build -ldflags="-s -w" -o RouterGen "./router_gen" cd ./router_gen
go build -o RouterGen
mv ./RouterGen ..
cd ..
echo "Running the router generator" echo "Running the router generator"
./RouterGen ./RouterGen
echo "Building the hook stub generator"
go build -ldflags="-s -w" -o HookStubGen "./cmd/hook_stub_gen"
echo "Running the hook stub generator"
./HookStubGen
echo "Building the hook generator"
go build -tags hookgen -ldflags="-s -w" -o HookGen "./cmd/hook_gen"
echo "Running the hook generator"
./HookGen
echo "Building the query generator" echo "Building the query generator"
go build -ldflags="-s -w" -o QueryGen "./cmd/query_gen" cd ./query_gen
go build -o QueryGen
mv ./QueryGen ..
cd ..
echo "Running the query generator" echo "Running the query generator"
./QueryGen ./QueryGen
echo "Building Gosora" echo "Building Gosora"
go generate go generate
go build -ldflags="-s -w" -o ./bin/gosora.exe go build -o Gosora
echo "Building the installer"
cd ./install
go build -o Install
mv ./Install ..
cd ..

View File

@ -1,38 +1,31 @@
echo "Deleting artifacts from previous builds" echo "Deleting artifacts from previous builds"
rm -f template_*.go rm -f template_*.go
rm -f tmpl_*.go
rm -f gen_*.go rm -f gen_*.go
rm -f tmpl_client/template_* rm -f tmpl_client/template_*.go
rm -f tmpl_client/tmpl_*
rm -f ./Gosora rm -f ./Gosora
rm -f ./common/gen_extend.go
echo "Building the router generator" echo "Building the router generator"
go build -ldflags="-s -w" -o RouterGen "./router_gen" cd ./router_gen
go build -o RouterGen
mv ./RouterGen ..
cd ..
echo "Running the router generator" echo "Running the router generator"
./RouterGen ./RouterGen
echo "Building the hook stub generator"
go build -ldflags="-s -w" -o HookStubGen "./cmd/hook_stub_gen"
echo "Running the hook stub generator"
./HookStubGen
echo "Building the hook generator"
go build -tags hookgen -ldflags="-s -w" -o HookGen "./cmd/hook_gen"
echo "Running the hook generator"
./HookGen
echo "Generating the JSON handlers"
easyjson -pkg common
echo "Building the query generator" echo "Building the query generator"
go build -ldflags="-s -w" -o QueryGen "./cmd/query_gen" cd ./query_gen
go build -o QueryGen
mv ./QueryGen ..
cd ..
echo "Running the query generator" echo "Running the query generator"
./QueryGen ./QueryGen
echo "Building Gosora" echo "Building Gosora"
go generate go generate
go build -ldflags="-s -w" -o Gosora -tags no_ws go build -o Gosora -tags no_ws
echo "Building the installer" echo "Building the installer"
go build -ldflags="-s -w" -o Installer "./install" cd ./install
go build -o Install
mv ./Install ..
cd ..

43
build-nowebsockets.bat Normal file
View File

@ -0,0 +1,43 @@
@echo off
rem TODO: Make these deletes a little less noisy
del "template_*.go"
del "gen_*.go"
del "tmpl_client/template_*.go"
del "gosora.exe"
echo Generating the dynamic code
go generate
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
echo Building the executable
go build -o gosora.exe -tags no_ws
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
echo Building the installer
go build ./install
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
echo Building the router generator
go build ./router_gen
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
echo Building the query generator
go build ./query_gen
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
echo Gosora was successfully built
pause

45
build.bat Normal file
View File

@ -0,0 +1,45 @@
@echo off
rem TODO: Make these deletes a little less noisy
del "template_*.go"
del "gen_*.go"
cd tmpl_client
del "template_*.go"
cd ..
del "gosora.exe"
echo Generating the dynamic code
go generate
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
echo Building the executable
go build -o gosora.exe
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
echo Building the installer
go build ./install
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
echo Building the router generator
go build ./router_gen
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
echo Building the query generator
go build ./query_gen
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
echo Gosora was successfully built
pause

11
build_templates.bat Normal file
View File

@ -0,0 +1,11 @@
echo Building the templates
gosora.exe -build-templates
echo Rebuilding the executable
go build -o gosora.exe
if %errorlevel% neq 0 (
pause
exit /b %errorlevel%
)
pause

View File

@ -1,378 +0,0 @@
package main
import (
"context"
"log"
"net/http"
"git.tuxpa.in/a/gosora/common"
"git.tuxpa.in/a/gosora/routes"
"git.tuxpa.in/a/gosora/routes/panel"
"git.tuxpa.in/a/gosora/uutils"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
type ContextKey string
var ContextKey_USER ContextKey = "gosora_user"
var ContextKey_EXTRADATA ContextKey = "gosora_extradata"
func AuthContextMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user_copy, ok := common.PreRoute(w, r)
if !ok {
return
}
user := &user_copy
ctx := context.WithValue(r.Context(), ContextKey_USER, user)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func ParseFormMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, ok := r.Context().Value(ContextKey_USER).(*common.User)
if !ok {
log.Println("error grabbing user from context")
return
}
err := common.ParseForm(w, r, u)
if err != nil {
log.Println("error parsing form middleware")
return
}
next.ServeHTTP(w, r)
})
}
func NoBannedMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, ok := r.Context().Value(ContextKey_USER).(*common.User)
if !ok {
log.Println("error grabbing user from context")
return
}
err := common.NoBanned(w, r, user)
if err != nil {
log.Println(err)
return
}
next.ServeHTTP(w, r)
})
}
func NoSessionMismatchMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, ok := r.Context().Value(ContextKey_USER).(*common.User)
if !ok {
log.Println("error grabbing user from context")
return
}
err := common.NoSessionMismatch(w, r, user)
if err != nil {
log.Println(err)
return
}
next.ServeHTTP(w, r)
})
}
func MemberOnlyMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, ok := r.Context().Value(ContextKey_USER).(*common.User)
if !ok {
log.Println("error grabbing user from context")
return
}
err := common.MemberOnly(w, r, user)
if err != nil {
log.Println(err)
return
}
next.ServeHTTP(w, r)
})
}
func SuperModOnlyMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, ok := r.Context().Value(ContextKey_USER).(*common.User)
if !ok {
log.Println("error grabbing user from context")
return
}
err := common.SuperModOnly(w, r, user)
if err != nil {
log.Println(err)
return
}
next.ServeHTTP(w, r)
})
}
func SuperAdminOnlyMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, ok := r.Context().Value(ContextKey_USER).(*common.User)
if !ok {
log.Println("error grabbing user from context")
return
}
err := common.SuperAdminOnly(w, r, user)
if err != nil {
log.Println(err)
return
}
next.ServeHTTP(w, r)
})
}
type UserRoute = func(w http.ResponseWriter, r *http.Request, u *common.User) common.RouteError
type ExtraRoute = func(w http.ResponseWriter, r *http.Request, u *common.User, s string) common.RouteError
type HeaderRoute = func(w http.ResponseWriter, r *http.Request, u *common.User, h *common.Header) common.RouteError
type HeaderExtraRoute = func(w http.ResponseWriter, r *http.Request, u *common.User, h *common.Header, s string) common.RouteError
func ConvertUserRoute(fn UserRoute) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, ok := r.Context().Value(ContextKey_USER).(*common.User)
if !ok {
log.Println("error grabbing user from context")
return
}
err := fn(w, r, u)
if err != nil {
log.Println("error handling %s %s", fn, err)
}
})
}
func ConvertHeaderRoute(fn HeaderRoute) http.HandlerFunc {
return ConvertUserRoute(func(w http.ResponseWriter, r *http.Request, u *common.User) common.RouteError {
h, err := common.UserCheckNano(w, r, u, uutils.Nanotime())
if err != nil {
return err
}
return fn(w, r, u, h)
})
}
func ConvertExtraRoute(fn ExtraRoute) http.HandlerFunc {
return ConvertUserRoute(func(w http.ResponseWriter, r *http.Request, u *common.User) common.RouteError {
s := chi.URLParam(r, "extra")
return fn(w, r, u, s)
})
}
func ConvertHeaderExtraRoute(fn HeaderExtraRoute) http.HandlerFunc {
return ConvertHeaderRoute(func(w http.ResponseWriter, r *http.Request, u *common.User, h *common.Header) common.RouteError {
s := chi.URLParam(r, "extra")
return fn(w, r, u, h, s)
})
}
func NewChiRouter() {
r := chi.NewRouter()
// A good base middleware stack
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(AuthContextMiddleware)
r.Route("/overview", func(r chi.Router) {
r.Get("/", ConvertHeaderRoute(routes.Overview))
})
r.Route("/pages", func(r chi.Router) {
r.Get("/{extra}", ConvertHeaderExtraRoute(routes.CustomPage))
})
r.Route("/forums", func(r chi.Router) {
r.Get("/", ConvertHeaderRoute(routes.ForumList))
})
r.Route("/forum", func(r chi.Router) {
r.Get("/{extra}", ConvertHeaderExtraRoute(routes.ViewForum))
})
r.Route("/theme", func(r chi.Router) {
r.Use(ParseFormMiddleware)
r.Get("/", ConvertUserRoute(routes.ChangeTheme))
})
r.Route("/attachs", func(r chi.Router) {
r.Use(ParseFormMiddleware)
r.Get("/{extra}", ConvertExtraRoute(routes.ShowAttachment))
})
r.Route("/ws", func(r chi.Router) {
r.Get("/{extra}", ConvertUserRoute(common.RouteWebsockets))
})
r.Route("/api", func(r chi.Router) {
r.Get("/phrases*", ConvertUserRoute(routeAPIPhrases))
r.Get("/me*", ConvertUserRoute(routes.APIMe))
r.Get("/watches*", ConvertUserRoute(routeJSAntispam))
r.Get("/*", ConvertUserRoute(routeAPI))
})
r.Route("/report", func(r chi.Router) {
r.Use(NoBannedMiddleware)
r.Use(NoSessionMismatchMiddleware)
r.Use(MemberOnlyMiddleware)
r.Get("/submit/{extra}", ConvertExtraRoute(routes.ReportSubmit))
})
r.Route("/topics", func(r chi.Router) {
r.Get("/most-viewed", ConvertHeaderRoute(routes.TopicListMostViewed))
r.Get("/week-views", ConvertHeaderRoute(routes.TopicListWeekViews))
r.Get("/create", ConvertHeaderExtraRoute(routes.CreateTopic))
r.Get("/", ConvertHeaderRoute(routes.TopicList))
})
r.Route("/panel", func(r chi.Router) {
r.Use(SuperModOnlyMiddleware)
r.Use(NoSessionMismatchMiddleware)
r.Route("/forums", func(r chi.Router) {
r.Get("/", ConvertUserRoute(panel.Forums))
r.Get("/create", ConvertUserRoute(panel.ForumsCreateSubmit))
r.Get("/delete/{extra}", ConvertExtraRoute(panel.ForumsDelete))
r.Get("/delete/submit/{extra}", ConvertExtraRoute(panel.ForumsDeleteSubmit))
r.Get("/order/edit/submit", ConvertUserRoute(panel.ForumsOrderSubmit))
r.Get("/edit/{extra}", ConvertExtraRoute(panel.ForumsEdit))
r.Get("/edit/submit/{extra}", ConvertExtraRoute(panel.ForumsEditSubmit))
r.Get("/edit/perms/submit/{extra}", ConvertExtraRoute(panel.ForumsEditPermsSubmit))
r.Get("/edit/perms/{extra}", ConvertExtraRoute(panel.ForumsEditPermsAdvance))
r.Get("/edit/perms/adv/submit/{extra}", ConvertExtraRoute(panel.ForumsEditPermsAdvanceSubmit))
r.Get("/action/create/submit/{extra}", ConvertExtraRoute(panel.ForumsEditActionCreateSubmit))
r.Get("/action/delete/submit/{extra}", ConvertExtraRoute(panel.ForumsEditActionDeleteSubmit))
})
r.Route("/settings", func(r chi.Router) {
r.Get("/", ConvertUserRoute(panel.Settings))
r.Get("/edit/{extra}", ConvertExtraRoute(panel.SettingEdit))
r.Get("/edit/submit/{extra}", ConvertExtraRoute(panel.SettingEditSubmit))
r.Get("/word-filters", ConvertUserRoute(panel.WordFilters))
r.Get("/word-filters/create", ConvertUserRoute(panel.WordFiltersCreateSubmit))
r.Get("/word-filters/edit/{extra}", ConvertExtraRoute(panel.WordFiltersEdit))
r.Get("/word-filters/edit/submit/{extra}", ConvertExtraRoute(panel.WordFiltersEdit))
r.Get("/word-filters/delete/submit/{extra}", ConvertExtraRoute(panel.WordFiltersDeleteSubmit))
})
r.Route("/pages", func(r chi.Router) {
r.Get("/create/submit", ConvertUserRoute(panel.PagesCreateSubmit))
r.Get("/edit/{extra}", ConvertExtraRoute(panel.PagesEdit))
r.Get("/edit/submit/{extra}", ConvertExtraRoute(panel.PagesEditSubmit))
r.Get("/delete/submit/{extra}", ConvertExtraRoute(panel.PagesDeleteSubmit))
})
r.Route("/themes", func(r chi.Router) {
r.Get("/", ConvertUserRoute(panel.Themes))
r.Get("/default/{extra}", ConvertExtraRoute(panel.ThemesSetDefault))
r.Get("/menus", ConvertUserRoute(panel.ThemesMenus))
r.Get("/menus/edit/{extra}", ConvertExtraRoute(panel.ThemesMenusEdit))
r.Get("/menus/item/edit/{extra}", ConvertExtraRoute(panel.ThemesMenuItemEdit))
r.Get("/menus/item/edit/submit/{extra}", ConvertExtraRoute(panel.ThemesMenuItemEditSubmit))
r.Get("/menus/item/create/submit", ConvertUserRoute(panel.ThemesMenuItemCreateSubmit))
r.Get("/menus/item/delete/submit/{extra}", ConvertExtraRoute(panel.ThemesMenuItemDeleteSubmit))
r.Get("/menus/item/order/edit/submit/{extra}", ConvertExtraRoute(panel.ThemesMenuItemOrderSubmit))
r.Get("/widgets/edit/submit/{extra}", ConvertExtraRoute(panel.ThemesWidgetsEditSubmit))
r.Get("/widgets/create/submit", ConvertUserRoute(panel.ThemesWidgetsCreateSubmit))
r.Get("/widgets/delete/submit/{extra}", ConvertExtraRoute(panel.ThemesWidgetsDeleteSubmit))
})
r.Route("/plugins", func(r chi.Router) {
r.Get("/{extra}", ConvertExtraRoute(panel.PluginsInstall))
r.Get("/activate/{extra}", ConvertExtraRoute(panel.PluginsActivate))
r.Get("/deactivate/{extra}", ConvertExtraRoute(panel.PluginsDeactivate))
r.Get("/install/{extra}", ConvertExtraRoute(panel.PluginsInstall))
})
})
r.Route("/user", func(r chi.Router) {
r.Use(MemberOnlyMiddleware)
r.Route("/edit", func(r chi.Router) {
r.Get("/", ConvertHeaderRoute(routes.AccountEdit))
r.Get("/password", ConvertHeaderRoute(routes.AccountEditPassword))
r.Get("/password/submit", ConvertUserRoute(routes.AccountEditPasswordSubmit))
r.Get("/avatar/submit", ConvertUserRoute(routes.AccountEditAvatarSubmit))
r.Get("/avatar/revoke/submit", ConvertUserRoute(routes.AccountEditRevokeAvatarSubmit))
r.Get("/username/submit", ConvertUserRoute(routes.AccountEditUsernameSubmit))
r.Get("/privacy", ConvertHeaderRoute(routes.AccountEditPrivacy))
r.Get("/privacy/submit", ConvertUserRoute(routes.AccountEditPrivacySubmit))
r.Get("/mfa", ConvertHeaderRoute(routes.AccountEditMFA))
r.Get("/mfa/setup", ConvertHeaderRoute(routes.AccountEditMFASetup))
r.Get("/mfa/setup/submit", ConvertUserRoute(routes.AccountEditMFASetupSubmit))
r.Get("/mfa/disable/submit", ConvertUserRoute(routes.AccountEditMFADisableSubmit))
r.Get("/email", ConvertHeaderRoute(routes.AccountEditEmail))
r.Get("/token", ConvertExtraRoute(routes.AccountEditEmailTokenSubmit))
r.Get("/logins", ConvertHeaderRoute(routes.AccountLogins))
r.Get("/blocked", ConvertHeaderRoute(routes.AccountBlocked))
})
r.Get("/levels", ConvertHeaderRoute(routes.LevelList))
r.Get("/convos", ConvertHeaderRoute(routes.Convos))
r.Get("/convos/create", ConvertHeaderRoute(routes.ConvosCreate))
r.Get("/convo", ConvertHeaderExtraRoute(routes.Convo))
r.Get("/convos/create/submit", ConvertUserRoute(routes.ConvosCreateSubmit))
r.Get("/convo/create/submit", ConvertUserRoute(routes.ConvosCreateSubmit))
r.Get("/convo/delete/submit", ConvertExtraRoute(routes.ConvosDeleteReplySubmit))
r.Get("/convo/edit/submit", ConvertExtraRoute(routes.ConvosEditReplySubmit))
r.Get("/block/create", ConvertHeaderExtraRoute(routes.RelationsBlockCreate))
r.Get("/block/create/submit", ConvertExtraRoute(routes.RelationsBlockCreateSubmit))
r.Get("/block/remove", ConvertHeaderExtraRoute(routes.RelationsBlockRemove))
r.Get("/block/remove/submit", ConvertExtraRoute(routes.RelationsBlockRemoveSubmit))
r.Get("/{user}", ConvertHeaderRoute(routes.ViewProfile))
})
r.Route("/users", func(r chi.Router) {
r.Use(NoSessionMismatchMiddleware)
r.Get("/ban/submit", ConvertExtraRoute(routes.BanUserSubmit))
r.Get("/unban", ConvertExtraRoute(routes.UnbanUser))
r.Get("/activate", ConvertExtraRoute(routes.ActivateUser))
r.Get("/ips", ConvertHeaderRoute(routes.IPSearch))
r.Get("/delete-posts/submit", ConvertExtraRoute(routes.DeletePostsSubmit))
})
r.Route("/topic", func(r chi.Router) {
r.Use(MemberOnlyMiddleware)
r.Use(NoSessionMismatchMiddleware)
r.Get("/create/submit", ConvertUserRoute(routes.CreateTopicSubmit))
r.Get("/edit/submit", ConvertExtraRoute(routes.EditTopicSubmit))
r.Get("/delete/submit", ConvertUserRoute(routes.DeleteTopicSubmit))
r.Get("/stick/submit", ConvertExtraRoute(routes.StickTopicSubmit))
r.Get("/unstick/submit", ConvertExtraRoute(routes.UnstickTopicSubmit))
r.Get("/lock/submit", ConvertUserRoute(routes.LockTopicSubmit))
r.Get("/unlock/submit", ConvertExtraRoute(routes.UnlockTopicSubmit))
r.Get("/move/submit", ConvertExtraRoute(routes.MoveTopicSubmit))
r.Get("/like/submit", ConvertExtraRoute(routes.LikeTopicSubmit))
r.Get("/unlike/submit", ConvertExtraRoute(routes.UnlikeTopicSubmit))
r.Get("/attach/add/submit", ConvertExtraRoute(routes.AddAttachToTopicSubmit))
r.Get("/attach/remove/submit", ConvertExtraRoute(routes.RemoveAttachFromTopicSubmit))
})
r.Route("/reply", func(r chi.Router) {
r.Use(MemberOnlyMiddleware)
r.Get("/create", ConvertUserRoute(routes.ProfileReplyCreateSubmit))
r.Get("/edit/submit", ConvertExtraRoute(routes.ReplyEditSubmit))
r.Get("/delete/submit", ConvertExtraRoute(routes.ReplyDeleteSubmit))
r.Get("/like/submit", ConvertExtraRoute(routes.ReplyLikeSubmit))
r.Get("/unlike/submit", ConvertExtraRoute(routes.ReplyUnlikeSubmit))
r.Get("/attach/add/submit", ConvertExtraRoute(routes.AddAttachToReplySubmit))
r.Get("/attach/remove/submit", ConvertExtraRoute(routes.RemoveAttachFromReplySubmit))
})
r.Route("/profile", func(r chi.Router) {
r.Use(MemberOnlyMiddleware)
r.Use(NoSessionMismatchMiddleware)
r.Get("/reply/create", ConvertUserRoute(routes.ProfileReplyCreateSubmit))
r.Get("/reply/edit/submit", ConvertExtraRoute(routes.ProfileReplyEditSubmit))
r.Get("/reply/edit/delete/submit", ConvertExtraRoute(routes.ProfileReplyDeleteSubmit))
})
r.Route("/poll", func(r chi.Router) {
r.Use(MemberOnlyMiddleware)
r.Use(NoSessionMismatchMiddleware)
r.Get("/poll/vote", ConvertExtraRoute(routes.PollVote))
r.Get("/poll/results", ConvertExtraRoute(routes.PollResults))
})
r.Route("/accounts", func(r chi.Router) {
r.Get("/login", ConvertHeaderRoute(routes.AccountLogin))
r.Get("/create", ConvertHeaderRoute(routes.AccountRegister))
r.Get("/logout", ConvertUserRoute(routes.AccountLogout))
r.Get("/login/submit", ConvertUserRoute(routes.AccountLoginSubmit))
r.Get("/mfa_verify", ConvertHeaderRoute(routes.AccountLoginMFAVerify))
r.Get("/mfa_verify/submit", ConvertUserRoute(routes.AccountLoginMFAVerifySubmit))
r.Get("/create/submit", ConvertUserRoute(routes.AccountRegisterSubmit))
r.Get("/password-reset", ConvertHeaderRoute(routes.AccountPasswordReset))
r.Get("/password-reset/submit", ConvertUserRoute(routes.AccountPasswordResetSubmit))
r.Get("/password-reset/token", ConvertHeaderRoute(routes.AccountPasswordResetToken))
r.Get("/password-reset/token/submit", ConvertUserRoute(routes.AccountPasswordResetTokenSubmit))
})
r.Route("/static", func(r chi.Router) {})
r.Route("/uploads", func(r chi.Router) {})
http.ListenAndServe(":3333", r)
}

View File

@ -1,115 +0,0 @@
package hookgen
import (
"bytes"
"log"
"os"
"text/template"
)
type HookVars struct {
Imports []string
Hooks []Hook
}
type Hook struct {
Name string
Params string
Params2 string
Ret string
Type string
Any bool
MultiHook bool
Skip bool
DefaultRet string
Pure string
}
func AddHooks(add func(name, params, ret, htype string, multiHook, skip bool, defaultRet, pure string)) {
vhookskip := func(name, params string) {
add(name, params, "(bool,RouteError)", "VhookSkippable_", false, true, "false,nil", "")
}
vhookskip("simple_forum_check_pre_perms", "w http.ResponseWriter,r *http.Request,u *User,fid *int,h *HeaderLite")
vhookskip("forum_check_pre_perms", "w http.ResponseWriter,r *http.Request,u *User,fid *int,h *Header")
vhookskip("router_after_filters", "w http.ResponseWriter,r *http.Request,prefix string")
vhookskip("router_pre_route", "w http.ResponseWriter,r *http.Request,u *User,prefix string")
vhookskip("route_forum_list_start", "w http.ResponseWriter,r *http.Request,u *User,h *Header")
vhookskip("route_topic_list_start", "w http.ResponseWriter,r *http.Request,u *User,h *Header")
vhookskip("route_attach_start", "w http.ResponseWriter,r *http.Request,u *User,fname string")
vhookskip("route_attach_post_get", "w http.ResponseWriter,r *http.Request,u *User,a *Attachment")
vhooknoret := func(name, params string) {
add(name, params, "", "Vhooks", false, false, "false,nil", "")
}
vhooknoret("router_end", "w http.ResponseWriter,r *http.Request,u *User,prefix string,extraData string")
vhooknoret("topic_reply_row_assign", "r *ReplyUser")
vhooknoret("counters_perf_tick_row", "low int64,high int64,avg int64")
//forums_frow_assign
//Hook(name string, data interface{}) interface{}
/*hook := func(name, params, ret, pure string) {
add(name,params,ret,"Hooks",true,false,ret,pure)
}*/
hooknoret := func(name, params string) {
add(name, params, "", "HooksNoRet", true, false, "", "")
}
hooknoret("forums_frow_assign", "f *Forum")
hookskip := func(name, params string) {
add(name, params, "(skip bool)", "HooksSkip", true, true, "", "")
}
//hookskip("forums_frow_assign","f *Forum")
hookskip("topic_create_frow_assign", "f *Forum")
hookss := func(name string) {
add(name, "d string", "string", "Sshooks", true, false, "", "d")
}
hookss("topic_ogdesc_assign")
}
func Write(hookVars HookVars) {
fileData := `// Code generated by Gosora's Hook Generator. DO NOT EDIT.
/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
package common
import ({{range .Imports}}
"{{.}}"{{end}}
)
{{range .Hooks}}
func H_{{.Name}}_hook(t *HookTable,{{.Params}}) {{.Ret}} { {{if .Any}}
{{if .MultiHook}}for _, hook := range t.{{.Type}}["{{.Name}}"] {
{{if .Skip}}if skip = hook({{.Params2}}); skip {
break
}{{else}}{{if .Pure}}{{.Pure}} = {{else if .Ret}}return {{end}}hook({{.Params2}}){{end}}
}{{else}}hook := t.{{.Type}}["{{.Name}}"]
if hook != nil {
{{if .Ret}}return {{end}}hook({{.Params2}})
} {{end}}{{end}}{{if .Pure}}
return {{.Pure}}{{else if .Ret}}
return {{.DefaultRet}}{{end}}
}{{end}}
`
tmpl := template.Must(template.New("hooks").Parse(fileData))
var b bytes.Buffer
if e := tmpl.Execute(&b, hookVars); e != nil {
log.Fatal(e)
}
err := writeFile("./common/gen_extend.go", b.String())
if err != nil {
log.Fatal(err)
}
}
func writeFile(name, body string) error {
f, e := os.Create(name)
if e != nil {
return e
}
if _, e = f.WriteString(body); e != nil {
return e
}
if e = f.Sync(); e != nil {
return e
}
return f.Close()
}

View File

@ -1,266 +0,0 @@
// Work in progress
package main
import (
"context"
"database/sql"
"encoding/json"
"errors"
"log"
"os"
"strconv"
c "git.tuxpa.in/a/gosora/common"
qgen "git.tuxpa.in/a/gosora/query_gen"
"gopkg.in/olivere/elastic.v6"
)
func main() {
log.Print("Loading the configuration data")
err := c.LoadConfig()
if err != nil {
log.Fatal(err)
}
log.Print("Processing configuration data")
err = c.ProcessConfig()
if err != nil {
log.Fatal(err)
}
if c.DbConfig.Adapter != "mysql" && c.DbConfig.Adapter != "" {
log.Fatal("Only MySQL is supported for upgrades right now, please wait for a newer build of the patcher")
}
err = prepMySQL()
if err != nil {
log.Fatal(err)
}
client, err := elastic.NewClient(elastic.SetErrorLog(log.New(os.Stdout, "ES ", log.LstdFlags)))
if err != nil {
log.Fatal(err)
}
_, _, err = client.Ping("http://127.0.0.1:9200").Do(context.Background())
if err != nil {
log.Fatal(err)
}
err = setupIndices(client)
if err != nil {
log.Fatal(err)
}
err = setupData(client)
if err != nil {
log.Fatal(err)
}
}
func prepMySQL() error {
return qgen.Builder.Init("mysql", map[string]string{
"host": c.DbConfig.Host,
"port": c.DbConfig.Port,
"name": c.DbConfig.Dbname,
"username": c.DbConfig.Username,
"password": c.DbConfig.Password,
"collation": "utf8mb4_general_ci",
})
}
type ESIndexBase struct {
Mappings ESIndexMappings `json:"mappings"`
}
type ESIndexMappings struct {
Doc ESIndexDoc `json:"_doc"`
}
type ESIndexDoc struct {
Properties map[string]map[string]string `json:"properties"`
}
type ESDocMap map[string]map[string]string
func (d ESDocMap) Add(column string, cType string) {
d["column"] = map[string]string{"type": cType}
}
func setupIndices(client *elastic.Client) error {
exists, err := client.IndexExists("topics").Do(context.Background())
if err != nil {
return err
}
if exists {
deleteIndex, err := client.DeleteIndex("topics").Do(context.Background())
if err != nil {
return err
}
if !deleteIndex.Acknowledged {
return errors.New("delete not acknowledged")
}
}
docMap := make(ESDocMap)
docMap.Add("tid", "integer")
docMap.Add("title", "text")
docMap.Add("content", "text")
docMap.Add("createdBy", "integer")
docMap.Add("ip", "ip")
docMap.Add("suggest", "completion")
indexBase := ESIndexBase{ESIndexMappings{ESIndexDoc{docMap}}}
oBytes, err := json.Marshal(indexBase)
if err != nil {
return err
}
createIndex, err := client.CreateIndex("topics").Body(string(oBytes)).Do(context.Background())
if err != nil {
return err
}
if !createIndex.Acknowledged {
return errors.New("not acknowledged")
}
exists, err = client.IndexExists("replies").Do(context.Background())
if err != nil {
return err
}
if exists {
deleteIndex, err := client.DeleteIndex("replies").Do(context.Background())
if err != nil {
return err
}
if !deleteIndex.Acknowledged {
return errors.New("delete not acknowledged")
}
}
docMap = make(ESDocMap)
docMap.Add("rid", "integer")
docMap.Add("tid", "integer")
docMap.Add("content", "text")
docMap.Add("createdBy", "integer")
docMap.Add("ip", "ip")
docMap.Add("suggest", "completion")
indexBase = ESIndexBase{ESIndexMappings{ESIndexDoc{docMap}}}
oBytes, err = json.Marshal(indexBase)
if err != nil {
return err
}
createIndex, err = client.CreateIndex("replies").Body(string(oBytes)).Do(context.Background())
if err != nil {
return err
}
if !createIndex.Acknowledged {
return errors.New("not acknowledged")
}
return nil
}
type ESTopic struct {
ID int `json:"tid"`
Title string `json:"title"`
Content string `json:"content"`
CreatedBy int `json:"createdBy"`
IP string `json:"ip"`
}
type ESReply struct {
ID int `json:"rid"`
TID int `json:"tid"`
Content string `json:"content"`
CreatedBy int `json:"createdBy"`
IP string `json:"ip"`
}
func setupData(client *elastic.Client) error {
tcount := 4
errs := make(chan error)
go func() {
tin := make([]chan ESTopic, tcount)
tf := func(tin chan ESTopic) {
for {
topic, more := <-tin
if !more {
break
}
_, err := client.Index().Index("topics").Type("_doc").Id(strconv.Itoa(topic.ID)).BodyJson(topic).Do(context.Background())
if err != nil {
errs <- err
}
}
}
for i := 0; i < 4; i++ {
go tf(tin[i])
}
oi := 0
err := qgen.NewAcc().Select("topics").Cols("tid,title,content,createdBy,ip").Each(func(rows *sql.Rows) error {
t := ESTopic{}
err := rows.Scan(&t.ID, &t.Title, &t.Content, &t.CreatedBy, &t.IP)
if err != nil {
return err
}
tin[oi] <- t
if oi < 3 {
oi++
}
return nil
})
for i := 0; i < 4; i++ {
close(tin[i])
}
errs <- err
}()
go func() {
rin := make([]chan ESReply, tcount)
rf := func(rin chan ESReply) {
for {
reply, more := <-rin
if !more {
break
}
_, err := client.Index().Index("replies").Type("_doc").Id(strconv.Itoa(reply.ID)).BodyJson(reply).Do(context.Background())
if err != nil {
errs <- err
}
}
}
for i := 0; i < 4; i++ {
rf(rin[i])
}
oi := 0
err := qgen.NewAcc().Select("replies").Cols("rid,tid,content,createdBy,ip").Each(func(rows *sql.Rows) error {
r := ESReply{}
err := rows.Scan(&r.ID, &r.TID, &r.Content, &r.CreatedBy, &r.IP)
if err != nil {
return err
}
rin[oi] <- r
if oi < 3 {
oi++
}
return nil
})
for i := 0; i < 4; i++ {
close(rin[i])
}
errs <- err
}()
fin := 0
for {
err := <-errs
if err == nil {
fin++
if fin == 2 {
return nil
}
} else {
return err
}
}
}

View File

@ -1,70 +0,0 @@
//go:build hookgen
// +build hookgen
package main // import "git.tuxpa.in/a/gosora/hook_gen"
import (
"fmt"
"log"
"runtime/debug"
"strings"
h "git.tuxpa.in/a/gosora/cmd/common_hook_gen"
c "git.tuxpa.in/a/gosora/common"
_ "git.tuxpa.in/a/gosora/extend"
)
// TODO: Make sure all the errors in this file propagate upwards properly
func main() {
// Capture panics instead of closing the window at a superhuman speed before the user can read the message on Windows
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
debug.PrintStack()
}
}()
hooks := make(map[string]int)
for _, pl := range c.Plugins {
if len(pl.Meta.Hooks) > 0 {
for _, hook := range pl.Meta.Hooks {
hooks[hook]++
}
continue
}
if pl.Init != nil {
if e := pl.Init(pl); e != nil {
log.Print("early plugin init err: ", e)
return
}
}
if pl.Hooks != nil {
log.Print("Hooks not nil for ", pl.UName)
for hook, _ := range pl.Hooks {
hooks[hook] += 1
}
}
}
log.Printf("hooks: %+v\n", hooks)
imports := []string{"net/http"}
hookVars := h.HookVars{imports, nil}
var params2sb strings.Builder
add := func(name, params, ret, htype string, multiHook, skip bool, defaultRet, pure string) {
first := true
for _, param := range strings.Split(params, ",") {
if !first {
params2sb.WriteRune(',')
}
pspl := strings.Split(strings.ReplaceAll(strings.TrimSpace(param), " ", " "), " ")
params2sb.WriteString(pspl[0])
first = false
}
hookVars.Hooks = append(hookVars.Hooks, h.Hook{name, params, params2sb.String(), ret, htype, hooks[name] > 0, multiHook, skip, defaultRet, pure})
params2sb.Reset()
}
h.AddHooks(add)
h.Write(hookVars)
log.Println("Successfully generated the hooks")
}

View File

@ -1,41 +0,0 @@
package main // import "git.tuxpa.in/a/gosora/hook_stub_gen"
import (
"fmt"
"log"
"runtime/debug"
"strings"
h "git.tuxpa.in/a/gosora/cmd/common_hook_gen"
)
// TODO: Make sure all the errors in this file propagate upwards properly
func main() {
// Capture panics instead of closing the window at a superhuman speed before the user can read the message on Windows
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
debug.PrintStack()
}
}()
imports := []string{"net/http"}
hookVars := h.HookVars{imports,nil}
add := func(name, params, ret, htype string, multiHook, skip bool, defaultRet, pure string) {
var params2 string
first := true
for _, param := range strings.Split(params,",") {
if !first {
params2 += ","
}
pspl := strings.Split(strings.ReplaceAll(strings.TrimSpace(param)," "," ")," ")
params2 += pspl[0]
first = false
}
hookVars.Hooks = append(hookVars.Hooks, h.Hook{name, params, params2, ret, htype, true, multiHook, skip, defaultRet,pure})
}
h.AddHooks(add)
h.Write(hookVars)
log.Println("Successfully generated the hooks")
}

View File

@ -1,321 +0,0 @@
/*
*
* Gosora Installer
* Copyright Azareal 2017 - 2019
*
*/
package main
import (
"bufio"
"errors"
"fmt"
"os"
"runtime/debug"
"strconv"
"strings"
"git.tuxpa.in/a/gosora/install"
)
var scanner *bufio.Scanner
var siteShortName string
var siteName string
var siteURL string
var serverPort string
var defaultAdapter = "mysql"
var defaultHost = "localhost"
var defaultUsername = "root"
var defaultDbname = "gosora"
var defaultSiteShortName = "SN"
var defaultSiteName = "Site Name"
var defaultsiteURL = "localhost"
var defaultServerPort = "80" // 8080's a good one, if you're testing and don't want it to clash with port 80
func main() {
// Capture panics instead of closing the window at a superhuman speed before the user can read the message on Windows
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
debug.PrintStack()
pressAnyKey()
return
}
}()
scanner = bufio.NewScanner(os.Stdin)
fmt.Println("Welcome to Gosora's Installer")
fmt.Println("We're going to take you through a few steps to help you get started :)")
adap, ok := handleDatabaseDetails()
if !ok {
err := scanner.Err()
if err != nil {
fmt.Println(err)
} else {
err = errors.New("Something went wrong!")
}
abortError(err)
return
}
if !getSiteDetails() {
err := scanner.Err()
if err != nil {
fmt.Println(err)
} else {
err = errors.New("Something went wrong!")
}
abortError(err)
return
}
err := adap.InitDatabase()
if err != nil {
abortError(err)
return
}
err = adap.TableDefs()
if err != nil {
abortError(err)
return
}
err = adap.CreateAdmin()
if err != nil {
abortError(err)
return
}
err = adap.InitialData()
if err != nil {
abortError(err)
return
}
configContents := []byte(`{
"Site": {
"ShortName":"` + siteShortName + `",
"Name":"` + siteName + `",
"URL":"` + siteURL + `",
"Port":"` + serverPort + `",
"EnableSsl":false,
"EnableEmails":false,
"HasProxy":false,
"Language": "english"
},
"Config": {
"SslPrivkey": "",
"SslFullchain": "",
"SMTPServer": "",
"SMTPUsername": "",
"SMTPPassword": "",
"SMTPPort": "25",
"MaxRequestSizeStr":"5MB",
"UserCache":"static",
"TopicCache":"static",
"ReplyCache":"static",
"UserCacheCapacity":180,
"TopicCacheCapacity":400,
"ReplyCacheCapacity":20,
"DefaultPath":"/topics/",
"DefaultGroup":3,
"ActivationGroup":5,
"StaffCSS":"staff_post",
"DefaultForum":2,
"MinifyTemplates":true,
"BuildSlugs":true,
"ServerCount":1,
"Noavatar":"https://api.adorable.io/avatars/{width}/{id}.png",
"ItemsPerPage":25
},
"Database": {
"Adapter": "` + adap.Name() + `",
"Host": "` + adap.DBHost() + `",
"Username": "` + adap.DBUsername() + `",
"Password": "` + adap.DBPassword() + `",
"Dbname": "` + adap.DBName() + `",
"Port": "` + adap.DBPort() + `",
"TestAdapter": "` + adap.Name() + `",
"TestHost": "",
"TestUsername": "",
"TestPassword": "",
"TestDbname": "",
"TestPort": ""
},
"Dev": {
"DebugMode":true,
"SuperDebug":false
}
}`)
fmt.Println("Opening the configuration file")
configFile, err := os.Create("./config/config.json")
if err != nil {
abortError(err)
return
}
fmt.Println("Writing to the configuration file...")
_, err = configFile.Write(configContents)
if err != nil {
abortError(err)
return
}
configFile.Sync()
configFile.Close()
fmt.Println("Finished writing to the configuration file")
fmt.Println("Yay, you have successfully installed Gosora!")
fmt.Println("Your name is Admin and you can login with the password 'password'. Don't forget to change it! Seriously. It's really insecure.")
pressAnyKey()
}
func abortError(err error) {
fmt.Println(err)
fmt.Println("Aborting installation...")
pressAnyKey()
}
func handleDatabaseDetails() (adap install.InstallAdapter, ok bool) {
var dbAdapter string
var dbHost string
var dbUsername string
var dbPassword string
var dbName string
// TODO: Let the admin set the database port?
//var dbPort string
for {
fmt.Println("Which database adapter do you wish to use? mysql or mssql? Default: mysql")
if !scanner.Scan() {
return nil, false
}
dbAdapter := strings.TrimSpace(scanner.Text())
if dbAdapter == "" {
dbAdapter = defaultAdapter
}
adap, ok = install.Lookup(dbAdapter)
if ok {
break
}
fmt.Println("That adapter doesn't exist")
}
fmt.Println("Set database adapter to " + dbAdapter)
fmt.Println("Database Host? Default: " + defaultHost)
if !scanner.Scan() {
return nil, false
}
dbHost = scanner.Text()
if dbHost == "" {
dbHost = defaultHost
}
fmt.Println("Set database host to " + dbHost)
fmt.Println("Database Username? Default: " + defaultUsername)
if !scanner.Scan() {
return nil, false
}
dbUsername = scanner.Text()
if dbUsername == "" {
dbUsername = defaultUsername
}
fmt.Println("Set database username to " + dbUsername)
fmt.Println("Database Password? Default: ''")
if !scanner.Scan() {
return nil, false
}
dbPassword = scanner.Text()
if len(dbPassword) == 0 {
fmt.Println("You didn't set a password for this user. This won't block the installation process, but it might create security issues in the future.")
fmt.Println("")
} else {
fmt.Println("Set password to " + obfuscatePassword(dbPassword))
}
fmt.Println("Database Name? Pick a name you like or one provided to you. Default: " + defaultDbname)
if !scanner.Scan() {
return nil, false
}
dbName = scanner.Text()
if dbName == "" {
dbName = defaultDbname
}
fmt.Println("Set database name to " + dbName)
adap.SetConfig(dbHost, dbUsername, dbPassword, dbName, adap.DefaultPort())
return adap, true
}
func getSiteDetails() bool {
fmt.Println("Okay. We also need to know some actual information about your site!")
fmt.Println("What's your site's name? Default: " + defaultSiteName)
if !scanner.Scan() {
return false
}
siteName = scanner.Text()
if siteName == "" {
siteName = defaultSiteName
}
fmt.Println("Set the site name to " + siteName)
// ? - We could compute this based on the first letter of each word in the site's name, if it's name spans multiple words. I'm not sure how to do this for single word names.
fmt.Println("Can we have a short abbreviation for your site? Default: " + defaultSiteShortName)
if !scanner.Scan() {
return false
}
siteShortName = scanner.Text()
if siteShortName == "" {
siteShortName = defaultSiteShortName
}
fmt.Println("Set the short name to " + siteShortName)
fmt.Println("What's your site's url? Default: " + defaultsiteURL)
if !scanner.Scan() {
return false
}
siteURL = scanner.Text()
if siteURL == "" {
siteURL = defaultsiteURL
}
fmt.Println("Set the site url to " + siteURL)
fmt.Println("What port do you want the server to listen on? If you don't know what this means, you should probably leave it on the default. Default: " + defaultServerPort)
if !scanner.Scan() {
return false
}
serverPort = scanner.Text()
if serverPort == "" {
serverPort = defaultServerPort
}
_, err := strconv.Atoi(serverPort)
if err != nil {
fmt.Println("That's not a valid number!")
return false
}
fmt.Println("Set the server port to " + serverPort)
return true
}
func obfuscatePassword(password string) (out string) {
for i := 0; i < len(password); i++ {
out += "*"
}
return out
}
func pressAnyKey() {
//fmt.Println("Press any key to exit...")
fmt.Println("Please press enter to exit...")
for scanner.Scan() {
_ = scanner.Text()
return
}
}

View File

@ -1,408 +0,0 @@
/* WIP Under Construction */
package main // import "git.tuxpa.in/a/gosora/query_gen"
import (
"encoding/json"
"fmt"
"log"
"os"
"runtime/debug"
"strconv"
"strings"
c "git.tuxpa.in/a/gosora/common"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
// TODO: Make sure all the errors in this file propagate upwards properly
func main() {
// Capture panics instead of closing the window at a superhuman speed before the user can read the message on Windows
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
debug.PrintStack()
return
}
}()
log.Println("Running the query generator")
for _, a := range qgen.Registry {
log.Printf("Building the queries for the %s adapter", a.GetName())
qgen.Install.SetAdapterInstance(a)
qgen.Install.AddPlugins(NewPrimaryKeySpitter()) // TODO: Do we really need to fill the spitter for every adapter?
e := writeStatements(a)
if e != nil {
log.Print(e)
}
e = qgen.Install.Write()
if e != nil {
log.Print(e)
}
e = a.Write()
if e != nil {
log.Print(e)
}
}
}
// nolint
func writeStatements(a qgen.Adapter) (err error) {
e := func(f func(qgen.Adapter) error) {
if err != nil {
return
}
err = f(a)
}
e(createTables)
e(seedTables)
e(writeSelects)
e(writeLeftJoins)
e(writeInnerJoins)
e(writeInserts)
e(writeUpdates)
e(writeDeletes)
e(writeSimpleCounts)
e(writeInsertSelects)
e(writeInsertLeftJoins)
e(writeInsertInnerJoins)
return err
}
type si = map[string]interface{}
type tK = tblKey
func seedTables(a qgen.Adapter) error {
qgen.Install.AddIndex("topics", "parentID", "parentID")
qgen.Install.AddIndex("replies", "tid", "tid")
qgen.Install.AddIndex("polls", "parentID", "parentID")
qgen.Install.AddIndex("likes", "targetItem", "targetItem")
qgen.Install.AddIndex("emails", "uid", "uid")
qgen.Install.AddIndex("attachments", "originID", "originID")
qgen.Install.AddIndex("attachments", "path", "path")
qgen.Install.AddIndex("activity_stream_matches", "watcher", "watcher")
// TODO: Remove these keys to save space when Elasticsearch is active?
//qgen.Install.AddKey("topics", "title", tK{"title", "fulltext", "", false})
//qgen.Install.AddKey("topics", "content", tK{"content", "fulltext", "", false})
//qgen.Install.AddKey("topics", "title,content", tK{"title,content", "fulltext", "", false})
//qgen.Install.AddKey("replies", "content", tK{"content", "fulltext", "", false})
insert := func(tbl, cols, vals string) {
qgen.Install.SimpleInsert(tbl, cols, vals)
}
insert("sync", "last_update", "UTC_TIMESTAMP()")
addSetting := func(name, content, stype string, constraints ...string) {
if strings.Contains(name, "'") {
panic("name contains '")
}
if strings.Contains(stype, "'") {
panic("stype contains '")
}
// TODO: Add more field validators
cols := "name,content,type"
if len(constraints) > 0 {
cols += ",constraints"
}
q := func(s string) string {
return "'" + s + "'"
}
c := func() string {
if len(constraints) == 0 {
return ""
}
return "," + q(constraints[0])
}
insert("settings", cols, q(name)+","+q(content)+","+q(stype)+c())
}
addSetting("activation_type", "1", "list", "1-3")
addSetting("bigpost_min_words", "250", "int")
addSetting("megapost_min_words", "1000", "int")
addSetting("meta_desc", "", "html-attribute")
addSetting("rapid_loading", "1", "bool")
addSetting("google_site_verify", "", "html-attribute")
addSetting("avatar_visibility", "0", "list", "0-1")
insert("themes", "uname, default", "'cosora',1")
insert("emails", "email, uid, validated", "'admin@localhost',1,1") // ? - Use a different default email or let the admin input it during installation?
/*
The Permissions:
Global Permissions:
BanUsers
ActivateUsers
EditUser
EditUserEmail
EditUserPassword
EditUserGroup
EditUserGroupSuperMod
EditUserGroupAdmin
EditGroup
EditGroupLocalPerms
EditGroupGlobalPerms
EditGroupSuperMod
EditGroupAdmin
ManageForums
EditSettings
ManageThemes
ManagePlugins
ViewAdminLogs
ViewIPs
Non-staff Global Permissions:
UploadFiles
UploadAvatars
UseConvos
UseConvosOnlyWithMod
CreateProfileReply
AutoEmbed
AutoLink
// CreateConvo ?
// CreateConvoReply ?
Forum Permissions:
ViewTopic
LikeItem
CreateTopic
EditTopic
DeleteTopic
CreateReply
EditReply
DeleteReply
PinTopic
CloseTopic
MoveTopic
*/
p := func(perms *c.Perms) string {
jBytes, err := json.Marshal(perms)
if err != nil {
panic(err)
}
return string(jBytes)
}
addGroup := func(name string, perms c.Perms, mod, admin, banned bool, tag string) {
mi, ai, bi := "0", "0", "0"
if mod {
mi = "1"
}
if admin {
ai = "1"
}
if banned {
bi = "1"
}
insert("users_groups", "name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag", `'`+name+`','`+p(&perms)+`','{}',`+mi+`,`+ai+`,`+bi+`,"`+tag+`"`)
}
perms := c.AllPerms
perms.EditUserGroupAdmin = false
perms.EditGroupAdmin = false
addGroup("Administrator", perms, true, true, false, "Admin")
perms = c.Perms{BanUsers: true, ActivateUsers: true, EditUser: true, EditUserEmail: false, EditUserGroup: true, ViewIPs: true, UploadFiles: true, UploadAvatars: true, UseConvos: true, UseConvosOnlyWithMod: true, CreateProfileReply: true, AutoEmbed: true, AutoLink: true, ViewTopic: true, LikeItem: true, CreateTopic: true, EditTopic: true, DeleteTopic: true, CreateReply: true, EditReply: true, DeleteReply: true, PinTopic: true, CloseTopic: true, MoveTopic: true}
addGroup("Moderator", perms, true, false, false, "Mod")
perms = c.Perms{UploadFiles: true, UploadAvatars: true, UseConvos: true, UseConvosOnlyWithMod: true, CreateProfileReply: true, AutoEmbed: true, AutoLink: true, ViewTopic: true, LikeItem: true, CreateTopic: true, CreateReply: true}
addGroup("Member", perms, false, false, false, "")
perms = c.Perms{ViewTopic: true}
addGroup("Banned", perms, false, false, true, "")
addGroup("Awaiting Activation", c.Perms{ViewTopic: true, UseConvosOnlyWithMod: true}, false, false, false, "")
addGroup("Not Loggedin", perms, false, false, false, "Guest")
//
// TODO: Stop processFields() from stripping the spaces in the descriptions in the next commit
insert("forums", "name, active, desc, tmpl", "'Reports',0,'All the reports go here',''")
insert("forums", "name, lastTopicID, lastReplyerID, desc, tmpl", "'General',1,1,'A place for general discussions which don't fit elsewhere',''")
//
/*var addForumPerm = func(gid, fid int, permStr string) {
insert("forums_permissions", "gid, fid, permissions", strconv.Itoa(gid)+`,`+strconv.Itoa(fid)+`,'`+permStr+`'`)
}*/
insert("forums_permissions", "gid, fid, permissions", `1,1,'{"ViewTopic":true,"CreateReply":true,"CreateTopic":true,"PinTopic":true,"CloseTopic":true}'`)
insert("forums_permissions", "gid, fid, permissions", `2,1,'{"ViewTopic":true,"CreateReply":true,"CloseTopic":true}'`)
insert("forums_permissions", "gid, fid, permissions", "3,1,'{}'")
insert("forums_permissions", "gid, fid, permissions", "4,1,'{}'")
insert("forums_permissions", "gid, fid, permissions", "5,1,'{}'")
insert("forums_permissions", "gid, fid, permissions", "6,1,'{}'")
//
insert("forums_permissions", "gid, fid, permissions", `1,2,'{"ViewTopic":true,"CreateReply":true,"CreateTopic":true,"LikeItem":true,"EditTopic":true,"DeleteTopic":true,"EditReply":true,"DeleteReply":true,"PinTopic":true,"CloseTopic":true,"MoveTopic":true}'`)
insert("forums_permissions", "gid, fid, permissions", `2,2,'{"ViewTopic":true,"CreateReply":true,"CreateTopic":true,"LikeItem":true,"EditTopic":true,"DeleteTopic":true,"EditReply":true,"DeleteReply":true,"PinTopic":true,"CloseTopic":true,"MoveTopic":true}'`)
insert("forums_permissions", "gid, fid, permissions", `3,2,'{"ViewTopic":true,"CreateReply":true,"CreateTopic":true,"LikeItem":true}'`)
insert("forums_permissions", "gid, fid, permissions", `4,2,'{"ViewTopic":true}'`)
insert("forums_permissions", "gid, fid, permissions", `5,2,'{"ViewTopic":true}'`)
insert("forums_permissions", "gid, fid, permissions", `6,2,'{"ViewTopic":true}'`)
//
insert("topics", "title, content, parsed_content, createdAt, lastReplyAt, lastReplyBy, createdBy, parentID, ip", "'Test Topic','A topic automatically generated by the software.','A topic automatically generated by the software.',UTC_TIMESTAMP(),UTC_TIMESTAMP(),1,1,2,''")
insert("replies", "tid, content, parsed_content, createdAt, createdBy, lastUpdated, lastEdit, lastEditBy, ip", "1,'A reply!','A reply!',UTC_TIMESTAMP(),1,UTC_TIMESTAMP(),0,0,''")
insert("menus", "", "")
// Go maps have a random iteration order, so we have to do this, otherwise the schema files will become unstable and harder to audit
order := 0
mOrder := "mid, name, htmlID, cssClass, position, path, aria, tooltip, guestOnly, memberOnly, staffOnly, adminOnly"
addMenuItem := func(data map[string]interface{}) {
if data["mid"] == nil {
data["mid"] = 1
}
if data["position"] == nil {
data["position"] = "left"
}
cols, values := qgen.InterfaceMapToInsertStrings(data, mOrder)
insert("menu_items", cols+", order", values+","+strconv.Itoa(order))
order++
}
addMenuItem(si{"name": "{lang.menu_forums}", "htmlID": "menu_forums", "path": "/forums/", "aria": "{lang.menu_forums_aria}", "tooltip": "{lang.menu_forums_tooltip}"})
addMenuItem(si{"name": "{lang.menu_topics}", "htmlID": "menu_topics", "cssClass": "menu_topics", "path": "/topics/", "aria": "{lang.menu_topics_aria}", "tooltip": "{lang.menu_topics_tooltip}"})
addMenuItem(si{"htmlID": "general_alerts", "cssClass": "menu_alerts", "position": "right", "tmplName": "menu_alerts"})
addMenuItem(si{"name": "{lang.menu_account}", "cssClass": "menu_account", "path": "/user/edit/", "aria": "{lang.menu_account_aria}", "tooltip": "{lang.menu_account_tooltip}", "memberOnly": true})
addMenuItem(si{"name": "{lang.menu_profile}", "cssClass": "menu_profile", "path": "{me.Link}", "aria": "{lang.menu_profile_aria}", "tooltip": "{lang.menu_profile_tooltip}", "memberOnly": true})
addMenuItem(si{"name": "{lang.menu_panel}", "cssClass": "menu_panel menu_account", "path": "/panel/", "aria": "{lang.menu_panel_aria}", "tooltip": "{lang.menu_panel_tooltip}", "memberOnly": true, "staffOnly": true})
addMenuItem(si{"name": "{lang.menu_logout}", "cssClass": "menu_logout", "path": "/accounts/logout/?s={me.Session}", "aria": "{lang.menu_logout_aria}", "tooltip": "{lang.menu_logout_tooltip}", "memberOnly": true})
addMenuItem(si{"name": "{lang.menu_register}", "cssClass": "menu_register", "path": "/accounts/create/", "aria": "{lang.menu_register_aria}", "tooltip": "{lang.menu_register_tooltip}", "guestOnly": true})
addMenuItem(si{"name": "{lang.menu_login}", "cssClass": "menu_login", "path": "/accounts/login/", "aria": "{lang.menu_login_aria}", "tooltip": "{lang.menu_login_tooltip}", "guestOnly": true})
/*var fSet []string
for _, table := range tables {
fSet = append(fSet, "'"+table+"'")
}
qgen.Install.SimpleBulkInsert("tables", "name", fSet)*/
return nil
}
// ? - What is this for?
/*func copyInsertMap(in map[string]interface{}) (out map[string]interface{}) {
out = make(map[string]interface{})
for col, value := range in {
out[col] = value
}
return out
}*/
type LitStr string
func writeSelects(a qgen.Adapter) error {
b := a.Builder()
// Looking for getTopic? Your statement is in another castle
//b.Select("isPluginInstalled").Table("plugins").Columns("installed").Where("uname = ?").Parse()
b.Select("forumEntryExists").Table("forums").Columns("fid").Where("name = ''").Orderby("fid ASC").Limit("0,1").Parse()
b.Select("groupEntryExists").Table("users_groups").Columns("gid").Where("name = ''").Orderby("gid ASC").Limit("0,1").Parse()
return nil
}
func writeLeftJoins(a qgen.Adapter) error {
a.SimpleLeftJoin("getForumTopics", "topics", "users", "topics.tid, topics.title, topics.content, topics.createdBy, topics.is_closed, topics.sticky, topics.createdAt, topics.lastReplyAt, topics.parentID, users.name, users.avatar", "topics.createdBy = users.uid", "topics.parentID = ?", "topics.sticky DESC, topics.lastReplyAt DESC, topics.createdBy desc", "")
return nil
}
func writeInnerJoins(a qgen.Adapter) (err error) {
return nil
}
func writeInserts(a qgen.Adapter) error {
b := a.Builder()
b.Insert("addForumPermsToForum").Table("forums_permissions").Columns("gid,fid,preset,permissions").Fields("?,?,?,?").Parse()
return nil
}
func writeUpdates(a qgen.Adapter) error {
b := a.Builder()
b.Update("updateEmail").Table("emails").Set("email = ?, uid = ?, validated = ?, token = ?").Where("email = ?").Parse()
b.Update("setTempGroup").Table("users").Set("temp_group = ?").Where("uid = ?").Parse()
b.Update("bumpSync").Table("sync").Set("last_update = UTC_TIMESTAMP()").Parse()
return nil
}
func writeDeletes(a qgen.Adapter) error {
b := a.Builder()
//b.Delete("deleteForumPermsByForum").Table("forums_permissions").Where("fid=?").Parse()
b.Delete("deleteActivityStreamMatch").Table("activity_stream_matches").Where("watcher=? AND asid=?").Parse()
//b.Delete("deleteActivityStreamMatchesByWatcher").Table("activity_stream_matches").Where("watcher=?").Parse()
return nil
}
func writeSimpleCounts(a qgen.Adapter) error {
return nil
}
func writeInsertSelects(a qgen.Adapter) error {
/*a.SimpleInsertSelect("addForumPermsToForumAdmins",
qgen.DB_Insert{"forums_permissions", "gid, fid, preset, permissions", ""},
qgen.DB_Select{"users_groups", "gid, ? AS fid, ? AS preset, ? AS permissions", "is_admin = 1", "", ""},
)*/
/*a.SimpleInsertSelect("addForumPermsToForumStaff",
qgen.DB_Insert{"forums_permissions", "gid, fid, preset, permissions", ""},
qgen.DB_Select{"users_groups", "gid, ? AS fid, ? AS preset, ? AS permissions", "is_admin = 0 AND is_mod = 1", "", ""},
)*/
/*a.SimpleInsertSelect("addForumPermsToForumMembers",
qgen.DB_Insert{"forums_permissions", "gid, fid, preset, permissions", ""},
qgen.DB_Select{"users_groups", "gid, ? AS fid, ? AS preset, ? AS permissions", "is_admin = 0 AND is_mod = 0 AND is_banned = 0", "", ""},
)*/
return nil
}
// nolint
func writeInsertLeftJoins(a qgen.Adapter) error {
return nil
}
func writeInsertInnerJoins(a qgen.Adapter) error {
return nil
}
func writeFile(name, content string) (err error) {
f, err := os.Create(name)
if err != nil {
return err
}
_, err = f.WriteString(content)
if err != nil {
return err
}
err = f.Sync()
if err != nil {
return err
}
return f.Close()
}

View File

@ -1,48 +0,0 @@
package main
import (
"strings"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
type PrimaryKeySpitter struct {
keys map[string]string
}
func NewPrimaryKeySpitter() *PrimaryKeySpitter {
return &PrimaryKeySpitter{make(map[string]string)}
}
func (spit *PrimaryKeySpitter) Hook(name string, args ...interface{}) error {
if name == "CreateTableStart" {
var found string
var table = args[0].(*qgen.DBInstallTable)
for _, key := range table.Keys {
if key.Type == "primary" {
expl := strings.Split(key.Columns, ",")
if len(expl) > 1 {
continue
}
found = key.Columns
}
if found != "" {
table := table.Name
spit.keys[table] = found
}
}
}
return nil
}
func (spit *PrimaryKeySpitter) Write() error {
out := `// Generated by Gosora's Query Generator. DO NOT EDIT.
package main
var dbTablePrimaryKeys = map[string]string{
`
for table, key := range spit.keys {
out += "\t\"" + table + "\":\"" + key + "\",\n"
}
return writeFile("./gen_tables.go", out+"}\n")
}

View File

@ -1,831 +0,0 @@
package main
import qgen "git.tuxpa.in/a/gosora/query_gen"
var mysqlPre = "utf8mb4"
var mysqlCol = "utf8mb4_general_ci"
var tables []string
type tblColumn = qgen.DBTableColumn
type tC = tblColumn
type tblKey = qgen.DBTableKey
func createTables(a qgen.Adapter) error {
tables = nil
f := func(table, charset, collation string, cols []tC, keys []tblKey) error {
tables = append(tables, table)
return qgen.Install.CreateTable(table, charset, collation, cols, keys)
}
return createTables2(a, f)
}
func createTables2(a qgen.Adapter, f func(table, charset, collation string, columns []tC, keys []tblKey) error) (err error) {
createTable := func(table, charset, collation string, cols []tC, keys []tblKey) {
if err != nil {
return
}
err = f(table, charset, collation, cols, keys)
}
bcol := func(col string, val bool) qgen.DBTableColumn {
if val {
return tC{col, "boolean", 0, false, false, "1"}
}
return tC{col, "boolean", 0, false, false, "0"}
}
ccol := func(col string, size int, sdefault string) qgen.DBTableColumn {
return tC{col, "varchar", size, false, false, sdefault}
}
text := func(params ...string) qgen.DBTableColumn {
if len(params) == 0 {
return tC{"", "text", 0, false, false, ""}
}
col, sdefault := params[0], ""
if len(params) > 1 {
sdefault = params[1]
if sdefault == "" {
sdefault = "''"
}
}
return tC{col, "text", 0, false, false, sdefault}
}
createdAt := func(coll ...string) qgen.DBTableColumn {
var col string
if len(coll) > 0 {
col = coll[0]
}
if col == "" {
col = "createdAt"
}
return tC{col, "createdAt", 0, false, false, ""}
}
createTable("users", mysqlPre, mysqlCol,
[]tC{
{"uid", "int", 0, false, true, ""},
ccol("name", 100, ""),
ccol("password", 100, ""),
ccol("salt", 80, "''"),
{"group", "int", 0, false, false, ""}, // TODO: Make this a foreign key
bcol("active", false),
bcol("is_super_admin", false),
createdAt(),
{"lastActiveAt", "datetime", 0, false, false, ""},
ccol("session", 200, "''"),
//ccol("authToken", 200, "''"),
ccol("last_ip", 200, "''"),
{"profile_comments", "int", 0, false, false, "0"},
{"who_can_convo", "int", 0, false, false, "0"},
{"enable_embeds", "int", 0, false, false, "-1"},
ccol("email", 200, "''"),
ccol("avatar", 100, "''"),
text("message"),
// TODO: Drop these columns?
ccol("url_prefix", 20, "''"),
ccol("url_name", 100, "''"),
//text("pub_key"),
{"level", "smallint", 0, false, false, "0"},
{"score", "int", 0, false, false, "0"},
{"posts", "int", 0, false, false, "0"},
{"bigposts", "int", 0, false, false, "0"},
{"megaposts", "int", 0, false, false, "0"},
{"topics", "int", 0, false, false, "0"},
{"liked", "int", 0, false, false, "0"},
// These two are to bound liked queries with little bits of information we know about the user to reduce the server load
{"oldestItemLikedCreatedAt", "datetime", 0, false, false, ""}, // For internal use only, semantics may change
{"lastLiked", "datetime", 0, false, false, ""}, // For internal use only, semantics may change
//{"penalty_count","int",0,false,false,"0"},
{"temp_group", "int", 0, false, false, "0"}, // For temporary groups, set this to zero when a temporary group isn't in effect
},
[]tK{
{"uid", "primary", "", false},
{"name", "unique", "", false},
},
)
createTable("users_groups", mysqlPre, mysqlCol,
[]tC{
{"gid", "int", 0, false, true, ""},
ccol("name", 100, ""),
text("permissions"),
text("plugin_perms"),
bcol("is_mod", false),
bcol("is_admin", false),
bcol("is_banned", false),
{"user_count", "int", 0, false, false, "0"}, // TODO: Implement this
ccol("tag", 50, "''"),
},
[]tK{
{"gid", "primary", "", false},
},
)
createTable("users_groups_promotions", mysqlPre, mysqlCol,
[]tC{
{"pid", "int", 0, false, true, ""},
{"from_gid", "int", 0, false, false, ""},
{"to_gid", "int", 0, false, false, ""},
bcol("two_way", false), // If a user no longer meets the requirements for this promotion then they will be demoted if this flag is set
// Requirements
{"level", "int", 0, false, false, ""},
{"posts", "int", 0, false, false, "0"},
{"minTime", "int", 0, false, false, ""}, // How long someone needs to have been in their current group before being promoted
{"registeredFor", "int", 0, false, false, "0"}, // minutes
},
[]tK{
{"pid", "primary", "", false},
},
)
/*
createTable("users_groups_promotions_scheduled","","",
[]tC{
{"prid","int",0,false,false,""},
{"uid","int",0,false,false,""},
{"runAt","datetime",0,false,false,""},
},
[]tK{
// TODO: Test to see that the compound primary key works
{"prid,uid", "primary", "", false},
},
)
*/
createTable("users_2fa_keys", mysqlPre, mysqlCol,
[]tC{
{"uid", "int", 0, false, false, ""},
ccol("secret", 100, ""),
ccol("scratch1", 50, ""),
ccol("scratch2", 50, ""),
ccol("scratch3", 50, ""),
ccol("scratch4", 50, ""),
ccol("scratch5", 50, ""),
ccol("scratch6", 50, ""),
ccol("scratch7", 50, ""),
ccol("scratch8", 50, ""),
{"createdAt", "createdAt", 0, false, false, ""},
},
[]tK{
{"uid", "primary", "", false},
},
)
// What should we do about global penalties? Put them on the users table for speed? Or keep them here?
// Should we add IP Penalties? No, that's a stupid idea, just implement IP Bans properly. What about shadowbans?
// TODO: Perm overrides
// TODO: Add a mod-queue and other basic auto-mod features. This is needed for awaiting activation and the mod_queue penalty flag
// TODO: Add a penalty type where a user is stopped from creating plugin_guilds social groups
// TODO: Shadow bans. We will probably have a CanShadowBan permission for this, as we *really* don't want people using this lightly.
/*createTable("users_penalties","","",
[]tC{
{"uid","int",0,false,false,""},
{"element_id","int",0,false,false,""},
ccol("element_type",50,""), //forum, profile?, and social_group. Leave blank for global.
text("overrides","{}"),
bcol("mod_queue",false),
bcol("shadow_ban",false),
bcol("no_avatar",false), // Coming Soon. Should this be a perm override instead?
// Do we *really* need rate-limit penalty types? Are we going to be allowing bots or something?
//{"posts_per_hour","int",0,false,false,"0"},
//{"topics_per_hour","int",0,false,false,"0"},
//{"posts_count","int",0,false,false,"0"},
//{"topic_count","int",0,false,false,"0"},
//{"last_hour","int",0,false,false,"0"}, // UNIX Time, as we don't need to do anything too fancy here. When an hour has elapsed since that time, reset the hourly penalty counters.
{"issued_by","int",0,false,false,""},
createdAt("issued_at"),
{"expires_at","datetime",0,false,false,""},
}, nil,
)*/
createTable("users_groups_scheduler", "", "",
[]tC{
{"uid", "int", 0, false, false, ""},
{"set_group", "int", 0, false, false, ""},
{"issued_by", "int", 0, false, false, ""},
createdAt("issued_at"),
{"revert_at", "datetime", 0, false, false, ""},
{"temporary", "boolean", 0, false, false, ""}, // special case for permanent bans to do the necessary bookkeeping, might be removed in the future
},
[]tK{
{"uid", "primary", "", false},
},
)
// TODO: Can we use a piece of software dedicated to persistent queues for this rather than relying on the database for it?
createTable("users_avatar_queue", "", "",
[]tC{
{"uid", "int", 0, false, false, ""}, // TODO: Make this a foreign key
},
[]tK{
{"uid", "primary", "", false},
},
)
// TODO: Should we add a users prefix to this table to fit the "unofficial convention"?
// TODO: Add an autoincrement key?
createTable("emails", "", "",
[]tC{
ccol("email", 200, ""),
{"uid", "int", 0, false, false, ""}, // TODO: Make this a foreign key
bcol("validated", false),
ccol("token", 200, "''"),
}, nil,
)
// TODO: Allow for patterns in domains, if the bots try to shake things up there?
/*
createTable("email_domain_blacklist", "", "",
[]tC{
ccol("domain", 200, ""),
bcol("gtld", false),
},
[]tK{
{"domain", "primary"},
},
)
*/
// TODO: Implement password resets
createTable("password_resets", "", "",
[]tC{
ccol("email", 200, ""),
{"uid", "int", 0, false, false, ""}, // TODO: Make this a foreign key
ccol("validated", 200, ""), // Token given once the one-use token is consumed, used to prevent multiple people consuming the same one-use token
ccol("token", 200, ""),
createdAt(),
}, nil,
)
createTable("forums", mysqlPre, mysqlCol,
[]tC{
{"fid", "int", 0, false, true, ""},
ccol("name", 100, ""),
ccol("desc", 200, ""),
ccol("tmpl", 200, "''"),
bcol("active", true),
{"order", "int", 0, false, false, "0"},
{"topicCount", "int", 0, false, false, "0"},
ccol("preset", 100, "''"),
{"parentID", "int", 0, false, false, "0"},
ccol("parentType", 50, "''"),
{"lastTopicID", "int", 0, false, false, "0"},
{"lastReplyerID", "int", 0, false, false, "0"},
},
[]tK{
{"fid", "primary", "", false},
},
)
createTable("forums_permissions", "", "",
[]tC{
{"fid", "int", 0, false, false, ""},
{"gid", "int", 0, false, false, ""},
ccol("preset", 100, "''"),
text("permissions", "{}"),
},
[]tK{
// TODO: Test to see that the compound primary key works
{"fid,gid", "primary", "", false},
},
)
createTable("topics", mysqlPre, mysqlCol,
[]tC{
{"tid", "int", 0, false, true, ""},
ccol("title", 100, ""), // TODO: Increase the max length to 200?
text("content"),
text("parsed_content"),
createdAt(),
{"lastReplyAt", "datetime", 0, false, false, ""},
{"lastReplyBy", "int", 0, false, false, ""},
{"lastReplyID", "int", 0, false, false, "0"},
{"createdBy", "int", 0, false, false, ""}, // TODO: Make this a foreign key
bcol("is_closed", false),
bcol("sticky", false),
// TODO: Add an index for this
{"parentID", "int", 0, false, false, "2"},
ccol("ip", 200, "''"),
{"postCount", "int", 0, false, false, "1"},
{"likeCount", "int", 0, false, false, "0"},
{"attachCount", "int", 0, false, false, "0"},
{"words", "int", 0, false, false, "0"},
{"views", "int", 0, false, false, "0"},
//{"dayViews", "int", 0, false, false, "0"},
{"weekEvenViews", "int", 0, false, false, "0"},
{"weekOddViews", "int", 0, false, false, "0"},
///{"weekViews", "int", 0, false, false, "0"},
///{"lastWeekViews", "int", 0, false, false, "0"},
//{"monthViews", "int", 0, false, false, "0"},
// ? - A little hacky, maybe we could do something less likely to bite us with huge numbers of topics?
// TODO: Add an index for this?
//{"lastMonth", "datetime", 0, false, false, ""},
ccol("css_class", 100, "''"),
{"poll", "int", 0, false, false, "0"},
ccol("data", 200, "''"),
},
[]tK{
{"tid", "primary", "", false},
{"title", "fulltext", "", false},
{"content", "fulltext", "", false},
},
)
createTable("replies", mysqlPre, mysqlCol,
[]tC{
{"rid", "int", 0, false, true, ""}, // TODO: Rename to replyID?
{"tid", "int", 0, false, false, ""}, // TODO: Rename to topicID?
text("content"),
text("parsed_content"),
createdAt(),
{"createdBy", "int", 0, false, false, ""}, // TODO: Make this a foreign key
{"lastEdit", "int", 0, false, false, "0"},
{"lastEditBy", "int", 0, false, false, "0"},
{"lastUpdated", "datetime", 0, false, false, ""},
ccol("ip", 200, "''"),
{"likeCount", "int", 0, false, false, "0"},
{"attachCount", "int", 0, false, false, "0"},
{"words", "int", 0, false, false, "1"}, // ? - replies has a default of 1 and topics has 0? why?
ccol("actionType", 20, "''"),
{"poll", "int", 0, false, false, "0"},
},
[]tK{
{"rid", "primary", "", false},
{"content", "fulltext", "", false},
},
)
createTable("attachments", mysqlPre, mysqlCol,
[]tC{
{"attachID", "int", 0, false, true, ""},
{"sectionID", "int", 0, false, false, "0"},
ccol("sectionTable", 200, "forums"),
{"originID", "int", 0, false, false, ""},
ccol("originTable", 200, "replies"),
{"uploadedBy", "int", 0, false, false, ""}, // TODO; Make this a foreign key
ccol("path", 200, ""),
ccol("extra", 200, ""),
},
[]tK{
{"attachID", "primary", "", false},
},
)
createTable("revisions", mysqlPre, mysqlCol,
[]tC{
{"reviseID", "int", 0, false, true, ""},
text("content"),
{"contentID", "int", 0, false, false, ""},
ccol("contentType", 100, "replies"),
createdAt(),
// TODO: Add a createdBy column?
},
[]tK{
{"reviseID", "primary", "", false},
},
)
createTable("polls", mysqlPre, mysqlCol,
[]tC{
{"pollID", "int", 0, false, true, ""},
{"parentID", "int", 0, false, false, "0"},
ccol("parentTable", 100, "topics"), // topics, replies
{"type", "int", 0, false, false, "0"},
{"options", "json", 0, false, false, ""},
{"votes", "int", 0, false, false, "0"},
},
[]tK{
{"pollID", "primary", "", false},
},
)
createTable("polls_options", "", "",
[]tC{
{"pollID", "int", 0, false, false, ""},
{"option", "int", 0, false, false, "0"},
{"votes", "int", 0, false, false, "0"},
}, nil,
)
createTable("polls_votes", mysqlPre, mysqlCol,
[]tC{
{"pollID", "int", 0, false, false, ""},
{"uid", "int", 0, false, false, ""}, // TODO: Make this a foreign key
{"option", "int", 0, false, false, "0"},
createdAt("castAt"),
ccol("ip", 200, "''"),
}, nil,
)
createTable("users_replies", mysqlPre, mysqlCol,
[]tC{
{"rid", "int", 0, false, true, ""},
{"uid", "int", 0, false, false, ""}, // TODO: Make this a foreign key
text("content"),
text("parsed_content"),
createdAt(),
{"createdBy", "int", 0, false, false, ""}, // TODO: Make this a foreign key
{"lastEdit", "int", 0, false, false, "0"},
{"lastEditBy", "int", 0, false, false, "0"},
ccol("ip", 200, "''"),
},
[]tK{
{"rid", "primary", "", false},
},
)
createTable("likes", "", "",
[]tC{
{"weight", "tinyint", 0, false, false, "1"},
{"targetItem", "int", 0, false, false, ""},
ccol("targetType", 50, "replies"),
{"sentBy", "int", 0, false, false, ""}, // TODO: Make this a foreign key
createdAt(),
{"recalc", "tinyint", 0, false, false, "0"},
}, nil,
)
//columns("participants,createdBy,createdAt,lastReplyBy,lastReplyAt").Where("cid=?")
createTable("conversations", "", "",
[]tC{
{"cid", "int", 0, false, true, ""},
{"createdBy", "int", 0, false, false, ""}, // TODO: Make this a foreign key
createdAt(),
{"lastReplyAt", "datetime", 0, false, false, ""},
{"lastReplyBy", "int", 0, false, false, ""},
},
[]tK{
{"cid", "primary", "", false},
},
)
createTable("conversations_posts", "", "",
[]tC{
{"pid", "int", 0, false, true, ""},
{"cid", "int", 0, false, false, ""},
{"createdBy", "int", 0, false, false, ""},
ccol("body", 50, ""),
ccol("post", 50, "''"),
},
[]tK{
{"pid", "primary", "", false},
},
)
createTable("conversations_participants", "", "",
[]tC{
{"uid", "int", 0, false, false, ""},
{"cid", "int", 0, false, false, ""},
}, nil,
)
/*
createTable("users_friends", "", "",
[]tC{
{"uid", "int", 0, false, false, ""},
{"uid2", "int", 0, false, false, ""},
}, nil,
)
createTable("users_friends_invites", "", "",
[]tC{
{"requester", "int", 0, false, false, ""},
{"target", "int", 0, false, false, ""},
}, nil,
)
*/
createTable("users_blocks", "", "",
[]tC{
{"blocker", "int", 0, false, false, ""},
{"blockedUser", "int", 0, false, false, ""},
}, nil,
)
createTable("activity_stream_matches", "", "",
[]tC{
{"watcher", "int", 0, false, false, ""}, // TODO: Make this a foreign key
{"asid", "int", 0, false, false, ""}, // TODO: Make this a foreign key
},
[]tK{
{"asid,asid", "foreign", "activity_stream", true},
},
)
createTable("activity_stream", "", "",
[]tC{
{"asid", "int", 0, false, true, ""},
{"actor", "int", 0, false, false, ""}, /* the one doing the act */ // TODO: Make this a foreign key
{"targetUser", "int", 0, false, false, ""}, /* the user who created the item the actor is acting on, some items like forums may lack a targetUser field */
ccol("event", 50, ""), /* mention, like, reply (as in the act of replying to an item, not the reply item type, you can "reply" to a forum by making a topic in it), friend_invite */
ccol("elementType", 50, ""), /* topic, post (calling it post here to differentiate it from the 'reply' event), forum, user */
// replacement for elementType
tC{"elementTable", "int", 0, false, false, "0"},
{"elementID", "int", 0, false, false, ""}, /* the ID of the element being acted upon */
createdAt(),
ccol("extra", 200, "''"),
},
[]tK{
{"asid", "primary", "", false},
},
)
createTable("activity_subscriptions", "", "",
[]tC{
{"user", "int", 0, false, false, ""}, // TODO: Make this a foreign key
{"targetID", "int", 0, false, false, ""}, /* the ID of the element being acted upon */
ccol("targetType", 50, ""), /* topic, post (calling it post here to differentiate it from the 'reply' event), forum, user */
{"level", "int", 0, false, false, "0"}, /* 0: Mentions (aka the global default for any post), 1: Replies To You, 2: All Replies*/
}, nil,
)
/* Due to MySQL's design, we have to drop the unique keys for table settings, plugins, and themes down from 200 to 180 or it will error */
createTable("settings", "", "",
[]tC{
ccol("name", 180, ""),
ccol("content", 250, ""),
ccol("type", 50, ""),
ccol("constraints", 200, "''"),
},
[]tK{
{"name", "unique", "", false},
},
)
createTable("word_filters", "", "",
[]tC{
{"wfid", "int", 0, false, true, ""},
ccol("find", 200, ""),
ccol("replacement", 200, ""),
},
[]tK{
{"wfid", "primary", "", false},
},
)
createTable("plugins", "", "",
[]tC{
ccol("uname", 180, ""),
bcol("active", false),
bcol("installed", false),
},
[]tK{
{"uname", "unique", "", false},
},
)
createTable("themes", "", "",
[]tC{
ccol("uname", 180, ""),
bcol("default", false),
//text("profileUserVars"),
},
[]tK{
{"uname", "unique", "", false},
},
)
createTable("widgets", "", "",
[]tC{
{"wid", "int", 0, false, true, ""},
{"position", "int", 0, false, false, ""},
ccol("side", 100, ""),
ccol("type", 100, ""),
bcol("active", false),
ccol("location", 100, ""),
text("data"),
},
[]tK{
{"wid", "primary", "", false},
},
)
createTable("menus", "", "",
[]tC{
{"mid", "int", 0, false, true, ""},
},
[]tK{
{"mid", "primary", "", false},
},
)
createTable("menu_items", "", "",
[]tC{
{"miid", "int", 0, false, true, ""},
{"mid", "int", 0, false, false, ""},
ccol("name", 200, "''"),
ccol("htmlID", 200, "''"),
ccol("cssClass", 200, "''"),
ccol("position", 100, ""),
ccol("path", 200, "''"),
ccol("aria", 200, "''"),
ccol("tooltip", 200, "''"),
ccol("tmplName", 200, "''"),
{"order", "int", 0, false, false, "0"},
bcol("guestOnly", false),
bcol("memberOnly", false),
bcol("staffOnly", false),
bcol("adminOnly", false),
},
[]tK{
{"miid", "primary", "", false},
},
)
createTable("pages", mysqlPre, mysqlCol,
[]tC{
{"pid", "int", 0, false, true, ""},
//ccol("path", 200, ""),
ccol("name", 200, ""),
ccol("title", 200, ""),
text("body"),
// TODO: Make this a table?
text("allowedGroups"),
{"menuID", "int", 0, false, false, "-1"}, // simple sidebar menu
},
[]tK{
{"pid", "primary", "", false},
},
)
createTable("registration_logs", "", "",
[]tC{
{"rlid", "int", 0, false, true, ""},
ccol("username", 100, ""),
{"email", "varchar", 100, false, false, ""},
ccol("failureReason", 100, ""),
bcol("success", false), // Did this attempt succeed?
ccol("ipaddress", 200, ""),
createdAt("doneAt"),
},
[]tK{
{"rlid", "primary", "", false},
},
)
createTable("login_logs", "", "",
[]tC{
{"lid", "int", 0, false, true, ""},
{"uid", "int", 0, false, false, ""},
bcol("success", false), // Did this attempt succeed?
ccol("ipaddress", 200, ""),
createdAt("doneAt"),
},
[]tK{
{"lid", "primary", "", false},
},
)
createTable("moderation_logs", "", "",
[]tC{
ccol("action", 100, ""),
{"elementID", "int", 0, false, false, ""},
ccol("elementType", 100, ""),
ccol("ipaddress", 200, ""),
{"actorID", "int", 0, false, false, ""}, // TODO: Make this a foreign key
{"doneAt", "datetime", 0, false, false, ""},
text("extra"),
}, nil,
)
createTable("administration_logs", "", "",
[]tC{
ccol("action", 100, ""),
{"elementID", "int", 0, false, false, ""},
ccol("elementType", 100, ""),
ccol("ipaddress", 200, ""),
{"actorID", "int", 0, false, false, ""}, // TODO: Make this a foreign key
{"doneAt", "datetime", 0, false, false, ""},
text("extra"),
}, nil,
)
createTable("viewchunks", "", "",
[]tC{
{"count", "int", 0, false, false, "0"},
{"avg", "int", 0, false, false, "0"},
{"createdAt", "datetime", 0, false, false, ""},
ccol("route", 200, ""), // TODO: set a default empty here
}, nil,
)
createTable("viewchunks_agents", "", "",
[]tC{
{"count", "int", 0, false, false, "0"},
{"createdAt", "datetime", 0, false, false, ""},
ccol("browser", 200, ""), // googlebot, firefox, opera, etc.
//ccol("version",0,""), // the version of the browser or bot
}, nil,
)
createTable("viewchunks_systems", "", "",
[]tC{
{"count", "int", 0, false, false, "0"},
{"createdAt", "datetime", 0, false, false, ""},
ccol("system", 200, ""), // windows, android, unknown, etc.
}, nil,
)
createTable("viewchunks_langs", "", "",
[]tC{
{"count", "int", 0, false, false, "0"},
{"createdAt", "datetime", 0, false, false, ""},
ccol("lang", 200, ""), // en, ru, etc.
}, nil,
)
createTable("viewchunks_referrers", "", "",
[]tC{
{"count", "int", 0, false, false, "0"},
{"createdAt", "datetime", 0, false, false, ""},
ccol("domain", 200, ""),
}, nil,
)
createTable("viewchunks_forums", "", "",
[]tC{
{"count", "int", 0, false, false, "0"},
{"createdAt", "datetime", 0, false, false, ""},
{"forum", "int", 0, false, false, ""},
}, nil,
)
createTable("topicchunks", "", "",
[]tC{
{"count", "int", 0, false, false, "0"},
{"createdAt", "datetime", 0, false, false, ""},
// TODO: Add a column for the parent forum?
}, nil,
)
createTable("postchunks", "", "",
[]tC{
{"count", "int", 0, false, false, "0"},
{"createdAt", "datetime", 0, false, false, ""},
// TODO: Add a column for the parent topic / profile?
}, nil,
)
createTable("memchunks", "", "",
[]tC{
{"count", "int", 0, false, false, "0"},
{"stack", "int", 0, false, false, "0"},
{"heap", "int", 0, false, false, "0"},
{"createdAt", "datetime", 0, false, false, ""},
}, nil,
)
createTable("perfchunks", "", "",
[]tC{
{"low", "int", 0, false, false, "0"},
{"high", "int", 0, false, false, "0"},
{"avg", "int", 0, false, false, "0"},
{"createdAt", "datetime", 0, false, false, ""},
}, nil,
)
createTable("sync", "", "",
[]tC{
{"last_update", "datetime", 0, false, false, ""},
}, nil,
)
createTable("updates", "", "",
[]tC{
{"dbVersion", "int", 0, false, false, "0"},
}, nil,
)
createTable("meta", "", "",
[]tC{
ccol("name", 200, ""),
ccol("value", 200, ""),
}, nil,
)
/*createTable("tables", "", "",
[]tC{
{"id", "int", 0, false, true, ""},
ccol("name", 200, ""),
},
[]tK{
{"id", "primary", "", false},
{"name", "unique", "", false},
},
)*/
return err
}

View File

@ -1,113 +0,0 @@
package common
import (
"database/sql"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var Activity ActivityStream
type ActivityStream interface {
Add(a Alert) (int, error)
Get(id int) (Alert, error)
Delete(id int) error
DeleteByParams(event string, targetID int, targetType string) error
DeleteByParamsExtra(event string, targetID int, targetType, extra string) error
AidsByParams(event string, elementID int, elementType string) (aids []int, err error)
AidsByParamsExtra(event string, elementID int, elementType, extra string) (aids []int, err error)
Count() (count int)
}
type DefaultActivityStream struct {
add *sql.Stmt
get *sql.Stmt
delete *sql.Stmt
deleteByParams *sql.Stmt
deleteByParamsExtra *sql.Stmt
aidsByParams *sql.Stmt
aidsByParamsExtra *sql.Stmt
count *sql.Stmt
}
func NewDefaultActivityStream(acc *qgen.Accumulator) (*DefaultActivityStream, error) {
as := "activity_stream"
cols := "actor,targetUser,event,elementType,elementID,createdAt,extra"
return &DefaultActivityStream{
add: acc.Insert(as).Columns(cols).Fields("?,?,?,?,?,UTC_TIMESTAMP(),?").Prepare(),
get: acc.Select(as).Columns(cols).Where("asid=?").Prepare(),
delete: acc.Delete(as).Where("asid=?").Prepare(),
deleteByParams: acc.Delete(as).Where("event=? AND elementID=? AND elementType=?").Prepare(),
deleteByParamsExtra: acc.Delete(as).Where("event=? AND elementID=? AND elementType=? AND extra=?").Prepare(),
aidsByParams: acc.Select(as).Columns("asid").Where("event=? AND elementID=? AND elementType=?").Prepare(),
aidsByParamsExtra: acc.Select(as).Columns("asid").Where("event=? AND elementID=? AND elementType=? AND extra=?").Prepare(),
count: acc.Count(as).Prepare(),
}, acc.FirstError()
}
func (s *DefaultActivityStream) Add(a Alert) (int, error) {
res, err := s.add.Exec(a.ActorID, a.TargetUserID, a.Event, a.ElementType, a.ElementID, a.Extra)
if err != nil {
return 0, err
}
lastID, err := res.LastInsertId()
return int(lastID), err
}
func (s *DefaultActivityStream) Get(id int) (Alert, error) {
a := Alert{ASID: id}
err := s.get.QueryRow(id).Scan(&a.ActorID, &a.TargetUserID, &a.Event, &a.ElementType, &a.ElementID, &a.CreatedAt, &a.Extra)
return a, err
}
func (s *DefaultActivityStream) Delete(id int) error {
_, err := s.delete.Exec(id)
return err
}
func (s *DefaultActivityStream) DeleteByParams(event string, elementID int, elementType string) error {
_, err := s.deleteByParams.Exec(event, elementID, elementType)
return err
}
func (s *DefaultActivityStream) DeleteByParamsExtra(event string, elementID int, elementType, extra string) error {
_, err := s.deleteByParamsExtra.Exec(event, elementID, elementType, extra)
return err
}
func (s *DefaultActivityStream) AidsByParams(event string, elementID int, elementType string) (aids []int, err error) {
rows, err := s.aidsByParams.Query(event, elementID, elementType)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var aid int
if err := rows.Scan(&aid); err != nil {
return nil, err
}
aids = append(aids, aid)
}
return aids, rows.Err()
}
func (s *DefaultActivityStream) AidsByParamsExtra(event string, elementID int, elementType, extra string) (aids []int, e error) {
rows, e := s.aidsByParamsExtra.Query(event, elementID, elementType, extra)
if e != nil {
return nil, e
}
defer rows.Close()
for rows.Next() {
var aid int
if e := rows.Scan(&aid); e != nil {
return nil, e
}
aids = append(aids, aid)
}
return aids, rows.Err()
}
// Count returns the total number of activity stream items
func (s *DefaultActivityStream) Count() (count int) {
return Count(s.count)
}

View File

@ -1,54 +0,0 @@
package common
import (
"database/sql"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var ActivityMatches ActivityStreamMatches
type ActivityStreamMatches interface {
Add(watcher, asid int) error
Delete(watcher, asid int) error
DeleteAndCountChanged(watcher, asid int) (int, error)
CountAsid(asid int) int
}
type DefaultActivityStreamMatches struct {
add *sql.Stmt
delete *sql.Stmt
countAsid *sql.Stmt
}
func NewDefaultActivityStreamMatches(acc *qgen.Accumulator) (*DefaultActivityStreamMatches, error) {
asm := "activity_stream_matches"
return &DefaultActivityStreamMatches{
add: acc.Insert(asm).Columns("watcher,asid").Fields("?,?").Prepare(),
delete: acc.Delete(asm).Where("watcher=? AND asid=?").Prepare(),
countAsid: acc.Count(asm).Where("asid=?").Prepare(),
}, acc.FirstError()
}
func (s *DefaultActivityStreamMatches) Add(watcher, asid int) error {
_, e := s.add.Exec(watcher, asid)
return e
}
func (s *DefaultActivityStreamMatches) Delete(watcher, asid int) error {
_, e := s.delete.Exec(watcher, asid)
return e
}
func (s *DefaultActivityStreamMatches) DeleteAndCountChanged(watcher, asid int) (int, error) {
res, e := s.delete.Exec(watcher, asid)
if e != nil {
return 0, e
}
c64, e := res.RowsAffected()
return int(c64), e
}
func (s *DefaultActivityStreamMatches) CountAsid(asid int) int {
return Countf(s.countAsid, asid)
}

View File

@ -1,7 +1,7 @@
/* /*
* *
* Gosora Alerts System * Gosora Alerts System
* Copyright Azareal 2017 - 2020 * Copyright Azareal 2017 - 2018
* *
*/ */
package common package common
@ -11,30 +11,16 @@ import (
"errors" "errors"
"strconv" "strconv"
"strings" "strings"
"time"
//"fmt" "../query_gen/lib"
"git.tuxpa.in/a/gosora/common/phrases"
qgen "git.tuxpa.in/a/gosora/query_gen"
) )
type Alert struct {
ASID int
ActorID int
TargetUserID int
Event string
ElementType string
ElementID int
CreatedAt time.Time
Extra string
Actor *User
}
type AlertStmts struct { type AlertStmts struct {
addActivity *sql.Stmt
notifyWatchers *sql.Stmt notifyWatchers *sql.Stmt
notifyOne *sql.Stmt
getWatchers *sql.Stmt getWatchers *sql.Stmt
getActivityEntry *sql.Stmt
} }
var alertStmts AlertStmts var alertStmts AlertStmts
@ -44,297 +30,169 @@ var alertStmts AlertStmts
func init() { func init() {
DbInits.Add(func(acc *qgen.Accumulator) error { DbInits.Add(func(acc *qgen.Accumulator) error {
alertStmts = AlertStmts{ alertStmts = AlertStmts{
addActivity: acc.Insert("activity_stream").Columns("actor, targetUser, event, elementType, elementID").Fields("?,?,?,?,?").Prepare(),
notifyWatchers: acc.SimpleInsertInnerJoin( notifyWatchers: acc.SimpleInsertInnerJoin(
qgen.DBInsert{"activity_stream_matches", "watcher, asid", ""}, qgen.DBInsert{"activity_stream_matches", "watcher, asid", ""},
qgen.DBJoin{"activity_stream", "activity_subscriptions", "activity_subscriptions.user, activity_stream.asid", "activity_subscriptions.targetType = activity_stream.elementType AND activity_subscriptions.targetID = activity_stream.elementID AND activity_subscriptions.user != activity_stream.actor", "asid = ?", "", ""}, qgen.DBJoin{"activity_stream", "activity_subscriptions", "activity_subscriptions.user, activity_stream.asid", "activity_subscriptions.targetType = activity_stream.elementType AND activity_subscriptions.targetID = activity_stream.elementID AND activity_subscriptions.user != activity_stream.actor", "asid = ?", "", ""},
), ),
notifyOne: acc.Insert("activity_stream_matches").Columns("watcher, asid").Fields("?,?").Prepare(),
getWatchers: acc.SimpleInnerJoin("activity_stream", "activity_subscriptions", "activity_subscriptions.user", "activity_subscriptions.targetType = activity_stream.elementType AND activity_subscriptions.targetID = activity_stream.elementID AND activity_subscriptions.user != activity_stream.actor", "asid = ?", "", ""), getWatchers: acc.SimpleInnerJoin("activity_stream", "activity_subscriptions", "activity_subscriptions.user", "activity_subscriptions.targetType = activity_stream.elementType AND activity_subscriptions.targetID = activity_stream.elementID AND activity_subscriptions.user != activity_stream.actor", "asid = ?", "", ""),
getActivityEntry: acc.Select("activity_stream").Columns("actor, targetUser, event, elementType, elementID").Where("asid = ?").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })
} }
const AlertsGrowHint = len(`{"msgs":[],"count":,"tc":}`) + 1 + 10 // These notes are for me, don't worry about it too much ^_^
/*
"You received a friend invite from {user}"
"{x}{mentioned you on}{user}{'s profile}"
"{x}{mentioned you in}{topic}"
"{x}{likes}{you}"
"{x}{liked}{your topic}{topic}"
"{x}{liked}{your post on}{user}{'s profile}" todo
"{x}{liked}{your post in}{topic}"
"{x}{replied to}{your post in}{topic}" todo
"{x}{replied to}{topic}"
"{x}{replied to}{your topic}{topic}"
"{x}{created a new topic}{topic}"
*/
// TODO: See if we can json.Marshal instead? func BuildAlert(asid int, event string, elementType string, actorID int, targetUserID int, elementID int, user User /* The current user */) (string, error) {
func escapeTextInJson(in string) string {
in = strings.Replace(in, "\"", "\\\"", -1)
return strings.Replace(in, "/", "\\/", -1)
}
func BuildAlert(a Alert, user User /* The current user */) (out string, err error) {
var targetUser *User var targetUser *User
if a.Actor == nil {
a.Actor, err = Users.Get(a.ActorID) actor, err := Users.Get(actorID)
if err != nil { if err != nil {
return "", errors.New(phrases.GetErrorPhrase("alerts_no_actor")) return "", errors.New("Unable to find the actor")
}
} }
/*if a.ElementType != "forum" { /*if elementType != "forum" {
targetUser, err = users.Get(a.TargetUserID) targetUser, err = users.Get(targetUser_id)
if err != nil { if err != nil {
LocalErrorJS("Unable to find the target user",w,r) LocalErrorJS("Unable to find the target user",w,r)
return return
} }
}*/ }*/
if a.Event == "friend_invite" {
return buildAlertString(".new_friend_invite", []string{a.Actor.Name}, a.Actor.Link, a.Actor.Avatar, a.ASID), nil if event == "friend_invite" {
return `{"msg":"You received a friend invite from {0}","sub":["` + actor.Name + `"],"path":"` + actor.Link + `","avatar":"` + strings.Replace(actor.Avatar, "/", "\\/", -1) + `","asid":"` + strconv.Itoa(asid) + `"}`, nil
} }
// Not that many events for us to handle in a forum var act, postAct, url, area string
if a.ElementType == "forum" { var startFrag, endFrag string
if a.Event == "reply" { switch elementType {
topic, err := Topics.Get(a.ElementID) case "forum":
if event == "reply" {
act = "created a new topic"
topic, err := Topics.Get(elementID)
if err != nil { if err != nil {
DebugLogf("Unable to find linked topic %d", a.ElementID) DebugLogf("Unable to find linked topic %d", elementID)
return "", errors.New(phrases.GetErrorPhrase("alerts_no_linked_topic")) return "", errors.New("Unable to find the linked topic")
} }
url = topic.Link
area = topic.Title
// Store the forum ID in the targetUser column instead of making a new one? o.O // Store the forum ID in the targetUser column instead of making a new one? o.O
// Add an additional column for extra information later on when we add the ability to link directly to posts. We don't need the forum data for now... // Add an additional column for extra information later on when we add the ability to link directly to posts. We don't need the forum data for now...
return buildAlertString(".forum_new_topic", []string{a.Actor.Name, topic.Title}, topic.Link, a.Actor.Avatar, a.ASID), nil } else {
act = "did something in a forum"
} }
return buildAlertString(".forum_unknown_action", []string{a.Actor.Name}, "", a.Actor.Avatar, a.ASID), nil
}
var url, area, phraseName string
own := false
// TODO: Avoid loading a bit of data twice
switch a.ElementType {
case "convo":
convo, err := Convos.Get(a.ElementID)
if err != nil {
DebugLogf("Unable to find linked convo %d", a.ElementID)
return "", errors.New(phrases.GetErrorPhrase("alerts_no_linked_convo"))
}
url = convo.Link
case "topic": case "topic":
topic, err := Topics.Get(a.ElementID) topic, err := Topics.Get(elementID)
if err != nil { if err != nil {
DebugLogf("Unable to find linked topic %d", a.ElementID) DebugLogf("Unable to find linked topic %d", elementID)
return "", errors.New(phrases.GetErrorPhrase("alerts_no_linked_topic")) return "", errors.New("Unable to find the linked topic")
} }
url = topic.Link url = topic.Link
area = topic.Title area = topic.Title
own = a.TargetUserID == user.ID
if targetUserID == user.ID {
postAct = " your topic"
}
case "user": case "user":
targetUser, err = Users.Get(a.ElementID) targetUser, err = Users.Get(elementID)
if err != nil { if err != nil {
DebugLogf("Unable to find target user %d", a.ElementID) DebugLogf("Unable to find target user %d", elementID)
return "", errors.New(phrases.GetErrorPhrase("alerts_no_target_user")) return "", errors.New("Unable to find the target user")
} }
area = targetUser.Name area = targetUser.Name
endFrag = "'s profile"
url = targetUser.Link url = targetUser.Link
own = a.TargetUserID == user.ID
case "post": case "post":
topic, err := TopicByReplyID(a.ElementID) topic, err := TopicByReplyID(elementID)
if err != nil { if err != nil {
DebugLogf("Unable to find linked topic by reply ID %d", a.ElementID) return "", errors.New("Unable to find the linked reply or parent topic")
return "", errors.New(phrases.GetErrorPhrase("alerts_no_linked_topic_by_reply"))
} }
url = topic.Link url = topic.Link
area = topic.Title area = topic.Title
own = a.TargetUserID == user.ID if targetUserID == user.ID {
postAct = " your post in"
}
default: default:
return "", errors.New(phrases.GetErrorPhrase("alerts_invalid_elementtype")) return "", errors.New("Invalid elementType")
} }
badEv := false switch event {
switch a.Event { case "like":
case "create", "like", "mention", "reply": if elementType == "user" {
// skip act = "likes"
default: endFrag = ""
badEv = true if targetUser.ID == user.ID {
area = "you"
} }
if own && !badEv {
phraseName = "." + a.ElementType + "_own_" + a.Event
} else if !badEv {
phraseName = "." + a.ElementType + "_" + a.Event
} else if own {
phraseName = "." + a.ElementType + "_own"
} else { } else {
phraseName = "." + a.ElementType act = "liked"
} }
case "mention":
return buildAlertString(phraseName, []string{a.Actor.Name, area}, url, a.Actor.Avatar, a.ASID), nil if elementType == "user" {
} act = "mentioned you on"
func buildAlertString(msg string, sub []string, path, avatar string, asid int) string {
var sb strings.Builder
buildAlertSb(&sb, msg, sub, path, avatar, asid)
return sb.String()
}
const AlertsGrowHint2 = len(`{"msg":"","sub":[],"path":"","img":"","id":}`) + 5 + 3 + 1 + 1 + 1
// TODO: Use a string builder?
func buildAlertSb(sb *strings.Builder, msg string, sub []string, path, avatar string, asid int) {
sb.WriteString(`{"msg":"`)
sb.WriteString(escapeTextInJson(msg))
sb.WriteString(`","sub":[`)
for i, it := range sub {
if i != 0 {
sb.WriteString(",\"")
} else { } else {
sb.WriteString("\"") act = "mentioned you in"
postAct = ""
} }
sb.WriteString(escapeTextInJson(it)) case "reply":
sb.WriteString("\"") act = "replied to"
}
sb.WriteString(`],"path":"`)
sb.WriteString(escapeTextInJson(path))
sb.WriteString(`","img":"`)
sb.WriteString(escapeTextInJson(avatar))
sb.WriteString(`","id":`)
sb.WriteString(strconv.Itoa(asid))
sb.WriteRune('}')
} }
func BuildAlertSb(sb *strings.Builder, a *Alert, u *User /* The current user */) (err error) { return `{"msg":"{0} ` + startFrag + act + postAct + ` {1}` + endFrag + `","sub":["` + actor.Name + `","` + area + `"],"path":"` + url + `","avatar":"` + actor.Avatar + `","asid":"` + strconv.Itoa(asid) + `"}`, nil
var targetUser *User
if a.Actor == nil {
a.Actor, err = Users.Get(a.ActorID)
if err != nil {
return errors.New(phrases.GetErrorPhrase("alerts_no_actor"))
}
} }
/*if a.ElementType != "forum" { func AddActivityAndNotifyAll(actor int, targetUser int, event string, elementType string, elementID int) error {
targetUser, err = users.Get(a.TargetUserID) res, err := alertStmts.addActivity.Exec(actor, targetUser, event, elementType, elementID)
if err != nil {
LocalErrorJS("Unable to find the target user",w,r)
return
}
}*/
if a.Event == "friend_invite" {
buildAlertSb(sb, ".new_friend_invite", []string{a.Actor.Name}, a.Actor.Link, a.Actor.Avatar, a.ASID)
return nil
}
// Not that many events for us to handle in a forum
if a.ElementType == "forum" {
if a.Event == "reply" {
topic, err := Topics.Get(a.ElementID)
if err != nil {
DebugLogf("Unable to find linked topic %d", a.ElementID)
return errors.New(phrases.GetErrorPhrase("alerts_no_linked_topic"))
}
// Store the forum ID in the targetUser column instead of making a new one? o.O
// Add an additional column for extra information later on when we add the ability to link directly to posts. We don't need the forum data for now...
buildAlertSb(sb, ".forum_new_topic", []string{a.Actor.Name, topic.Title}, topic.Link, a.Actor.Avatar, a.ASID)
return nil
}
buildAlertSb(sb, ".forum_unknown_action", []string{a.Actor.Name}, "", a.Actor.Avatar, a.ASID)
return nil
}
var url, area string
own := false
// TODO: Avoid loading a bit of data twice
switch a.ElementType {
case "convo":
convo, err := Convos.Get(a.ElementID)
if err != nil {
DebugLogf("Unable to find linked convo %d", a.ElementID)
return errors.New(phrases.GetErrorPhrase("alerts_no_linked_convo"))
}
url = convo.Link
case "topic":
topic, err := Topics.Get(a.ElementID)
if err != nil {
DebugLogf("Unable to find linked topic %d", a.ElementID)
return errors.New(phrases.GetErrorPhrase("alerts_no_linked_topic"))
}
url = topic.Link
area = topic.Title
own = a.TargetUserID == u.ID
case "user":
targetUser, err = Users.Get(a.ElementID)
if err != nil {
DebugLogf("Unable to find target user %d", a.ElementID)
return errors.New(phrases.GetErrorPhrase("alerts_no_target_user"))
}
area = targetUser.Name
url = targetUser.Link
own = a.TargetUserID == u.ID
case "post":
t, err := TopicByReplyID(a.ElementID)
if err != nil {
DebugLogf("Unable to find linked topic by reply ID %d", a.ElementID)
return errors.New(phrases.GetErrorPhrase("alerts_no_linked_topic_by_reply"))
}
url = t.Link
area = t.Title
own = a.TargetUserID == u.ID
default:
return errors.New(phrases.GetErrorPhrase("alerts_invalid_elementtype"))
}
sb.WriteString(`{"msg":".`)
sb.WriteString(a.ElementType)
if own {
sb.WriteString("_own_")
} else {
sb.WriteRune('_')
}
switch a.Event {
case "create", "like", "mention", "reply":
sb.WriteString(a.Event)
}
sb.WriteString(`","sub":["`)
sb.WriteString(escapeTextInJson(a.Actor.Name))
sb.WriteString("\",\"")
sb.WriteString(escapeTextInJson(area))
sb.WriteString(`"],"path":"`)
sb.WriteString(escapeTextInJson(url))
sb.WriteString(`","img":"`)
sb.WriteString(escapeTextInJson(a.Actor.Avatar))
sb.WriteString(`","id":`)
sb.WriteString(strconv.Itoa(a.ASID))
sb.WriteRune('}')
return nil
}
//const AlertsGrowHint3 = len(`{"msg":"._","sub":["",""],"path":"","img":"","id":}`) + 3 + 2 + 2 + 2 + 2 + 1
// TODO: Create a notifier structure?
func AddActivityAndNotifyAll(a Alert) error {
id, err := Activity.Add(a)
if err != nil { if err != nil {
return err return err
} }
return NotifyWatchers(id) lastID, err := res.LastInsertId()
}
// TODO: Create a notifier structure?
func AddActivityAndNotifyTarget(a Alert) error {
id, err := Activity.Add(a)
if err != nil { if err != nil {
return err return err
} }
return NotifyWatchers(lastID)
}
err = ActivityMatches.Add(a.TargetUserID, id) func AddActivityAndNotifyTarget(actor int, targetUser int, event string, elementType string, elementID int) error {
res, err := alertStmts.addActivity.Exec(actor, targetUser, event, elementType, elementID)
if err != nil {
return err
}
lastID, err := res.LastInsertId()
if err != nil {
return err
}
err = NotifyOne(targetUser, lastID)
if err != nil { if err != nil {
return err return err
} }
a.ASID = id
// Live alerts, if the target is online and WebSockets is enabled // Live alerts, if the target is online and WebSockets is enabled
if EnableWebsockets { _ = WsHub.pushAlert(targetUser, int(lastID), event, elementType, actor, targetUser, elementID)
go func() {
defer EatPanics()
_ = WsHub.pushAlert(a.TargetUserID, a)
//fmt.Println("err:",err)
}()
}
return nil return nil
} }
// TODO: Create a notifier structure? func NotifyOne(watcher int, asid int64) error {
func NotifyWatchers(asid int) error { _, err := alertStmts.notifyOne.Exec(watcher, asid)
return err
}
func NotifyWatchers(asid int64) error {
_, err := alertStmts.notifyWatchers.Exec(asid) _, err := alertStmts.notifyWatchers.Exec(asid)
if err != nil { if err != nil {
return err return err
@ -342,18 +200,16 @@ func NotifyWatchers(asid int) error {
// Alert the subscribers about this without blocking us from doing something else // Alert the subscribers about this without blocking us from doing something else
if EnableWebsockets { if EnableWebsockets {
go func() { go notifyWatchers(asid)
defer EatPanics()
notifyWatchers(asid)
}()
} }
return nil return nil
} }
func notifyWatchers(asid int) { func notifyWatchers(asid int64) {
rows, e := alertStmts.getWatchers.Query(asid) rows, err := alertStmts.getWatchers.Query(asid)
if e != nil && e != ErrNoRows { if err != nil && err != ErrNoRows {
LogError(e) LogError(err)
return return
} }
defer rows.Close() defer rows.Close()
@ -361,25 +217,26 @@ func notifyWatchers(asid int) {
var uid int var uid int
var uids []int var uids []int
for rows.Next() { for rows.Next() {
if e := rows.Scan(&uid); e != nil { err := rows.Scan(&uid)
LogError(e) if err != nil {
LogError(err)
return return
} }
uids = append(uids, uid) uids = append(uids, uid)
} }
if e = rows.Err(); e != nil { err = rows.Err()
LogError(e) if err != nil {
LogError(err)
return return
} }
alert, e := Activity.Get(asid) var actorID, targetUserID, elementID int
if e != nil && e != ErrNoRows { var event, elementType string
LogError(e) err = alertStmts.getActivityEntry.QueryRow(asid).Scan(&actorID, &targetUserID, &event, &elementType, &elementID)
if err != nil && err != ErrNoRows {
LogError(err)
return return
} }
_ = WsHub.pushAlerts(uids, alert)
}
func DismissAlert(uid, aid int) { _ = WsHub.pushAlerts(uids, int(asid), event, elementType, actorID, targetUserID, elementID)
_ = WsHub.PushMessage(uid, `{"event":"dismiss-alert","id":`+strconv.Itoa(aid)+`}`)
} }

View File

@ -1,91 +0,0 @@
package common
import (
"database/sql"
"log"
"time"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var Analytics AnalyticsStore
type AnalyticsTimeRange struct {
Quantity int
Unit string
Slices int
SliceWidth int
Range string
}
type AnalyticsStore interface {
FillViewMap(tbl string, tr *AnalyticsTimeRange, labelList []int64, viewMap map[int64]int64, param string, args ...interface{}) (map[int64]int64, error)
}
type DefaultAnalytics struct {
}
func NewDefaultAnalytics() *DefaultAnalytics {
return &DefaultAnalytics{}
}
/*
rows, e := qgen.NewAcc().Select("viewchunks_systems").Columns("count,createdAt").Where("system=?").DateCutoff("createdAt", timeRange.Quantity, timeRange.Unit).Query(system)
if e != nil && e != sql.ErrNoRows {
return c.InternalError(e, w, r)
}
viewMap, e = c.AnalyticsRowsToViewMap(rows, labelList, viewMap)
if e != nil {
return c.InternalError(e, w, r)
}
*/
func (s *DefaultAnalytics) FillViewMap(tbl string, tr *AnalyticsTimeRange, labelList []int64, viewMap map[int64]int64, param string, args ...interface{}) (map[int64]int64, error) {
ac := qgen.NewAcc().Select(tbl).Columns("count,createdAt")
if param != "" {
ac = ac.Where(param + "=?")
}
rows, e := ac.DateCutoff("createdAt", tr.Quantity, tr.Unit).Query(args...)
if e != nil && e != sql.ErrNoRows {
return nil, e
}
return AnalyticsRowsToViewMap(rows, labelList, viewMap)
}
// TODO: Clamp it rather than using an offset off the current time to avoid chaotic changes in stats as adjacent sets converge and diverge?
func AnalyticsTimeRangeToLabelList(tr *AnalyticsTimeRange) (revLabelList []int64, labelList []int64, viewMap map[int64]int64) {
viewMap = make(map[int64]int64)
currentTime := time.Now().Unix()
for i := 1; i <= tr.Slices; i++ {
label := currentTime - int64(i*tr.SliceWidth)
revLabelList = append(revLabelList, label)
viewMap[label] = 0
}
labelList = append(labelList, revLabelList...)
return revLabelList, labelList, viewMap
}
func AnalyticsRowsToViewMap(rows *sql.Rows, labelList []int64, viewMap map[int64]int64) (map[int64]int64, error) {
defer rows.Close()
for rows.Next() {
var count int64
var createdAt time.Time
e := rows.Scan(&count, &createdAt)
if e != nil {
return viewMap, e
}
unixCreatedAt := createdAt.Unix()
// TODO: Bulk log this
if Dev.SuperDebug {
log.Print("count: ", count)
log.Print("createdAt: ", createdAt, " - ", unixCreatedAt)
}
for _, value := range labelList {
if unixCreatedAt > value {
viewMap[value] += count
break
}
}
}
return viewMap, rows.Err()
}

View File

@ -2,327 +2,28 @@ package common
import ( import (
"database/sql" "database/sql"
"errors"
//"fmt" "../query_gen/lib"
"os"
"path/filepath"
"strings"
qgen "git.tuxpa.in/a/gosora/query_gen"
) )
var Attachments AttachmentStore var Attachments AttachmentStore
var ErrCorruptAttachPath = errors.New("corrupt attachment path")
type MiniAttachment struct {
ID int
SectionID int
OriginID int
UploadedBy int
Path string
Extra string
Image bool
Ext string
}
type Attachment struct {
ID int
SectionTable string
SectionID int
OriginTable string
OriginID int
UploadedBy int
Path string
Extra string
Image bool
Ext string
}
type AttachmentStore interface { type AttachmentStore interface {
GetForRenderRoute(filename string, sid int, sectionTable string) (*Attachment, error) Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path string) error
FGet(id int) (*Attachment, error)
Get(id int) (*MiniAttachment, error)
MiniGetList(originTable string, originID int) (alist []*MiniAttachment, err error)
BulkMiniGetList(originTable string, ids []int) (amap map[int][]*MiniAttachment, err error)
Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path, extra string) (int, error)
MoveTo(sectionID, originID int, originTable string) error
MoveToByExtra(sectionID int, originTable, extra string) error
Count() int
CountIn(originTable string, oid int) int
CountInPath(path string) int
Delete(id int) error
AddLinked(otable string, oid int) (err error)
RemoveLinked(otable string, oid int) (err error)
} }
type DefaultAttachmentStore struct { type DefaultAttachmentStore struct {
getForRenderRoute *sql.Stmt
fget *sql.Stmt
get *sql.Stmt
getByObj *sql.Stmt
add *sql.Stmt add *sql.Stmt
count *sql.Stmt
countIn *sql.Stmt
countInPath *sql.Stmt
move *sql.Stmt
moveByExtra *sql.Stmt
delete *sql.Stmt
replyUpdateAttachs *sql.Stmt
topicUpdateAttachs *sql.Stmt
} }
func NewDefaultAttachmentStore(acc *qgen.Accumulator) (*DefaultAttachmentStore, error) { func NewDefaultAttachmentStore() (*DefaultAttachmentStore, error) {
a := "attachments" acc := qgen.NewAcc()
return &DefaultAttachmentStore{ return &DefaultAttachmentStore{
getForRenderRoute: acc.Select(a).Columns("sectionTable, originID, originTable, uploadedBy, path").Where("path=? AND sectionID=? AND sectionTable=?").Prepare(), add: acc.Insert("attachments").Columns("sectionID, sectionTable, originID, originTable, uploadedBy, path").Fields("?,?,?,?,?,?").Prepare(),
fget: acc.Select(a).Columns("originTable, originID, sectionTable, sectionID, uploadedBy, path, extra").Where("attachID=?").Prepare(),
get: acc.Select(a).Columns("originID, sectionID, uploadedBy, path, extra").Where("attachID=?").Prepare(),
getByObj: acc.Select(a).Columns("attachID, sectionID, uploadedBy, path, extra").Where("originTable=? AND originID=?").Prepare(),
add: acc.Insert(a).Columns("sectionID, sectionTable, originID, originTable, uploadedBy, path, extra").Fields("?,?,?,?,?,?,?").Prepare(),
count: acc.Count(a).Prepare(),
countIn: acc.Count(a).Where("originTable=? and originID=?").Prepare(),
countInPath: acc.Count(a).Where("path=?").Prepare(),
move: acc.Update(a).Set("sectionID=?").Where("originID=? AND originTable=?").Prepare(),
moveByExtra: acc.Update(a).Set("sectionID=?").Where("originTable=? AND extra=?").Prepare(),
delete: acc.Delete(a).Where("attachID=?").Prepare(),
// TODO: Less race-y attachment count updates
replyUpdateAttachs: acc.Update("replies").Set("attachCount=?").Where("rid=?").Prepare(),
topicUpdateAttachs: acc.Update("topics").Set("attachCount=?").Where("tid=?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
// TODO: Revamp this to make it less of a copy-paste from the original code in the route func (store *DefaultAttachmentStore) Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path string) error {
// ! Lacks some attachment initialisation code _, err := store.add.Exec(sectionID, sectionTable, originID, originTable, uploadedBy, path)
func (s *DefaultAttachmentStore) GetForRenderRoute(filename string, sid int, sectionTable string) (*Attachment, error) {
a := &Attachment{SectionID: sid}
e := s.getForRenderRoute.QueryRow(filename, sid, sectionTable).Scan(&a.SectionTable, &a.OriginID, &a.OriginTable, &a.UploadedBy, &a.Path)
// TODO: Initialise attachment struct fields?
return a, e
}
func (s *DefaultAttachmentStore) MiniGetList(originTable string, originID int) (alist []*MiniAttachment, err error) {
rows, err := s.getByObj.Query(originTable, originID)
defer rows.Close()
for rows.Next() {
a := &MiniAttachment{OriginID: originID}
err := rows.Scan(&a.ID, &a.SectionID, &a.UploadedBy, &a.Path, &a.Extra)
if err != nil {
return nil, err
}
a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(a.Ext) == 0 {
return nil, ErrCorruptAttachPath
}
a.Image = ImageFileExts.Contains(a.Ext)
alist = append(alist, a)
}
if err = rows.Err(); err != nil {
return nil, err
}
if len(alist) == 0 {
err = sql.ErrNoRows
}
return alist, err
}
func (s *DefaultAttachmentStore) BulkMiniGetList(originTable string, ids []int) (amap map[int][]*MiniAttachment, err error) {
if len(ids) == 0 {
return nil, sql.ErrNoRows
}
if len(ids) == 1 {
res, err := s.MiniGetList(originTable, ids[0])
return map[int][]*MiniAttachment{ids[0]: res}, err
}
amap = make(map[int][]*MiniAttachment)
var buffer []*MiniAttachment
var currentID int
rows, err := qgen.NewAcc().Select("attachments").Columns("attachID,sectionID,originID,uploadedBy,path").Where("originTable=?").In("originID", ids).Orderby("originID ASC").Query(originTable)
defer rows.Close()
for rows.Next() {
a := &MiniAttachment{}
err := rows.Scan(&a.ID, &a.SectionID, &a.OriginID, &a.UploadedBy, &a.Path)
if err != nil {
return nil, err
}
a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(a.Ext) == 0 {
return nil, ErrCorruptAttachPath
}
a.Image = ImageFileExts.Contains(a.Ext)
if currentID == 0 {
currentID = a.OriginID
}
if a.OriginID != currentID {
if len(buffer) > 0 {
amap[currentID] = buffer
currentID = a.OriginID
buffer = nil
}
}
buffer = append(buffer, a)
}
if len(buffer) > 0 {
amap[currentID] = buffer
}
return amap, rows.Err()
}
func (s *DefaultAttachmentStore) FGet(id int) (*Attachment, error) {
a := &Attachment{ID: id}
e := s.fget.QueryRow(id).Scan(&a.OriginTable, &a.OriginID, &a.SectionTable, &a.SectionID, &a.UploadedBy, &a.Path, &a.Extra)
if e != nil {
return nil, e
}
a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(a.Ext) == 0 {
return nil, ErrCorruptAttachPath
}
a.Image = ImageFileExts.Contains(a.Ext)
return a, nil
}
func (s *DefaultAttachmentStore) Get(id int) (*MiniAttachment, error) {
a := &MiniAttachment{ID: id}
err := s.get.QueryRow(id).Scan(&a.OriginID, &a.SectionID, &a.UploadedBy, &a.Path, &a.Extra)
if err != nil {
return nil, err
}
a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(a.Ext) == 0 {
return nil, ErrCorruptAttachPath
}
a.Image = ImageFileExts.Contains(a.Ext)
return a, nil
}
func (s *DefaultAttachmentStore) Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path, extra string) (int, error) {
res, err := s.add.Exec(sectionID, sectionTable, originID, originTable, uploadedBy, path, extra)
if err != nil {
return 0, err
}
lid, err := res.LastInsertId()
return int(lid), err
}
func (s *DefaultAttachmentStore) MoveTo(sectionID, originID int, originTable string) error {
_, err := s.move.Exec(sectionID, originID, originTable)
return err return err
} }
func (s *DefaultAttachmentStore) MoveToByExtra(sectionID int, originTable, extra string) error {
_, err := s.moveByExtra.Exec(sectionID, originTable, extra)
return err
}
func (s *DefaultAttachmentStore) Count() (count int) {
e := s.count.QueryRow().Scan(&count)
if e != nil {
LogError(e)
}
return count
}
func (s *DefaultAttachmentStore) CountIn(originTable string, oid int) (count int) {
e := s.countIn.QueryRow(originTable, oid).Scan(&count)
if e != nil {
LogError(e)
}
return count
}
func (s *DefaultAttachmentStore) CountInPath(path string) (count int) {
e := s.countInPath.QueryRow(path).Scan(&count)
if e != nil {
LogError(e)
}
return count
}
func (s *DefaultAttachmentStore) Delete(id int) error {
_, e := s.delete.Exec(id)
return e
}
// TODO: Split this out of this store
func (s *DefaultAttachmentStore) AddLinked(otable string, oid int) (err error) {
switch otable {
case "topics":
_, err = s.topicUpdateAttachs.Exec(s.CountIn(otable, oid), oid)
if err != nil {
return err
}
err = Topics.Reload(oid)
case "replies":
_, err = s.replyUpdateAttachs.Exec(s.CountIn(otable, oid), oid)
if err != nil {
return err
}
err = Rstore.GetCache().Remove(oid)
}
if err == sql.ErrNoRows {
err = nil
}
return err
}
// TODO: Split this out of this store
func (s *DefaultAttachmentStore) RemoveLinked(otable string, oid int) (err error) {
switch otable {
case "topics":
_, err = s.topicUpdateAttachs.Exec(s.CountIn(otable, oid), oid)
if err != nil {
return err
}
if tc := Topics.GetCache(); tc != nil {
tc.Remove(oid)
}
case "replies":
_, err = s.replyUpdateAttachs.Exec(s.CountIn(otable, oid), oid)
if err != nil {
return err
}
err = Rstore.GetCache().Remove(oid)
}
return err
}
// TODO: Add a table for the files and lock the file row when performing tasks related to the file
func DeleteAttachment(aid int) error {
a, err := Attachments.FGet(aid)
if err != nil {
return err
}
err = deleteAttachment(a)
if err != nil {
return err
}
_ = Attachments.RemoveLinked(a.OriginTable, a.OriginID)
return nil
}
func deleteAttachment(a *Attachment) error {
err := Attachments.Delete(a.ID)
if err != nil {
return err
}
count := Attachments.CountInPath(a.Path)
if count == 0 {
err := os.Remove("./attachs/" + a.Path)
if err != nil {
return err
}
}
return nil
}

View File

@ -2,9 +2,8 @@ package common
import ( import (
"database/sql" "database/sql"
"time"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var ModLogs LogStore var ModLogs LogStore
@ -14,17 +13,15 @@ type LogItem struct {
Action string Action string
ElementID int ElementID int
ElementType string ElementType string
IP string IPAddress string
ActorID int ActorID int
DoneAt string DoneAt string
Extra string
} }
type LogStore interface { type LogStore interface {
Create(action string, elementID int, elementType, ip string, actorID int) (err error) Create(action string, elementID int, elementType string, ipaddress string, actorID int) (err error)
CreateExtra(action string, elementID int, elementType, ip string, actorID int, extra string) (err error) GlobalCount() int
Count() int GetOffset(offset int, perPage int) (logs []LogItem, err error)
GetOffset(offset, perPage int) (logs []LogItem, err error)
} }
type SQLModLogStore struct { type SQLModLogStore struct {
@ -34,50 +31,41 @@ type SQLModLogStore struct {
} }
func NewModLogStore(acc *qgen.Accumulator) (*SQLModLogStore, error) { func NewModLogStore(acc *qgen.Accumulator) (*SQLModLogStore, error) {
ml := "moderation_logs"
// TODO: Shorten name of ipaddress column to ip
cols := "action, elementID, elementType, ipaddress, actorID, doneAt, extra"
return &SQLModLogStore{ return &SQLModLogStore{
create: acc.Insert(ml).Columns(cols).Fields("?,?,?,?,?,UTC_TIMESTAMP(),?").Prepare(), create: acc.Insert("moderation_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(),
count: acc.Count(ml).Prepare(), count: acc.Count("moderation_logs").Prepare(),
getOffset: acc.Select(ml).Columns(cols).Orderby("doneAt DESC").Limit("?,?").Prepare(), getOffset: acc.Select("moderation_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Orderby("doneAt DESC").Limit("?,?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
// TODO: Make a store for this? // TODO: Make a store for this?
func (s *SQLModLogStore) Create(action string, elementID int, elementType, ip string, actorID int) (err error) { func (store *SQLModLogStore) Create(action string, elementID int, elementType string, ipaddress string, actorID int) (err error) {
return s.CreateExtra(action, elementID, elementType, ip, actorID, "") _, err = store.create.Exec(action, elementID, elementType, ipaddress, actorID)
}
func (s *SQLModLogStore) CreateExtra(action string, elementID int, elementType, ip string, actorID int, extra string) (err error) {
_, err = s.create.Exec(action, elementID, elementType, ip, actorID, extra)
return err return err
} }
func (s *SQLModLogStore) Count() (count int) { func (store *SQLModLogStore) GlobalCount() (logCount int) {
err := s.count.QueryRow().Scan(&count) err := store.count.QueryRow().Scan(&logCount)
if err != nil { if err != nil {
LogError(err) LogError(err)
} }
return count return logCount
} }
func buildLogList(rows *sql.Rows) (logs []LogItem, err error) { func buildLogList(rows *sql.Rows) (logs []LogItem, err error) {
for rows.Next() { for rows.Next() {
var l LogItem var log LogItem
var doneAt time.Time err := rows.Scan(&log.Action, &log.ElementID, &log.ElementType, &log.IPAddress, &log.ActorID, &log.DoneAt)
err := rows.Scan(&l.Action, &l.ElementID, &l.ElementType, &l.IP, &l.ActorID, &doneAt, &l.Extra)
if err != nil { if err != nil {
return logs, err return logs, err
} }
l.DoneAt = doneAt.Format("2006-01-02 15:04:05") logs = append(logs, log)
logs = append(logs, l)
} }
return logs, rows.Err() return logs, rows.Err()
} }
func (s *SQLModLogStore) GetOffset(offset, perPage int) (logs []LogItem, err error) { func (store *SQLModLogStore) GetOffset(offset int, perPage int) (logs []LogItem, err error) {
rows, err := s.getOffset.Query(offset, perPage) rows, err := store.getOffset.Query(offset, perPage)
if err != nil { if err != nil {
return logs, err return logs, err
} }
@ -92,35 +80,29 @@ type SQLAdminLogStore struct {
} }
func NewAdminLogStore(acc *qgen.Accumulator) (*SQLAdminLogStore, error) { func NewAdminLogStore(acc *qgen.Accumulator) (*SQLAdminLogStore, error) {
al := "administration_logs"
cols := "action, elementID, elementType, ipaddress, actorID, doneAt, extra"
return &SQLAdminLogStore{ return &SQLAdminLogStore{
create: acc.Insert(al).Columns(cols).Fields("?,?,?,?,?,UTC_TIMESTAMP(),?").Prepare(), create: acc.Insert("administration_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(),
count: acc.Count(al).Prepare(), count: acc.Count("administration_logs").Prepare(),
getOffset: acc.Select(al).Columns(cols).Orderby("doneAt DESC").Limit("?,?").Prepare(), getOffset: acc.Select("administration_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Orderby("doneAt DESC").Limit("?,?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
// TODO: Make a store for this? // TODO: Make a store for this?
func (s *SQLAdminLogStore) Create(action string, elementID int, elementType, ip string, actorID int) (err error) { func (store *SQLAdminLogStore) Create(action string, elementID int, elementType string, ipaddress string, actorID int) (err error) {
return s.CreateExtra(action, elementID, elementType, ip, actorID, "") _, err = store.create.Exec(action, elementID, elementType, ipaddress, actorID)
}
func (s *SQLAdminLogStore) CreateExtra(action string, elementID int, elementType, ip string, actorID int, extra string) (err error) {
_, err = s.create.Exec(action, elementID, elementType, ip, actorID, extra)
return err return err
} }
func (s *SQLAdminLogStore) Count() (count int) { func (store *SQLAdminLogStore) GlobalCount() (logCount int) {
err := s.count.QueryRow().Scan(&count) err := store.count.QueryRow().Scan(&logCount)
if err != nil { if err != nil {
LogError(err) LogError(err)
} }
return count return logCount
} }
func (s *SQLAdminLogStore) GetOffset(offset, perPage int) (logs []LogItem, err error) { func (store *SQLAdminLogStore) GetOffset(offset int, perPage int) (logs []LogItem, err error) {
rows, err := s.getOffset.Query(offset, perPage) rows, err := store.getOffset.Query(offset, perPage)
if err != nil { if err != nil {
return logs, err return logs, err
} }

View File

@ -1,7 +1,7 @@
/* /*
* *
* Gosora Authentication Interface * Gosora Authentication Interface
* Copyright Azareal 2017 - 2020 * Copyright Azareal 2017 - 2019
* *
*/ */
package common package common
@ -16,9 +16,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"git.tuxpa.in/a/gosora/common/gauth" "../query_gen/lib"
qgen "git.tuxpa.in/a/gosora/query_gen" "./gauth"
//"golang.org/x/crypto/argon2" //"golang.org/x/crypto/argon2"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
@ -41,7 +40,6 @@ var ErrPasswordTooLong = errors.New("The password you selected is too long")
var ErrWrongPassword = errors.New("That's not the correct password.") var ErrWrongPassword = errors.New("That's not the correct password.")
var ErrBadMFAToken = errors.New("I'm not sure where you got that from, but that's not a valid 2FA token") var ErrBadMFAToken = errors.New("I'm not sure where you got that from, but that's not a valid 2FA token")
var ErrWrongMFAToken = errors.New("That 2FA token isn't correct") var ErrWrongMFAToken = errors.New("That 2FA token isn't correct")
var ErrNoMFAToken = errors.New("This user doesn't have 2FA setup")
var ErrSecretError = errors.New("There was a glitch in the system. Please contact your local administrator.") var ErrSecretError = errors.New("There was a glitch in the system. Please contact your local administrator.")
var ErrNoUserByName = errors.New("We couldn't find an account with that username.") var ErrNoUserByName = errors.New("We couldn't find an account with that username.")
var DefaultHashAlgo = "bcrypt" // Override this in the configuration file, not here var DefaultHashAlgo = "bcrypt" // Override this in the configuration file, not here
@ -66,16 +64,16 @@ var HashPrefixes = map[string]string{
// AuthInt is the main authentication interface. // AuthInt is the main authentication interface.
type AuthInt interface { type AuthInt interface {
Authenticate(name, password string) (uid int, err error, requiresExtraAuth bool) Authenticate(username string, password string) (uid int, err error, requiresExtraAuth bool)
ValidateMFAToken(mfaToken string, uid int) error ValidateMFAToken(mfaToken string, uid int) error
Logout(w http.ResponseWriter, uid int) Logout(w http.ResponseWriter, uid int)
ForceLogout(uid int) error ForceLogout(uid int) error
SetCookies(w http.ResponseWriter, uid int, session string) SetCookies(w http.ResponseWriter, uid int, session string)
SetProvisionalCookies(w http.ResponseWriter, uid int, session, signedSession string) // To avoid logging someone in until they've passed the MFA check SetProvisionalCookies(w http.ResponseWriter, uid int, session string, signedSession string) // To avoid logging someone in until they've passed the MFA check
GetCookies(r *http.Request) (uid int, session string, err error) GetCookies(r *http.Request) (uid int, session string, err error)
SessionCheck(w http.ResponseWriter, r *http.Request) (u *User, halt bool) SessionCheck(w http.ResponseWriter, r *http.Request) (user *User, halt bool)
CreateSession(uid int) (session string, err error) CreateSession(uid int) (session string, err error)
CreateProvisionalSession(uid int) (provSession, signedSession string, err error) // To avoid logging someone in until they've passed the MFA check CreateProvisionalSession(uid int) (provSession string, signedSession string, err error) // To avoid logging someone in until they've passed the MFA check
} }
// DefaultAuth is the default authenticator used by Gosora, may be swapped with an alternate authenticator in some situations. E.g. To support LDAP. // DefaultAuth is the default authenticator used by Gosora, may be swapped with an alternate authenticator in some situations. E.g. To support LDAP.
@ -98,9 +96,9 @@ func NewDefaultAuth() (*DefaultAuth, error) {
// Authenticate checks if a specific username and password is valid and returns the UID for the corresponding user, if so. Otherwise, a user safe error. // Authenticate checks if a specific username and password is valid and returns the UID for the corresponding user, if so. Otherwise, a user safe error.
// IF MFA is enabled, then pass it back a flag telling the caller that authentication isn't complete yet // IF MFA is enabled, then pass it back a flag telling the caller that authentication isn't complete yet
// TODO: Find a better way of handling errors we don't want to reach the user // TODO: Find a better way of handling errors we don't want to reach the user
func (auth *DefaultAuth) Authenticate(name, password string) (uid int, err error, requiresExtraAuth bool) { func (auth *DefaultAuth) Authenticate(username string, password string) (uid int, err error, requiresExtraAuth bool) {
var realPassword, salt string var realPassword, salt string
err = auth.login.QueryRow(name).Scan(&uid, &realPassword, &salt) err = auth.login.QueryRow(username).Scan(&uid, &realPassword, &salt)
if err == ErrNoRows { if err == ErrNoRows {
return 0, ErrNoUserByName, false return 0, ErrNoUserByName, false
} else if err != nil { } else if err != nil {
@ -134,10 +132,7 @@ func (auth *DefaultAuth) ValidateMFAToken(mfaToken string, uid int) error {
LogError(err) LogError(err)
return ErrSecretError return ErrSecretError
} }
if err == ErrNoRows { if err != ErrNoRows {
return ErrNoMFAToken
}
ok, err := VerifyGAuthToken(mfaItem.Secret, mfaToken) ok, err := VerifyGAuthToken(mfaItem.Secret, mfaToken)
if err != nil { if err != nil {
return ErrBadMFAToken return ErrBadMFAToken
@ -145,7 +140,6 @@ func (auth *DefaultAuth) ValidateMFAToken(mfaToken string, uid int) error {
if ok { if ok {
return nil return nil
} }
for i, scratch := range mfaItem.Scratch { for i, scratch := range mfaItem.Scratch {
if subtle.ConstantTimeCompare([]byte(scratch), []byte(mfaToken)) == 1 { if subtle.ConstantTimeCompare([]byte(scratch), []byte(mfaToken)) == 1 {
err = mfaItem.BurnScratch(i) err = mfaItem.BurnScratch(i)
@ -156,7 +150,7 @@ func (auth *DefaultAuth) ValidateMFAToken(mfaToken string, uid int) error {
return nil return nil
} }
} }
}
return ErrWrongMFAToken return ErrWrongMFAToken
} }
@ -169,9 +163,11 @@ func (auth *DefaultAuth) ForceLogout(uid int) error {
} }
// Flush the user out of the cache // Flush the user out of the cache
if uc := Users.GetCache(); uc != nil { ucache := Users.GetCache()
uc.Remove(uid) if ucache != nil {
ucache.Remove(uid)
} }
return nil return nil
} }
@ -211,7 +207,7 @@ func (auth *DefaultAuth) SetCookies(w http.ResponseWriter, uid int, session stri
// TODO: Set the cookie domain // TODO: Set the cookie domain
// SetProvisionalCookies sets the two cookies required for guests to be recognised as having passed the initial login but not having passed the additional checks (e.g. multi-factor authentication) // SetProvisionalCookies sets the two cookies required for guests to be recognised as having passed the initial login but not having passed the additional checks (e.g. multi-factor authentication)
func (auth *DefaultAuth) SetProvisionalCookies(w http.ResponseWriter, uid int, provSession, signedSession string) { func (auth *DefaultAuth) SetProvisionalCookies(w http.ResponseWriter, uid int, provSession string, signedSession string) {
cookie := http.Cookie{Name: "uid", Value: strconv.Itoa(uid), Path: "/", MaxAge: int(Year)} cookie := http.Cookie{Name: "uid", Value: strconv.Itoa(uid), Path: "/", MaxAge: int(Year)}
setCookie(w, &cookie, "lax") setCookie(w, &cookie, "lax")
cookie = http.Cookie{Name: "provSession", Value: provSession, Path: "/", MaxAge: int(Year)} cookie = http.Cookie{Name: "provSession", Value: provSession, Path: "/", MaxAge: int(Year)}
@ -282,7 +278,7 @@ func (auth *DefaultAuth) CreateSession(uid int) (session string, err error) {
return session, nil return session, nil
} }
func (auth *DefaultAuth) CreateProvisionalSession(uid int) (provSession, signedSession string, err error) { func (auth *DefaultAuth) CreateProvisionalSession(uid int) (provSession string, signedSession string, err error) {
provSession, err = GenerateSafeString(SessionLength) provSession, err = GenerateSafeString(SessionLength)
if err != nil { if err != nil {
return "", "", err return "", "", err
@ -295,7 +291,7 @@ func (auth *DefaultAuth) CreateProvisionalSession(uid int) (provSession, signedS
return provSession, hex.EncodeToString(h.Sum(nil)), nil return provSession, hex.EncodeToString(h.Sum(nil)), nil
} }
func CheckPassword(realPassword, password, salt string) (err error) { func CheckPassword(realPassword string, password string, salt string) (err error) {
blasted := strings.Split(realPassword, "$") blasted := strings.Split(realPassword, "$")
prefix := blasted[0] prefix := blasted[0]
if len(blasted) > 1 { if len(blasted) > 1 {
@ -309,7 +305,7 @@ func CheckPassword(realPassword, password, salt string) (err error) {
return checker(realPassword, password, salt) return checker(realPassword, password, salt)
} }
func GeneratePassword(password string) (hash, salt string, err error) { func GeneratePassword(password string) (hash string, salt string, err error) {
gen, ok := GeneratePasswordFuncs[DefaultHashAlgo] gen, ok := GeneratePasswordFuncs[DefaultHashAlgo]
if !ok { if !ok {
return "", "", ErrHashNotExist return "", "", ErrHashNotExist
@ -317,12 +313,12 @@ func GeneratePassword(password string) (hash, salt string, err error) {
return gen(password) return gen(password)
} }
func BcryptCheckPassword(realPassword, password, salt string) (err error) { func BcryptCheckPassword(realPassword string, password string, salt string) (err error) {
return bcrypt.CompareHashAndPassword([]byte(realPassword), []byte(password+salt)) return bcrypt.CompareHashAndPassword([]byte(realPassword), []byte(password+salt))
} }
// Note: The salt is in the hash, therefore the salt parameter is blank // Note: The salt is in the hash, therefore the salt parameter is blank
func BcryptGeneratePassword(password string) (hash, salt string, err error) { func BcryptGeneratePassword(password string) (hash string, salt string, err error) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil { if err != nil {
return "", "", err return "", "", err
@ -337,7 +333,7 @@ func BcryptGeneratePassword(password string) (hash, salt string, err error) {
argon2KeyLen uint32 = 32 argon2KeyLen uint32 = 32
) )
func Argon2CheckPassword(realPassword, password, salt string) (err error) { func Argon2CheckPassword(realPassword string, password string, salt string) (err error) {
split := strings.Split(realPassword, "$") split := strings.Split(realPassword, "$")
// TODO: Better validation // TODO: Better validation
if len(split) < 5 { if len(split) < 5 {
@ -355,7 +351,7 @@ func Argon2CheckPassword(realPassword, password, salt string) (err error) {
return nil return nil
} }
func Argon2GeneratePassword(password string) (hash, salt string, err error) { func Argon2GeneratePassword(password string) (hash string, salt string, err error) {
sbytes := make([]byte, SaltLength) sbytes := make([]byte, SaltLength)
_, err = rand.Read(sbytes) _, err = rand.Read(sbytes)
if err != nil { if err != nil {
@ -380,7 +376,7 @@ func FriendlyGAuthSecret(secret string) (out string) {
func GenerateGAuthSecret() (string, error) { func GenerateGAuthSecret() (string, error) {
return GenerateStd32SafeString(14) return GenerateStd32SafeString(14)
} }
func VerifyGAuthToken(secret, token string) (bool, error) { func VerifyGAuthToken(secret string, token string) (bool, error) {
trueToken, err := gauth.GetTOTPToken(secret) trueToken, err := gauth.GetTOTPToken(secret)
return subtle.ConstantTimeCompare([]byte(trueToken), []byte(token)) == 1, err return subtle.ConstantTimeCompare([]byte(trueToken), []byte(token)) == 1, err
} }

View File

@ -14,7 +14,7 @@ type DataStore interface {
DirtyGet(id int) interface{} DirtyGet(id int) interface{}
Get(id int) (interface{}, error) Get(id int) (interface{}, error)
BypassGet(id int) (interface{}, error) BypassGet(id int) (interface{}, error)
//Count() int //GlobalCount()
} }
// nolint // nolint

View File

@ -1,70 +1,42 @@
/* package common
*
* Gosora Common Resources
* Copyright Azareal 2018 - 2020
*
*/
package common // import "git.tuxpa.in/a/gosora/common"
import ( import (
"database/sql" "database/sql"
"io"
"log" "log"
"net"
"net/http"
"os"
"runtime/debug"
"strconv"
"strings"
"sync/atomic" "sync/atomic"
"time" "time"
meta "git.tuxpa.in/a/gosora/common/meta" "../query_gen/lib"
qgen "git.tuxpa.in/a/gosora/query_gen"
) )
var SoftwareVersion = Version{Major: 0, Minor: 3, Patch: 0, Tag: "dev"}
var Meta meta.MetaStore
// nolint I don't want to write comments for each of these o.o // nolint I don't want to write comments for each of these o.o
const Hour int = 60 * 60 const Hour int = 60 * 60
const Day = Hour * 24 const Day int = Hour * 24
const Week = Day * 7 const Week int = Day * 7
const Month = Day * 30 const Month int = Day * 30
const Year = Day * 365 const Year int = Day * 365
const Kilobyte int = 1024 const Kilobyte int = 1024
const Megabyte = Kilobyte * 1024 const Megabyte int = Kilobyte * 1024
const Gigabyte = Megabyte * 1024 const Gigabyte int = Megabyte * 1024
const Terabyte = Gigabyte * 1024 const Terabyte int = Gigabyte * 1024
const Petabyte = Terabyte * 1024 const Petabyte int = Terabyte * 1024
var StartTime time.Time var StartTime time.Time
var GzipStartEtag string
var StartEtag string
var TmplPtrMap = make(map[string]interface{}) var TmplPtrMap = make(map[string]interface{})
// Anti-spam token with rotated key // Anti-spam token with rotated key
var JSTokenBox atomic.Value // TODO: Move this and some of these other globals somewhere else var JSTokenBox atomic.Value // TODO: Move this and some of these other globals somewhere else
var SessionSigningKeyBox atomic.Value // For MFA to avoid hitting the database unneccessarily var SessionSigningKeyBox atomic.Value // For MFA to avoid hitting the database unneccesarily
var OldSessionSigningKeyBox atomic.Value // Just in case we've signed with a key that's about to go stale so we don't annoy the user too much var OldSessionSigningKeyBox atomic.Value // Just in case we've signed with a key that's about to go stale so we don't annoy the user too much
var IsDBDown int32 = 0 // 0 = false, 1 = true. this is value which should be manipulated with package atomic for representing whether the database is down so we don't spam the log with lots of redundant errors var IsDBDown int32 = 0 // 0 = false, 1 = true. this is value which should be manipulated with package atomic for representing whether the database is down so we don't spam the log with lots of redundant errors
// ErrNoRows is an alias of sql.ErrNoRows, just in case we end up with non-database/sql datastores // ErrNoRows is an alias of sql.ErrNoRows, just in case we end up with non-database/sql datastores
var ErrNoRows = sql.ErrNoRows var ErrNoRows = sql.ErrNoRows
//var StrSlicePool sync.Pool
// ? - Make this more customisable? // ? - Make this more customisable?
/*var ExternalSites = map[string]string{ var ExternalSites = map[string]string{
"YT": "https://www.youtube.com/", "YT": "https://www.youtube.com/",
}*/ }
// TODO: Make this more customisable
var SpammyDomainBits = []string{"porn", "sex", "acup", "nude", "milf", "tits", "vape", "busty", "kink", "lingerie", "strapon", "problog", "fet", "xblog", "blogin", "blognetwork", "relayblog"}
var Chrome, Firefox int // ! Temporary Hack for http push
var SimpleBots []int // ! Temporary hack to stop semrush, ahrefs, python bots and other from wasting resources
type StringList []string type StringList []string
@ -72,40 +44,22 @@ type StringList []string
// TODO: Let admins manage this from the Control Panel // TODO: Let admins manage this from the Control Panel
// apng is commented out for now, as we have no way of re-encoding it into a smaller file // apng is commented out for now, as we have no way of re-encoding it into a smaller file
var AllowedFileExts = StringList{ var AllowedFileExts = StringList{
"png", "jpg", "jpe", "jpeg", "jif", "jfi", "jfif", "svg", "bmp", "gif", "tiff", "tif", "webp", "apng", "avif", "flif", "heif", "heic", "bpg", // images (encodable) + apng (browser support) + bpg + avif + flif + heif / heic "png", "jpg", "jpeg", "svg", "bmp", "gif", "tif", "webp", /*"apng",*/ // images
"txt", "xml", "json", "yaml", "toml", "ini", "md", "html", "rtf", "js", "py", "rb", "css", "scss", "less", "eqcss", "pcss", "java", "ts", "cs", "c", "cc", "cpp", "cxx", "C", "c++", "h", "hh", "hpp", "hxx", "h++", "rs", "rlib", "htaccess", "gitignore", /*"go","php",*/ // text "txt", "xml", "json", "yaml", "toml", "ini", "md", "html", "rtf", "js", "py", "rb", "css", "scss", "less", "eqcss", "pcss", "java", "ts", "cs", "c", "cc", "cpp", "cxx", "C", "c++", "h", "hh", "hpp", "hxx", "h++", "rs", "rlib", "htaccess", "gitignore", /*"go","php",*/ // text
"wav", "mp3", "oga", "m4a", "flac", "ac3", "aac", "opus", // audio "mp3", "mp4", "avi", "wmv", "webm", // video
"mp4", "avi", "ogg", "ogv", "ogx", "wmv", "webm", "flv", "f4v", "xvid", "mov", "movie", "qt", // video
"otf", "woff2", "woff", "ttf", "eot", // fonts "otf", "woff2", "woff", "ttf", "eot", // fonts
"bz2", "zip", "zipx", "gz", "7z", "tar", "cab", "rar", "kgb", "pea", "xz", "zz", "tgz", "xpi", // archives
"docx", "pdf", // documents
} }
var ImageFileExts = StringList{ var ImageFileExts = StringList{
"png", "jpg", "jpe", "jpeg", "jif", "jfi", "jfif", "svg", "bmp", "gif", "tiff", "tif", "webp", /* "apng", "bpg", "avif", */ "png", "jpg", "jpeg", "svg", "bmp", "gif", "tif", "webp", /* "apng",*/
}
var TextFileExts = StringList{
"txt", "xml", "json", "yaml", "toml", "ini", "md", "html", "rtf", "js", "py", "rb", "css", "scss", "less", "eqcss", "pcss", "java", "ts", "cs", "c", "cc", "cpp", "cxx", "C", "c++", "h", "hh", "hpp", "hxx", "h++", "rs", "rlib", "htaccess", "gitignore", /*"go","php",*/
}
var VideoFileExts = StringList{
"mp4", "avi", "ogg", "ogv", "ogx", "wmv", "webm", "flv", "f4v", "xvid", "mov", "movie", "qt",
}
var WebVideoFileExts = StringList{
"mp4", "avi", "ogg", "ogv", "webm",
}
var WebAudioFileExts = StringList{
"wav", "mp3", "oga", "m4a", "flac",
} }
var ArchiveFileExts = StringList{ var ArchiveFileExts = StringList{
"bz2", "zip", "zipx", "gz", "7z", "tar", "cab", "rar", "kgb", "pea", "xz", "zz", "tgz", "xpi", "bz2", "zip", "gz", "7z", "tar", "cab",
} }
var ExecutableFileExts = StringList{ var ExecutableFileExts = StringList{
"exe", "jar", "phar", "shar", "iso", "apk", "deb", "exe", "jar", "phar", "shar", "iso",
} }
func init() { func init() {
@ -115,51 +69,31 @@ func init() {
} }
// TODO: Write a test for this // TODO: Write a test for this
func (sl StringList) Contains(needle string) bool { func (slice StringList) Contains(needle string) bool {
for _, it := range sl { for _, item := range slice {
if it == needle { if item == needle {
return true return true
} }
} }
return false return false
} }
/*var DbTables []string
var TableToID = make(map[string]int)
var IDToTable = make(map[int]string)
func InitTables(acc *qgen.Accumulator) error {
stmt := acc.Select("tables").Columns("id,name").Prepare()
if e := acc.FirstError(); e != nil {
return e
}
return eachall(stmt, func(r *sql.Rows) error {
var id int
var name string
if e := r.Scan(&id, &name); e != nil {
return e
}
TableToID[name] = id
IDToTable[id] = name
return nil
})
}*/
type dbInits []func(acc *qgen.Accumulator) error type dbInits []func(acc *qgen.Accumulator) error
var DbInits dbInits var DbInits dbInits
func (inits dbInits) Run() error { func (inits dbInits) Run() error {
for _, i := range inits { for _, init := range inits {
if e := i(qgen.NewAcc()); e != nil { err := init(qgen.NewAcc())
return e if err != nil {
return err
} }
} }
return nil return nil
} }
func (inits dbInits) Add(i ...func(acc *qgen.Accumulator) error) { func (inits dbInits) Add(init ...func(acc *qgen.Accumulator) error) {
DbInits = dbInits(append(DbInits, i...)) DbInits = dbInits(append(DbInits, init...))
} }
// TODO: Add a graceful shutdown function // TODO: Add a graceful shutdown function
@ -170,10 +104,6 @@ func StoppedServer(msg ...interface{}) {
var StopServerChan = make(chan []interface{}) var StopServerChan = make(chan []interface{})
var LogWriter = io.MultiWriter(os.Stdout)
var ErrLogWriter = io.MultiWriter(os.Stderr)
var ErrLogger = log.New(os.Stderr, "", log.LstdFlags)
func DebugDetail(args ...interface{}) { func DebugDetail(args ...interface{}) {
if Dev.SuperDebug { if Dev.SuperDebug {
log.Print(args...) log.Print(args...)
@ -197,141 +127,3 @@ func DebugLogf(str string, args ...interface{}) {
log.Printf(str, args...) log.Printf(str, args...)
} }
} }
func Log(args ...interface{}) {
log.Print(args...)
}
func Logf(str string, args ...interface{}) {
log.Printf(str, args...)
}
func Err(args ...interface{}) {
ErrLogger.Print(args...)
}
func Count(stmt *sql.Stmt) (count int) {
e := stmt.QueryRow().Scan(&count)
if e != nil {
LogError(e)
}
return count
}
func Countf(stmt *sql.Stmt, args ...interface{}) (count int) {
e := stmt.QueryRow(args...).Scan(&count)
if e != nil {
LogError(e)
}
return count
}
func Createf(stmt *sql.Stmt, args ...interface{}) (id int, e error) {
res, e := stmt.Exec(args...)
if e != nil {
return 0, e
}
id64, e := res.LastInsertId()
return int(id64), e
}
func eachall(stmt *sql.Stmt, f func(r *sql.Rows) error) error {
rows, e := stmt.Query()
if e != nil {
return e
}
defer rows.Close()
for rows.Next() {
if e := f(rows); e != nil {
return e
}
}
return rows.Err()
}
var qcache = []string{0: "?", 1: "?,?", 2: "?,?,?", 3: "?,?,?,?", 4: "?,?,?,?,?", 5: "?,?,?,?,?,?", 6: "?,?,?,?,?,?,?", 7: "?,?,?,?,?,?,?,?", 8: "?,?,?,?,?,?,?,?,?"}
func inqbuild(ids []int) ([]interface{}, string) {
if len(ids) < 8 {
idList := make([]interface{}, len(ids))
for i, id := range ids {
idList[i] = strconv.Itoa(id)
}
return idList, qcache[len(ids)-1]
}
var sb strings.Builder
sb.Grow((len(ids) * 2) - 1)
idList := make([]interface{}, len(ids))
for i, id := range ids {
idList[i] = strconv.Itoa(id)
if i == 0 {
sb.WriteRune('?')
} else {
sb.WriteString(",?")
}
}
return idList, sb.String()
}
func inqbuild2(count int) string {
if count <= 8 {
return qcache[count-1]
}
var sb strings.Builder
sb.Grow((count * 2) - 1)
for i := 0; i < count; i++ {
if i == 0 {
sb.WriteRune('?')
} else {
sb.WriteString(",?")
}
}
return sb.String()
}
func inqbuildstr(strs []string) ([]interface{}, string) {
if len(strs) < 8 {
idList := make([]interface{}, len(strs))
for i, id := range strs {
idList[i] = id
}
return idList, qcache[len(strs)-1]
}
var sb strings.Builder
sb.Grow((len(strs) * 2) - 1)
idList := make([]interface{}, len(strs))
for i, id := range strs {
idList[i] = id
if i == 0 {
sb.WriteRune('?')
} else {
sb.WriteString(",?")
}
}
return idList, sb.String()
}
var ConnWatch = &ConnWatcher{}
type ConnWatcher struct {
n int64
}
func (cw *ConnWatcher) StateChange(conn net.Conn, state http.ConnState) {
switch state {
case http.StateNew:
atomic.AddInt64(&cw.n, 1)
case http.StateHijacked, http.StateClosed:
atomic.AddInt64(&cw.n, -1)
}
}
func (cw *ConnWatcher) Count() int {
return int(atomic.LoadInt64(&cw.n))
}
func EatPanics() {
if r := recover(); r != nil {
log.Print(r)
debug.PrintStack()
log.Fatal("Fatal error.")
}
}

View File

@ -1,628 +0,0 @@
// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
package common
import (
json "encoding/json"
easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)
// suppress unused package warning
var (
_ *json.RawMessage
_ *jlexer.Lexer
_ *jwriter.Writer
_ easyjson.Marshaler
)
func easyjsonC803d3e7DecodeGithubComAzarealGosoraCommon(in *jlexer.Lexer, out *WsTopicList) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "Topics":
if in.IsNull() {
in.Skip()
out.Topics = nil
} else {
in.Delim('[')
if out.Topics == nil {
if !in.IsDelim(']') {
out.Topics = make([]*WsTopicsRow, 0, 8)
} else {
out.Topics = []*WsTopicsRow{}
}
} else {
out.Topics = (out.Topics)[:0]
}
for !in.IsDelim(']') {
var v1 *WsTopicsRow
if in.IsNull() {
in.Skip()
v1 = nil
} else {
if v1 == nil {
v1 = new(WsTopicsRow)
}
easyjsonC803d3e7DecodeGithubComAzarealGosoraCommon1(in, v1)
}
out.Topics = append(out.Topics, v1)
in.WantComma()
}
in.Delim(']')
}
case "LastPage":
out.LastPage = int(in.Int())
case "LastUpdate":
out.LastUpdate = int64(in.Int64())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjsonC803d3e7EncodeGithubComAzarealGosoraCommon(out *jwriter.Writer, in WsTopicList) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"Topics\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
if in.Topics == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 {
out.RawString("null")
} else {
out.RawByte('[')
for v2, v3 := range in.Topics {
if v2 > 0 {
out.RawByte(',')
}
if v3 == nil {
out.RawString("null")
} else {
easyjsonC803d3e7EncodeGithubComAzarealGosoraCommon1(out, *v3)
}
}
out.RawByte(']')
}
}
{
const prefix string = ",\"LastPage\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.LastPage))
}
{
const prefix string = ",\"LastUpdate\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int64(int64(in.LastUpdate))
}
out.RawByte('}')
}
// MarshalJSON supports json.Marshaler interface
func (v WsTopicList) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
easyjsonC803d3e7EncodeGithubComAzarealGosoraCommon(&w, v)
return w.Buffer.BuildBytes(), w.Error
}
// MarshalEasyJSON supports easyjson.Marshaler interface
func (v WsTopicList) MarshalEasyJSON(w *jwriter.Writer) {
easyjsonC803d3e7EncodeGithubComAzarealGosoraCommon(w, v)
}
// UnmarshalJSON supports json.Unmarshaler interface
func (v *WsTopicList) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
easyjsonC803d3e7DecodeGithubComAzarealGosoraCommon(&r, v)
return r.Error()
}
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (v *WsTopicList) UnmarshalEasyJSON(l *jlexer.Lexer) {
easyjsonC803d3e7DecodeGithubComAzarealGosoraCommon(l, v)
}
func easyjsonC803d3e7DecodeGithubComAzarealGosoraCommon1(in *jlexer.Lexer, out *WsTopicsRow) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "ID":
out.ID = int(in.Int())
case "Link":
out.Link = string(in.String())
case "Title":
out.Title = string(in.String())
case "CreatedBy":
out.CreatedBy = int(in.Int())
case "IsClosed":
out.IsClosed = bool(in.Bool())
case "Sticky":
out.Sticky = bool(in.Bool())
case "CreatedAt":
if data := in.Raw(); in.Ok() {
in.AddError((out.CreatedAt).UnmarshalJSON(data))
}
case "LastReplyAt":
if data := in.Raw(); in.Ok() {
in.AddError((out.LastReplyAt).UnmarshalJSON(data))
}
case "RelativeLastReplyAt":
out.RelativeLastReplyAt = string(in.String())
case "LastReplyBy":
out.LastReplyBy = int(in.Int())
case "LastReplyID":
out.LastReplyID = int(in.Int())
case "ParentID":
out.ParentID = int(in.Int())
case "ViewCount":
out.ViewCount = int64(in.Int64())
case "PostCount":
out.PostCount = int(in.Int())
case "LikeCount":
out.LikeCount = int(in.Int())
case "AttachCount":
out.AttachCount = int(in.Int())
case "ClassName":
out.ClassName = string(in.String())
case "Creator":
if in.IsNull() {
in.Skip()
out.Creator = nil
} else {
if out.Creator == nil {
out.Creator = new(WsJSONUser)
}
easyjsonC803d3e7DecodeGithubComAzarealGosoraCommon2(in, out.Creator)
}
case "LastUser":
if in.IsNull() {
in.Skip()
out.LastUser = nil
} else {
if out.LastUser == nil {
out.LastUser = new(WsJSONUser)
}
easyjsonC803d3e7DecodeGithubComAzarealGosoraCommon2(in, out.LastUser)
}
case "ForumName":
out.ForumName = string(in.String())
case "ForumLink":
out.ForumLink = string(in.String())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjsonC803d3e7EncodeGithubComAzarealGosoraCommon1(out *jwriter.Writer, in WsTopicsRow) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"ID\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.ID))
}
{
const prefix string = ",\"Link\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Link))
}
{
const prefix string = ",\"Title\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Title))
}
{
const prefix string = ",\"CreatedBy\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.CreatedBy))
}
{
const prefix string = ",\"IsClosed\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Bool(bool(in.IsClosed))
}
{
const prefix string = ",\"Sticky\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Bool(bool(in.Sticky))
}
{
const prefix string = ",\"CreatedAt\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Raw((in.CreatedAt).MarshalJSON())
}
{
const prefix string = ",\"LastReplyAt\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Raw((in.LastReplyAt).MarshalJSON())
}
{
const prefix string = ",\"RelativeLastReplyAt\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.RelativeLastReplyAt))
}
{
const prefix string = ",\"LastReplyBy\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.LastReplyBy))
}
{
const prefix string = ",\"LastReplyID\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.LastReplyID))
}
{
const prefix string = ",\"ParentID\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.ParentID))
}
{
const prefix string = ",\"ViewCount\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int64(int64(in.ViewCount))
}
{
const prefix string = ",\"PostCount\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.PostCount))
}
{
const prefix string = ",\"LikeCount\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.LikeCount))
}
{
const prefix string = ",\"AttachCount\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.AttachCount))
}
{
const prefix string = ",\"ClassName\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.ClassName))
}
{
const prefix string = ",\"Creator\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
if in.Creator == nil {
out.RawString("null")
} else {
easyjsonC803d3e7EncodeGithubComAzarealGosoraCommon2(out, *in.Creator)
}
}
{
const prefix string = ",\"LastUser\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
if in.LastUser == nil {
out.RawString("null")
} else {
easyjsonC803d3e7EncodeGithubComAzarealGosoraCommon2(out, *in.LastUser)
}
}
{
const prefix string = ",\"ForumName\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.ForumName))
}
{
const prefix string = ",\"ForumLink\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.ForumLink))
}
out.RawByte('}')
}
func easyjsonC803d3e7DecodeGithubComAzarealGosoraCommon2(in *jlexer.Lexer, out *WsJSONUser) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "ID":
out.ID = int(in.Int())
case "Link":
out.Link = string(in.String())
case "Name":
out.Name = string(in.String())
case "Group":
out.Group = int(in.Int())
case "IsMod":
out.IsMod = bool(in.Bool())
case "Avatar":
out.Avatar = string(in.String())
case "MicroAvatar":
out.MicroAvatar = string(in.String())
case "Level":
out.Level = int(in.Int())
case "Score":
out.Score = int(in.Int())
case "Liked":
out.Liked = int(in.Int())
default:
in.SkipRecursive()
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
func easyjsonC803d3e7EncodeGithubComAzarealGosoraCommon2(out *jwriter.Writer, in WsJSONUser) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = ",\"ID\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.ID))
}
{
const prefix string = ",\"Link\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Link))
}
{
const prefix string = ",\"Name\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Name))
}
{
const prefix string = ",\"Group\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.Group))
}
{
const prefix string = ",\"IsMod\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Bool(bool(in.IsMod))
}
{
const prefix string = ",\"Avatar\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.Avatar))
}
{
const prefix string = ",\"MicroAvatar\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.String(string(in.MicroAvatar))
}
{
const prefix string = ",\"Level\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.Level))
}
{
const prefix string = ",\"Score\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.Score))
}
{
const prefix string = ",\"Liked\":"
if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
}
out.Int(int(in.Liked))
}
out.RawByte('}')
}

View File

@ -1,371 +0,0 @@
package common
import (
"errors"
"time"
//"log"
"database/sql"
"strconv"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var Convos ConversationStore
var convoStmts ConvoStmts
type ConvoStmts struct {
fetchPost *sql.Stmt
getPosts *sql.Stmt
countPosts *sql.Stmt
edit *sql.Stmt
create *sql.Stmt
delete *sql.Stmt
has *sql.Stmt
editPost *sql.Stmt
createPost *sql.Stmt
deletePost *sql.Stmt
getUsers *sql.Stmt
}
func init() {
DbInits.Add(func(acc *qgen.Accumulator) error {
cpo := "conversations_posts"
convoStmts = ConvoStmts{
fetchPost: acc.Select(cpo).Columns("cid,body,post,createdBy").Where("pid=?").Prepare(),
getPosts: acc.Select(cpo).Columns("pid,body,post,createdBy").Where("cid=?").Limit("?,?").Prepare(),
countPosts: acc.Count(cpo).Where("cid=?").Prepare(),
edit: acc.Update("conversations").Set("lastReplyBy=?,lastReplyAt=?").Where("cid=?").Prepare(),
create: acc.Insert("conversations").Columns("createdAt,lastReplyAt").Fields("UTC_TIMESTAMP(),UTC_TIMESTAMP()").Prepare(),
has: acc.Count("conversations_participants").Where("uid=? AND cid=?").Prepare(),
editPost: acc.Update(cpo).Set("body=?,post=?").Where("pid=?").Prepare(),
createPost: acc.Insert(cpo).Columns("cid,body,post,createdBy").Fields("?,?,?,?").Prepare(),
deletePost: acc.Delete(cpo).Where("pid=?").Prepare(),
getUsers: acc.Select("conversations_participants").Columns("uid").Where("cid=?").Prepare(),
}
return acc.FirstError()
})
}
type Conversation struct {
ID int
Link string
CreatedBy int
CreatedAt time.Time
LastReplyBy int
LastReplyAt time.Time
}
func (co *Conversation) Posts(offset, itemsPerPage int) (posts []*ConversationPost, err error) {
rows, err := convoStmts.getPosts.Query(co.ID, offset, itemsPerPage)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
p := &ConversationPost{CID: co.ID}
err := rows.Scan(&p.ID, &p.Body, &p.Post, &p.CreatedBy)
if err != nil {
return nil, err
}
p, err = ConvoPostProcess.OnLoad(p)
if err != nil {
return nil, err
}
posts = append(posts, p)
}
return posts, rows.Err()
}
func (co *Conversation) PostsCount() (count int) {
return Countf(convoStmts.countPosts, co.ID)
}
func (co *Conversation) Uids() (ids []int, err error) {
rows, e := convoStmts.getUsers.Query(co.ID)
if e != nil {
return nil, e
}
defer rows.Close()
for rows.Next() {
var id int
if e := rows.Scan(&id); e != nil {
return nil, e
}
ids = append(ids, id)
}
return ids, rows.Err()
}
func (co *Conversation) Has(uid int) (in bool) {
return Countf(convoStmts.has, uid, co.ID) > 0
}
func (co *Conversation) Update() error {
_, err := convoStmts.edit.Exec(co.CreatedAt, co.LastReplyBy, co.LastReplyAt, co.ID)
return err
}
func (co *Conversation) Create() (int, error) {
res, err := convoStmts.create.Exec()
if err != nil {
return 0, err
}
lastID, err := res.LastInsertId()
return int(lastID), err
}
func BuildConvoURL(coid int) string {
return "/user/convo/" + strconv.Itoa(coid)
}
type ConversationExtra struct {
*Conversation
Users []*User
}
type ConversationStore interface {
Get(id int) (*Conversation, error)
GetUser(uid, offset int) (cos []*Conversation, err error)
GetUserExtra(uid, offset int) (cos []*ConversationExtra, err error)
GetUserCount(uid int) (count int)
Delete(id int) error
Count() (count int)
Create(content string, createdBy int, participants []int) (int, error)
}
type DefaultConversationStore struct {
get *sql.Stmt
getUser *sql.Stmt
getUserCount *sql.Stmt
delete *sql.Stmt
deletePosts *sql.Stmt
deleteParticipants *sql.Stmt
create *sql.Stmt
addParticipant *sql.Stmt
count *sql.Stmt
}
func NewDefaultConversationStore(acc *qgen.Accumulator) (*DefaultConversationStore, error) {
co := "conversations"
return &DefaultConversationStore{
get: acc.Select(co).Columns("createdBy,createdAt,lastReplyBy,lastReplyAt").Where("cid=?").Prepare(),
getUser: acc.SimpleInnerJoin("conversations_participants AS cp", "conversations AS c", "cp.cid, c.createdBy, c.createdAt, c.lastReplyBy, c.lastReplyAt", "cp.cid=c.cid", "cp.uid=?", "c.lastReplyAt DESC, c.createdAt DESC, c.cid DESC", "?,?"),
getUserCount: acc.Count("conversations_participants").Where("uid=?").Prepare(),
delete: acc.Delete(co).Where("cid=?").Prepare(),
deletePosts: acc.Delete("conversations_posts").Where("cid=?").Prepare(),
deleteParticipants: acc.Delete("conversations_participants").Where("cid=?").Prepare(),
create: acc.Insert(co).Columns("createdBy,createdAt,lastReplyBy,lastReplyAt").Fields("?,UTC_TIMESTAMP(),?,UTC_TIMESTAMP()").Prepare(),
addParticipant: acc.Insert("conversations_participants").Columns("uid,cid").Fields("?,?").Prepare(),
count: acc.Count(co).Prepare(),
}, acc.FirstError()
}
func (s *DefaultConversationStore) Get(id int) (*Conversation, error) {
co := &Conversation{ID: id}
err := s.get.QueryRow(id).Scan(&co.CreatedBy, &co.CreatedAt, &co.LastReplyBy, &co.LastReplyAt)
co.Link = BuildConvoURL(co.ID)
return co, err
}
func (s *DefaultConversationStore) GetUser(uid, offset int) (cos []*Conversation, err error) {
rows, err := s.getUser.Query(uid, offset, Config.ItemsPerPage)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
co := &Conversation{}
err := rows.Scan(&co.ID, &co.CreatedBy, &co.CreatedAt, &co.LastReplyBy, &co.LastReplyAt)
if err != nil {
return nil, err
}
co.Link = BuildConvoURL(co.ID)
cos = append(cos, co)
}
err = rows.Err()
if err != nil {
return nil, err
}
if len(cos) == 0 {
err = sql.ErrNoRows
}
return cos, err
}
func (s *DefaultConversationStore) GetUserExtra(uid, offset int) (cos []*ConversationExtra, err error) {
raw, err := s.GetUser(uid, offset)
if err != nil {
return nil, err
}
//log.Printf("raw: %+v\n", raw)
if len(raw) == 1 {
//log.Print("r0b2")
uids, err := raw[0].Uids()
if err != nil {
return nil, err
}
//log.Println("r1b2")
umap, err := Users.BulkGetMap(uids)
if err != nil {
return nil, err
}
//log.Println("r2b2")
users := make([]*User, len(umap))
var i int
for _, user := range umap {
users[i] = user
i++
}
return []*ConversationExtra{{raw[0], users}}, nil
}
//log.Println("1")
cmap := make(map[int]*ConversationExtra, len(raw))
for _, co := range raw {
cmap[co.ID] = &ConversationExtra{co, nil}
}
// TODO: Use inqbuild for this or a similar function
var q string
idList := make([]interface{}, len(raw))
for i, co := range raw {
if i == 0 {
q = "?"
} else {
q += ",?"
}
idList[i] = strconv.Itoa(co.ID)
}
rows, err := qgen.NewAcc().Select("conversations_participants").Columns("uid,cid").Where("cid IN(" + q + ")").Query(idList...)
if err != nil {
return nil, err
}
defer rows.Close()
//log.Println("2")
idmap := make(map[int][]int) // cid: []uid
puidmap := make(map[int]struct{})
for rows.Next() {
var uid, cid int
err := rows.Scan(&uid, &cid)
if err != nil {
return nil, err
}
idmap[cid] = append(idmap[cid], uid)
puidmap[uid] = struct{}{}
}
if err = rows.Err(); err != nil {
return nil, err
}
//log.Println("3")
//log.Printf("idmap: %+v\n", idmap)
//log.Printf("puidmap: %+v\n",puidmap)
puids := make([]int, len(puidmap))
var i int
for puid, _ := range puidmap {
puids[i] = puid
i++
}
umap, err := Users.BulkGetMap(puids)
if err != nil {
return nil, err
}
//log.Println("4")
//log.Printf("umap: %+v\n", umap)
for cid, uids := range idmap {
co := cmap[cid]
for _, uid := range uids {
co.Users = append(co.Users, umap[uid])
}
//log.Printf("co.Conversation: %+v\n", co.Conversation)
//log.Printf("co.Users: %+v\n", co.Users)
cmap[cid] = co
}
//log.Printf("cmap: %+v\n", cmap)
for _, ra := range raw {
cos = append(cos, cmap[ra.ID])
}
//log.Printf("cos: %+v\n", cos)
return cos, rows.Err()
}
func (s *DefaultConversationStore) GetUserCount(uid int) (count int) {
err := s.getUserCount.QueryRow(uid).Scan(&count)
if err != nil {
LogError(err)
}
return count
}
// TODO: Use a foreign key or transaction
func (s *DefaultConversationStore) Delete(id int) error {
_, err := s.delete.Exec(id)
if err != nil {
return err
}
_, err = s.deletePosts.Exec(id)
if err != nil {
return err
}
_, err = s.deleteParticipants.Exec(id)
return err
}
func (s *DefaultConversationStore) Create(content string, createdBy int, participants []int) (int, error) {
if len(participants) == 0 {
return 0, errors.New("no participants set")
}
res, err := s.create.Exec(createdBy, createdBy)
if err != nil {
return 0, err
}
lastID, err := res.LastInsertId()
if err != nil {
return 0, err
}
post := &ConversationPost{CID: int(lastID), Body: content, CreatedBy: createdBy}
_, err = post.Create()
if err != nil {
return 0, err
}
for _, p := range participants {
if p == createdBy {
continue
}
_, err := s.addParticipant.Exec(p, lastID)
if err != nil {
return 0, err
}
}
_, err = s.addParticipant.Exec(createdBy, lastID)
if err != nil {
return 0, err
}
return int(lastID), err
}
// Count returns the total number of topics on these forums
func (s *DefaultConversationStore) Count() (count int) {
err := s.count.QueryRow().Scan(&count)
if err != nil {
LogError(err)
}
return count
}

View File

@ -1,142 +0,0 @@
package common
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"io"
)
var ConvoPostProcess ConvoPostProcessor = NewDefaultConvoPostProcessor()
type ConvoPostProcessor interface {
OnLoad(co *ConversationPost) (*ConversationPost, error)
OnSave(co *ConversationPost) (*ConversationPost, error)
}
type DefaultConvoPostProcessor struct {
}
func NewDefaultConvoPostProcessor() *DefaultConvoPostProcessor {
return &DefaultConvoPostProcessor{}
}
func (pr *DefaultConvoPostProcessor) OnLoad(co *ConversationPost) (*ConversationPost, error) {
return co, nil
}
func (pr *DefaultConvoPostProcessor) OnSave(co *ConversationPost) (*ConversationPost, error) {
return co, nil
}
type AesConvoPostProcessor struct {
}
func NewAesConvoPostProcessor() *AesConvoPostProcessor {
return &AesConvoPostProcessor{}
}
func (pr *AesConvoPostProcessor) OnLoad(co *ConversationPost) (*ConversationPost, error) {
if co.Post != "aes" {
return co, nil
}
key, _ := hex.DecodeString(Config.ConvoKey)
ciphertext, err := hex.DecodeString(co.Body)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonceSize := aesgcm.NonceSize()
if len(ciphertext) < nonceSize {
return nil, err
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
lco := *co
lco.Body = string(plaintext)
return &lco, nil
}
func (pr *AesConvoPostProcessor) OnSave(co *ConversationPost) (*ConversationPost, error) {
key, _ := hex.DecodeString(Config.ConvoKey)
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
nonce := make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
ciphertext := aesgcm.Seal(nil, nonce, []byte(co.Body), nil)
lco := *co
lco.Body = hex.EncodeToString(ciphertext)
lco.Post = "aes"
return &lco, nil
}
type ConversationPost struct {
ID int
CID int
Body string
Post string // aes, ''
CreatedBy int
}
// TODO: Should we run OnLoad on this? Or maybe add a FetchMeta method to avoid having to decode the message when it's not necessary?
func (co *ConversationPost) Fetch() error {
return convoStmts.fetchPost.QueryRow(co.ID).Scan(&co.CID, &co.Body, &co.Post, &co.CreatedBy)
}
func (co *ConversationPost) Update() error {
lco, err := ConvoPostProcess.OnSave(co)
if err != nil {
return err
}
//GetHookTable().VhookNoRet("convo_post_update", lco)
_, err = convoStmts.editPost.Exec(lco.Body, lco.Post, lco.ID)
return err
}
func (co *ConversationPost) Create() (int, error) {
lco, err := ConvoPostProcess.OnSave(co)
if err != nil {
return 0, err
}
//GetHookTable().VhookNoRet("convo_post_create", lco)
res, err := convoStmts.createPost.Exec(lco.CID, lco.Body, lco.Post, lco.CreatedBy)
if err != nil {
return 0, err
}
lastID, err := res.LastInsertId()
return int(lastID), err
}
func (co *ConversationPost) Delete() error {
_, err := convoStmts.deletePost.Exec(co.ID)
return err
}

View File

@ -2,57 +2,67 @@ package counters
import ( import (
"database/sql" "database/sql"
"sync/atomic"
c "git.tuxpa.in/a/gosora/common" ".."
qgen "git.tuxpa.in/a/gosora/query_gen" "../../query_gen/lib"
"github.com/pkg/errors"
) )
var AgentViewCounter *DefaultAgentViewCounter var AgentViewCounter *DefaultAgentViewCounter
type DefaultAgentViewCounter struct { type DefaultAgentViewCounter struct {
buckets []int64 //[AgentID]count agentBuckets []*RWMutexCounterBucket //[AgentID]count
insert *sql.Stmt insert *sql.Stmt
} }
func NewDefaultAgentViewCounter(acc *qgen.Accumulator) (*DefaultAgentViewCounter, error) { func NewDefaultAgentViewCounter() (*DefaultAgentViewCounter, error) {
co := &DefaultAgentViewCounter{ acc := qgen.NewAcc()
buckets: make([]int64, len(agentMapEnum)), var agentBuckets = make([]*RWMutexCounterBucket, len(agentMapEnum))
for bucketID, _ := range agentBuckets {
agentBuckets[bucketID] = &RWMutexCounterBucket{counter: 0}
}
counter := &DefaultAgentViewCounter{
agentBuckets: agentBuckets,
insert: acc.Insert("viewchunks_agents").Columns("count, createdAt, browser").Fields("?,UTC_TIMESTAMP(),?").Prepare(), insert: acc.Insert("viewchunks_agents").Columns("count, createdAt, browser").Fields("?,UTC_TIMESTAMP(),?").Prepare(),
} }
c.Tasks.FifteenMin.Add(co.Tick) common.AddScheduledFifteenMinuteTask(counter.Tick)
//c.Tasks.Sec.Add(co.Tick) //common.AddScheduledSecondTask(counter.Tick)
c.Tasks.Shutdown.Add(co.Tick) common.AddShutdownTask(counter.Tick)
return co, acc.FirstError() return counter, acc.FirstError()
} }
func (co *DefaultAgentViewCounter) Tick() error { func (counter *DefaultAgentViewCounter) Tick() error {
for id, _ := range co.buckets { for agentID, agentBucket := range counter.agentBuckets {
count := atomic.SwapInt64(&co.buckets[id], 0) var count int
e := co.insertChunk(count, id) // TODO: Bulk insert for speed? agentBucket.RLock()
if e != nil { count = agentBucket.counter
return errors.Wrap(errors.WithStack(e), "agent counter") agentBucket.counter = 0
agentBucket.RUnlock()
err := counter.insertChunk(count, agentID) // TODO: Bulk insert for speed?
if err != nil {
return err
} }
} }
return nil return nil
} }
func (co *DefaultAgentViewCounter) insertChunk(count int64, agent int) error { func (counter *DefaultAgentViewCounter) insertChunk(count int, agent int) error {
if count == 0 { if count == 0 {
return nil return nil
} }
agentName := reverseAgentMapEnum[agent] var agentName = reverseAgentMapEnum[agent]
c.DebugLogf("Inserting a vchunk with a count of %d for agent %s (%d)", count, agentName, agent) common.DebugLogf("Inserting a viewchunk with a count of %d for agent %s (%d)", count, agentName, agent)
_, e := co.insert.Exec(count, agentName) _, err := counter.insert.Exec(count, agentName)
return e return err
} }
func (co *DefaultAgentViewCounter) Bump(agent int) { func (counter *DefaultAgentViewCounter) Bump(agent int) {
// TODO: Test this check // TODO: Test this check
c.DebugDetail("buckets ", agent, ": ", co.buckets[agent]) common.DebugDetail("counter.agentBuckets[", agent, "]: ", counter.agentBuckets[agent])
if len(co.buckets) <= agent || agent < 0 { if len(counter.agentBuckets) <= agent || agent < 0 {
return return
} }
atomic.AddInt64(&co.buckets[agent], 1) counter.agentBuckets[agent].Lock()
counter.agentBuckets[agent].counter++
counter.agentBuckets[agent].Unlock()
} }

View File

@ -40,13 +40,3 @@ type RWMutexCounterBucket struct {
counter int counter int
sync.RWMutex sync.RWMutex
} }
type MutexCounterBucket struct {
counter int
sync.Mutex
}
type MutexCounter64Bucket struct {
counter int64
sync.Mutex
}

View File

@ -4,9 +4,8 @@ import (
"database/sql" "database/sql"
"sync" "sync"
c "git.tuxpa.in/a/gosora/common" ".."
qgen "git.tuxpa.in/a/gosora/query_gen" "../../query_gen/lib"
"github.com/pkg/errors"
) )
var ForumViewCounter *DefaultForumViewCounter var ForumViewCounter *DefaultForumViewCounter
@ -24,84 +23,95 @@ type DefaultForumViewCounter struct {
func NewDefaultForumViewCounter() (*DefaultForumViewCounter, error) { func NewDefaultForumViewCounter() (*DefaultForumViewCounter, error) {
acc := qgen.NewAcc() acc := qgen.NewAcc()
co := &DefaultForumViewCounter{ counter := &DefaultForumViewCounter{
oddMap: make(map[int]*RWMutexCounterBucket), oddMap: make(map[int]*RWMutexCounterBucket),
evenMap: make(map[int]*RWMutexCounterBucket), evenMap: make(map[int]*RWMutexCounterBucket),
insert: acc.Insert("viewchunks_forums").Columns("count, createdAt, forum").Fields("?,UTC_TIMESTAMP(),?").Prepare(), insert: acc.Insert("viewchunks_forums").Columns("count, createdAt, forum").Fields("?,UTC_TIMESTAMP(),?").Prepare(),
} }
c.Tasks.FifteenMin.Add(co.Tick) // There could be a lot of routes, so we don't want to be running this every second common.AddScheduledFifteenMinuteTask(counter.Tick) // There could be a lot of routes, so we don't want to be running this every second
//c.Tasks.Sec.Add(co.Tick) //common.AddScheduledSecondTask(counter.Tick)
c.Tasks.Shutdown.Add(co.Tick) common.AddShutdownTask(counter.Tick)
return co, acc.FirstError() return counter, acc.FirstError()
} }
func (co *DefaultForumViewCounter) Tick() error { func (counter *DefaultForumViewCounter) Tick() error {
cLoop := func(l *sync.RWMutex, m map[int]*RWMutexCounterBucket) error { counter.oddLock.RLock()
l.RLock() oddMap := counter.oddMap
for fid, f := range m { counter.oddLock.RUnlock()
l.RUnlock() for forumID, forum := range oddMap {
var count int var count int
f.RLock() forum.RLock()
count = f.counter count = forum.counter
f.RUnlock() forum.RUnlock()
// TODO: Only delete the bucket when it's zero to avoid hitting popular forums? // TODO: Only delete the bucket when it's zero to avoid hitting popular forums?
l.Lock() counter.oddLock.Lock()
delete(m, fid) delete(counter.oddMap, forumID)
l.Unlock() counter.oddLock.Unlock()
e := co.insertChunk(count, fid) err := counter.insertChunk(count, forumID)
if e != nil { if err != nil {
return errors.Wrap(errors.WithStack(e), "forum counter") return err
} }
l.RLock()
} }
l.RUnlock()
counter.evenLock.RLock()
evenMap := counter.evenMap
counter.evenLock.RUnlock()
for forumID, forum := range evenMap {
var count int
forum.RLock()
count = forum.counter
forum.RUnlock()
// TODO: Only delete the bucket when it's zero to avoid hitting popular forums?
counter.evenLock.Lock()
delete(counter.evenMap, forumID)
counter.evenLock.Unlock()
err := counter.insertChunk(count, forumID)
if err != nil {
return err
}
}
return nil return nil
} }
e := cLoop(&co.oddLock, co.oddMap)
if e != nil {
return e
}
return cLoop(&co.evenLock, co.evenMap)
}
func (co *DefaultForumViewCounter) insertChunk(count, forum int) error { func (counter *DefaultForumViewCounter) insertChunk(count int, forum int) error {
if count == 0 { if count == 0 {
return nil return nil
} }
c.DebugLogf("Inserting a vchunk with a count of %d for forum %d", count, forum) common.DebugLogf("Inserting a viewchunk with a count of %d for forum %d", count, forum)
_, e := co.insert.Exec(count, forum) _, err := counter.insert.Exec(count, forum)
return e return err
} }
func (co *DefaultForumViewCounter) Bump(fid int) { func (counter *DefaultForumViewCounter) Bump(forumID int) {
// Is the ID even? // Is the ID even?
if fid%2 == 0 { if forumID%2 == 0 {
co.evenLock.RLock() counter.evenLock.RLock()
f, ok := co.evenMap[fid] forum, ok := counter.evenMap[forumID]
co.evenLock.RUnlock() counter.evenLock.RUnlock()
if ok { if ok {
f.Lock() forum.Lock()
f.counter++ forum.counter++
f.Unlock() forum.Unlock()
} else { } else {
co.evenLock.Lock() counter.evenLock.Lock()
co.evenMap[fid] = &RWMutexCounterBucket{counter: 1} counter.evenMap[forumID] = &RWMutexCounterBucket{counter: 1}
co.evenLock.Unlock() counter.evenLock.Unlock()
} }
return return
} }
co.oddLock.RLock() counter.oddLock.RLock()
f, ok := co.oddMap[fid] forum, ok := counter.oddMap[forumID]
co.oddLock.RUnlock() counter.oddLock.RUnlock()
if ok { if ok {
f.Lock() forum.Lock()
f.counter++ forum.counter++
f.Unlock() forum.Unlock()
} else { } else {
co.oddLock.Lock() counter.oddLock.Lock()
co.oddMap[fid] = &RWMutexCounterBucket{counter: 1} counter.oddMap[forumID] = &RWMutexCounterBucket{counter: 1}
co.oddLock.Unlock() counter.oddLock.Unlock()
} }
} }

View File

@ -1,19 +1,14 @@
package counters package counters
import ( import "database/sql"
"database/sql" import ".."
"sync/atomic" import "../../query_gen/lib"
c "git.tuxpa.in/a/gosora/common"
qgen "git.tuxpa.in/a/gosora/query_gen"
"github.com/pkg/errors"
)
var LangViewCounter *DefaultLangViewCounter var LangViewCounter *DefaultLangViewCounter
var langCodes = []string{ var langCodes = []string{
"unknown", "unknown",
"", "none",
"af", "af",
"ar", "ar",
"az", "az",
@ -52,7 +47,6 @@ var langCodes = []string{
"kn", "kn",
"ko", "ko",
"kok", "kok",
"kw",
"ky", "ky",
"lt", "lt",
"lv", "lv",
@ -100,78 +94,74 @@ var langCodes = []string{
} }
type DefaultLangViewCounter struct { type DefaultLangViewCounter struct {
//buckets []*MutexCounterBucket //[OSID]count buckets []*RWMutexCounterBucket //[OSID]count
buckets []int64 //[OSID]count
codesToIndices map[string]int codesToIndices map[string]int
insert *sql.Stmt insert *sql.Stmt
} }
func NewDefaultLangViewCounter(acc *qgen.Accumulator) (*DefaultLangViewCounter, error) { func NewDefaultLangViewCounter() (*DefaultLangViewCounter, error) {
codesToIndices := make(map[string]int, len(langCodes)) acc := qgen.NewAcc()
var langBuckets = make([]*RWMutexCounterBucket, len(langCodes))
for bucketID, _ := range langBuckets {
langBuckets[bucketID] = &RWMutexCounterBucket{counter: 0}
}
var codesToIndices = make(map[string]int)
for index, code := range langCodes { for index, code := range langCodes {
codesToIndices[code] = index codesToIndices[code] = index
} }
co := &DefaultLangViewCounter{
buckets: make([]int64, len(langCodes)), counter := &DefaultLangViewCounter{
buckets: langBuckets,
codesToIndices: codesToIndices, codesToIndices: codesToIndices,
insert: acc.Insert("viewchunks_langs").Columns("count, createdAt, lang").Fields("?,UTC_TIMESTAMP(),?").Prepare(), insert: acc.Insert("viewchunks_langs").Columns("count, createdAt, lang").Fields("?,UTC_TIMESTAMP(),?").Prepare(),
} }
c.Tasks.FifteenMin.Add(co.Tick) common.AddScheduledFifteenMinuteTask(counter.Tick)
//c.Tasks.Sec.Add(co.Tick) //common.AddScheduledSecondTask(counter.Tick)
c.Tasks.Shutdown.Add(co.Tick) common.AddShutdownTask(counter.Tick)
return co, acc.FirstError() return counter, acc.FirstError()
} }
func (co *DefaultLangViewCounter) Tick() error { func (counter *DefaultLangViewCounter) Tick() error {
for id := 0; id < len(co.buckets); id++ { for id, bucket := range counter.buckets {
count := atomic.SwapInt64(&co.buckets[id], 0) var count int
e := co.insertChunk(count, id) // TODO: Bulk insert for speed? bucket.RLock()
if e != nil { count = bucket.counter
return errors.Wrap(errors.WithStack(e), "langview counter") bucket.counter = 0 // TODO: Add a SetZero method to reduce the amount of duplicate code between the OS and agent counters?
bucket.RUnlock()
err := counter.insertChunk(count, id) // TODO: Bulk insert for speed?
if err != nil {
return err
} }
} }
return nil return nil
} }
func (co *DefaultLangViewCounter) insertChunk(count int64, id int) error { func (counter *DefaultLangViewCounter) insertChunk(count int, id int) error {
if count == 0 { if count == 0 {
return nil return nil
} }
langCode := langCodes[id] var langCode = langCodes[id]
if langCode == "" { common.DebugLogf("Inserting a viewchunk with a count of %d for lang %s (%d)", count, langCode, id)
langCode = "none" _, err := counter.insert.Exec(count, langCode)
} return err
c.DebugLogf("Inserting a vchunk with a count of %d for lang %s (%d)", count, langCode, id)
_, e := co.insert.Exec(count, langCode)
return e
} }
func (co *DefaultLangViewCounter) Bump(langCode string) (validCode bool) { func (counter *DefaultLangViewCounter) Bump(langCode string) {
validCode = true id, ok := counter.codesToIndices[langCode]
id, ok := co.codesToIndices[langCode]
if !ok { if !ok {
// TODO: Tell the caller that the code's invalid // TODO: Tell the caller that the code's invalid
id = 0 // Unknown id = 0 // Unknown
validCode = false
} }
// TODO: Test this check // TODO: Test this check
c.DebugDetail("buckets ", id, ": ", co.buckets[id]) common.DebugDetail("counter.buckets[", id, "]: ", counter.buckets[id])
if len(co.buckets) <= id || id < 0 { if len(counter.buckets) <= id || id < 0 {
return validCode
}
atomic.AddInt64(&co.buckets[id], 1)
return validCode
}
func (co *DefaultLangViewCounter) Bump2(id int) {
// TODO: Test this check
c.DebugDetail("bucket ", id, ": ", co.buckets[id])
if len(co.buckets) <= id || id < 0 {
return return
} }
atomic.AddInt64(&co.buckets[id], 1) counter.buckets[id].Lock()
counter.buckets[id].counter++
counter.buckets[id].Unlock()
} }

View File

@ -1,92 +0,0 @@
package counters
import (
"database/sql"
"runtime"
"sync"
"time"
c "git.tuxpa.in/a/gosora/common"
qgen "git.tuxpa.in/a/gosora/query_gen"
"github.com/pkg/errors"
)
var MemoryCounter *DefaultMemoryCounter
type DefaultMemoryCounter struct {
insert *sql.Stmt
totMem uint64
totCount uint64
stackMem uint64
stackCount uint64
heapMem uint64
heapCount uint64
sync.Mutex
}
func NewMemoryCounter(acc *qgen.Accumulator) (*DefaultMemoryCounter, error) {
co := &DefaultMemoryCounter{
insert: acc.Insert("memchunks").Columns("count,stack,heap,createdAt").Fields("?,?,?,UTC_TIMESTAMP()").Prepare(),
}
c.Tasks.FifteenMin.Add(co.Tick)
//c.Tasks.Sec.Add(co.Tick)
c.Tasks.Shutdown.Add(co.Tick)
ticker := time.NewTicker(time.Minute)
go func() {
defer c.EatPanics()
for {
select {
case <-ticker.C:
var m runtime.MemStats
runtime.ReadMemStats(&m)
co.Lock()
co.totCount++
co.totMem += m.Sys
co.stackCount++
co.stackMem += m.StackInuse
co.heapCount++
co.heapMem += m.HeapAlloc
co.Unlock()
}
}
}()
return co, acc.FirstError()
}
func (co *DefaultMemoryCounter) Tick() (e error) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
var rTotMem, rTotCount, rStackMem, rStackCount, rHeapMem, rHeapCount uint64
co.Lock()
rTotMem = co.totMem
rTotCount = co.totCount
rStackMem = co.stackMem
rStackCount = co.stackCount
rHeapMem = co.heapMem
rHeapCount = co.heapCount
co.totMem = 0
co.totCount = 0
co.stackMem = 0
co.stackCount = 0
co.heapMem = 0
co.heapCount = 0
co.Unlock()
var avgMem, avgStack, avgHeap uint64
avgMem = (rTotMem + m.Sys) / (rTotCount + 1)
avgStack = (rStackMem + m.StackInuse) / (rStackCount + 1)
avgHeap = (rHeapMem + m.HeapAlloc) / (rHeapCount + 1)
c.DebugLogf("Inserting a memchunk with a value of %d - %d - %d", avgMem, avgStack, avgHeap)
_, e = co.insert.Exec(avgMem, avgStack, avgHeap)
if e != nil {
return errors.Wrap(errors.WithStack(e), "mem counter")
}
return nil
}

View File

@ -1,118 +0,0 @@
package counters
import (
"database/sql"
"math"
"time"
c "git.tuxpa.in/a/gosora/common"
qgen "git.tuxpa.in/a/gosora/query_gen"
"github.com/pkg/errors"
)
var PerfCounter *DefaultPerfCounter
type PerfCounterBucket struct {
low *MutexCounter64Bucket
high *MutexCounter64Bucket
avg *MutexCounter64Bucket
}
// TODO: Track perf on a per route basis
type DefaultPerfCounter struct {
buckets []*PerfCounterBucket
insert *sql.Stmt
}
func NewDefaultPerfCounter(acc *qgen.Accumulator) (*DefaultPerfCounter, error) {
co := &DefaultPerfCounter{
buckets: []*PerfCounterBucket{
{
low: &MutexCounter64Bucket{counter: math.MaxInt64},
high: &MutexCounter64Bucket{counter: 0},
avg: &MutexCounter64Bucket{counter: 0},
},
},
insert: acc.Insert("perfchunks").Columns("low,high,avg,createdAt").Fields("?,?,?,UTC_TIMESTAMP()").Prepare(),
}
c.Tasks.FifteenMin.Add(co.Tick)
//c.Tasks.Sec.Add(co.Tick)
c.Tasks.Shutdown.Add(co.Tick)
return co, acc.FirstError()
}
func (co *DefaultPerfCounter) Tick() error {
getCounter := func(b *MutexCounter64Bucket) (c int64) {
b.Lock()
c = b.counter
b.counter = 0
b.Unlock()
return c
}
var low int64
hTbl := c.GetHookTable()
for _, b := range co.buckets {
b.low.Lock()
low, b.low.counter = b.low.counter, math.MaxInt64
b.low.Unlock()
if low == math.MaxInt64 {
low = 0
}
high := getCounter(b.high)
avg := getCounter(b.avg)
c.H_counters_perf_tick_row_hook(hTbl, low, high, avg)
if e := co.insertChunk(low, high, avg); e != nil { // TODO: Bulk insert for speed?
return errors.Wrap(errors.WithStack(e), "perf counter")
}
}
return nil
}
func (co *DefaultPerfCounter) insertChunk(low, high, avg int64) error {
if low == 0 && high == 0 && avg == 0 {
return nil
}
c.DebugLogf("Inserting a pchunk with low %d, high %d, avg %d", low, high, avg)
if c.Dev.LogNewLongRoute && high > (5*1000*1000) {
c.Logf("pchunk high %d", high)
}
_, e := co.insert.Exec(low, high, avg)
return e
}
func (co *DefaultPerfCounter) Push(dur time.Duration /*,_ bool*/) {
id := 0
b := co.buckets[id]
//c.DebugDetail("buckets ", id, ": ", b)
micro := dur.Microseconds()
if micro >= math.MaxInt32 {
c.LogWarning(errors.New("dur should not be int32 max or higher"))
}
low := b.low
low.Lock()
if micro < low.counter {
low.counter = micro
}
low.Unlock()
high := b.high
high.Lock()
if micro > high.counter {
high.counter = micro
}
high.Unlock()
avg := b.avg
avg.Lock()
if micro != avg.counter {
if avg.counter == 0 {
avg.counter = micro
} else {
avg.counter = (micro + avg.counter) / 2
}
}
avg.Unlock()
}

View File

@ -4,9 +4,8 @@ import (
"database/sql" "database/sql"
"sync/atomic" "sync/atomic"
c "git.tuxpa.in/a/gosora/common" ".."
qgen "git.tuxpa.in/a/gosora/query_gen" "../../query_gen/lib"
"github.com/pkg/errors"
) )
var PostCounter *DefaultPostCounter var PostCounter *DefaultPostCounter
@ -20,44 +19,40 @@ type DefaultPostCounter struct {
func NewPostCounter() (*DefaultPostCounter, error) { func NewPostCounter() (*DefaultPostCounter, error) {
acc := qgen.NewAcc() acc := qgen.NewAcc()
co := &DefaultPostCounter{ counter := &DefaultPostCounter{
currentBucket: 0, currentBucket: 0,
insert: acc.Insert("postchunks").Columns("count, createdAt").Fields("?,UTC_TIMESTAMP()").Prepare(), insert: acc.Insert("postchunks").Columns("count, createdAt").Fields("?,UTC_TIMESTAMP()").Prepare(),
} }
c.Tasks.FifteenMin.Add(co.Tick) common.AddScheduledFifteenMinuteTask(counter.Tick)
//c.Tasks.Sec.Add(co.Tick) //common.AddScheduledSecondTask(counter.Tick)
c.Tasks.Shutdown.Add(co.Tick) common.AddShutdownTask(counter.Tick)
return co, acc.FirstError() return counter, acc.FirstError()
} }
func (co *DefaultPostCounter) Tick() (err error) { func (counter *DefaultPostCounter) Tick() (err error) {
oldBucket := co.currentBucket var oldBucket = counter.currentBucket
var nextBucket int64 // 0 var nextBucket int64 // 0
if co.currentBucket == 0 { if counter.currentBucket == 0 {
nextBucket = 1 nextBucket = 1
} }
atomic.AddInt64(&co.buckets[oldBucket], co.buckets[nextBucket]) atomic.AddInt64(&counter.buckets[oldBucket], counter.buckets[nextBucket])
atomic.StoreInt64(&co.buckets[nextBucket], 0) atomic.StoreInt64(&counter.buckets[nextBucket], 0)
atomic.StoreInt64(&co.currentBucket, nextBucket) atomic.StoreInt64(&counter.currentBucket, nextBucket)
previousViewChunk := co.buckets[oldBucket] var previousViewChunk = counter.buckets[oldBucket]
atomic.AddInt64(&co.buckets[oldBucket], -previousViewChunk) atomic.AddInt64(&counter.buckets[oldBucket], -previousViewChunk)
err = co.insertChunk(previousViewChunk) return counter.insertChunk(previousViewChunk)
if err != nil {
return errors.Wrap(errors.WithStack(err), "post counter")
}
return nil
} }
func (co *DefaultPostCounter) Bump() { func (counter *DefaultPostCounter) Bump() {
atomic.AddInt64(&co.buckets[co.currentBucket], 1) atomic.AddInt64(&counter.buckets[counter.currentBucket], 1)
} }
func (co *DefaultPostCounter) insertChunk(count int64) error { func (counter *DefaultPostCounter) insertChunk(count int64) error {
if count == 0 { if count == 0 {
return nil return nil
} }
c.DebugLogf("Inserting a postchunk with a count of %d", count) common.DebugLogf("Inserting a postchunk with a count of %d", count)
_, err := co.insert.Exec(count) _, err := counter.insert.Exec(count)
return err return err
} }

View File

@ -5,9 +5,8 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
c "git.tuxpa.in/a/gosora/common" ".."
qgen "git.tuxpa.in/a/gosora/query_gen" "../../query_gen/lib"
"github.com/pkg/errors"
) )
var ReferrerTracker *DefaultReferrerTracker var ReferrerTracker *DefaultReferrerTracker
@ -37,9 +36,9 @@ func NewDefaultReferrerTracker() (*DefaultReferrerTracker, error) {
even: make(map[string]*ReferrerItem), even: make(map[string]*ReferrerItem),
insert: acc.Insert("viewchunks_referrers").Columns("count, createdAt, domain").Fields("?,UTC_TIMESTAMP(),?").Prepare(), // TODO: Do something more efficient than doing a query for each referrer insert: acc.Insert("viewchunks_referrers").Columns("count, createdAt, domain").Fields("?,UTC_TIMESTAMP(),?").Prepare(), // TODO: Do something more efficient than doing a query for each referrer
} }
c.Tasks.FifteenMin.Add(refTracker.Tick) common.AddScheduledFifteenMinuteTask(refTracker.Tick)
//c.Tasks.Sec.Add(refTracker.Tick) //common.AddScheduledSecondTask(refTracker.Tick)
c.Tasks.Shutdown.Add(refTracker.Tick) common.AddShutdownTask(refTracker.Tick)
return refTracker, acc.FirstError() return refTracker, acc.FirstError()
} }
@ -51,41 +50,50 @@ func (ref *DefaultReferrerTracker) Tick() (err error) {
if count != 0 { if count != 0 {
err := ref.insertChunk(referrer, count) // TODO: Bulk insert for speed? err := ref.insertChunk(referrer, count) // TODO: Bulk insert for speed?
if err != nil { if err != nil {
return errors.Wrap(errors.WithStack(err), "ref counter") return err
} }
} }
delete(referrersToDelete, referrer) delete(referrersToDelete, referrer)
} }
// Run the queries and schedule zero view refs for deletion from memory // Run the queries and schedule zero view refs for deletion from memory
refLoop := func(l *sync.RWMutex, m map[string]*ReferrerItem) error { ref.oddLock.Lock()
l.Lock() for referrer, counter := range ref.odd {
defer l.Unlock()
for referrer, counter := range m {
if counter.Count == 0 { if counter.Count == 0 {
referrersToDelete[referrer] = counter referrersToDelete[referrer] = counter
delete(m, referrer) delete(ref.odd, referrer)
} }
count := atomic.SwapInt64(&counter.Count, 0) count := atomic.SwapInt64(&counter.Count, 0)
err := ref.insertChunk(referrer, count) // TODO: Bulk insert for speed? err := ref.insertChunk(referrer, count) // TODO: Bulk insert for speed?
if err != nil { if err != nil {
return errors.Wrap(errors.WithStack(err), "ref counter") return err
} }
} }
return nil ref.oddLock.Unlock()
ref.evenLock.Lock()
for referrer, counter := range ref.even {
if counter.Count == 0 {
referrersToDelete[referrer] = counter
delete(ref.even, referrer)
} }
err = refLoop(&ref.oddLock, ref.odd) count := atomic.SwapInt64(&counter.Count, 0)
err := ref.insertChunk(referrer, count) // TODO: Bulk insert for speed?
if err != nil { if err != nil {
return err return err
} }
return refLoop(&ref.evenLock, ref.even) }
ref.evenLock.Unlock()
return nil
} }
func (ref *DefaultReferrerTracker) insertChunk(referrer string, count int64) error { func (ref *DefaultReferrerTracker) insertChunk(referrer string, count int64) error {
if count == 0 { if count == 0 {
return nil return nil
} }
c.DebugDetailf("Inserting a vchunk with a count of %d for ref %s", count, referrer) common.DebugDetailf("Inserting a viewchunk with a count of %d for referrer %s", count, referrer)
_, err := ref.insert.Exec(count, referrer) _, err := ref.insert.Exec(count, referrer)
return err return err
} }

View File

@ -4,9 +4,8 @@ import (
"database/sql" "database/sql"
"sync/atomic" "sync/atomic"
c "git.tuxpa.in/a/gosora/common" ".."
qgen "git.tuxpa.in/a/gosora/query_gen" "../../query_gen/lib"
"github.com/pkg/errors"
) )
// TODO: Rename this? // TODO: Rename this?
@ -21,45 +20,40 @@ type DefaultViewCounter struct {
} }
func NewGlobalViewCounter(acc *qgen.Accumulator) (*DefaultViewCounter, error) { func NewGlobalViewCounter(acc *qgen.Accumulator) (*DefaultViewCounter, error) {
co := &DefaultViewCounter{ counter := &DefaultViewCounter{
currentBucket: 0, currentBucket: 0,
insert: acc.Insert("viewchunks").Columns("count,createdAt,route").Fields("?,UTC_TIMESTAMP(),''").Prepare(), insert: acc.Insert("viewchunks").Columns("count, createdAt").Fields("?,UTC_TIMESTAMP()").Prepare(),
} }
c.Tasks.FifteenMin.Add(co.Tick) // This is run once every fifteen minutes to match the frequency of the RouteViewCounter common.AddScheduledFifteenMinuteTask(counter.Tick) // This is run once every fifteen minutes to match the frequency of the RouteViewCounter
//c.Tasks.Sec.Add(co.Tick) //common.AddScheduledSecondTask(counter.Tick)
c.Tasks.Shutdown.Add(co.Tick) common.AddShutdownTask(counter.Tick)
return co, acc.FirstError() return counter, acc.FirstError()
} }
// TODO: Simplify the atomics used here func (counter *DefaultViewCounter) Tick() (err error) {
func (co *DefaultViewCounter) Tick() (err error) { var oldBucket = counter.currentBucket
oldBucket := co.currentBucket
var nextBucket int64 // 0 var nextBucket int64 // 0
if co.currentBucket == 0 { if counter.currentBucket == 0 {
nextBucket = 1 nextBucket = 1
} }
atomic.AddInt64(&co.buckets[oldBucket], co.buckets[nextBucket]) atomic.AddInt64(&counter.buckets[oldBucket], counter.buckets[nextBucket])
atomic.StoreInt64(&co.buckets[nextBucket], 0) atomic.StoreInt64(&counter.buckets[nextBucket], 0)
atomic.StoreInt64(&co.currentBucket, nextBucket) atomic.StoreInt64(&counter.currentBucket, nextBucket)
previousViewChunk := co.buckets[oldBucket] var previousViewChunk = counter.buckets[oldBucket]
atomic.AddInt64(&co.buckets[oldBucket], -previousViewChunk) atomic.AddInt64(&counter.buckets[oldBucket], -previousViewChunk)
err = co.insertChunk(previousViewChunk) return counter.insertChunk(previousViewChunk)
if err != nil {
return errors.Wrap(errors.WithStack(err), "req counter")
}
return nil
} }
func (co *DefaultViewCounter) Bump() { func (counter *DefaultViewCounter) Bump() {
atomic.AddInt64(&co.buckets[co.currentBucket], 1) atomic.AddInt64(&counter.buckets[counter.currentBucket], 1)
} }
func (co *DefaultViewCounter) insertChunk(count int64) error { func (counter *DefaultViewCounter) insertChunk(count int64) error {
if count == 0 { if count == 0 {
return nil return nil
} }
c.DebugLogf("Inserting a vchunk with a count of %d", count) common.DebugLogf("Inserting a viewchunk with a count of %d", count)
_, err := co.insert.Exec(count) _, err := counter.insert.Exec(count)
return err return err
} }

View File

@ -1,187 +1,66 @@
package counters package counters
import ( import "database/sql"
"database/sql" import ".."
"sync" import "../../query_gen/lib"
"sync/atomic"
"time"
c "git.tuxpa.in/a/gosora/common"
qgen "git.tuxpa.in/a/gosora/query_gen"
"git.tuxpa.in/a/gosora/uutils"
"github.com/pkg/errors"
)
var RouteViewCounter *DefaultRouteViewCounter var RouteViewCounter *DefaultRouteViewCounter
type RVBucket struct {
counter int64
avg int
sync.Mutex
}
// TODO: Make this lockless? // TODO: Make this lockless?
type DefaultRouteViewCounter struct { type DefaultRouteViewCounter struct {
buckets []*RVBucket //[RouteID]count buckets []*RWMutexCounterBucket //[RouteID]count
insert *sql.Stmt insert *sql.Stmt
insert5 *sql.Stmt
} }
func NewDefaultRouteViewCounter(acc *qgen.Accumulator) (*DefaultRouteViewCounter, error) { func NewDefaultRouteViewCounter() (*DefaultRouteViewCounter, error) {
routeBuckets := make([]*RVBucket, len(routeMapEnum)) acc := qgen.NewAcc()
var routeBuckets = make([]*RWMutexCounterBucket, len(routeMapEnum))
for bucketID, _ := range routeBuckets { for bucketID, _ := range routeBuckets {
routeBuckets[bucketID] = &RVBucket{counter: 0, avg: 0} routeBuckets[bucketID] = &RWMutexCounterBucket{counter: 0}
} }
counter := &DefaultRouteViewCounter{
fields := "?,?,UTC_TIMESTAMP(),?"
co := &DefaultRouteViewCounter{
buckets: routeBuckets, buckets: routeBuckets,
insert: acc.Insert("viewchunks").Columns("count,avg,createdAt,route").Fields(fields).Prepare(), insert: acc.Insert("viewchunks").Columns("count, createdAt, route").Fields("?,UTC_TIMESTAMP(),?").Prepare(),
insert5: acc.BulkInsert("viewchunks").Columns("count,avg,createdAt,route").Fields(fields, fields, fields, fields, fields).Prepare(),
} }
if !c.Config.DisableAnalytics { common.AddScheduledFifteenMinuteTask(counter.Tick) // There could be a lot of routes, so we don't want to be running this every second
c.Tasks.FifteenMin.Add(co.Tick) // There could be a lot of routes, so we don't want to be running this every second //common.AddScheduledSecondTask(counter.Tick)
//c.Tasks.Sec.Add(co.Tick) common.AddShutdownTask(counter.Tick)
c.Tasks.Shutdown.Add(co.Tick) return counter, acc.FirstError()
}
return co, acc.FirstError()
} }
type RVCount struct { func (counter *DefaultRouteViewCounter) Tick() error {
RouteID int for routeID, routeBucket := range counter.buckets {
Count int64 var count int
Avg int routeBucket.RLock()
} count = routeBucket.counter
routeBucket.counter = 0
routeBucket.RUnlock()
func (co *DefaultRouteViewCounter) Tick() (err error) { err := counter.insertChunk(count, routeID) // TODO: Bulk insert for speed?
var tb []RVCount
for routeID, b := range co.buckets {
var avg int
count := atomic.SwapInt64(&b.counter, 0)
b.Lock()
avg = b.avg
b.avg = 0
b.Unlock()
if count == 0 {
continue
}
tb = append(tb, RVCount{routeID, count, avg})
}
// TODO: Expand on this?
var i int
if len(tb) >= 5 {
for ; len(tb) > (i + 5); i += 5 {
err := co.insert5Chunk(tb[i : i+5])
if err != nil { if err != nil {
c.DebugLogf("tb: %+v\n", tb) return err
c.DebugLog("i: ", i)
return errors.Wrap(errors.WithStack(err), "route counter x 5")
} }
} }
}
for ; len(tb) > i; i++ {
it := tb[i]
err = co.insertChunk(it.Count, it.Avg, it.RouteID)
if err != nil {
c.DebugLogf("tb: %+v\n", tb)
c.DebugLog("i: ", i)
return errors.Wrap(errors.WithStack(err), "route counter")
}
}
return nil return nil
} }
func (co *DefaultRouteViewCounter) insertChunk(count int64, avg, route int) error { func (counter *DefaultRouteViewCounter) insertChunk(count int, route int) error {
routeName := reverseRouteMapEnum[route] if count == 0 {
c.DebugLogf("Inserting vchunk with count %d, avg %d for route %s (%d)", count, avg, routeName, route) return nil
_, err := co.insert.Exec(count, avg, routeName) }
var routeName = reverseRouteMapEnum[route]
common.DebugLogf("Inserting a viewchunk with a count of %d for route %s (%d)", count, routeName, route)
_, err := counter.insert.Exec(count, routeName)
return err return err
} }
func (co *DefaultRouteViewCounter) insert5Chunk(rvs []RVCount) error { func (counter *DefaultRouteViewCounter) Bump(route int) {
args := make([]interface{}, len(rvs)*3)
i := 0
for _, rv := range rvs {
routeName := reverseRouteMapEnum[rv.RouteID]
if rv.Avg == 0 {
c.DebugLogf("Queueing vchunk with count %d for routes %s (%d)", rv.Count, routeName, rv.RouteID)
} else {
c.DebugLogf("Queueing vchunk with count %d, avg %d for routes %s (%d)", rv.Count, rv.Avg, routeName, rv.RouteID)
}
args[i] = rv.Count
args[i+1] = rv.Avg
args[i+2] = routeName
i += 3
}
c.DebugDetailf("args: %+v\n", args)
_, err := co.insert5.Exec(args...)
return err
}
func (co *DefaultRouteViewCounter) Bump(route int) {
if c.Config.DisableAnalytics {
return
}
// TODO: Test this check // TODO: Test this check
b := co.buckets[route] common.DebugDetail("counter.buckets[", route, "]: ", counter.buckets[route])
c.DebugDetail("bucket ", route, ": ", b) if len(counter.buckets) <= route || route < 0 {
if len(co.buckets) <= route || route < 0 {
return return
} }
atomic.AddInt64(&b.counter, 1) counter.buckets[route].Lock()
} counter.buckets[route].counter++
counter.buckets[route].Unlock()
// TODO: Eliminate the lock?
func (co *DefaultRouteViewCounter) Bump2(route int, t time.Time) {
if c.Config.DisableAnalytics {
return
}
// TODO: Test this check
b := co.buckets[route]
c.DebugDetail("bucket ", route, ": ", b)
if len(co.buckets) <= route || route < 0 {
return
}
micro := int(time.Since(t).Microseconds())
//co.PerfCounter.Push(since, true)
atomic.AddInt64(&b.counter, 1)
b.Lock()
if micro != b.avg {
if b.avg == 0 {
b.avg = micro
} else {
b.avg = (micro + b.avg) / 2
}
}
b.Unlock()
}
// TODO: Eliminate the lock?
func (co *DefaultRouteViewCounter) Bump3(route int, nano int64) {
if c.Config.DisableAnalytics {
return
}
// TODO: Test this check
b := co.buckets[route]
c.DebugDetail("bucket ", route, ": ", b)
if len(co.buckets) <= route || route < 0 {
return
}
micro := int((uutils.Nanotime() - nano) / 1000)
//co.PerfCounter.Push(since, true)
atomic.AddInt64(&b.counter, 1)
b.Lock()
if micro != b.avg {
if b.avg == 0 {
b.avg = micro
} else {
b.avg = (micro + b.avg) / 2
}
}
b.Unlock()
} }

View File

@ -1,57 +1,65 @@
package counters package counters
import ( import "database/sql"
"database/sql" import ".."
"sync/atomic" import "../../query_gen/lib"
c "git.tuxpa.in/a/gosora/common"
qgen "git.tuxpa.in/a/gosora/query_gen"
"github.com/pkg/errors"
)
var OSViewCounter *DefaultOSViewCounter var OSViewCounter *DefaultOSViewCounter
type DefaultOSViewCounter struct { type DefaultOSViewCounter struct {
buckets []int64 //[OSID]count buckets []*RWMutexCounterBucket //[OSID]count
insert *sql.Stmt insert *sql.Stmt
} }
func NewDefaultOSViewCounter(acc *qgen.Accumulator) (*DefaultOSViewCounter, error) { func NewDefaultOSViewCounter() (*DefaultOSViewCounter, error) {
co := &DefaultOSViewCounter{ acc := qgen.NewAcc()
buckets: make([]int64, len(osMapEnum)), var osBuckets = make([]*RWMutexCounterBucket, len(osMapEnum))
for bucketID, _ := range osBuckets {
osBuckets[bucketID] = &RWMutexCounterBucket{counter: 0}
}
counter := &DefaultOSViewCounter{
buckets: osBuckets,
insert: acc.Insert("viewchunks_systems").Columns("count, createdAt, system").Fields("?,UTC_TIMESTAMP(),?").Prepare(), insert: acc.Insert("viewchunks_systems").Columns("count, createdAt, system").Fields("?,UTC_TIMESTAMP(),?").Prepare(),
} }
c.Tasks.FifteenMin.Add(co.Tick) common.AddScheduledFifteenMinuteTask(counter.Tick)
//c.Tasks.Sec.Add(co.Tick) //common.AddScheduledSecondTask(counter.Tick)
c.Tasks.Shutdown.Add(co.Tick) common.AddShutdownTask(counter.Tick)
return co, acc.FirstError() return counter, acc.FirstError()
} }
func (co *DefaultOSViewCounter) Tick() error { func (counter *DefaultOSViewCounter) Tick() error {
for id, _ := range co.buckets { for id, bucket := range counter.buckets {
count := atomic.SwapInt64(&co.buckets[id], 0) var count int
if e := co.insertChunk(count, id); e != nil { // TODO: Bulk insert for speed? bucket.RLock()
return errors.Wrap(errors.WithStack(e), "system counter") count = bucket.counter
bucket.counter = 0 // TODO: Add a SetZero method to reduce the amount of duplicate code between the OS and agent counters?
bucket.RUnlock()
err := counter.insertChunk(count, id) // TODO: Bulk insert for speed?
if err != nil {
return err
} }
} }
return nil return nil
} }
func (co *DefaultOSViewCounter) insertChunk(count int64, os int) error { func (counter *DefaultOSViewCounter) insertChunk(count int, os int) error {
if count == 0 { if count == 0 {
return nil return nil
} }
osName := reverseOSMapEnum[os] var osName = reverseOSMapEnum[os]
c.DebugLogf("Inserting a vchunk with a count of %d for OS %s (%d)", count, osName, os) common.DebugLogf("Inserting a viewchunk with a count of %d for OS %s (%d)", count, osName, os)
_, err := co.insert.Exec(count, osName) _, err := counter.insert.Exec(count, osName)
return err return err
} }
func (co *DefaultOSViewCounter) Bump(id int) { func (counter *DefaultOSViewCounter) Bump(id int) {
// TODO: Test this check // TODO: Test this check
c.DebugDetail("bucket ", id, ": ", co.buckets[id]) common.DebugDetail("counter.buckets[", id, "]: ", counter.buckets[id])
if len(co.buckets) <= id || id < 0 { if len(counter.buckets) <= id || id < 0 {
return return
} }
atomic.AddInt64(&co.buckets[id], 1) counter.buckets[id].Lock()
counter.buckets[id].counter++
counter.buckets[id].Unlock()
} }

View File

@ -4,9 +4,8 @@ import (
"database/sql" "database/sql"
"sync/atomic" "sync/atomic"
c "git.tuxpa.in/a/gosora/common" ".."
qgen "git.tuxpa.in/a/gosora/query_gen" "../../query_gen/lib"
"github.com/pkg/errors"
) )
var TopicCounter *DefaultTopicCounter var TopicCounter *DefaultTopicCounter
@ -20,44 +19,40 @@ type DefaultTopicCounter struct {
func NewTopicCounter() (*DefaultTopicCounter, error) { func NewTopicCounter() (*DefaultTopicCounter, error) {
acc := qgen.NewAcc() acc := qgen.NewAcc()
co := &DefaultTopicCounter{ counter := &DefaultTopicCounter{
currentBucket: 0, currentBucket: 0,
insert: acc.Insert("topicchunks").Columns("count, createdAt").Fields("?,UTC_TIMESTAMP()").Prepare(), insert: acc.Insert("topicchunks").Columns("count, createdAt").Fields("?,UTC_TIMESTAMP()").Prepare(),
} }
c.Tasks.FifteenMin.Add(co.Tick) common.AddScheduledFifteenMinuteTask(counter.Tick)
//c.Tasks.Sec.Add(co.Tick) //common.AddScheduledSecondTask(counter.Tick)
c.Tasks.Shutdown.Add(co.Tick) common.AddShutdownTask(counter.Tick)
return co, acc.FirstError() return counter, acc.FirstError()
} }
func (co *DefaultTopicCounter) Tick() (e error) { func (counter *DefaultTopicCounter) Tick() (err error) {
oldBucket := co.currentBucket var oldBucket = counter.currentBucket
var nextBucket int64 // 0 var nextBucket int64 // 0
if co.currentBucket == 0 { if counter.currentBucket == 0 {
nextBucket = 1 nextBucket = 1
} }
atomic.AddInt64(&co.buckets[oldBucket], co.buckets[nextBucket]) atomic.AddInt64(&counter.buckets[oldBucket], counter.buckets[nextBucket])
atomic.StoreInt64(&co.buckets[nextBucket], 0) atomic.StoreInt64(&counter.buckets[nextBucket], 0)
atomic.StoreInt64(&co.currentBucket, nextBucket) atomic.StoreInt64(&counter.currentBucket, nextBucket)
previousViewChunk := co.buckets[oldBucket] var previousViewChunk = counter.buckets[oldBucket]
atomic.AddInt64(&co.buckets[oldBucket], -previousViewChunk) atomic.AddInt64(&counter.buckets[oldBucket], -previousViewChunk)
e = co.insertChunk(previousViewChunk) return counter.insertChunk(previousViewChunk)
if e != nil {
return errors.Wrap(errors.WithStack(e), "topics counter")
}
return nil
} }
func (co *DefaultTopicCounter) Bump() { func (counter *DefaultTopicCounter) Bump() {
atomic.AddInt64(&co.buckets[co.currentBucket], 1) atomic.AddInt64(&counter.buckets[counter.currentBucket], 1)
} }
func (co *DefaultTopicCounter) insertChunk(count int64) error { func (counter *DefaultTopicCounter) insertChunk(count int64) error {
if count == 0 { if count == 0 {
return nil return nil
} }
c.DebugLogf("Inserting a topicchunk with a count of %d", count) common.DebugLogf("Inserting a topicchunk with a count of %d", count)
_, e := co.insert.Exec(count) _, err := counter.insert.Exec(count)
return e return err
} }

View File

@ -2,15 +2,11 @@ package counters
import ( import (
"database/sql" "database/sql"
"strconv"
"strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time"
c "git.tuxpa.in/a/gosora/common" ".."
qgen "git.tuxpa.in/a/gosora/query_gen" "../../query_gen/lib"
"github.com/pkg/errors"
) )
var TopicViewCounter *DefaultTopicViewCounter var TopicViewCounter *DefaultTopicViewCounter
@ -22,237 +18,117 @@ type DefaultTopicViewCounter struct {
oddLock sync.RWMutex oddLock sync.RWMutex
evenLock sync.RWMutex evenLock sync.RWMutex
weekState byte
update *sql.Stmt update *sql.Stmt
resetOdd *sql.Stmt
resetEven *sql.Stmt
resetBoth *sql.Stmt
insertListBuf []TopicViewInsert
saveTick *SavedTick
} }
func NewDefaultTopicViewCounter() (*DefaultTopicViewCounter, error) { func NewDefaultTopicViewCounter() (*DefaultTopicViewCounter, error) {
acc := qgen.NewAcc() acc := qgen.NewAcc()
t := "topics" counter := &DefaultTopicViewCounter{
co := &DefaultTopicViewCounter{
oddTopics: make(map[int]*RWMutexCounterBucket), oddTopics: make(map[int]*RWMutexCounterBucket),
evenTopics: make(map[int]*RWMutexCounterBucket), evenTopics: make(map[int]*RWMutexCounterBucket),
update: acc.Update("topics").Set("views = views + ?").Where("tid = ?").Prepare(),
//update: acc.Update(t).Set("views=views+?").Where("tid=?").Prepare(),
update: acc.Update(t).Set("views=views+?,weekEvenViews=weekEvenViews+?,weekOddViews=weekOddViews+?").Where("tid=?").Prepare(),
resetOdd: acc.Update(t).Set("weekOddViews=0").Prepare(),
resetEven: acc.Update(t).Set("weekEvenViews=0").Prepare(),
resetBoth: acc.Update(t).Set("weekOddViews=0,weekEvenViews=0").Prepare(),
//insertListBuf: make([]TopicViewInsert, 1024),
} }
e := co.WeekResetInit() common.AddScheduledFifteenMinuteTask(counter.Tick) // Who knows how many topics we have queued up, we probably don't want this running too frequently
if e != nil { //common.AddScheduledSecondTask(counter.Tick)
return co, e common.AddShutdownTask(counter.Tick)
return counter, acc.FirstError()
} }
tick := func(f func() error) { func (counter *DefaultTopicViewCounter) Tick() error {
c.Tasks.FifteenMin.Add(f) // Who knows how many topics we have queued up, we probably don't want this running too frequently
//c.Tasks.Sec.Add(f)
c.Tasks.Shutdown.Add(f)
}
tick(co.Tick)
tick(co.WeekResetTick)
return co, acc.FirstError()
}
type TopicViewInsert struct {
Count int
TopicID int
}
type SavedTick struct {
I int
I2 int
}
func (co *DefaultTopicViewCounter) handleInsertListBuf(i, i2 int) error {
ilb := co.insertListBuf
var lastSuccess int
for i3 := i2; i3 < i; i3++ {
iitem := ilb[i3]
if e := co.insertChunk(iitem.Count, iitem.TopicID); e != nil {
co.saveTick = &SavedTick{I: i, I2: lastSuccess + 1}
for i3 := i2; i3 < i && i3 <= lastSuccess; i3++ {
ilb[i3].Count, ilb[i3].TopicID = 0, 0
}
return errors.Wrap(errors.WithStack(e), "topicview counter")
}
lastSuccess = i3
}
for i3 := i2; i3 < i; i3++ {
ilb[i3].Count, ilb[i3].TopicID = 0, 0
}
return nil
}
func (co *DefaultTopicViewCounter) Tick() error {
// TODO: Fold multiple 1 view topics into one query // TODO: Fold multiple 1 view topics into one query
/*if co.saveTick != nil { counter.oddLock.RLock()
e := co.handleInsertListBuf(co.saveTick.I, co.saveTick.I2) oddTopics := counter.oddTopics
if e != nil { counter.oddLock.RUnlock()
return e for topicID, topic := range oddTopics {
}
co.saveTick = nil
}*/
cLoop := func(l *sync.RWMutex, m map[int]*RWMutexCounterBucket) error {
//i := 0
l.RLock()
for topicID, topic := range m {
l.RUnlock()
var count int var count int
topic.RLock() topic.RLock()
count = topic.counter count = topic.counter
topic.RUnlock() topic.RUnlock()
// TODO: Only delete the bucket when it's zero to avoid hitting popular topics? // TODO: Only delete the bucket when it's zero to avoid hitting popular topics?
l.Lock() counter.oddLock.Lock()
delete(m, topicID) delete(counter.oddTopics, topicID)
l.Unlock() counter.oddLock.Unlock()
/*if len(co.insertListBuf) >= i { err := counter.insertChunk(count, topicID)
co.insertListBuf[i].Count = count if err != nil {
co.insertListBuf[i].TopicID = topicID return err
i++
} else if i < 4096 {
co.insertListBuf = append(co.insertListBuf, TopicViewInsert{count, topicID})
} else */if e := co.insertChunk(count, topicID); e != nil {
return errors.Wrap(errors.WithStack(e), "topicview counter")
} }
l.RLock()
}
l.RUnlock()
return nil //co.handleInsertListBuf(i, 0)
}
e := cLoop(&co.oddLock, co.oddTopics)
if e != nil {
return e
}
return cLoop(&co.evenLock, co.evenTopics)
} }
func (co *DefaultTopicViewCounter) WeekResetInit() error { counter.evenLock.RLock()
lastWeekResetStr, e := c.Meta.Get("lastWeekReset") evenTopics := counter.evenTopics
if e != nil && e != sql.ErrNoRows { counter.evenLock.RUnlock()
return e for topicID, topic := range evenTopics {
var count int
topic.RLock()
count = topic.counter
topic.RUnlock()
// TODO: Only delete the bucket when it's zero to avoid hitting popular topics?
counter.evenLock.Lock()
delete(counter.evenTopics, topicID)
counter.evenLock.Unlock()
err := counter.insertChunk(count, topicID)
if err != nil {
return err
}
} }
spl := strings.Split(lastWeekResetStr, "-")
if len(spl) <= 1 {
return nil return nil
} }
weekState, e := strconv.Atoi(spl[1])
if e != nil {
return e
}
co.weekState = byte(weekState)
unixLastWeekReset, e := strconv.ParseInt(spl[0], 10, 64)
if e != nil {
return e
}
resetTime := time.Unix(unixLastWeekReset, 0)
if time.Since(resetTime).Hours() >= (24 * 7) {
_, e = co.resetBoth.Exec()
}
return e
}
func (co *DefaultTopicViewCounter) WeekResetTick() (e error) {
now := time.Now()
_, week := now.ISOWeek()
if week != int(co.weekState) {
if week%2 == 0 { // is even?
_, e = co.resetOdd.Exec()
} else {
_, e = co.resetEven.Exec()
}
co.weekState = byte(week)
}
// TODO: Retry?
if e != nil {
return e
}
return c.Meta.Set("lastWeekReset", strconv.FormatInt(now.Unix(), 10)+"-"+strconv.Itoa(week))
}
// TODO: Optimise this further. E.g. Using IN() on every one view topic. Rinse and repeat for two views, three views, four views and five views. // TODO: Optimise this further. E.g. Using IN() on every one view topic. Rinse and repeat for two views, three views, four views and five views.
func (co *DefaultTopicViewCounter) insertChunk(count, topicID int) (err error) { func (counter *DefaultTopicViewCounter) insertChunk(count int, topicID int) error {
if count == 0 { if count == 0 {
return nil return nil
} }
c.DebugLogf("Inserting %d views into topic %d", count, topicID) common.DebugLogf("Inserting %d views into topic %d", count, topicID)
even, odd := 0, 0 _, err := counter.update.Exec(count, topicID)
_, week := time.Now().ISOWeek() if err != nil {
if week%2 == 0 { // is even?
even += count
} else {
odd += count
}
if true {
_, err = co.update.Exec(count, even, odd, topicID)
} else {
_, err = co.update.Exec(count, topicID)
}
if err == sql.ErrNoRows {
return nil
} else if err != nil {
return err return err
} }
// TODO: Add a way to disable this for extra speed ;) // TODO: Add a way to disable this for extra speed ;)
tc := c.Topics.GetCache() tcache := common.Topics.GetCache()
if tc != nil { if tcache != nil {
t, err := tc.Get(topicID) topic, err := tcache.Get(topicID)
if err == sql.ErrNoRows { if err != nil {
return nil
} else if err != nil {
return err return err
} }
atomic.AddInt64(&t.ViewCount, int64(count)) atomic.AddInt64(&topic.ViewCount, int64(count))
} }
return nil return nil
} }
func (co *DefaultTopicViewCounter) Bump(topicID int) { func (counter *DefaultTopicViewCounter) Bump(topicID int) {
// Is the ID even? // Is the ID even?
if topicID%2 == 0 { if topicID%2 == 0 {
co.evenLock.RLock() counter.evenLock.RLock()
t, ok := co.evenTopics[topicID] topic, ok := counter.evenTopics[topicID]
co.evenLock.RUnlock() counter.evenLock.RUnlock()
if ok { if ok {
t.Lock() topic.Lock()
t.counter++ topic.counter++
t.Unlock() topic.Unlock()
} else { } else {
co.evenLock.Lock() counter.evenLock.Lock()
co.evenTopics[topicID] = &RWMutexCounterBucket{counter: 1} counter.evenTopics[topicID] = &RWMutexCounterBucket{counter: 1}
co.evenLock.Unlock() counter.evenLock.Unlock()
} }
return return
} }
co.oddLock.RLock() counter.oddLock.RLock()
t, ok := co.oddTopics[topicID] topic, ok := counter.oddTopics[topicID]
co.oddLock.RUnlock() counter.oddLock.RUnlock()
if ok { if ok {
t.Lock() topic.Lock()
t.counter++ topic.counter++
t.Unlock() topic.Unlock()
} else { } else {
co.oddLock.Lock() counter.oddLock.Lock()
co.oddTopics[topicID] = &RWMutexCounterBucket{counter: 1} counter.oddTopics[topicID] = &RWMutexCounterBucket{counter: 1}
co.oddLock.Unlock() counter.oddLock.Unlock()
} }
} }

View File

@ -1,20 +0,0 @@
package common
import (
"path/filepath"
"os"
)
func DirSize(path string) (int, error) {
var size int64
err := filepath.Walk(path, func(_ string, file os.FileInfo, err error) error {
if err != nil {
return err
}
if !file.IsDir() {
size += file.Size()
}
return err
})
return int(size), err
}

View File

@ -1,125 +1,75 @@
package common package common
import ( import (
"crypto/tls"
"fmt" "fmt"
"net/mail"
"net/smtp" "net/smtp"
"strings"
p "git.tuxpa.in/a/gosora/common/phrases"
) )
func SendActivationEmail(username, email, token string) error { type Email struct {
schema := "http" UserID int
if Config.SslSchema { Email string
Validated bool
Primary bool
Token string
}
func SendValidationEmail(username string, email string, token string) bool {
var schema = "http"
if Site.EnableSsl {
schema += "s" schema += "s"
} }
// TODO: Move these to the phrase system // TODO: Move these to the phrase system
subject := "Account Activation - " + Site.Name subject := "Validate Your Email @ " + Site.Name
msg := "Dear " + username + ", to complete your registration on our forums, we need you to validate your email, so that we can confirm that this email actually belongs to you.\n\nClick on the following link to do so. " + schema + "://" + Site.URL + "/user/edit/token/" + token + "\n\nIf you haven't created an account here, then please feel free to ignore this email.\nWe're sorry for the inconvenience this may have caused." msg := "Dear " + username + ", following your registration on our forums, we ask you to validate your email, so that we can confirm that this email actually belongs to you.\n\nClick on the following link to do so. " + schema + "://" + Site.URL + "/user/edit/token/" + token + "\n\nIf you haven't created an account here, then please feel free to ignore this email.\nWe're sorry for the inconvenience this may have caused."
return SendEmail(email, subject, msg) return SendEmail(email, subject, msg)
} }
func SendValidationEmail(username, email, token string) error {
schema := "http"
if Config.SslSchema {
schema += "s"
}
r := func(body *string) func(name, val string) {
return func(name, val string) {
*body = strings.Replace(*body, "{{"+name+"}}", val, -1)
}
}
subject := p.GetAccountPhrase("ValidateEmailSubject")
r1 := r(&subject)
r1("name", Site.Name)
body := p.GetAccountPhrase("ValidateEmailBody")
r2 := r(&body)
r2("username", username)
r2("schema", schema)
r2("url", Site.URL)
r2("token", token)
return SendEmail(email, subject, body)
}
// TODO: Refactor this // TODO: Refactor this
func SendEmail(email, subject, msg string) (err error) { // TODO: Add support for TLS
func SendEmail(email string, subject string, msg string) bool {
// This hook is useful for plugin_sendmail or for testing tools. Possibly to hook it into some sort of mail server? // This hook is useful for plugin_sendmail or for testing tools. Possibly to hook it into some sort of mail server?
ret, hasHook := GetHookTable().VhookNeedHook("email_send_intercept", email, subject, msg) // TODO: Abstract this
if hasHook { if Vhooks["email_send_intercept"] != nil {
return ret.(error) return Vhooks["email_send_intercept"](email, subject, msg).(bool)
} }
body := "Subject: " + subject + "\n\n" + msg + "\n"
from := mail.Address{"", Site.Email} con, err := smtp.Dial(Config.SMTPServer + ":" + Config.SMTPPort)
to := mail.Address{"", email}
headers := make(map[string]string)
headers["From"] = from.String()
headers["To"] = to.String()
headers["Subject"] = subject
body := ""
for k, v := range headers {
body += fmt.Sprintf("%s: %s\r\n", k, v)
}
body += "\r\n" + msg
var c *smtp.Client
var conn *tls.Conn
if Config.SMTPEnableTLS {
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: Config.SMTPServer,
}
conn, err = tls.Dial("tcp", Config.SMTPServer+":"+Config.SMTPPort, tlsconfig)
if err != nil { if err != nil {
LogWarning(err) return false
return err
}
c, err = smtp.NewClient(conn, Config.SMTPServer)
} else {
c, err = smtp.Dial(Config.SMTPServer + ":" + Config.SMTPPort)
}
if err != nil {
LogWarning(err)
return err
} }
if Config.SMTPUsername != "" { if Config.SMTPUsername != "" {
auth := smtp.PlainAuth("", Config.SMTPUsername, Config.SMTPPassword, Config.SMTPServer) auth := smtp.PlainAuth("", Config.SMTPUsername, Config.SMTPPassword, Config.SMTPServer)
err = c.Auth(auth) err = con.Auth(auth)
if err != nil { if err != nil {
LogWarning(err) return false
return err
} }
} }
if err = c.Mail(from.Address); err != nil {
LogWarning(err)
return err
}
if err = c.Rcpt(to.Address); err != nil {
LogWarning(err)
return err
}
w, err := c.Data() err = con.Mail(Site.Email)
if err != nil { if err != nil {
LogWarning(err) return false
return err
} }
_, err = w.Write([]byte(body)) err = con.Rcpt(email)
if err != nil { if err != nil {
LogWarning(err) return false
return err
}
if err = w.Close(); err != nil {
LogWarning(err)
return err
}
if err = c.Quit(); err != nil {
LogWarning(err)
return err
} }
return nil emailData, err := con.Data()
if err != nil {
return false
}
_, err = fmt.Fprintf(emailData, body)
if err != nil {
return false
}
err = emailData.Close()
if err != nil {
return false
}
return con.Quit() == nil
} }

View File

@ -1,90 +1,52 @@
package common package common
import ( import "database/sql"
"database/sql" import "../query_gen/lib"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var Emails EmailStore var Emails EmailStore
type Email struct {
UserID int
Email string
Validated bool
Primary bool
Token string
}
type EmailStore interface { type EmailStore interface {
// TODO: Add an autoincrement key GetEmailsByUser(user *User) (emails []Email, err error)
Get(u *User, email string) (Email, error)
GetEmailsByUser(u *User) (emails []Email, err error)
Add(uid int, email, token string) error
Delete(uid int, email string) error
VerifyEmail(email string) error VerifyEmail(email string) error
} }
type DefaultEmailStore struct { type DefaultEmailStore struct {
get *sql.Stmt
getEmailsByUser *sql.Stmt getEmailsByUser *sql.Stmt
add *sql.Stmt
delete *sql.Stmt
verifyEmail *sql.Stmt verifyEmail *sql.Stmt
} }
func NewDefaultEmailStore(acc *qgen.Accumulator) (*DefaultEmailStore, error) { func NewDefaultEmailStore(acc *qgen.Accumulator) (*DefaultEmailStore, error) {
e := "emails"
return &DefaultEmailStore{ return &DefaultEmailStore{
get: acc.Select(e).Columns("email,validated,token").Where("uid=? AND email=?").Prepare(), getEmailsByUser: acc.Select("emails").Columns("email, validated, token").Where("uid = ?").Prepare(),
getEmailsByUser: acc.Select(e).Columns("email,validated,token").Where("uid=?").Prepare(),
add: acc.Insert(e).Columns("uid,email,validated,token").Fields("?,?,?,?").Prepare(),
delete: acc.Delete(e).Where("uid=? AND email=?").Prepare(),
// Need to fix this: Empty string isn't working, it gets set to 1 instead x.x -- Has this been fixed? // Need to fix this: Empty string isn't working, it gets set to 1 instead x.x -- Has this been fixed?
verifyEmail: acc.Update(e).Set("validated=1,token=''").Where("email=?").Prepare(), verifyEmail: acc.Update("emails").Set("validated = 1, token = ''").Where("email = ?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
func (s *DefaultEmailStore) Get(user *User, email string) (Email, error) { func (store *DefaultEmailStore) GetEmailsByUser(user *User) (emails []Email, err error) {
e := Email{UserID: user.ID, Primary: email != "" && user.Email == email} email := Email{UserID: user.ID}
err := s.get.QueryRow(user.ID, email).Scan(&e.Email, &e.Validated, &e.Token) rows, err := store.getEmailsByUser.Query(user.ID)
return e, err
}
func (s *DefaultEmailStore) GetEmailsByUser(user *User) (emails []Email, err error) {
e := Email{UserID: user.ID}
rows, err := s.getEmailsByUser.Query(user.ID)
if err != nil { if err != nil {
return emails, err return emails, err
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
err := rows.Scan(&e.Email, &e.Validated, &e.Token) err := rows.Scan(&email.Email, &email.Validated, &email.Token)
if err != nil { if err != nil {
return emails, err return emails, err
} }
if e.Email == user.Email {
e.Primary = true if email.Email == user.Email {
email.Primary = true
} }
emails = append(emails, e) emails = append(emails, email)
} }
return emails, rows.Err() return emails, rows.Err()
} }
func (s *DefaultEmailStore) Add(uid int, email, token string) error { func (store *DefaultEmailStore) VerifyEmail(email string) error {
email = CanonEmail(SanitiseSingleLine(email)) _, err := store.verifyEmail.Exec(email)
_, err := s.add.Exec(uid, email, 0, token)
return err
}
func (s *DefaultEmailStore) Delete(uid int, email string) error {
_, err := s.delete.Exec(uid, email)
return err
}
func (s *DefaultEmailStore) VerifyEmail(email string) error {
email = CanonEmail(SanitiseSingleLine(email))
_, err := s.verifyEmail.Exec(email)
return err return err
} }

View File

@ -1,15 +1,11 @@
package common package common
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"runtime/debug" "runtime/debug"
"strings" "strings"
"sync" "sync"
"sync/atomic"
p "git.tuxpa.in/a/gosora/common/phrases"
) )
type ErrorItem struct { type ErrorItem struct {
@ -21,8 +17,7 @@ type ErrorItem struct {
// TODO: Use the errorBuffer variable to construct the system log in the Control Panel. Should we log errors caused by users too? Or just collect statistics on those or do nothing? Intercept recover()? Could we intercept the logger instead here? We might get too much information, if we intercept the logger, maybe make it part of the Debug page? // TODO: Use the errorBuffer variable to construct the system log in the Control Panel. Should we log errors caused by users too? Or just collect statistics on those or do nothing? Intercept recover()? Could we intercept the logger instead here? We might get too much information, if we intercept the logger, maybe make it part of the Debug page?
// ? - Should we pass Header / HeaderLite rather than forcing the errors to pull the global Header instance? // ? - Should we pass Header / HeaderLite rather than forcing the errors to pull the global Header instance?
var errorBufferMutex sync.RWMutex var errorBufferMutex sync.RWMutex
//var errorBuffer []ErrorItem var errorBuffer []ErrorItem
var ErrorCountSinceStartup int64
//var notfoundCountPerSecond int //var notfoundCountPerSecond int
//var nopermsCountPerSecond int //var nopermsCountPerSecond int
@ -34,16 +29,12 @@ var tList []interface{}
type RouteError interface { type RouteError interface {
Type() string Type() string
Error() string Error() string
Cause() string
JSON() bool JSON() bool
Handled() bool Handled() bool
Wrap(string)
} }
type RouteErrorImpl struct { type RouteErrorImpl struct {
userText string text string
sysText string
system bool system bool
json bool json bool
handled bool handled bool
@ -58,14 +49,7 @@ func (err *RouteErrorImpl) Type() string {
} }
func (err *RouteErrorImpl) Error() string { func (err *RouteErrorImpl) Error() string {
return err.userText return err.text
}
func (err *RouteErrorImpl) Cause() string {
if err.sysText == "" {
return err.Error()
}
return err.sysText
} }
// Respond with JSON? // Respond with JSON?
@ -78,83 +62,54 @@ func (err *RouteErrorImpl) Handled() bool {
return err.handled return err.handled
} }
// Move the current error into the system error slot and add a new one to the user error slot to show the user
func (err *RouteErrorImpl) Wrap(userErr string) {
err.sysText = err.userText
err.userText = userErr
}
func HandledRouteError() RouteError { func HandledRouteError() RouteError {
return &RouteErrorImpl{"", "", false, false, true} return &RouteErrorImpl{"", false, false, true}
}
func Error(errmsg string) RouteError {
return &RouteErrorImpl{errmsg, "", false, false, false}
}
func FromError(err error) RouteError {
return &RouteErrorImpl{err.Error(), "", false, false, false}
}
func ErrorJSQ(errmsg string, js bool) RouteError {
return &RouteErrorImpl{errmsg, "", false, js, false}
}
func SysError(errmsg string) RouteError {
return &RouteErrorImpl{errmsg, errmsg, true, false, false}
} }
// LogError logs internal handler errors which can't be handled with InternalError() as a wrapper for log.Fatal(), we might do more with it in the future. // LogError logs internal handler errors which can't be handled with InternalError() as a wrapper for log.Fatal(), we might do more with it in the future.
// TODO: Clean-up extra as a way of passing additional context // TODO: Clean-up extra as a way of passing additional context
func LogError(err error, extra ...string) { func LogError(err error, extra ...string) {
LogWarning(err, extra...) LogWarning(err, extra...)
ErrLogger.Fatal("") log.Fatal("")
} }
func LogWarning(err error, extra ...string) { func LogWarning(err error, extra ...string) {
var esb strings.Builder var errmsg string
for _, extraBit := range extra { for _, extraBit := range extra {
esb.WriteString(extraBit) errmsg += extraBit + "\n"
esb.WriteRune(10)
} }
if err == nil { if err == nil {
esb.WriteString("nil error found") errmsg += "Unknown error"
} else { } else {
esb.WriteString(err.Error()) errmsg += err.Error()
} }
esb.WriteRune(10) stack := debug.Stack()
errmsg := esb.String() log.Print(errmsg+"\n", string(stack))
errorBufferMutex.Lock() errorBufferMutex.Lock()
defer errorBufferMutex.Unlock() defer errorBufferMutex.Unlock()
stack := debug.Stack() // debug.Stack() can't be executed concurrently, so we'll guard this with a mutex too errorBuffer = append(errorBuffer, ErrorItem{err, stack})
Err(errmsg, string(stack))
//errorBuffer = append(errorBuffer, ErrorItem{err, stack})
atomic.AddInt64(&ErrorCountSinceStartup,1)
} }
func errorHeader(w http.ResponseWriter, u *User, title string) *Header { func errorHeader(w http.ResponseWriter, user User, title string) *Header {
h := DefaultHeader(w, u) header := DefaultHeader(w, user)
h.Title = title header.Title = title
h.Zone = "error" return header
return h
} }
// TODO: Dump the request? // TODO: Dump the request?
// InternalError is the main function for handling internal errors, while simultaneously printing out a page for the end-user to let them know that *something* has gone wrong // InternalError is the main function for handling internal errors, while simultaneously printing out a page for the end-user to let them know that *something* has gone wrong
// ? - Add a user parameter? // ? - Add a user parameter?
// ! Do not call CustomError here or we might get an error loop
func InternalError(err error, w http.ResponseWriter, r *http.Request) RouteError { func InternalError(err error, w http.ResponseWriter, r *http.Request) RouteError {
pi := ErrorPage{errorHeader(w, &GuestUser, p.GetErrorPhrase("internal_error_title")), p.GetErrorPhrase("internal_error_body")} pi := ErrorPage{errorHeader(w, GuestUser, "Internal Server Error"), "A problem has occurred in the system."}
handleErrorTemplate(w, r, pi, 500) handleErrorTemplate(w, r, pi)
LogError(err) LogError(err)
return HandledRouteError() return HandledRouteError()
} }
// InternalErrorJSQ is the JSON "maybe" version of InternalError which can handle both JSON and normal requests // InternalErrorJSQ is the JSON "maybe" version of InternalError which can handle both JSON and normal requests
// ? - Add a user parameter? // ? - Add a user parameter?
func InternalErrorJSQ(err error, w http.ResponseWriter, r *http.Request, js bool) RouteError { func InternalErrorJSQ(err error, w http.ResponseWriter, r *http.Request, isJs bool) RouteError {
if !js { if !isJs {
return InternalError(err, w, r) return InternalError(err, w, r)
} }
return InternalErrorJS(err, w, r) return InternalErrorJS(err, w, r)
@ -164,23 +119,25 @@ func InternalErrorJSQ(err error, w http.ResponseWriter, r *http.Request, js bool
// ? - Add a user parameter? // ? - Add a user parameter?
func InternalErrorJS(err error, w http.ResponseWriter, r *http.Request) RouteError { func InternalErrorJS(err error, w http.ResponseWriter, r *http.Request) RouteError {
w.WriteHeader(500) w.WriteHeader(500)
writeJsonError(p.GetErrorPhrase("internal_error_body"), w) writeJsonError("A problem has occurred in the system.", w)
LogError(err) LogError(err)
return HandledRouteError() return HandledRouteError()
} }
// When the task system detects if the database is down, some database errors might slip by this // When the task system detects if the database is down, some database errors might lip by this
func DatabaseError(w http.ResponseWriter, r *http.Request) RouteError { func DatabaseError(w http.ResponseWriter, r *http.Request) RouteError {
pi := ErrorPage{errorHeader(w, &GuestUser, p.GetErrorPhrase("internal_error_title")), p.GetErrorPhrase("internal_error_body")} pi := ErrorPage{errorHeader(w, GuestUser, "Internal Server Error"), "A problem has occurred in the system."}
handleErrorTemplate(w, r, pi, 500) handleErrorTemplate(w, r, pi)
return HandledRouteError() return HandledRouteError()
} }
var xmlInternalError = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<error>A problem has occured</error>`)
func InternalErrorXML(err error, w http.ResponseWriter, r *http.Request) RouteError { func InternalErrorXML(err error, w http.ResponseWriter, r *http.Request) RouteError {
w.Header().Set("Content-Type", "application/xml") w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(500) w.WriteHeader(500)
w.Write([]byte(`<?xml version="1.0"encoding="UTF-8"?> w.Write(xmlInternalError)
<error>` + p.GetErrorPhrase("internal_error_body") + `</error>`))
LogError(err) LogError(err)
return HandledRouteError() return HandledRouteError()
} }
@ -189,16 +146,15 @@ func InternalErrorXML(err error, w http.ResponseWriter, r *http.Request) RouteEr
func SilentInternalErrorXML(err error, w http.ResponseWriter, r *http.Request) RouteError { func SilentInternalErrorXML(err error, w http.ResponseWriter, r *http.Request) RouteError {
w.Header().Set("Content-Type", "application/xml") w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(500) w.WriteHeader(500)
w.Write([]byte(`<?xml version="1.0"encoding="UTF-8"?> w.Write(xmlInternalError)
<error>` + p.GetErrorPhrase("internal_error_body") + `</error>`))
log.Print("InternalError: ", err) log.Print("InternalError: ", err)
return HandledRouteError() return HandledRouteError()
} }
// ! Do not call CustomError here otherwise we might get an error loop
func PreError(errmsg string, w http.ResponseWriter, r *http.Request) RouteError { func PreError(errmsg string, w http.ResponseWriter, r *http.Request) RouteError {
pi := ErrorPage{errorHeader(w, &GuestUser, p.GetErrorPhrase("error_title")), errmsg} w.WriteHeader(500)
handleErrorTemplate(w, r, pi, 500) pi := ErrorPage{errorHeader(w, GuestUser, "Error"), errmsg}
handleErrorTemplate(w, r, pi)
return HandledRouteError() return HandledRouteError()
} }
@ -208,8 +164,8 @@ func PreErrorJS(errmsg string, w http.ResponseWriter, r *http.Request) RouteErro
return HandledRouteError() return HandledRouteError()
} }
func PreErrorJSQ(errmsg string, w http.ResponseWriter, r *http.Request, js bool) RouteError { func PreErrorJSQ(errmsg string, w http.ResponseWriter, r *http.Request, isJs bool) RouteError {
if !js { if !isJs {
return PreError(errmsg, w, r) return PreError(errmsg, w, r)
} }
return PreErrorJS(errmsg, w, r) return PreErrorJS(errmsg, w, r)
@ -217,35 +173,16 @@ func PreErrorJSQ(errmsg string, w http.ResponseWriter, r *http.Request, js bool)
// LocalError is an error shown to the end-user when something goes wrong and it's not the software's fault // LocalError is an error shown to the end-user when something goes wrong and it's not the software's fault
// TODO: Pass header in for this and similar errors instead of having to pass in both user and w? Would also allow for more stateful things, although this could be a problem // TODO: Pass header in for this and similar errors instead of having to pass in both user and w? Would also allow for more stateful things, although this could be a problem
/*func LocalError(errmsg string, w http.ResponseWriter, r *http.Request, user *User) RouteError { func LocalError(errmsg string, w http.ResponseWriter, r *http.Request, user User) RouteError {
w.WriteHeader(500) w.WriteHeader(500)
pi := ErrorPage{errorHeader(w, user, p.GetErrorPhrase("local_error_title")), errmsg} pi := ErrorPage{errorHeader(w, user, "Local Error"), errmsg}
handleErrorTemplate(w, r, pi) handleErrorTemplate(w, r, pi)
return HandledRouteError() return HandledRouteError()
}*/
func LocalError(errmsg string, w http.ResponseWriter, r *http.Request, u *User) RouteError {
return SimpleError(errmsg, w, r, errorHeader(w, u, ""))
} }
func LocalErrorf(errmsg string, w http.ResponseWriter, r *http.Request, u *User, params ...interface{}) RouteError { func LocalErrorJSQ(errmsg string, w http.ResponseWriter, r *http.Request, user User, isJs bool) RouteError {
return LocalError(fmt.Sprintf(errmsg, params), w, r, u) if !isJs {
} return LocalError(errmsg, w, r, user)
func SimpleError(errmsg string, w http.ResponseWriter, r *http.Request, h *Header) RouteError {
if h == nil {
h = errorHeader(w, &GuestUser, p.GetErrorPhrase("local_error_title"))
} else {
h.Title = p.GetErrorPhrase("local_error_title")
}
pi := ErrorPage{h, errmsg}
handleErrorTemplate(w, r, pi, 500)
return HandledRouteError()
}
func LocalErrorJSQ(errmsg string, w http.ResponseWriter, r *http.Request, u *User, js bool) RouteError {
if !js {
return SimpleError(errmsg, w, r, errorHeader(w, u, ""))
} }
return LocalErrorJS(errmsg, w, r) return LocalErrorJS(errmsg, w, r)
} }
@ -258,138 +195,119 @@ func LocalErrorJS(errmsg string, w http.ResponseWriter, r *http.Request) RouteEr
// TODO: We might want to centralise the error logic in the future and just return what the error handler needs to construct the response rather than handling it here // TODO: We might want to centralise the error logic in the future and just return what the error handler needs to construct the response rather than handling it here
// NoPermissions is an error shown to the end-user when they try to access an area which they aren't authorised to access // NoPermissions is an error shown to the end-user when they try to access an area which they aren't authorised to access
func NoPermissions(w http.ResponseWriter, r *http.Request, u *User) RouteError { func NoPermissions(w http.ResponseWriter, r *http.Request, user User) RouteError {
pi := ErrorPage{errorHeader(w, u, p.GetErrorPhrase("no_permissions_title")), p.GetErrorPhrase("no_permissions_body")} w.WriteHeader(403)
handleErrorTemplate(w, r, pi, 403) pi := ErrorPage{errorHeader(w, user, "Local Error"), "You don't have permission to do that."}
handleErrorTemplate(w, r, pi)
return HandledRouteError() return HandledRouteError()
} }
func NoPermissionsJSQ(w http.ResponseWriter, r *http.Request, u *User, js bool) RouteError { func NoPermissionsJSQ(w http.ResponseWriter, r *http.Request, user User, isJs bool) RouteError {
if !js { if !isJs {
return NoPermissions(w, r, u) return NoPermissions(w, r, user)
} }
return NoPermissionsJS(w, r, u) return NoPermissionsJS(w, r, user)
} }
func NoPermissionsJS(w http.ResponseWriter, r *http.Request, u *User) RouteError { func NoPermissionsJS(w http.ResponseWriter, r *http.Request, user User) RouteError {
w.WriteHeader(403) w.WriteHeader(403)
writeJsonError(p.GetErrorPhrase("no_permissions_body"), w) writeJsonError("You don't have permission to do that.", w)
return HandledRouteError() return HandledRouteError()
} }
// ? - Is this actually used? Should it be used? A ban in Gosora should be more of a permission revocation to stop them posting rather than something which spits up an error page, right? // ? - Is this actually used? Should it be used? A ban in Gosora should be more of a permission revocation to stop them posting rather than something which spits up an error page, right?
func Banned(w http.ResponseWriter, r *http.Request, u *User) RouteError { func Banned(w http.ResponseWriter, r *http.Request, user User) RouteError {
pi := ErrorPage{errorHeader(w, u, p.GetErrorPhrase("banned_title")), p.GetErrorPhrase("banned_body")} w.WriteHeader(403)
handleErrorTemplate(w, r, pi, 403) pi := ErrorPage{errorHeader(w, user, "Banned"), "You have been banned from this site."}
handleErrorTemplate(w, r, pi)
return HandledRouteError() return HandledRouteError()
} }
// nolint // nolint
// BannedJSQ is the version of the banned error page which handles both JavaScript requests and normal page loads // BannedJSQ is the version of the banned error page which handles both JavaScript requests and normal page loads
func BannedJSQ(w http.ResponseWriter, r *http.Request, user *User, js bool) RouteError { func BannedJSQ(w http.ResponseWriter, r *http.Request, user User, isJs bool) RouteError {
if !js { if !isJs {
return Banned(w, r, user) return Banned(w, r, user)
} }
return BannedJS(w, r, user) return BannedJS(w, r, user)
} }
func BannedJS(w http.ResponseWriter, r *http.Request, u *User) RouteError { func BannedJS(w http.ResponseWriter, r *http.Request, user User) RouteError {
w.WriteHeader(403) w.WriteHeader(403)
writeJsonError(p.GetErrorPhrase("banned_body"), w) writeJsonError("You have been banned from this site.", w)
return HandledRouteError() return HandledRouteError()
} }
// nolint // nolint
func LoginRequiredJSQ(w http.ResponseWriter, r *http.Request, u *User, js bool) RouteError { func LoginRequiredJSQ(w http.ResponseWriter, r *http.Request, user User, isJs bool) RouteError {
if !js { if !isJs {
return LoginRequired(w, r, u) return LoginRequired(w, r, user)
} }
return LoginRequiredJS(w, r, u) return LoginRequiredJS(w, r, user)
} }
// ? - Where is this used? Should we use it more? // ? - Where is this used? Should we use it more?
// LoginRequired is an error shown to the end-user when they try to access an area which requires them to login // LoginRequired is an error shown to the end-user when they try to access an area which requires them to login
func LoginRequired(w http.ResponseWriter, r *http.Request, u *User) RouteError { func LoginRequired(w http.ResponseWriter, r *http.Request, user User) RouteError {
return CustomError(p.GetErrorPhrase("login_required_body"), 401, p.GetErrorPhrase("no_permissions_title"), w, r, nil, u) w.WriteHeader(401)
pi := ErrorPage{errorHeader(w, user, "Local Error"), "You need to login to do that."}
handleErrorTemplate(w, r, pi)
return HandledRouteError()
} }
// nolint // nolint
func LoginRequiredJS(w http.ResponseWriter, r *http.Request, u *User) RouteError { func LoginRequiredJS(w http.ResponseWriter, r *http.Request, user User) RouteError {
w.WriteHeader(401) w.WriteHeader(401)
writeJsonError(p.GetErrorPhrase("login_required_body"), w) writeJsonError("You need to login to do that.", w)
return HandledRouteError() return HandledRouteError()
} }
// SecurityError is used whenever a session mismatch is found // SecurityError is used whenever a session mismatch is found
// ? - Should we add JS and JSQ versions of this? // ? - Should we add JS and JSQ versions of this?
func SecurityError(w http.ResponseWriter, r *http.Request, u *User) RouteError { func SecurityError(w http.ResponseWriter, r *http.Request, user User) RouteError {
pi := ErrorPage{errorHeader(w, u, p.GetErrorPhrase("security_error_title")), p.GetErrorPhrase("security_error_body")}
w.Header().Set("Content-Type", "text/html;charset=utf-8")
w.WriteHeader(403) w.WriteHeader(403)
e := RenderTemplateAlias("error", "security_error", w, r, pi.Header, pi) pi := ErrorPage{errorHeader(w, user, "Security Error"), "There was a security issue with your request."}
if e != nil { if RunPreRenderHook("pre_render_security_error", w, r, &user, &pi) {
LogError(e) return nil
} }
return HandledRouteError() err := Templates.ExecuteTemplate(w, "error.html", pi)
if err != nil {
LogError(err)
} }
var microNotFoundBytes = []byte("file not found")
func MicroNotFound(w http.ResponseWriter, r *http.Request) RouteError {
w.Header().Set("Content-Type", "text/html;charset=utf-8")
w.WriteHeader(404)
_, _ = w.Write(microNotFoundBytes)
return HandledRouteError() return HandledRouteError()
} }
// NotFound is used when the requested page doesn't exist // NotFound is used when the requested page doesn't exist
// ? - Add a JSQ version of this? // ? - Add a JSQ and JS version of this?
// ? - Add a user parameter? // ? - Add a user parameter?
func NotFound(w http.ResponseWriter, r *http.Request, h *Header) RouteError { func NotFound(w http.ResponseWriter, r *http.Request, header *Header) RouteError {
return CustomError(p.GetErrorPhrase("not_found_body"), 404, p.GetErrorPhrase("not_found_title"), w, r, h, &GuestUser) return CustomError("The requested page doesn't exist.", 404, "Not Found", w, r, header, GuestUser)
}
// ? - Add a user parameter?
func NotFoundJS(w http.ResponseWriter, r *http.Request) RouteError {
w.WriteHeader(404)
writeJsonError(p.GetErrorPhrase("not_found_body"), w)
return HandledRouteError()
}
func NotFoundJSQ(w http.ResponseWriter, r *http.Request, h *Header, js bool) RouteError {
if js {
return NotFoundJS(w, r)
}
if h == nil {
h = DefaultHeader(w, &GuestUser)
}
return NotFound(w, r, h)
} }
// CustomError lets us make custom error types which aren't covered by the generic functions above // CustomError lets us make custom error types which aren't covered by the generic functions above
func CustomError(errmsg string, errcode int, errtitle string, w http.ResponseWriter, r *http.Request, h *Header, u *User) (rerr RouteError) { func CustomError(errmsg string, errcode int, errtitle string, w http.ResponseWriter, r *http.Request, header *Header, user User) RouteError {
if h == nil { if header == nil {
h, rerr = UserCheck(w, r, u) header = DefaultHeader(w, user)
if rerr != nil {
h = errorHeader(w, u, errtitle)
} }
} w.WriteHeader(errcode)
h.Title = errtitle pi := ErrorPage{header, errmsg}
h.Zone = "error" handleErrorTemplate(w, r, pi)
pi := ErrorPage{h, errmsg}
handleErrorTemplate(w, r, pi, errcode)
return HandledRouteError() return HandledRouteError()
} }
// CustomErrorJSQ is a version of CustomError which lets us handle both JSON and regular pages depending on how it's being accessed // CustomErrorJSQ is a version of CustomError which lets us handle both JSON and regular pages depending on how it's being accessed
func CustomErrorJSQ(errmsg string, errcode int, errtitle string, w http.ResponseWriter, r *http.Request, h *Header, u *User, js bool) RouteError { func CustomErrorJSQ(errmsg string, errcode int, errtitle string, w http.ResponseWriter, r *http.Request, header *Header, user User, isJs bool) RouteError {
if !js { if !isJs {
return CustomError(errmsg, errcode, errtitle, w, r, h, u) if header == nil {
header = DefaultHeader(w, user)
} }
return CustomErrorJS(errmsg, errcode, w, r, u) return CustomError(errmsg, errcode, errtitle, w, r, header, user)
}
return CustomErrorJS(errmsg, errcode, w, r, user)
} }
// CustomErrorJS is the pure JSON version of CustomError // CustomErrorJS is the pure JSON version of CustomError
func CustomErrorJS(errmsg string, errcode int, w http.ResponseWriter, r *http.Request, u *User) RouteError { func CustomErrorJS(errmsg string, errcode int, w http.ResponseWriter, r *http.Request, user User) RouteError {
w.WriteHeader(errcode) w.WriteHeader(errcode)
writeJsonError(errmsg, w) writeJsonError(errmsg, w)
return HandledRouteError() return HandledRouteError()
@ -400,14 +318,13 @@ func writeJsonError(errmsg string, w http.ResponseWriter) {
_, _ = w.Write([]byte(`{"errmsg":"` + strings.Replace(errmsg, "\"", "", -1) + `"}`)) _, _ = w.Write([]byte(`{"errmsg":"` + strings.Replace(errmsg, "\"", "", -1) + `"}`))
} }
func handleErrorTemplate(w http.ResponseWriter, r *http.Request, pi ErrorPage, errcode int) { func handleErrorTemplate(w http.ResponseWriter, r *http.Request, pi ErrorPage) {
w.Header().Set("Content-Type", "text/html;charset=utf-8") // TODO: What to do about this hook?
w.WriteHeader(errcode) if RunPreRenderHook("pre_render_error", w, r, &pi.Header.CurrentUser, &pi) {
err := RenderTemplateAlias("error", "error", w, r, pi.Header, pi) return
}
err := RunThemeTemplate(pi.Header.Theme.Name, "error", pi, w)
if err != nil { if err != nil {
LogError(err) LogError(err)
} }
} }
// Alias of routes.renderTemplate
var RenderTemplateAlias func(tmplName, hookName string, w http.ResponseWriter, r *http.Request, h *Header, pi interface{}) error

View File

@ -1,7 +1,7 @@
/* /*
* *
* Gosora Plugin System * Gosora Plugin System
* Copyright Azareal 2016 - 2021 * Copyright Azareal 2016 - 2019
* *
*/ */
package common package common
@ -12,10 +12,9 @@ import (
"errors" "errors"
"log" "log"
"net/http" "net/http"
"sync"
"sync/atomic" "sync/atomic"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var ErrPluginNotInstallable = errors.New("This plugin is not installable") var ErrPluginNotInstallable = errors.New("This plugin is not installable")
@ -25,20 +24,20 @@ type PluginList map[string]*Plugin
// TODO: Have a proper store rather than a map? // TODO: Have a proper store rather than a map?
var Plugins PluginList = make(map[string]*Plugin) var Plugins PluginList = make(map[string]*Plugin)
func (l PluginList) Add(pl *Plugin) { func (list PluginList) Add(plugin *Plugin) {
buildPlugin(pl) buildPlugin(plugin)
l[pl.UName] = pl list[plugin.UName] = plugin
} }
func buildPlugin(pl *Plugin) { func buildPlugin(plugin *Plugin) {
pl.Installable = (pl.Install != nil) plugin.Installable = (plugin.Install != nil)
/* /*
The Active field should never be altered by a plugin. It's used internally by the software to determine whether an admin has enabled a plugin or not and whether to run it. This will be overwritten by the user's preference. The Active field should never be altered by a plugin. It's used internally by the software to determine whether an admin has enabled a plugin or not and whether to run it. This will be overwritten by the user's preference.
*/ */
pl.Active = false plugin.Active = false
pl.Installed = false plugin.Installed = false
pl.Hooks = make(map[string]int) plugin.Hooks = make(map[string]int)
pl.Data = nil plugin.Data = nil
} }
var hookTableBox atomic.Value var hookTableBox atomic.Value
@ -49,189 +48,40 @@ var hookTableBox atomic.Value
// TODO: Make the RunXHook functions methods on HookTable // TODO: Make the RunXHook functions methods on HookTable
// TODO: Have plugins update hooks on a mutex guarded map and create a copy of that map in a serial global goroutine which gets thrown in the atomic.Value // TODO: Have plugins update hooks on a mutex guarded map and create a copy of that map in a serial global goroutine which gets thrown in the atomic.Value
type HookTable struct { type HookTable struct {
//Hooks map[string][]func(interface{}) interface{} Hooks map[string][]func(interface{}) interface{}
HooksNoRet map[string][]func(interface{})
HooksSkip map[string][]func(interface{}) bool
Vhooks map[string]func(...interface{}) interface{} Vhooks map[string]func(...interface{}) interface{}
VhookSkippable_ map[string]func(...interface{}) (bool, RouteError) VhookSkippable map[string]func(...interface{}) (bool, RouteError)
Sshooks map[string][]func(string) string Sshooks map[string][]func(string) string
PreRenderHooks map[string][]func(http.ResponseWriter, *http.Request, *User, interface{}) bool PreRenderHooks map[string][]func(http.ResponseWriter, *http.Request, *User, interface{}) bool
// For future use: // For future use:
//messageHooks map[string][]func(Message, PageInt, ...interface{}) interface{} messageHooks map[string][]func(Message, PageInt, ...interface{}) interface{}
} }
func init() { func init() {
RebuildHookTable() hookTableBox.Store(new(HookTable))
} }
// For extend.go use only, access this via GetHookTable() elsewhere // Hooks with a single argument. Is this redundant? Might be useful for inlining, as variadics aren't inlined? Are closures even inlined to begin with?
var hookTable = &HookTable{ var Hooks = map[string][]func(interface{}) interface{}{
//map[string][]func(interface{}) interface{}{}, "forums_frow_assign": nil,
map[string][]func(interface{}){ "topic_create_frow_assign": nil,
"forums_frow_assign": nil, //hg }
},
map[string][]func(interface{}) bool{
"topic_create_frow_assign": nil, //hg
},
map[string]func(...interface{}) interface{}{
//"convo_post_update":nil,
//"convo_post_create":nil,
///"forum_trow_assign": nil, // Hooks with a variable number of arguments
var Vhooks = map[string]func(...interface{}) interface{}{
"forum_trow_assign": nil,
"topics_topic_row_assign": nil, "topics_topic_row_assign": nil,
//"topics_user_row_assign": nil, //"topics_user_row_assign": nil,
"topic_reply_row_assign": nil, "topic_reply_row_assign": nil,
"create_group_preappend": nil, // What is this? Investigate! "create_group_preappend": nil, // What is this? Investigate!
"topic_create_pre_loop": nil, "topic_create_pre_loop": nil,
"router_end": nil,
},
map[string]func(...interface{}) (bool, RouteError){
"simple_forum_check_pre_perms": nil, //hg
"forum_check_pre_perms": nil, //hg
"route_topic_list_start": nil,
"route_topic_list_mostviewed_start": nil,
"route_forum_list_start": nil,
"route_attach_start": nil,
"route_attach_post_get": nil,
"action_end_create_topic": nil,
"action_end_edit_topic": nil,
"action_end_delete_topic": nil,
"action_end_lock_topic": nil,
"action_end_unlock_topic": nil,
"action_end_stick_topic": nil,
"action_end_unstick_topic": nil,
"action_end_move_topic": nil,
"action_end_like_topic": nil,
"action_end_unlike_topic": nil,
"action_end_create_reply": nil,
"action_end_edit_reply": nil,
"action_end_delete_reply": nil,
"action_end_add_attach_to_reply": nil,
"action_end_remove_attach_from_reply": nil,
"action_end_like_reply": nil,
"action_end_unlike_reply": nil,
"action_end_ban_user": nil,
"action_end_unban_user": nil,
"action_end_activate_user": nil,
"router_after_filters": nil,
"router_pre_route": nil,
"tasks_tick_topic_list": nil,
"tasks_tick_widget_wol": nil,
"counters_perf_tick_row": nil,
},
map[string][]func(string) string{
"preparse_preassign": nil,
"parse_assign": nil,
"topic_ogdesc_assign": nil,
},
nil,
//nil,
}
var hookTableUpdateMutex sync.Mutex
func RebuildHookTable() {
hookTableUpdateMutex.Lock()
defer hookTableUpdateMutex.Unlock()
unsafeRebuildHookTable()
}
func unsafeRebuildHookTable() {
ihookTable := new(HookTable)
*ihookTable = *hookTable
hookTableBox.Store(ihookTable)
}
func GetHookTable() *HookTable {
return hookTableBox.Load().(*HookTable)
}
// Hooks with a single argument. Is this redundant? Might be useful for inlining, as variadics aren't inlined? Are closures even inlined to begin with?
/*func (t *HookTable) Hook(name string, data interface{}) interface{} {
for _, hook := range t.Hooks[name] {
data = hook(data)
}
return data
}*/
func (t *HookTable) HookNoRet(name string, data interface{}) {
for _, hook := range t.HooksNoRet[name] {
hook(data)
}
}
// To cover the case in routes/topic.go's CreateTopic route, we could probably obsolete this use and replace it
func (t *HookTable) HookSkip(name string, data interface{}) (skip bool) {
for _, hook := range t.HooksSkip[name] {
if skip = hook(data); skip {
break
}
}
return skip
}
// Hooks with a variable number of arguments
// TODO: Use RunHook semantics to allow multiple lined up plugins / modules their turn?
func (t *HookTable) Vhook(name string, data ...interface{}) interface{} {
if hook := t.Vhooks[name]; hook != nil {
return hook(data...)
}
return nil
}
func (t *HookTable) VhookNoRet(name string, data ...interface{}) {
if hook := t.Vhooks[name]; hook != nil {
_ = hook(data...)
}
}
// TODO: Find a better way of doing this
func (t *HookTable) VhookNeedHook(name string, data ...interface{}) (ret interface{}, hasHook bool) {
if hook := t.Vhooks[name]; hook != nil {
return hook(data...), true
}
return nil, false
} }
// Hooks with a variable number of arguments and return values for skipping the parent function and propagating an error upwards // Hooks with a variable number of arguments and return values for skipping the parent function and propagating an error upwards
func (t *HookTable) VhookSkippable(name string, data ...interface{}) (bool, RouteError) { var VhookSkippable = map[string]func(...interface{}) (bool, RouteError){
if hook := t.VhookSkippable_[name]; hook != nil { "simple_forum_check_pre_perms": nil,
return hook(data...) "forum_check_pre_perms": nil,
}
return false, nil
}
/*func VhookSkippableTest(t *HookTable, name string, data ...interface{}) (bool, RouteError) {
if hook := t.VhookSkippable_[name]; hook != nil {
return hook(data...)
}
return false, nil
}
func forum_check_pre_perms_hook(t *HookTable, w http.ResponseWriter, r *http.Request, u *User, fid *int, h *Header) (bool, RouteError) {
hook := t.VhookSkippable_["forum_check_pre_perms"]
if hook != nil {
return hook(w, r, u, fid, h)
}
return false, nil
}*/
// Hooks which take in and spit out a string. This is usually used for parser components
// Trying to get a teeny bit of type-safety where-ever possible, especially for such a critical set of hooks
func (t *HookTable) Sshook(name, data string) string {
for _, hook := range t.Sshooks[name] {
data = hook(data)
}
return data
} }
//var vhookErrorable = map[string]func(...interface{}) (interface{}, RouteError){} //var vhookErrorable = map[string]func(...interface{}) (interface{}, RouteError){}
@ -243,8 +93,6 @@ var taskHooks = map[string][]func() error{
"after_second_tick": nil, "after_second_tick": nil,
"before_fifteen_minute_tick": nil, "before_fifteen_minute_tick": nil,
"after_fifteen_minute_tick": nil, "after_fifteen_minute_tick": nil,
"before_shutdown_tick": nil,
"after_shutdown_tick": nil,
} }
// Coming Soon: // Coming Soon:
@ -269,14 +117,20 @@ var messageHooks = map[string][]func(Message, PageInt, ...interface{}) interface
"topic_reply_row_assign": nil, "topic_reply_row_assign": nil,
} }
// Hooks which take in and spit out a string. This is usually used for parser components
var Sshooks = map[string][]func(string) string{
"preparse_preassign": nil,
"parse_assign": nil,
}
// The hooks which run before the template is rendered for a route // The hooks which run before the template is rendered for a route
var PreRenderHooks = map[string][]func(http.ResponseWriter, *http.Request, *User, interface{}) bool{ var PreRenderHooks = map[string][]func(http.ResponseWriter, *http.Request, *User, interface{}) bool{
"pre_render": nil, "pre_render": nil,
"pre_render_forums": nil, "pre_render_forum_list": nil,
"pre_render_forum": nil, "pre_render_forum": nil,
"pre_render_topics": nil, "pre_render_topic_list": nil,
"pre_render_topic": nil, "pre_render_view_topic": nil,
"pre_render_profile": nil, "pre_render_profile": nil,
"pre_render_custom_page": nil, "pre_render_custom_page": nil,
"pre_render_tmpl_page": nil, "pre_render_tmpl_page": nil,
@ -288,7 +142,6 @@ var PreRenderHooks = map[string][]func(http.ResponseWriter, *http.Request, *User
"pre_render_account_own_edit_mfa": nil, "pre_render_account_own_edit_mfa": nil,
"pre_render_account_own_edit_mfa_setup": nil, "pre_render_account_own_edit_mfa_setup": nil,
"pre_render_account_own_edit_email": nil, "pre_render_account_own_edit_email": nil,
"pre_render_level_list": nil,
"pre_render_login": nil, "pre_render_login": nil,
"pre_render_login_mfa_verify": nil, "pre_render_login_mfa_verify": nil,
"pre_render_register": nil, "pre_render_register": nil,
@ -298,8 +151,7 @@ var PreRenderHooks = map[string][]func(http.ResponseWriter, *http.Request, *User
"pre_render_panel_dashboard": nil, "pre_render_panel_dashboard": nil,
"pre_render_panel_forums": nil, "pre_render_panel_forums": nil,
"pre_render_panel_delete_forum": nil, "pre_render_panel_delete_forum": nil,
"pre_render_panel_forum_edit": nil, "pre_render_panel_edit_forum": nil,
"pre_render_panel_forum_edit_perms": nil,
"pre_render_panel_analytics_views": nil, "pre_render_panel_analytics_views": nil,
"pre_render_panel_analytics_routes": nil, "pre_render_panel_analytics_routes": nil,
@ -317,10 +169,10 @@ var PreRenderHooks = map[string][]func(http.ResponseWriter, *http.Request, *User
"pre_render_panel_word_filters_edit": nil, "pre_render_panel_word_filters_edit": nil,
"pre_render_panel_plugins": nil, "pre_render_panel_plugins": nil,
"pre_render_panel_users": nil, "pre_render_panel_users": nil,
"pre_render_panel_user_edit": nil, "pre_render_panel_edit_user": nil,
"pre_render_panel_groups": nil, "pre_render_panel_groups": nil,
"pre_render_panel_group_edit": nil, "pre_render_panel_edit_group": nil,
"pre_render_panel_group_edit_perms": nil, "pre_render_panel_edit_group_perms": nil,
"pre_render_panel_themes": nil, "pre_render_panel_themes": nil,
"pre_render_panel_modlogs": nil, "pre_render_panel_modlogs": nil,
@ -343,33 +195,27 @@ type Plugin struct {
Installable bool Installable bool
Installed bool Installed bool
Init func(pl *Plugin) error Init func() error
Activate func(pl *Plugin) error Activate func() error
Deactivate func(pl *Plugin) // TODO: We might want to let this return an error? Deactivate func() // TODO: We might want to let this return an error?
Install func(pl *Plugin) error Install func() error
Uninstall func(pl *Plugin) error // TODO: I'm not sure uninstall is implemented Uninstall func() error // TODO: I'm not sure uninstall is implemented
Hooks map[string]int // Active hooks Hooks map[string]int
Meta PluginMetaData
Data interface{} // Usually used for hosting the VMs / reusable elements of non-native plugins Data interface{} // Usually used for hosting the VMs / reusable elements of non-native plugins
} }
type PluginMetaData struct { func (plugin *Plugin) BypassActive() (active bool, err error) {
Hooks []string err = extendStmts.isActive.QueryRow(plugin.UName).Scan(&active)
//StaticHooks map[string]string
}
func (pl *Plugin) BypassActive() (active bool, err error) {
err = extendStmts.isActive.QueryRow(pl.UName).Scan(&active)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
return false, err return false, err
} }
return active, nil return active, nil
} }
func (pl *Plugin) InDatabase() (exists bool, err error) { func (plugin *Plugin) InDatabase() (exists bool, err error) {
var sink bool var sink bool
err = extendStmts.isActive.QueryRow(pl.UName).Scan(&sink) err = extendStmts.isActive.QueryRow(plugin.UName).Scan(&sink)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
return false, err return false, err
} }
@ -377,31 +223,31 @@ func (pl *Plugin) InDatabase() (exists bool, err error) {
} }
// TODO: Silently add to the database, if it doesn't exist there rather than forcing users to call AddToDatabase instead? // TODO: Silently add to the database, if it doesn't exist there rather than forcing users to call AddToDatabase instead?
func (pl *Plugin) SetActive(active bool) (err error) { func (plugin *Plugin) SetActive(active bool) (err error) {
_, err = extendStmts.setActive.Exec(active, pl.UName) _, err = extendStmts.setActive.Exec(active, plugin.UName)
if err == nil { if err == nil {
pl.Active = active plugin.Active = active
} }
return err return err
} }
// TODO: Silently add to the database, if it doesn't exist there rather than forcing users to call AddToDatabase instead? // TODO: Silently add to the database, if it doesn't exist there rather than forcing users to call AddToDatabase instead?
func (pl *Plugin) SetInstalled(installed bool) (err error) { func (plugin *Plugin) SetInstalled(installed bool) (err error) {
if !pl.Installable { if !plugin.Installable {
return ErrPluginNotInstallable return ErrPluginNotInstallable
} }
_, err = extendStmts.setInstalled.Exec(installed, pl.UName) _, err = extendStmts.setInstalled.Exec(installed, plugin.UName)
if err == nil { if err == nil {
pl.Installed = installed plugin.Installed = installed
} }
return err return err
} }
func (pl *Plugin) AddToDatabase(active, installed bool) (err error) { func (plugin *Plugin) AddToDatabase(active bool, installed bool) (err error) {
_, err = extendStmts.add.Exec(pl.UName, active, installed) _, err = extendStmts.add.Exec(plugin.UName, active, installed)
if err == nil { if err == nil {
pl.Active = active plugin.Active = active
pl.Installed = installed plugin.Installed = installed
} }
return err return err
} }
@ -419,21 +265,20 @@ var extendStmts ExtendStmts
func init() { func init() {
DbInits.Add(func(acc *qgen.Accumulator) error { DbInits.Add(func(acc *qgen.Accumulator) error {
pl := "plugins"
extendStmts = ExtendStmts{ extendStmts = ExtendStmts{
getPlugins: acc.Select(pl).Columns("uname,active,installed").Prepare(), getPlugins: acc.Select("plugins").Columns("uname, active, installed").Prepare(),
isActive: acc.Select(pl).Columns("active").Where("uname=?").Prepare(), isActive: acc.Select("plugins").Columns("active").Where("uname = ?").Prepare(),
setActive: acc.Update(pl).Set("active=?").Where("uname=?").Prepare(), setActive: acc.Update("plugins").Set("active = ?").Where("uname = ?").Prepare(),
setInstalled: acc.Update(pl).Set("installed=?").Where("uname=?").Prepare(), setInstalled: acc.Update("plugins").Set("installed = ?").Where("uname = ?").Prepare(),
add: acc.Insert(pl).Columns("uname,active,installed").Fields("?,?,?").Prepare(), add: acc.Insert("plugins").Columns("uname, active, installed").Fields("?,?,?").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })
} }
func InitExtend() error { func InitExtend() (err error) {
err := InitPluginLangs() err = InitPluginLangs()
if err != nil { if err != nil {
return err return err
} }
@ -441,7 +286,7 @@ func InitExtend() error {
} }
// Load polls the database to see which plugins have been activated and which have been installed // Load polls the database to see which plugins have been activated and which have been installed
func (l PluginList) Load() error { func (plugins PluginList) Load() error {
rows, err := extendStmts.getPlugins.Query() rows, err := extendStmts.getPlugins.Query()
if err != nil { if err != nil {
return err return err
@ -457,118 +302,101 @@ func (l PluginList) Load() error {
} }
// Was the plugin deleted at some point? // Was the plugin deleted at some point?
pl, ok := l[uname] plugin, ok := plugins[uname]
if !ok { if !ok {
continue continue
} }
pl.Active = active plugin.Active = active
pl.Installed = installed plugin.Installed = installed
l[uname] = pl plugins[uname] = plugin
} }
return rows.Err() return rows.Err()
} }
// ? - Is this racey? // ? - Is this racey?
// TODO: Generate the cases in this switch // TODO: Generate the cases in this switch
func (pl *Plugin) AddHook(name string, hInt interface{}) { func (plugin *Plugin) AddHook(name string, handler interface{}) {
hookTableUpdateMutex.Lock() switch h := handler.(type) {
defer hookTableUpdateMutex.Unlock() case func(interface{}) interface{}:
if len(Hooks[name]) == 0 {
switch h := hInt.(type) { var hookSlice []func(interface{}) interface{}
/*case func(interface{}) interface{}: hookSlice = append(hookSlice, h)
if len(hookTable.Hooks[name]) == 0 { Hooks[name] = hookSlice
hookTable.Hooks[name] = []func(interface{}) interface{}{} } else {
Hooks[name] = append(Hooks[name], h)
} }
hookTable.Hooks[name] = append(hookTable.Hooks[name], h) plugin.Hooks[name] = len(Hooks[name]) - 1
pl.Hooks[name] = len(hookTable.Hooks[name]) - 1*/
case func(interface{}):
if len(hookTable.HooksNoRet[name]) == 0 {
hookTable.HooksNoRet[name] = []func(interface{}){}
}
hookTable.HooksNoRet[name] = append(hookTable.HooksNoRet[name], h)
pl.Hooks[name] = len(hookTable.HooksNoRet[name]) - 1
case func(interface{}) bool:
if len(hookTable.HooksSkip[name]) == 0 {
hookTable.HooksSkip[name] = []func(interface{}) bool{}
}
hookTable.HooksSkip[name] = append(hookTable.HooksSkip[name], h)
pl.Hooks[name] = len(hookTable.HooksSkip[name]) - 1
case func(string) string: case func(string) string:
if len(hookTable.Sshooks[name]) == 0 { if len(Sshooks[name]) == 0 {
hookTable.Sshooks[name] = []func(string) string{} var hookSlice []func(string) string
hookSlice = append(hookSlice, h)
Sshooks[name] = hookSlice
} else {
Sshooks[name] = append(Sshooks[name], h)
} }
hookTable.Sshooks[name] = append(hookTable.Sshooks[name], h) plugin.Hooks[name] = len(Sshooks[name]) - 1
pl.Hooks[name] = len(hookTable.Sshooks[name]) - 1
case func(http.ResponseWriter, *http.Request, *User, interface{}) bool: case func(http.ResponseWriter, *http.Request, *User, interface{}) bool:
if len(PreRenderHooks[name]) == 0 { if len(PreRenderHooks[name]) == 0 {
PreRenderHooks[name] = []func(http.ResponseWriter, *http.Request, *User, interface{}) bool{} var hookSlice []func(http.ResponseWriter, *http.Request, *User, interface{}) bool
} hookSlice = append(hookSlice, h)
PreRenderHooks[name] = hookSlice
} else {
PreRenderHooks[name] = append(PreRenderHooks[name], h) PreRenderHooks[name] = append(PreRenderHooks[name], h)
pl.Hooks[name] = len(PreRenderHooks[name]) - 1 }
plugin.Hooks[name] = len(PreRenderHooks[name]) - 1
case func() error: // ! We might want a more generic name, as we might use this signature for things other than tasks hooks case func() error: // ! We might want a more generic name, as we might use this signature for things other than tasks hooks
if len(taskHooks[name]) == 0 { if len(taskHooks[name]) == 0 {
taskHooks[name] = []func() error{} var hookSlice []func() error
} hookSlice = append(hookSlice, h)
taskHooks[name] = hookSlice
} else {
taskHooks[name] = append(taskHooks[name], h) taskHooks[name] = append(taskHooks[name], h)
pl.Hooks[name] = len(taskHooks[name]) - 1 }
plugin.Hooks[name] = len(taskHooks[name]) - 1
case func(...interface{}) interface{}: case func(...interface{}) interface{}:
hookTable.Vhooks[name] = h Vhooks[name] = h
pl.Hooks[name] = 0 plugin.Hooks[name] = 0
case func(...interface{}) (bool, RouteError): case func(...interface{}) (bool, RouteError):
hookTable.VhookSkippable_[name] = h VhookSkippable[name] = h
pl.Hooks[name] = 0 plugin.Hooks[name] = 0
default: default:
panic("I don't recognise this kind of handler!") // Should this be an error for the plugin instead of a panic()? panic("I don't recognise this kind of handler!") // Should this be an error for the plugin instead of a panic()?
} }
// TODO: Do this once during plugin activation / deactivation rather than doing it for each hook
unsafeRebuildHookTable()
} }
// ? - Is this racey? // ? - Is this racey?
// TODO: Generate the cases in this switch // TODO: Generate the cases in this switch
func (pl *Plugin) RemoveHook(name string, hInt interface{}) { func (plugin *Plugin) RemoveHook(name string, handler interface{}) {
hookTableUpdateMutex.Lock() switch handler.(type) {
defer hookTableUpdateMutex.Unlock() case func(interface{}) interface{}:
key, ok := plugin.Hooks[name]
key, ok := pl.Hooks[name]
if !ok { if !ok {
panic("handler not registered as hook") panic("handler not registered as hook")
} }
hook := Hooks[name]
switch hInt.(type) {
/*case func(interface{}) interface{}:
hook := hookTable.Hooks[name]
if len(hook) == 1 { if len(hook) == 1 {
hook = []func(interface{}) interface{}{} hook = []func(interface{}) interface{}{}
} else { } else {
hook = append(hook[:key], hook[key+1:]...) hook = append(hook[:key], hook[key+1:]...)
} }
hookTable.Hooks[name] = hook*/ Hooks[name] = hook
case func(interface{}):
hook := hookTable.HooksNoRet[name]
if len(hook) == 1 {
hook = []func(interface{}){}
} else {
hook = append(hook[:key], hook[key+1:]...)
}
hookTable.HooksNoRet[name] = hook
case func(interface{}) bool:
hook := hookTable.HooksSkip[name]
if len(hook) == 1 {
hook = []func(interface{}) bool{}
} else {
hook = append(hook[:key], hook[key+1:]...)
}
hookTable.HooksSkip[name] = hook
case func(string) string: case func(string) string:
hook := hookTable.Sshooks[name] key, ok := plugin.Hooks[name]
if !ok {
panic("handler not registered as hook")
}
hook := Sshooks[name]
if len(hook) == 1 { if len(hook) == 1 {
hook = []func(string) string{} hook = []func(string) string{}
} else { } else {
hook = append(hook[:key], hook[key+1:]...) hook = append(hook[:key], hook[key+1:]...)
} }
hookTable.Sshooks[name] = hook Sshooks[name] = hook
case func(http.ResponseWriter, *http.Request, *User, interface{}) bool: case func(http.ResponseWriter, *http.Request, *User, interface{}) bool:
key, ok := plugin.Hooks[name]
if !ok {
panic("handler not registered as hook")
}
hook := PreRenderHooks[name] hook := PreRenderHooks[name]
if len(hook) == 1 { if len(hook) == 1 {
hook = []func(http.ResponseWriter, *http.Request, *User, interface{}) bool{} hook = []func(http.ResponseWriter, *http.Request, *User, interface{}) bool{}
@ -577,6 +405,10 @@ func (pl *Plugin) RemoveHook(name string, hInt interface{}) {
} }
PreRenderHooks[name] = hook PreRenderHooks[name] = hook
case func() error: case func() error:
key, ok := plugin.Hooks[name]
if !ok {
panic("handler not registered as hook")
}
hook := taskHooks[name] hook := taskHooks[name]
if len(hook) == 1 { if len(hook) == 1 {
hook = []func() error{} hook = []func() error{}
@ -585,15 +417,13 @@ func (pl *Plugin) RemoveHook(name string, hInt interface{}) {
} }
taskHooks[name] = hook taskHooks[name] = hook
case func(...interface{}) interface{}: case func(...interface{}) interface{}:
delete(hookTable.Vhooks, name) delete(Vhooks, name)
case func(...interface{}) (bool, RouteError): case func(...interface{}) (bool, RouteError):
delete(hookTable.VhookSkippable_, name) delete(VhookSkippable, name)
default: default:
panic("I don't recognise this kind of handler!") // Should this be an error for the plugin instead of a panic()? panic("I don't recognise this kind of handler!") // Should this be an error for the plugin instead of a panic()?
} }
delete(pl.Hooks, name) delete(plugin.Hooks, name)
// TODO: Do this once during plugin activation / deactivation rather than doing it for each hook
unsafeRebuildHookTable()
} }
// TODO: Add a HasHook method to complete the AddHook, RemoveHook, etc. set? // TODO: Add a HasHook method to complete the AddHook, RemoveHook, etc. set?
@ -605,8 +435,9 @@ func InitPlugins() {
log.Printf("Added plugin '%s'", name) log.Printf("Added plugin '%s'", name)
if body.Active { if body.Active {
log.Printf("Initialised plugin '%s'", name) log.Printf("Initialised plugin '%s'", name)
if body.Init != nil { if Plugins[name].Init != nil {
if err := body.Init(body); err != nil { err := Plugins[name].Init()
if err != nil {
log.Print(err) log.Print(err)
} }
} else { } else {
@ -618,21 +449,82 @@ func InitPlugins() {
} }
// ? - Are the following functions racey? // ? - Are the following functions racey?
func RunHook(name string, data interface{}) interface{} {
hooks, ok := Hooks[name]
if ok {
for _, hook := range hooks {
data = hook(data)
}
}
return data
}
func RunHookNoreturn(name string, data interface{}) {
hooks, ok := Hooks[name]
if !ok {
return
}
for _, hook := range hooks {
_ = hook(data)
}
}
// TODO: Use RunHook semantics to allow multiple lined up plugins / modules their turn?
func RunVhook(name string, data ...interface{}) interface{} {
hook := Vhooks[name]
if hook != nil {
return hook(data...)
}
return nil
}
func RunVhookSkippable(name string, data ...interface{}) (bool, RouteError) {
return VhookSkippable[name](data...)
}
func RunVhookNoreturn(name string, data ...interface{}) {
hook := Vhooks[name]
if hook != nil {
_ = hook(data...)
}
}
// TODO: Find a better way of doing this
func RunVhookNeedHook(name string, data ...interface{}) (ret interface{}, hasHook bool) {
hook := Vhooks[name]
if hook != nil {
return hook(data...), true
}
return nil, false
}
func RunTaskHook(name string) error { func RunTaskHook(name string) error {
for _, hook := range taskHooks[name] { for _, hook := range taskHooks[name] {
if e := hook(); e != nil { err := hook()
return e if err != nil {
return err
} }
} }
return nil return nil
} }
func RunPreRenderHook(name string, w http.ResponseWriter, r *http.Request, u *User, data interface{}) (halt bool) { // Trying to get a teeny bit of type-safety where-ever possible, especially for such a critical set of hooks
func RunSshook(name string, data string) string {
ssHooks, ok := Sshooks[name]
if ok {
for _, hook := range ssHooks {
data = hook(data)
}
}
return data
}
func RunPreRenderHook(name string, w http.ResponseWriter, r *http.Request, user *User, data interface{}) (halt bool) {
// This hook runs on ALL PreRender hooks // This hook runs on ALL PreRender hooks
preRenderHooks, ok := PreRenderHooks["pre_render"] preRenderHooks, ok := PreRenderHooks["pre_render"]
if ok { if ok {
for _, hook := range preRenderHooks { for _, hook := range preRenderHooks {
if hook(w, r, u, data) { if hook(w, r, user, data) {
return true return true
} }
} }
@ -642,7 +534,7 @@ func RunPreRenderHook(name string, w http.ResponseWriter, r *http.Request, u *Us
preRenderHooks, ok = PreRenderHooks[name] preRenderHooks, ok = PreRenderHooks[name]
if ok { if ok {
for _, hook := range preRenderHooks { for _, hook := range preRenderHooks {
if hook(w, r, u, data) { if hook(w, r, user, data) {
return true return true
} }
} }

View File

@ -3,58 +3,31 @@ package common
import ( import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"mime" "mime"
"net/http" "net/http"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
tmpl "git.tuxpa.in/a/gosora/tmpl_client" "../tmpl_client"
"github.com/andybalholm/brotli"
) )
//type SFileList map[string]*SFile type SFileList map[string]SFile
//type SFileListShort map[string]*SFile
var StaticFiles = SFileList{"/s/", make(map[string]*SFile), make(map[string]*SFile)} var StaticFiles SFileList = make(map[string]SFile)
//var StaticFilesShort SFileList = make(map[string]*SFile)
var staticFileMutex sync.RWMutex var staticFileMutex sync.RWMutex
// ? Is it efficient to have two maps for this?
type SFileList struct {
Prefix string
Long map[string]*SFile
Short map[string]*SFile
}
type SFile struct { type SFile struct {
// TODO: Move these to the end?
Data []byte Data []byte
GzipData []byte GzipData []byte
BrData []byte
Sha256 string
Sha256I string
OName string
Pos int64 Pos int64
Length int64 Length int64
StrLength string
GzipLength int64 GzipLength int64
StrGzipLength string
BrLength int64
StrBrLength string
Mimetype string Mimetype string
Info os.FileInfo Info os.FileInfo
FormattedModTime string FormattedModTime string
@ -64,12 +37,22 @@ type CSSData struct {
Phrases map[string]string Phrases map[string]string
} }
func (l SFileList) JSTmplInit() error { func (list SFileList) JSTmplInit() error {
DebugLog("Initialising the client side templates") DebugLog("Initialising the client side templates")
var fragMap = make(map[string][][]byte)
fragMap["alert"] = tmpl.GetFrag("alert")
fragMap["topics_topic"] = tmpl.GetFrag("topics_topic")
fragMap["topic_posts"] = tmpl.GetFrag("topic_posts")
fragMap["topic_alt_posts"] = tmpl.GetFrag("topic_alt_posts")
DebugLog("fragMap: ", fragMap)
return filepath.Walk("./tmpl_client", func(path string, f os.FileInfo, err error) error { return filepath.Walk("./tmpl_client", func(path string, f os.FileInfo, err error) error {
if f.IsDir() || strings.HasSuffix(path, "tmpl_list.go") || strings.HasSuffix(path, "stub.go") { if f.IsDir() {
return nil return nil
} }
if strings.HasSuffix(path, "template_list.go") || strings.HasSuffix(path, "stub.go") {
return nil
}
path = strings.Replace(path, "\\", "/", -1) path = strings.Replace(path, "\\", "/", -1)
DebugLog("Processing client template " + path) DebugLog("Processing client template " + path)
data, err := ioutil.ReadFile(path) data, err := ioutil.ReadFile(path)
@ -78,26 +61,22 @@ func (l SFileList) JSTmplInit() error {
} }
path = strings.TrimPrefix(path, "tmpl_client/") path = strings.TrimPrefix(path, "tmpl_client/")
tmplName := strings.TrimSuffix(path, ".jgo") tmplName := strings.TrimSuffix(path, ".go")
shortName := strings.TrimPrefix(tmplName, "tmpl_") shortName := strings.TrimPrefix(tmplName, "template_")
replace := func(data []byte, replaceThis, withThis string) []byte { var replace = func(data []byte, replaceThis string, withThis string) []byte {
return bytes.Replace(data, []byte(replaceThis), []byte(withThis), -1) return bytes.Replace(data, []byte(replaceThis), []byte(withThis), -1)
} }
rep := func(replaceThis, withThis string) {
data = replace(data, replaceThis, withThis)
}
startIndex, hasFunc := skipAllUntilCharsExist(data, 0, []byte("if(tmplInits===undefined)")) startIndex, hasFunc := skipAllUntilCharsExist(data, 0, []byte("func init() {"))
if !hasFunc { if !hasFunc {
return errors.New("no init map found") return errors.New("no init function found")
} }
data = data[startIndex-len([]byte("if(tmplInits===undefined)")):] data = data[startIndex-len([]byte("func init() {")):]
rep("// nolint", "") data = replace(data, "func ", "function ")
//rep("func ", "function ") data = replace(data, "function init() {", "tmplInits[\""+tmplName+"\"] = ")
rep("func ", "function ") data = replace(data, " error {\n", " {\nlet out = \"\"\n")
rep(" error {\n", " {\nlet o=\"\"\n") funcIndex, hasFunc := skipAllUntilCharsExist(data, 0, []byte("function Template_"))
funcIndex, hasFunc := skipAllUntilCharsExist(data, 0, []byte("function Tmpl_"))
if !hasFunc { if !hasFunc {
return errors.New("no template function found") return errors.New("no template function found")
} }
@ -114,13 +93,13 @@ func (l SFileList) JSTmplInit() error {
fmt.Println("string(data[spaceIndex:endBrace]): ", string(data[spaceIndex:endBrace])) fmt.Println("string(data[spaceIndex:endBrace]): ", string(data[spaceIndex:endBrace]))
preLen := len(data) preLen := len(data)
rep(string(data[spaceIndex:endBrace]), "") data = replace(data, string(data[spaceIndex:endBrace]), "")
rep("))\n", " \n") data = replace(data, "))\n", "\n")
endBrace -= preLen - len(data) // Offset it as we've deleted portions endBrace -= preLen - len(data) // Offset it as we've deleted portions
fmt.Println("new endBrace: ", endBrace) fmt.Println("new endBrace: ", endBrace)
fmt.Println("data: ", string(data)) fmt.Println("data: ", string(data))
/*showPos := func(data []byte, index int) (out string) { /*var showPos = func(data []byte, index int) (out string) {
out = "[" out = "["
for j, char := range data { for j, char := range data {
if index == j { if index == j {
@ -133,9 +112,9 @@ func (l SFileList) JSTmplInit() error {
}*/ }*/
// ? Can we just use a regex? I'm thinking of going more efficient, or just outright rolling wasm, this is a temp hack in a place where performance doesn't particularly matter // ? Can we just use a regex? I'm thinking of going more efficient, or just outright rolling wasm, this is a temp hack in a place where performance doesn't particularly matter
each := func(phrase string, h func(index int)) { var each = func(phrase string, handle func(index int)) {
//fmt.Println("find each '" + phrase + "'") //fmt.Println("find each '" + phrase + "'")
index := endBrace var index = endBrace
if index < 0 { if index < 0 {
panic("index under zero: " + strconv.Itoa(index)) panic("index under zero: " + strconv.Itoa(index))
} }
@ -147,42 +126,37 @@ func (l SFileList) JSTmplInit() error {
if !foundIt { if !foundIt {
break break
} }
h(index) handle(index)
} }
} }
each("strconv.Itoa(", func(index int) { each("strconv.Itoa(", func(index int) {
braceAt, hasEndBrace := skipUntilIfExistsOrLine(data, index, ')') braceAt, hasEndBrace := skipUntilIfExists(data, index, ')')
// TODO: Make sure we don't go onto the next line in case someone misplaced a brace
if hasEndBrace { if hasEndBrace {
data[braceAt] = ' ' // Blank it data[braceAt] = ' ' // Blank it
} }
}) })
each("[]byte(", func(index int) { each("w.Write([]byte(", func(index int) {
braceAt, hasEndBrace := skipUntilIfExistsOrLine(data, index, ')') braceAt, hasEndBrace := skipUntilIfExists(data, index, ')')
// TODO: Make sure we don't go onto the next line in case someone misplaced a brace
if hasEndBrace { if hasEndBrace {
data[braceAt] = ' ' // Blank it data[braceAt] = ' ' // Blank it
} }
}) braceAt, hasEndBrace = skipUntilIfExists(data, braceAt, ')')
each("StringToBytes(", func(index int) {
braceAt, hasEndBrace := skipUntilIfExistsOrLine(data, index, ')')
if hasEndBrace { if hasEndBrace {
data[braceAt] = ' ' // Blank it data[braceAt] = ' ' // Blank this one too
} }
}) })
each("w.Write(", func(index int) { each("w.Write(", func(index int) {
braceAt, hasEndBrace := skipUntilIfExistsOrLine(data, index, ')') braceAt, hasEndBrace := skipUntilIfExists(data, index, ')')
// TODO: Make sure we don't go onto the next line in case someone misplaced a brace
if hasEndBrace { if hasEndBrace {
data[braceAt] = ' ' // Blank it data[braceAt] = ' ' // Blank it
} }
}) })
each("RelativeTime(", func(index int) {
braceAt, _ := skipUntilIfExistsOrLine(data, index, 10)
if data[braceAt-1] == ' ' {
data[braceAt-1] = ' ' // Blank it
}
})
each("if ", func(index int) { each("if ", func(index int) {
//fmt.Println("if index: ", index) //fmt.Println("if index: ", index)
braceAt, hasBrace := skipUntilIfExistsOrLine(data, index, '{') braceAt, hasBrace := skipUntilIfExists(data, index, '{')
if hasBrace { if hasBrace {
if data[braceAt-1] != ' ' { if data[braceAt-1] != ' ' {
panic("couldn't find space before brace, found ' " + string(data[braceAt-1]) + "' instead") panic("couldn't find space before brace, found ' " + string(data[braceAt-1]) + "' instead")
@ -200,126 +174,62 @@ func (l SFileList) JSTmplInit() error {
data[braceAt-1] = ')' // Drop a brace here to satisfy JS data[braceAt-1] = ')' // Drop a brace here to satisfy JS
} }
}) })
rep("for _, item := range ", "for(item of ") data = replace(data, "for _, item := range ", "for(item of ")
rep("w.Write([]byte(", "o += ") data = replace(data, "w.Write([]byte(", "out += ")
rep("w.Write(StringToBytes(", "o += ") data = replace(data, "w.Write(", "out += ")
rep("w.Write(", "o += ") data = replace(data, "strconv.Itoa(", "")
rep("+= c.", "+= ") data = replace(data, "strconv.FormatInt(", "")
rep("strconv.Itoa(", "") data = replace(data, "common.", "")
rep("strconv.FormatInt(", "") data = replace(data, ", 10;", "")
rep(" c.", "") data = replace(data, shortName+"_tmpl_phrase_id = RegisterTmplPhraseNames([]string{", "[")
rep("phrases.", "") data = replace(data, "var phrases = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "let phrases = tmplPhrases[\""+tmplName+"\"];")
rep(", 10;", "") //data = replace(data, "var phrases = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "let phrases = tmplPhrases[\""+tmplName+"\"];\nconsole.log('tmplName:','"+tmplName+"')\nconsole.log('phrases:', phrases);")
data = replace(data, "if ", "if(")
//rep("var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "const plist = tmplPhrases[\""+tmplName+"\"];") data = replace(data, "return nil", "return out")
//rep("//var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "const "+shortName+"_phrase_arr = tmplPhrases[\""+tmplName+"\"];") data = replace(data, " )", ")")
rep("//var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "const pl=tmplPhrases[\""+tmplName+"\"];") data = replace(data, " \n", "\n")
rep(shortName+"_phrase_arr", "pl") data = replace(data, "\n", ";\n")
rep(shortName+"_phrase", "pl") data = replace(data, "{;", "{")
rep("tmpl_"+shortName+"_vars", "t_v") data = replace(data, "};", "}")
data = replace(data, "[;", "[")
rep("var c_v_", "let c_v_") data = replace(data, ";;", ";")
rep(`t_vars, ok := tmpl_i.`, `/*`) data = replace(data, ",;", ",")
rep("[]byte(", "") data = replace(data, "=;", "=")
rep("StringToBytes(", "") data = replace(data, `,
rep("RelativeTime(t_v.", "t_v.Relative")
// TODO: Format dates properly on the client side
rep(".Format(\"2006-01-02 15:04:05\"", "")
rep(", 10", "")
rep("if ", "if(")
rep("return nil", "return o")
rep(" )", ")")
rep(" \n", "\n")
rep("\n", ";\n")
rep("{;", "{")
rep("};", "}")
rep("[;", "[")
rep(",;", ",")
rep("=;", "=")
rep(`,
}); });
}`, "\n\t];") }`, "\n\t];")
rep(`= data = replace(data, `=
}`, "= []") }`, "= []")
rep("o += ", "o+=")
rep(shortName+"_frags[", "fr[")
rep("function Tmpl_"+shortName+"(t_v) {", "var Tmpl_"+shortName+"=(t_v)=>{")
fragset := tmpl.GetFrag(shortName) fragset, ok := fragMap[shortName]
if fragset != nil { if !ok {
//sfrags := []byte("let " + shortName + "_frags=[\n") DebugLog("tmplName: ", tmplName)
sfrags := []byte("{const fr=[") return errors.New("couldn't find template in fragmap")
for i, frags := range fragset {
//sfrags = append(sfrags, []byte(shortName+"_frags.push(`"+string(frags)+"`);\n")...)
//sfrags = append(sfrags, []byte("`"+string(frags)+"`,\n")...)
if i == 0 {
sfrags = append(sfrags, []byte("`"+string(frags)+"`")...)
} else {
sfrags = append(sfrags, []byte(",`"+string(frags)+"`")...)
} }
var sfrags = []byte("let " + shortName + "_frags = [];\n")
for _, frags := range fragset {
sfrags = append(sfrags, []byte(shortName+"_frags.push(`"+string(frags)+"`);\n")...)
} }
//sfrags = append(sfrags, []byte("];\n")...)
sfrags = append(sfrags, []byte("];")...)
data = append(sfrags, data...) data = append(sfrags, data...)
} data = replace(data, "\n;", "\n")
rep("\n;", "\n")
rep(";;", ";")
data = append(data, '}')
for name, _ := range Themes {
if strings.HasSuffix(shortName, "_"+name) {
data = append(data, "var Tmpl_"+strings.TrimSuffix(shortName, "_"+name)+"=Tmpl_"+shortName+";"...)
break
}
}
path = tmplName + ".js" path = tmplName + ".js"
DebugLog("js path: ", path) DebugLog("js path: ", path)
ext := filepath.Ext("/tmpl_client/" + path) var ext = filepath.Ext("/tmpl_client/" + path)
gzipData, err := compressBytesGzip(data)
brData, err := CompressBytesBrotli(data)
if err != nil { if err != nil {
return err return err
} }
// Don't use Brotli if we get meagre gains from it as it takes longer to process the responses
if len(brData) >= (len(data) + 110) {
brData = nil
} else {
diff := len(data) - len(brData)
if diff <= len(data)/100 {
brData = nil
}
}
gzipData, err := CompressBytesGzip(data) list.Set("/static/"+path, SFile{data, gzipData, 0, int64(len(data)), int64(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)})
if err != nil {
return err
}
// Don't use Gzip if we get meagre gains from it as it takes longer to process the responses
if len(gzipData) >= (len(data) + 120) {
gzipData = nil
} else {
diff := len(data) - len(gzipData)
if diff <= len(data)/100 {
gzipData = nil
}
}
// Get a checksum for CSPs and cache busting
hasher := sha256.New()
hasher.Write(data)
sum := hasher.Sum(nil)
checksum := hex.EncodeToString(sum)
integrity := base64.StdEncoding.EncodeToString(sum)
l.Set(l.Prefix+path, &SFile{data, gzipData, brData, checksum, integrity, l.Prefix + path + "?h=" + checksum, 0, int64(len(data)), strconv.Itoa(len(data)), int64(len(gzipData)), strconv.Itoa(len(gzipData)), int64(len(brData)), strconv.Itoa(len(brData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)})
DebugLogf("Added the '%s' static file.", path) DebugLogf("Added the '%s' static file.", path)
return nil return nil
}) })
} }
func (l SFileList) Init() error { func (list SFileList) Init() error {
return filepath.Walk("./public", func(path string, f os.FileInfo, err error) error { return filepath.Walk("./public", func(path string, f os.FileInfo, err error) error {
if f.IsDir() { if f.IsDir() {
return nil return nil
@ -330,43 +240,20 @@ func (l SFileList) Init() error {
if err != nil { if err != nil {
return err return err
} }
path = strings.TrimPrefix(path, "public/") path = strings.TrimPrefix(path, "public/")
ext := filepath.Ext("/public/" + path) var ext = filepath.Ext("/public/" + path)
if ext == ".js" {
data = bytes.Replace(data, []byte("\r"), []byte(""), -1)
}
mimetype := mime.TypeByExtension(ext) mimetype := mime.TypeByExtension(ext)
// Get a checksum for CSPs and cache busting
hasher := sha256.New()
hasher.Write(data)
sum := hasher.Sum(nil)
checksum := hex.EncodeToString(sum)
integrity := base64.StdEncoding.EncodeToString(sum)
// Avoid double-compressing images // Avoid double-compressing images
var gzipData, brData []byte var gzipData []byte
if mimetype != "image/jpeg" && mimetype != "image/png" && mimetype != "image/gif" { if mimetype != "image/jpeg" && mimetype != "image/png" && mimetype != "image/gif" {
brData, err = CompressBytesBrotli(data) gzipData, err = compressBytesGzip(data)
if err != nil {
return err
}
// Don't use Brotli if we get meagre gains from it as it takes longer to process the responses
if len(brData) >= (len(data) + 130) {
brData = nil
} else {
diff := len(data) - len(brData)
if diff <= len(data)/100 {
brData = nil
}
}
gzipData, err = CompressBytesGzip(data)
if err != nil { if err != nil {
return err return err
} }
// Don't use Gzip if we get meagre gains from it as it takes longer to process the responses // Don't use Gzip if we get meagre gains from it as it takes longer to process the responses
if len(gzipData) >= (len(data) + 150) { if len(gzipData) >= (len(data) + 100) {
gzipData = nil gzipData = nil
} else { } else {
diff := len(data) - len(gzipData) diff := len(data) - len(gzipData)
@ -376,14 +263,14 @@ func (l SFileList) Init() error {
} }
} }
l.Set(l.Prefix+path, &SFile{data, gzipData, brData, checksum, integrity, l.Prefix + path + "?h=" + checksum, 0, int64(len(data)), strconv.Itoa(len(data)), int64(len(gzipData)), strconv.Itoa(len(gzipData)), int64(len(brData)), strconv.Itoa(len(brData)), mimetype, f, f.ModTime().UTC().Format(http.TimeFormat)}) list.Set("/static/"+path, SFile{data, gzipData, 0, int64(len(data)), int64(len(gzipData)), mimetype, f, f.ModTime().UTC().Format(http.TimeFormat)})
DebugLogf("Added the '%s' static file.", path) DebugLogf("Added the '%s' static file.", path)
return nil return nil
}) })
} }
func (l SFileList) Add(path, prefix string) error { func (list SFileList) Add(path string, prefix string) error {
data, err := ioutil.ReadFile(path) data, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return err return err
@ -397,92 +284,38 @@ func (l SFileList) Add(path, prefix string) error {
return err return err
} }
ext := filepath.Ext(path) var ext = filepath.Ext(path)
path = strings.TrimPrefix(path, prefix) path = strings.TrimPrefix(path, prefix)
gzipData, err := compressBytesGzip(data)
brData, err := CompressBytesBrotli(data)
if err != nil { if err != nil {
return err return err
} }
// Don't use Brotli if we get meagre gains from it as it takes longer to process the responses
if len(brData) >= (len(data) + 130) {
brData = nil
} else {
diff := len(data) - len(brData)
if diff <= len(data)/100 {
brData = nil
}
}
gzipData, err := CompressBytesGzip(data) list.Set("/static"+path, SFile{data, gzipData, 0, int64(len(data)), int64(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)})
if err != nil {
return err
}
// Don't use Gzip if we get meagre gains from it as it takes longer to process the responses
if len(gzipData) >= (len(data) + 150) {
gzipData = nil
} else {
diff := len(data) - len(gzipData)
if diff <= len(data)/100 {
gzipData = nil
}
}
// Get a checksum for CSPs and cache busting
hasher := sha256.New()
hasher.Write(data)
sum := hasher.Sum(nil)
checksum := hex.EncodeToString(sum)
integrity := base64.StdEncoding.EncodeToString(sum)
l.Set(l.Prefix+path, &SFile{data, gzipData, brData, checksum, integrity, l.Prefix + path + "?h=" + checksum, 0, int64(len(data)), strconv.Itoa(len(data)), int64(len(gzipData)), strconv.Itoa(len(gzipData)), int64(len(brData)), strconv.Itoa(len(brData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)})
DebugLogf("Added the '%s' static file", path) DebugLogf("Added the '%s' static file", path)
return nil return nil
} }
func (l SFileList) Get(path string) (file *SFile, exists bool) { func (list SFileList) Get(name string) (file SFile, exists bool) {
staticFileMutex.RLock() staticFileMutex.RLock()
defer staticFileMutex.RUnlock() defer staticFileMutex.RUnlock()
file, exists = l.Long[path] file, exists = list[name]
return file, exists return file, exists
} }
// fetch without /s/ to avoid allocing in pages.go func (list SFileList) Set(name string, data SFile) {
func (l SFileList) GetShort(name string) (file *SFile, exists bool) {
staticFileMutex.RLock()
defer staticFileMutex.RUnlock()
file, exists = l.Short[name]
return file, exists
}
func (l SFileList) Set(name string, data *SFile) {
staticFileMutex.Lock() staticFileMutex.Lock()
defer staticFileMutex.Unlock() defer staticFileMutex.Unlock()
// TODO: Propagate errors back up list[name] = data
uurl, err := url.Parse(name)
if err != nil {
return
}
l.Long[uurl.Path] = data
l.Short[strings.TrimPrefix(strings.TrimPrefix(name, l.Prefix), "/")] = data
} }
var gzipBestCompress sync.Pool func compressBytesGzip(in []byte) ([]byte, error) {
var buff bytes.Buffer
func CompressBytesGzip(in []byte) (b []byte, err error) { gz, err := gzip.NewWriterLevel(&buff, gzip.BestCompression)
var buf bytes.Buffer
ii := gzipBestCompress.Get()
var gz *gzip.Writer
if ii == nil {
gz, err = gzip.NewWriterLevel(&buf, gzip.BestCompression)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else {
gz = ii.(*gzip.Writer)
gz.Reset(&buf)
}
_, err = gz.Write(in) _, err = gz.Write(in)
if err != nil { if err != nil {
return nil, err return nil, err
@ -491,20 +324,5 @@ func CompressBytesGzip(in []byte) (b []byte, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
gzipBestCompress.Put(gz)
return buf.Bytes(), nil
}
func CompressBytesBrotli(in []byte) ([]byte, error) {
var buff bytes.Buffer
br := brotli.NewWriterLevel(&buff, brotli.BestCompression)
_, err := br.Write(in)
if err != nil {
return nil, err
}
err = br.Close()
if err != nil {
return nil, err
}
return buff.Bytes(), nil return buff.Bytes(), nil
} }

View File

@ -1,13 +1,13 @@
package common package common
//import "fmt"
import ( import (
//"log"
"database/sql" "database/sql"
"errors" "errors"
"strconv" "strconv"
"strings" "strings"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
@ -27,9 +27,7 @@ type Forum struct {
Link string Link string
Name string Name string
Desc string Desc string
Tmpl string
Active bool Active bool
Order int
Preset string Preset string
ParentID int ParentID int
ParentType string ParentType string
@ -40,7 +38,6 @@ type Forum struct {
LastReplyer *User LastReplyer *User
LastReplyerID int LastReplyerID int
LastTopicTime string // So that we can re-calculate the relative time on the spot in /forums/ LastTopicTime string // So that we can re-calculate the relative time on the spot in /forums/
LastPage int
} }
// ? - What is this for? // ? - What is this for?
@ -69,59 +66,59 @@ func init() {
} }
// Copy gives you a non-pointer concurrency safe copy of the forum // Copy gives you a non-pointer concurrency safe copy of the forum
func (f *Forum) Copy() (fcopy Forum) { func (forum *Forum) Copy() (fcopy Forum) {
fcopy = *f fcopy = *forum
return fcopy return fcopy
} }
// TODO: Write tests for this // TODO: Write tests for this
func (f *Forum) Update(name, desc string, active bool, preset string) error { func (forum *Forum) Update(name string, desc string, active bool, preset string) error {
if name == "" { if name == "" {
name = f.Name name = forum.Name
} }
// TODO: Do a line sanitise? Does it matter? // TODO: Do a line sanitise? Does it matter?
preset = strings.TrimSpace(preset) preset = strings.TrimSpace(preset)
_, err := forumStmts.update.Exec(name, desc, active, preset, f.ID) _, err := forumStmts.update.Exec(name, desc, active, preset, forum.ID)
if err != nil { if err != nil {
return err return err
} }
if f.Preset != preset && preset != "custom" && preset != "" { if forum.Preset != preset || preset == "custom" || preset == "" {
err = PermmapToQuery(PresetToPermmap(preset), f.ID) err = PermmapToQuery(PresetToPermmap(preset), forum.ID)
if err != nil { if err != nil {
return err return err
} }
} }
_ = Forums.Reload(f.ID) _ = Forums.Reload(forum.ID)
return nil return nil
} }
func (f *Forum) SetPreset(preset string, gid int) error { func (forum *Forum) SetPreset(preset string, gid int) error {
fp, changed := GroupForumPresetToForumPerms(preset) fperms, changed := GroupForumPresetToForumPerms(preset)
if changed { if changed {
return f.SetPerms(fp, preset, gid) return forum.SetPerms(fperms, preset, gid)
} }
return nil return nil
} }
// TODO: Refactor this // TODO: Refactor this
func (f *Forum) SetPerms(fperms *ForumPerms, preset string, gid int) (err error) { func (forum *Forum) SetPerms(fperms *ForumPerms, preset string, gid int) (err error) {
err = ReplaceForumPermsForGroup(gid, map[int]string{f.ID: preset}, map[int]*ForumPerms{f.ID: fperms}) err = ReplaceForumPermsForGroup(gid, map[int]string{forum.ID: preset}, map[int]*ForumPerms{forum.ID: fperms})
if err != nil { if err != nil {
LogError(err) LogError(err)
return errors.New("Unable to update the permissions") return errors.New("Unable to update the permissions")
} }
// TODO: Add this and replaceForumPermsForGroup into a transaction? // TODO: Add this and replaceForumPermsForGroup into a transaction?
_, err = forumStmts.setPreset.Exec("", f.ID) _, err = forumStmts.setPreset.Exec("", forum.ID)
if err != nil { if err != nil {
LogError(err) LogError(err)
return errors.New("Unable to update the forum") return errors.New("Unable to update the forum")
} }
err = Forums.Reload(f.ID) err = Forums.Reload(forum.ID)
if err != nil { if err != nil {
return errors.New("Unable to reload forum") return errors.New("Unable to reload forum")
} }
err = FPStore.Reload(f.ID) err = FPStore.Reload(forum.ID)
if err != nil { if err != nil {
return errors.New("Unable to reload the forum permissions") return errors.New("Unable to reload the forum permissions")
} }
@ -138,27 +135,12 @@ func (sf SortForum) Len() int {
func (sf SortForum) Swap(i, j int) { func (sf SortForum) Swap(i, j int) {
sf[i], sf[j] = sf[j], sf[i] sf[i], sf[j] = sf[j], sf[i]
} }
/*func (sf SortForum) Less(i,j int) bool {
l := sf.less(i,j)
if l {
log.Printf("%s is less than %s. order: %d. id: %d.",sf[i].Name, sf[j].Name, sf[i].Order, sf[i].ID)
} else {
log.Printf("%s is not less than %s. order: %d. id: %d.",sf[i].Name, sf[j].Name, sf[i].Order, sf[i].ID)
}
return l
}*/
func (sf SortForum) Less(i, j int) bool { func (sf SortForum) Less(i, j int) bool {
if sf[i].Order < sf[j].Order {
return true
} else if sf[i].Order == sf[j].Order {
return sf[i].ID < sf[j].ID return sf[i].ID < sf[j].ID
} }
return false
}
// ! Don't use this outside of tests and possibly template_init.go // ! Don't use this outside of tests and possibly template_init.go
func BlankForum(fid int, link, name, desc string, active bool, preset string, parentID int, parentType string, topicCount int) *Forum { func BlankForum(fid int, link string, name string, desc string, active bool, preset string, parentID int, parentType string, topicCount int) *Forum {
return &Forum{ID: fid, Link: link, Name: name, Desc: desc, Active: active, Preset: preset, ParentID: parentID, ParentType: parentType, TopicCount: topicCount} return &Forum{ID: fid, Link: link, Name: name, Desc: desc, Active: active, Preset: preset, ParentID: parentID, ParentType: parentType, TopicCount: topicCount}
} }

View File

@ -1,399 +0,0 @@
package common
import (
"database/sql"
"fmt"
"strconv"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var ForumActionStore ForumActionStoreInt
//var ForumActionRunnableStore ForumActionRunnableStoreInt
const (
ForumActionDelete = iota
ForumActionLock
ForumActionUnlock
ForumActionMove
)
func ConvStringToAct(s string) int {
switch s {
case "delete":
return ForumActionDelete
case "lock":
return ForumActionLock
case "unlock":
return ForumActionUnlock
case "move":
return ForumActionMove
}
return -1
}
func ConvActToString(a int) string {
switch a {
case ForumActionDelete:
return "delete"
case ForumActionLock:
return "lock"
case ForumActionUnlock:
return "unlock"
case ForumActionMove:
return "move"
}
return ""
}
var forumActionStmts ForumActionStmts
type ForumActionStmts struct {
get1 *sql.Stmt
get2 *sql.Stmt
lock1 *sql.Stmt
lock2 *sql.Stmt
unlock1 *sql.Stmt
unlock2 *sql.Stmt
}
type ForumAction struct {
ID int
Forum int
RunOnTopicCreation bool
RunDaysAfterTopicCreation int
RunDaysAfterTopicLastReply int
Action int
Extra string
}
func init() {
DbInits.Add(func(acc *qgen.Accumulator) error {
t := "topics"
forumActionStmts = ForumActionStmts{
get1: acc.Select(t).Cols("tid,createdBy,poll").Where("parentID=?").DateOlderThanQ("createdAt", "day").Stmt(),
get2: acc.Select(t).Cols("tid,createdBy,poll").Where("parentID=?").DateOlderThanQ("lastReplyAt", "day").Stmt(),
/*lock1: acc.Update(t).Set("is_closed=1").Where("parentID=?").DateOlderThanQ("createdAt", "day").Stmt(),
lock2: acc.Update(t).Set("is_closed=1").Where("parentID=?").DateOlderThanQ("lastReplyAt", "day").Stmt(),
unlock1: acc.Update(t).Set("is_closed=0").Where("parentID=?").DateOlderThanQ("createdAt", "day").Stmt(),
unlock2: acc.Update(t).Set("is_closed=0").Where("parentID=?").DateOlderThanQ("lastReplyAt", "day").Stmt(),*/
}
return acc.FirstError()
})
}
func (a *ForumAction) Run() error {
if a.RunDaysAfterTopicCreation > 0 {
if e := a.runDaysAfterTopicCreation(); e != nil {
return e
}
}
if a.RunDaysAfterTopicLastReply > 0 {
if e := a.runDaysAfterTopicLastReply(); e != nil {
return e
}
}
return nil
}
func (a *ForumAction) runQ(stmt *sql.Stmt, days int, f func(t *Topic) error) error {
rows, e := stmt.Query(days, a.Forum)
if e != nil {
return e
}
defer rows.Close()
for rows.Next() {
// TODO: Decouple this
t := &Topic{ParentID: a.Forum}
if e := rows.Scan(&t.ID, &t.CreatedBy, &t.Poll); e != nil {
return e
}
if e = f(t); e != nil {
return e
}
}
return rows.Err()
}
func (a *ForumAction) runDaysAfterTopicCreation() (e error) {
switch a.Action {
case ForumActionDelete:
// TODO: Bulk delete?
e = a.runQ(forumActionStmts.get1, a.RunDaysAfterTopicCreation, func(t *Topic) error {
return t.Delete()
})
case ForumActionLock:
/*_, e := forumActionStmts.lock1.Exec(a.Forum)
if e != nil {
return e
}*/
// TODO: Bulk lock? Lock and get resultset of changed topics somehow?
fmt.Println("ForumActionLock")
e = a.runQ(forumActionStmts.get1, a.RunDaysAfterTopicCreation, func(t *Topic) error {
fmt.Printf("t: %+v\n", t)
return t.Lock()
})
case ForumActionUnlock:
// TODO: Bulk unlock? Unlock and get resultset of changed topics somehow?
e = a.runQ(forumActionStmts.get1, a.RunDaysAfterTopicCreation, func(t *Topic) error {
return t.Unlock()
})
case ForumActionMove:
destForum, e := strconv.Atoi(a.Extra)
if e != nil {
return e
}
e = a.runQ(forumActionStmts.get1, a.RunDaysAfterTopicCreation, func(t *Topic) error {
return t.MoveTo(destForum)
})
}
return e
}
func (a *ForumAction) runDaysAfterTopicLastReply() (e error) {
switch a.Action {
case ForumActionDelete:
e = a.runQ(forumActionStmts.get2, a.RunDaysAfterTopicLastReply, func(t *Topic) error {
return t.Delete()
})
case ForumActionLock:
// TODO: Bulk lock? Lock and get resultset of changed topics somehow?
e = a.runQ(forumActionStmts.get2, a.RunDaysAfterTopicLastReply, func(t *Topic) error {
return t.Lock()
})
case ForumActionUnlock:
// TODO: Bulk unlock? Unlock and get resultset of changed topics somehow?
e = a.runQ(forumActionStmts.get2, a.RunDaysAfterTopicLastReply, func(t *Topic) error {
return t.Unlock()
})
case ForumActionMove:
destForum, e := strconv.Atoi(a.Extra)
if e != nil {
return e
}
e = a.runQ(forumActionStmts.get2, a.RunDaysAfterTopicLastReply, func(t *Topic) error {
return t.MoveTo(destForum)
})
}
return nil
}
func (a *ForumAction) TopicCreation(tid int) error {
if !a.RunOnTopicCreation {
return nil
}
return nil
}
type ForumActionStoreInt interface {
Get(faid int) (*ForumAction, error)
GetInForum(fid int) ([]*ForumAction, error)
GetAll() ([]*ForumAction, error)
GetNewTopicActions(fid int) ([]*ForumAction, error)
Add(fa *ForumAction) (int, error)
Delete(faid int) error
Exists(faid int) bool
Count() int
CountInForum(fid int) int
DailyTick() error
}
type DefaultForumActionStore struct {
get *sql.Stmt
getInForum *sql.Stmt
getAll *sql.Stmt
getNewTopicActions *sql.Stmt
add *sql.Stmt
delete *sql.Stmt
exists *sql.Stmt
count *sql.Stmt
countInForum *sql.Stmt
}
func NewDefaultForumActionStore(acc *qgen.Accumulator) (*DefaultForumActionStore, error) {
fa := "forums_actions"
allCols := "faid,fid,runOnTopicCreation,runDaysAfterTopicCreation,runDaysAfterTopicLastReply,action,extra"
return &DefaultForumActionStore{
get: acc.Select(fa).Columns("fid,runOnTopicCreation,runDaysAfterTopicCreation,runDaysAfterTopicLastReply,action,extra").Where("faid=?").Prepare(),
getInForum: acc.Select(fa).Columns("faid,runOnTopicCreation,runDaysAfterTopicCreation,runDaysAfterTopicLastReply,action,extra").Where("fid=?").Prepare(),
getAll: acc.Select(fa).Columns(allCols).Prepare(),
getNewTopicActions: acc.Select(fa).Columns(allCols).Where("fid=? AND runOnTopicCreation=1").Prepare(),
add: acc.Insert(fa).Columns("fid,runOnTopicCreation,runDaysAfterTopicCreation,runDaysAfterTopicLastReply,action,extra").Fields("?,?,?,?,?,?").Prepare(),
delete: acc.Delete(fa).Where("faid=?").Prepare(),
exists: acc.Exists(fa, "faid").Prepare(),
count: acc.Count(fa).Prepare(),
countInForum: acc.Count(fa).Where("fid=?").Prepare(),
}, acc.FirstError()
}
func (s *DefaultForumActionStore) DailyTick() error {
fas, e := s.GetAll()
if e != nil {
return e
}
for _, fa := range fas {
if e := fa.Run(); e != nil {
return e
}
}
return nil
}
func (s *DefaultForumActionStore) Get(id int) (*ForumAction, error) {
fa := ForumAction{ID: id}
var str string
e := s.get.QueryRow(id).Scan(&fa.Forum, &fa.RunOnTopicCreation, &fa.RunDaysAfterTopicCreation, &fa.RunDaysAfterTopicLastReply, &str, &fa.Extra)
fa.Action = ConvStringToAct(str)
return &fa, e
}
func (s *DefaultForumActionStore) GetInForum(fid int) (fas []*ForumAction, e error) {
rows, e := s.getInForum.Query(fid)
if e != nil {
return nil, e
}
defer rows.Close()
var str string
for rows.Next() {
fa := ForumAction{Forum: fid}
if e := rows.Scan(&fa.ID, &fa.RunOnTopicCreation, &fa.RunDaysAfterTopicCreation, &fa.RunDaysAfterTopicLastReply, &str, &fa.Extra); e != nil {
return nil, e
}
fa.Action = ConvStringToAct(str)
fas = append(fas, &fa)
}
return fas, rows.Err()
}
func (s *DefaultForumActionStore) GetAll() (fas []*ForumAction, e error) {
rows, e := s.getAll.Query()
if e != nil {
return nil, e
}
defer rows.Close()
var str string
for rows.Next() {
fa := ForumAction{}
if e := rows.Scan(&fa.ID, &fa.Forum, &fa.RunOnTopicCreation, &fa.RunDaysAfterTopicCreation, &fa.RunDaysAfterTopicLastReply, &str, &fa.Extra); e != nil {
return nil, e
}
fa.Action = ConvStringToAct(str)
fas = append(fas, &fa)
}
return fas, rows.Err()
}
func (s *DefaultForumActionStore) GetNewTopicActions(fid int) (fas []*ForumAction, e error) {
rows, e := s.getNewTopicActions.Query(fid)
if e != nil {
return nil, e
}
defer rows.Close()
var str string
for rows.Next() {
fa := ForumAction{RunOnTopicCreation: true}
if e := rows.Scan(&fa.ID, &fa.Forum, &fa.RunDaysAfterTopicCreation, &fa.RunDaysAfterTopicLastReply, &str, &fa.Extra); e != nil {
return nil, e
}
fa.Action = ConvStringToAct(str)
fas = append(fas, &fa)
}
return fas, rows.Err()
}
func (s *DefaultForumActionStore) Add(fa *ForumAction) (int, error) {
res, e := s.add.Exec(fa.Forum, fa.RunOnTopicCreation, fa.RunDaysAfterTopicCreation, fa.RunDaysAfterTopicLastReply, ConvActToString(fa.Action), fa.Extra)
if e != nil {
return 0, e
}
lastID, e := res.LastInsertId()
return int(lastID), e
}
func (s *DefaultForumActionStore) Delete(id int) error {
_, e := s.delete.Exec(id)
return e
}
func (s *DefaultForumActionStore) Exists(id int) bool {
err := s.exists.QueryRow(id).Scan(&id)
if err != nil && err != ErrNoRows {
LogError(err)
}
return err != ErrNoRows
}
func (s *DefaultForumActionStore) Count() (count int) {
err := s.count.QueryRow().Scan(&count)
if err != nil {
LogError(err)
}
return count
}
func (s *DefaultForumActionStore) CountInForum(fid int) (count int) {
return Countf(s.countInForum, fid)
}
/*type ForumActionRunnable struct {
ID int
ActionID int
TargetID int
TargetType int // 0 = topic
RunAfter int //unixtime
}
type ForumActionRunnableStoreInt interface {
GetAfterTime(unix int) ([]*ForumActionRunnable, error)
GetInForum(fid int) ([]*ForumActionRunnable, error)
Delete(faid int) error
DeleteInForum(fid int) error
DeleteByActionID(faid int) error
Count() int
CountInForum(fid int) int
}
type DefaultForumActionRunnableStore struct {
delete *sql.Stmt
deleteInForum *sql.Stmt
count *sql.Stmt
countInForum *sql.Stmt
}
func NewDefaultForumActionRunnableStore(acc *qgen.Accumulator) (*DefaultForumActionRunnableStore, error) {
fa := "forums_actions"
return &DefaultForumActionRunnableStore{
delete: acc.Delete(fa).Where("faid=?").Prepare(),
deleteInForum: acc.Delete(fa).Where("fid=?").Prepare(),
count: acc.Count(fa).Prepare(),
countInForum: acc.Count(fa).Where("faid=?").Prepare(),
}, acc.FirstError()
}
func (s *DefaultForumActionRunnableStore) Delete(id int) error {
_, e := s.delete.Exec(id)
return e
}
func (s *DefaultForumActionRunnableStore) DeleteInForum(fid int) error {
_, e := s.deleteInForum.Exec(id)
return e
}
func (s *DefaultForumActionRunnableStore) Count() (count int) {
err := s.count.QueryRow().Scan(&count)
if err != nil {
LogError(err)
}
return count
}
func (s *DefaultForumActionRunnableStore) CountInForum(fid int) (count int) {
return Countf(s.countInForum, fid)
}
*/

View File

@ -4,7 +4,7 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
// ? - Can we avoid duplicating the items in this list in a bunch of places? // ? - Can we avoid duplicating the items in this list in a bunch of places?
@ -99,6 +99,7 @@ func PermmapToQuery(permmap map[string]*ForumPerms, fid int) error {
if err != nil { if err != nil {
return err return err
} }
_, err = deleteForumPermsByForumTx.Exec(fid) _, err = deleteForumPermsByForumTx.Exec(fid)
if err != nil { if err != nil {
return err return err
@ -111,12 +112,13 @@ func PermmapToQuery(permmap map[string]*ForumPerms, fid int) error {
addForumPermsToForumAdminsTx, err := qgen.Builder.SimpleInsertSelectTx(tx, addForumPermsToForumAdminsTx, err := qgen.Builder.SimpleInsertSelectTx(tx,
qgen.DBInsert{"forums_permissions", "gid, fid, preset, permissions", ""}, qgen.DBInsert{"forums_permissions", "gid, fid, preset, permissions", ""},
qgen.DBSelect{"users_groups", "gid,?,'',?", "is_admin = 1", "", ""}, qgen.DBSelect{"users_groups", "gid, ? AS fid, ? AS preset, ? AS permissions", "is_admin = 1", "", ""},
) )
if err != nil { if err != nil {
return err return err
} }
_, err = addForumPermsToForumAdminsTx.Exec(fid, perms)
_, err = addForumPermsToForumAdminsTx.Exec(fid, "", perms)
if err != nil { if err != nil {
return err return err
} }
@ -128,12 +130,12 @@ func PermmapToQuery(permmap map[string]*ForumPerms, fid int) error {
addForumPermsToForumStaffTx, err := qgen.Builder.SimpleInsertSelectTx(tx, addForumPermsToForumStaffTx, err := qgen.Builder.SimpleInsertSelectTx(tx,
qgen.DBInsert{"forums_permissions", "gid, fid, preset, permissions", ""}, qgen.DBInsert{"forums_permissions", "gid, fid, preset, permissions", ""},
qgen.DBSelect{"users_groups", "gid,?,'',?", "is_admin = 0 AND is_mod = 1", "", ""}, qgen.DBSelect{"users_groups", "gid, ? AS fid, ? AS preset, ? AS permissions", "is_admin = 0 AND is_mod = 1", "", ""},
) )
if err != nil { if err != nil {
return err return err
} }
_, err = addForumPermsToForumStaffTx.Exec(fid, perms) _, err = addForumPermsToForumStaffTx.Exec(fid, "", perms)
if err != nil { if err != nil {
return err return err
} }
@ -145,12 +147,12 @@ func PermmapToQuery(permmap map[string]*ForumPerms, fid int) error {
addForumPermsToForumMembersTx, err := qgen.Builder.SimpleInsertSelectTx(tx, addForumPermsToForumMembersTx, err := qgen.Builder.SimpleInsertSelectTx(tx,
qgen.DBInsert{"forums_permissions", "gid, fid, preset, permissions", ""}, qgen.DBInsert{"forums_permissions", "gid, fid, preset, permissions", ""},
qgen.DBSelect{"users_groups", "gid,?,'',?", "is_admin = 0 AND is_mod = 0 AND is_banned = 0", "", ""}, qgen.DBSelect{"users_groups", "gid, ? AS fid, ? AS preset, ? AS permissions", "is_admin = 0 AND is_mod = 0 AND is_banned = 0", "", ""},
) )
if err != nil { if err != nil {
return err return err
} }
_, err = addForumPermsToForumMembersTx.Exec(fid, perms) _, err = addForumPermsToForumMembersTx.Exec(fid, "", perms)
if err != nil { if err != nil {
return err return err
} }
@ -172,8 +174,11 @@ func PermmapToQuery(permmap map[string]*ForumPerms, fid int) error {
if err != nil { if err != nil {
return err return err
} }
return FPStore.Reload(fid) err = FPStore.Reload(fid)
//return TopicList.RebuildPermTree() if err != nil {
return err
}
return TopicList.RebuildPermTree()
} }
// TODO: FPStore.Reload? // TODO: FPStore.Reload?
@ -188,8 +193,11 @@ func ReplaceForumPermsForGroup(gid int, presetSet map[int]string, permSets map[i
if err != nil { if err != nil {
return err return err
} }
return tx.Commit() err = tx.Commit()
//return TopicList.RebuildPermTree() if err != nil {
return err
}
return TopicList.RebuildPermTree()
} }
func ReplaceForumPermsForGroupTx(tx *sql.Tx, gid int, presetSets map[int]string, permSets map[int]*ForumPerms) error { func ReplaceForumPermsForGroupTx(tx *sql.Tx, gid int, presetSets map[int]string, permSets map[int]*ForumPerms) error {
@ -222,29 +230,29 @@ func ReplaceForumPermsForGroupTx(tx *sql.Tx, gid int, presetSets map[int]string,
// TODO: Refactor this and write tests for it // TODO: Refactor this and write tests for it
// TODO: We really need to improve the thread safety of this // TODO: We really need to improve the thread safety of this
func ForumPermsToGroupForumPreset(fp *ForumPerms) string { func ForumPermsToGroupForumPreset(fperms *ForumPerms) string {
if !fp.Overrides { if !fperms.Overrides {
return "default" return "default"
} }
if !fp.ViewTopic { if !fperms.ViewTopic {
return "no_access" return "no_access"
} }
canPost := (fp.LikeItem && fp.CreateTopic && fp.CreateReply) var canPost = (fperms.LikeItem && fperms.CreateTopic && fperms.CreateReply)
canModerate := (canPost && fp.EditTopic && fp.DeleteTopic && fp.EditReply && fp.DeleteReply && fp.PinTopic && fp.CloseTopic && fp.MoveTopic) var canModerate = (canPost && fperms.EditTopic && fperms.DeleteTopic && fperms.EditReply && fperms.DeleteReply && fperms.PinTopic && fperms.CloseTopic && fperms.MoveTopic)
if canModerate { if canModerate {
return "can_moderate" return "can_moderate"
} }
if fp.EditTopic || fp.DeleteTopic || fp.EditReply || fp.DeleteReply || fp.PinTopic || fp.CloseTopic || fp.MoveTopic { if fperms.EditTopic || fperms.DeleteTopic || fperms.EditReply || fperms.DeleteReply || fperms.PinTopic || fperms.CloseTopic || fperms.MoveTopic {
//if !canPost { if !canPost {
return "custom" return "custom"
//} }
//return "quasi_mod" return "quasi_mod"
} }
if canPost { if canPost {
return "can_post" return "can_post"
} }
if fp.ViewTopic && !fp.LikeItem && !fp.CreateTopic && !fp.CreateReply { if fperms.ViewTopic && !fperms.LikeItem && !fperms.CreateTopic && !fperms.CreateReply {
return "read_only" return "read_only"
} }
return "custom" return "custom"

View File

@ -5,7 +5,7 @@ import (
"encoding/json" "encoding/json"
"sync" "sync"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var FPStore ForumPermsStore var FPStore ForumPermsStore
@ -13,8 +13,8 @@ var FPStore ForumPermsStore
type ForumPermsStore interface { type ForumPermsStore interface {
Init() error Init() error
GetAllMap() (bigMap map[int]map[int]*ForumPerms) GetAllMap() (bigMap map[int]map[int]*ForumPerms)
Get(fid, gid int) (fp *ForumPerms, err error) Get(fid int, gid int) (fperms *ForumPerms, err error)
GetCopy(fid, gid int) (fp ForumPerms, err error) GetCopy(fid int, gid int) (fperms ForumPerms, err error)
ReloadAll() error ReloadAll() error
Reload(id int) error Reload(id int) error
} }
@ -34,71 +34,54 @@ type MemoryForumPermsStore struct {
func NewMemoryForumPermsStore() (*MemoryForumPermsStore, error) { func NewMemoryForumPermsStore() (*MemoryForumPermsStore, error) {
acc := qgen.NewAcc() acc := qgen.NewAcc()
fp := "forums_permissions"
return &MemoryForumPermsStore{ return &MemoryForumPermsStore{
getByForum: acc.Select(fp).Columns("gid,permissions").Where("fid=?").Orderby("gid ASC").Prepare(), getByForum: acc.Select("forums_permissions").Columns("gid, permissions").Where("fid = ?").Orderby("gid ASC").Prepare(),
getByForumGroup: acc.Select(fp).Columns("permissions").Where("fid=? AND gid=?").Prepare(), getByForumGroup: acc.Select("forums_permissions").Columns("permissions").Where("fid = ? AND gid = ?").Prepare(),
evenForums: make(map[int]map[int]*ForumPerms), evenForums: make(map[int]map[int]*ForumPerms),
oddForums: make(map[int]map[int]*ForumPerms), oddForums: make(map[int]map[int]*ForumPerms),
}, acc.FirstError() }, acc.FirstError()
} }
func (s *MemoryForumPermsStore) Init() error { func (fps *MemoryForumPermsStore) Init() error {
DebugLog("Initialising the forum perms store") DebugLog("Initialising the forum perms store")
return s.ReloadAll() return fps.ReloadAll()
} }
// TODO: Optimise this? func (fps *MemoryForumPermsStore) ReloadAll() error {
func (s *MemoryForumPermsStore) ReloadAll() error {
DebugLog("Reloading the forum perms") DebugLog("Reloading the forum perms")
fids, e := Forums.GetAllIDs() fids, err := Forums.GetAllIDs()
if e != nil { if err != nil {
return e return err
} }
for _, fid := range fids { for _, fid := range fids {
if e := s.reload(fid); e != nil { err := fps.Reload(fid)
return e if err != nil {
return err
} }
} }
if e := s.recalcCanSeeAll(); e != nil {
return e
}
TopicListThaw.Thaw()
return nil return nil
} }
func (s *MemoryForumPermsStore) parseForumPerm(perms []byte) (pperms *ForumPerms, e error) { func (fps *MemoryForumPermsStore) parseForumPerm(perms []byte) (pperms *ForumPerms, err error) {
DebugDetail("perms: ", string(perms)) DebugDetail("perms: ", string(perms))
pperms = BlankForumPerms() pperms = BlankForumPerms()
e = json.Unmarshal(perms, &pperms) err = json.Unmarshal(perms, &pperms)
pperms.ExtData = make(map[string]bool) pperms.ExtData = make(map[string]bool)
pperms.Overrides = true pperms.Overrides = true
return pperms, e return pperms, err
}
func (s *MemoryForumPermsStore) Reload(fid int) error {
e := s.reload(fid)
if e != nil {
return e
}
if e = s.recalcCanSeeAll(); e != nil {
return e
}
TopicListThaw.Thaw()
return nil
} }
// TODO: Need a more thread-safe way of doing this. Possibly with sync.Map? // TODO: Need a more thread-safe way of doing this. Possibly with sync.Map?
func (s *MemoryForumPermsStore) reload(fid int) error { func (fps *MemoryForumPermsStore) Reload(fid int) error {
DebugLogf("Reloading the forum permissions for forum #%d", fid) DebugLogf("Reloading the forum permissions for forum #%d", fid)
rows, err := s.getByForum.Query(fid) rows, err := fps.getByForum.Query(fid)
if err != nil { if err != nil {
return err return err
} }
defer rows.Close() defer rows.Close()
forumPerms := make(map[int]*ForumPerms) var forumPerms = make(map[int]*ForumPerms)
for rows.Next() { for rows.Next() {
var gid int var gid int
var perms []byte var perms []byte
@ -107,30 +90,23 @@ func (s *MemoryForumPermsStore) reload(fid int) error {
return err return err
} }
DebugLog("gid:", gid) pperms, err := fps.parseForumPerm(perms)
DebugLogf("perms: %+v\n", perms)
pperms, err := s.parseForumPerm(perms)
if err != nil { if err != nil {
return err return err
} }
DebugLogf("pperms: %+v\n", pperms)
forumPerms[gid] = pperms forumPerms[gid] = pperms
} }
DebugLogf("forumPerms: %+v\n", forumPerms) DebugLogf("forumPerms: %+v\n", forumPerms)
if fid%2 == 0 { if fid%2 == 0 {
s.evenLock.Lock() fps.evenLock.Lock()
s.evenForums[fid] = forumPerms fps.evenForums[fid] = forumPerms
s.evenLock.Unlock() fps.evenLock.Unlock()
} else { } else {
s.oddLock.Lock() fps.oddLock.Lock()
s.oddForums[fid] = forumPerms fps.oddForums[fid] = forumPerms
s.oddLock.Unlock() fps.oddLock.Unlock()
}
return nil
} }
func (s *MemoryForumPermsStore) recalcCanSeeAll() error {
groups, err := Groups.GetAll() groups, err := Groups.GetAll()
if err != nil { if err != nil {
return err return err
@ -140,116 +116,96 @@ func (s *MemoryForumPermsStore) recalcCanSeeAll() error {
return err return err
} }
gc, ok := Groups.(GroupCache) for _, group := range groups {
if !ok { DebugLogf("Updating the forum permissions for Group #%d", group.ID)
TopicListThaw.Thaw() group.CanSee = []int{}
return nil
}
// A separate loop to avoid contending on the odd-even locks as much
fForumPerms := make(map[int]map[int]*ForumPerms)
for _, fid := range fids { for _, fid := range fids {
DebugDetailf("Forum #%+v\n", fid)
var forumPerms map[int]*ForumPerms var forumPerms map[int]*ForumPerms
var ok bool var ok bool
if fid%2 == 0 { if fid%2 == 0 {
s.evenLock.RLock() fps.evenLock.RLock()
forumPerms, ok = s.evenForums[fid] forumPerms, ok = fps.evenForums[fid]
s.evenLock.RUnlock() fps.evenLock.RUnlock()
} else { } else {
s.oddLock.RLock() fps.oddLock.RLock()
forumPerms, ok = s.oddForums[fid] forumPerms, ok = fps.oddForums[fid]
s.oddLock.RUnlock() fps.oddLock.RUnlock()
}
if ok {
fForumPerms[fid] = forumPerms
}
} }
// TODO: Can we recalculate CanSee without calculating every other forum? var forumPerm *ForumPerms
for _, g := range groups {
DebugLogf("Updating the forum permissions for Group #%d", g.ID)
canSee := []int{}
for _, fid := range fids {
DebugDetailf("Forum #%+v\n", fid)
forumPerms, ok := fForumPerms[fid]
if !ok { if !ok {
continue continue
} }
fp, ok := forumPerms[g.ID] forumPerm, ok = forumPerms[group.ID]
if !ok { if !ok {
if g.Perms.ViewTopic {
canSee = append(canSee, fid)
}
continue continue
} }
if fp.Overrides { if forumPerm.Overrides {
if fp.ViewTopic { if forumPerm.ViewTopic {
canSee = append(canSee, fid) group.CanSee = append(group.CanSee, fid)
} }
} else if g.Perms.ViewTopic { } else if group.Perms.ViewTopic {
canSee = append(canSee, fid) group.CanSee = append(group.CanSee, fid)
} }
//DebugDetail("g.ID: ", g.ID) DebugDetail("group.ID: ", group.ID)
DebugDetailf("forumPerm: %+v\n", fp) DebugDetailf("forumPerm: %+v\n", forumPerm)
DebugDetail("canSee: ", canSee) DebugDetail("group.CanSee: ", group.CanSee)
} }
DebugDetailf("canSee (length %d): %+v \n", len(canSee), canSee) DebugDetailf("group.CanSee (length %d): %+v \n", len(group.CanSee), group.CanSee)
gc.SetCanSee(g.ID, canSee)
} }
return nil return nil
} }
// ! Throughput on this might be bad due to the excessive locking // ! Throughput on this might be bad due to the excessive locking
func (s *MemoryForumPermsStore) GetAllMap() (bigMap map[int]map[int]*ForumPerms) { func (fps *MemoryForumPermsStore) GetAllMap() (bigMap map[int]map[int]*ForumPerms) {
bigMap = make(map[int]map[int]*ForumPerms) bigMap = make(map[int]map[int]*ForumPerms)
s.evenLock.RLock() fps.evenLock.RLock()
for fid, subMap := range s.evenForums { for fid, subMap := range fps.evenForums {
bigMap[fid] = subMap bigMap[fid] = subMap
} }
s.evenLock.RUnlock() fps.evenLock.RUnlock()
s.oddLock.RLock() fps.oddLock.RLock()
for fid, subMap := range s.oddForums { for fid, subMap := range fps.oddForums {
bigMap[fid] = subMap bigMap[fid] = subMap
} }
s.oddLock.RUnlock() fps.oddLock.RUnlock()
return bigMap return bigMap
} }
// TODO: Add a hook here and have plugin_guilds use it // TODO: Add a hook here and have plugin_guilds use it
// TODO: Check if the forum exists? // TODO: Check if the forum exists?
// TODO: Fix the races // TODO: Fix the races
// TODO: Return BlankForumPerms() when the forum permission set doesn't exist? func (fps *MemoryForumPermsStore) Get(fid int, gid int) (fperms *ForumPerms, err error) {
func (s *MemoryForumPermsStore) Get(fid, gid int) (fp *ForumPerms, err error) {
var fmap map[int]*ForumPerms var fmap map[int]*ForumPerms
var ok bool var ok bool
if fid%2 == 0 { if fid%2 == 0 {
s.evenLock.RLock() fps.evenLock.RLock()
fmap, ok = s.evenForums[fid] fmap, ok = fps.evenForums[fid]
s.evenLock.RUnlock() fps.evenLock.RUnlock()
} else { } else {
s.oddLock.RLock() fps.oddLock.RLock()
fmap, ok = s.oddForums[fid] fmap, ok = fps.oddForums[fid]
s.oddLock.RUnlock() fps.oddLock.RUnlock()
} }
if !ok { if !ok {
return fp, ErrNoRows return fperms, ErrNoRows
} }
fp, ok = fmap[gid] fperms, ok = fmap[gid]
if !ok { if !ok {
return fp, ErrNoRows return fperms, ErrNoRows
} }
return fp, nil return fperms, nil
} }
// TODO: Check if the forum exists? // TODO: Check if the forum exists?
// TODO: Fix the races // TODO: Fix the races
func (s *MemoryForumPermsStore) GetCopy(fid, gid int) (fp ForumPerms, e error) { func (fps *MemoryForumPermsStore) GetCopy(fid int, gid int) (fperms ForumPerms, err error) {
fPermsPtr, e := s.Get(fid, gid) fPermsPtr, err := fps.Get(fid, gid)
if e != nil { if err != nil {
return fp, e return fperms, err
} }
return *fPermsPtr, nil return *fPermsPtr, nil
} }

View File

@ -1,7 +1,7 @@
/* /*
* *
* Gosora Forum Store * Gosora Forum Store
* Copyright Azareal 2017 - 2020 * Copyright Azareal 2017 - 2018
* *
*/ */
package common package common
@ -10,25 +10,20 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"log" "log"
//"fmt"
"sort" "sort"
"sync" "sync"
"sync/atomic" "sync/atomic"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var forumCreateMutex sync.Mutex var forumCreateMutex sync.Mutex
var forumPerms map[int]map[int]*ForumPerms // [gid][fid]*ForumPerms // TODO: Add an abstraction around this and make it more thread-safe var forumPerms map[int]map[int]*ForumPerms // [gid][fid]*ForumPerms // TODO: Add an abstraction around this and make it more thread-safe
var Forums ForumStore var Forums ForumStore
var ErrBlankName = errors.New("The name must not be blank")
var ErrNoDeleteReports = errors.New("You cannot delete the Reports forum")
// ForumStore is an interface for accessing the forums and the metadata stored on them // ForumStore is an interface for accessing the forums and the metadata stored on them
type ForumStore interface { type ForumStore interface {
LoadForums() error LoadForums() error
Each(h func(*Forum) error) error
DirtyGet(id int) *Forum DirtyGet(id int) *Forum
Get(id int) (*Forum, error) Get(id int) (*Forum, error)
BypassGet(id int) (*Forum, error) BypassGet(id int) (*Forum, error)
@ -36,10 +31,9 @@ type ForumStore interface {
Reload(id int) error // ? - Should we move this to ForumCache? It might require us to do some unnecessary casting though Reload(id int) error // ? - Should we move this to ForumCache? It might require us to do some unnecessary casting though
//Update(Forum) error //Update(Forum) error
Delete(id int) error Delete(id int) error
AddTopic(tid, uid, fid int) error AddTopic(tid int, uid int, fid int) error
RemoveTopic(fid int) error RemoveTopic(fid int) error
RemoveTopics(fid, count int) error UpdateLastTopic(tid int, uid int, fid int) error
UpdateLastTopic(tid, uid, fid int) error
Exists(id int) bool Exists(id int) bool
GetAll() ([]*Forum, error) GetAll() ([]*Forum, error)
GetAllIDs() ([]int, error) GetAllIDs() ([]int, error)
@ -47,15 +41,14 @@ type ForumStore interface {
GetAllVisibleIDs() ([]int, error) GetAllVisibleIDs() ([]int, error)
//GetChildren(parentID int, parentType string) ([]*Forum,error) //GetChildren(parentID int, parentType string) ([]*Forum,error)
//GetFirstChild(parentID int, parentType string) (*Forum,error) //GetFirstChild(parentID int, parentType string) (*Forum,error)
Create(name, desc string, active bool, preset string) (int, error) Create(forumName string, forumDesc string, active bool, preset string) (int, error)
UpdateOrder(updateMap map[int]int) error
Count() int GlobalCount() int
} }
type ForumCache interface { type ForumCache interface {
CacheGet(id int) (*Forum, error) CacheGet(id int) (*Forum, error)
CacheSet(f *Forum) error CacheSet(forum *Forum) error
CacheDelete(id int) CacheDelete(id int)
Length() int Length() int
} }
@ -73,190 +66,166 @@ type MemoryForumStore struct {
updateCache *sql.Stmt updateCache *sql.Stmt
addTopics *sql.Stmt addTopics *sql.Stmt
removeTopics *sql.Stmt removeTopics *sql.Stmt
lastTopic *sql.Stmt
updateOrder *sql.Stmt
} }
// NewMemoryForumStore gives you a new instance of MemoryForumStore // NewMemoryForumStore gives you a new instance of MemoryForumStore
func NewMemoryForumStore() (*MemoryForumStore, error) { func NewMemoryForumStore() (*MemoryForumStore, error) {
acc := qgen.NewAcc() acc := qgen.NewAcc()
f := "forums"
set := func(s string) *sql.Stmt {
return acc.Update(f).Set(s).Where("fid=?").Prepare()
}
// TODO: Do a proper delete // TODO: Do a proper delete
return &MemoryForumStore{ return &MemoryForumStore{
get: acc.Select(f).Columns("name, desc, tmpl, active, order, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID").Where("fid=?").Prepare(), get: acc.Select("forums").Columns("name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID").Where("fid = ?").Prepare(),
getAll: acc.Select(f).Columns("fid, name, desc, tmpl, active, order, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID").Orderby("order ASC, fid ASC").Prepare(), getAll: acc.Select("forums").Columns("fid, name, desc, active, preset, parentID, parentType, topicCount, lastTopicID, lastReplyerID").Orderby("fid ASC").Prepare(),
delete: set("name='',active=0"), delete: acc.Update("forums").Set("name= '', active = 0").Where("fid = ?").Prepare(),
create: acc.Insert(f).Columns("name,desc,tmpl,active,preset").Fields("?,?,'',?,?").Prepare(), create: acc.Insert("forums").Columns("name, desc, active, preset").Fields("?,?,?,?").Prepare(),
count: acc.Count(f).Where("name != ''").Prepare(), count: acc.Count("forums").Where("name != ''").Prepare(),
updateCache: set("lastTopicID=?,lastReplyerID=?"), updateCache: acc.Update("forums").Set("lastTopicID = ?, lastReplyerID = ?").Where("fid = ?").Prepare(),
addTopics: set("topicCount=topicCount+?"), addTopics: acc.Update("forums").Set("topicCount = topicCount + ?").Where("fid = ?").Prepare(),
removeTopics: set("topicCount=topicCount-?"), removeTopics: acc.Update("forums").Set("topicCount = topicCount - ?").Where("fid = ?").Prepare(),
lastTopic: acc.Select("topics").Columns("tid").Where("parentID=?").Orderby("lastReplyAt DESC,createdAt DESC").Limit("1").Prepare(),
updateOrder: set("order=?"),
}, acc.FirstError() }, acc.FirstError()
} }
// TODO: Rename to ReloadAll?
// TODO: Add support for subforums // TODO: Add support for subforums
func (s *MemoryForumStore) LoadForums() error { func (mfs *MemoryForumStore) LoadForums() error {
var forumView []*Forum var forumView []*Forum
addForum := func(f *Forum) { addForum := func(forum *Forum) {
s.forums.Store(f.ID, f) mfs.forums.Store(forum.ID, forum)
if f.Active && f.Name != "" && f.ParentType == "" { if forum.Active && forum.Name != "" && forum.ParentType == "" {
forumView = append(forumView, f) forumView = append(forumView, forum)
} }
} }
rows, err := s.getAll.Query() rows, err := mfs.getAll.Query()
if err != nil { if err != nil {
return err return err
} }
defer rows.Close() defer rows.Close()
i := 0 var i = 0
for ; rows.Next(); i++ { for ; rows.Next(); i++ {
f := &Forum{ID: 0, Active: true, Preset: "all"} forum := &Forum{ID: 0, Active: true, Preset: "all"}
err = rows.Scan(&f.ID, &f.Name, &f.Desc, &f.Tmpl, &f.Active, &f.Order, &f.Preset, &f.ParentID, &f.ParentType, &f.TopicCount, &f.LastTopicID, &f.LastReplyerID) err = rows.Scan(&forum.ID, &forum.Name, &forum.Desc, &forum.Active, &forum.Preset, &forum.ParentID, &forum.ParentType, &forum.TopicCount, &forum.LastTopicID, &forum.LastReplyerID)
if err != nil { if err != nil {
return err return err
} }
if f.Name == "" { if forum.Name == "" {
DebugLog("Adding a placeholder forum") DebugLog("Adding a placeholder forum")
} else { } else {
log.Printf("Adding the '%s' forum", f.Name) log.Printf("Adding the '%s' forum", forum.Name)
} }
f.Link = BuildForumURL(NameToSlug(f.Name), f.ID) forum.Link = BuildForumURL(NameToSlug(forum.Name), forum.ID)
f.LastTopic = Topics.DirtyGet(f.LastTopicID) forum.LastTopic = Topics.DirtyGet(forum.LastTopicID)
f.LastReplyer = Users.DirtyGet(f.LastReplyerID) forum.LastReplyer = Users.DirtyGet(forum.LastReplyerID)
// TODO: Create a specialised function with a bit less overhead for getting the last page for a post count
_, _, lastPage := PageOffset(f.LastTopic.PostCount, 1, Config.ItemsPerPage) addForum(forum)
f.LastPage = lastPage
addForum(f)
} }
s.forumView.Store(forumView) mfs.forumView.Store(forumView)
TopicListThaw.Thaw()
return rows.Err() return rows.Err()
} }
// TODO: Hide social groups too // TODO: Hide social groups too
// ? - Will this be hit a lot by plugin_guilds? // ? - Will this be hit a lot by plugin_guilds?
func (s *MemoryForumStore) rebuildView() { func (mfs *MemoryForumStore) rebuildView() {
var forumView []*Forum var forumView []*Forum
s.forums.Range(func(_, val interface{}) bool { mfs.forums.Range(func(_ interface{}, value interface{}) bool {
f := val.(*Forum) forum := value.(*Forum)
// ? - ParentType blank means that it doesn't have a parent // ? - ParentType blank means that it doesn't have a parent
if f.Active && f.Name != "" && f.ParentType == "" { if forum.Active && forum.Name != "" && forum.ParentType == "" {
forumView = append(forumView, f) forumView = append(forumView, forum)
} }
return true return true
}) })
sort.Sort(SortForum(forumView)) sort.Sort(SortForum(forumView))
s.forumView.Store(forumView) mfs.forumView.Store(forumView)
TopicListThaw.Thaw()
} }
func (s *MemoryForumStore) Each(h func(*Forum) error) (err error) { func (mfs *MemoryForumStore) DirtyGet(id int) *Forum {
s.forums.Range(func(_, val interface{}) bool { fint, ok := mfs.forums.Load(id)
err = h(val.(*Forum))
if err != nil {
return false
}
return true
})
return err
}
func (s *MemoryForumStore) DirtyGet(id int) *Forum {
fint, ok := s.forums.Load(id)
if !ok || fint.(*Forum).Name == "" { if !ok || fint.(*Forum).Name == "" {
return &Forum{ID: -1, Name: ""} return &Forum{ID: -1, Name: ""}
} }
return fint.(*Forum) return fint.(*Forum)
} }
func (s *MemoryForumStore) CacheGet(id int) (*Forum, error) { func (mfs *MemoryForumStore) CacheGet(id int) (*Forum, error) {
fint, ok := s.forums.Load(id) fint, ok := mfs.forums.Load(id)
if !ok || fint.(*Forum).Name == "" { if !ok || fint.(*Forum).Name == "" {
return nil, ErrNoRows return nil, ErrNoRows
} }
return fint.(*Forum), nil return fint.(*Forum), nil
} }
func (s *MemoryForumStore) Get(id int) (*Forum, error) { func (mfs *MemoryForumStore) Get(id int) (*Forum, error) {
fint, ok := s.forums.Load(id) fint, ok := mfs.forums.Load(id)
if ok { if !ok || fint.(*Forum).Name == "" {
forum := fint.(*Forum) var forum = &Forum{ID: id}
if forum.Name == "" { err := mfs.get.QueryRow(id).Scan(&forum.Name, &forum.Desc, &forum.Active, &forum.Preset, &forum.ParentID, &forum.ParentType, &forum.TopicCount, &forum.LastTopicID, &forum.LastReplyerID)
return nil, ErrNoRows
}
return forum, nil
}
forum, err := s.BypassGet(id)
if err != nil { if err != nil {
return nil, err
}
s.CacheSet(forum)
return forum, err return forum, err
} }
func (s *MemoryForumStore) BypassGet(id int) (*Forum, error) { forum.Link = BuildForumURL(NameToSlug(forum.Name), forum.ID)
f := &Forum{ID: id} forum.LastTopic = Topics.DirtyGet(forum.LastTopicID)
err := s.get.QueryRow(id).Scan(&f.Name, &f.Desc, &f.Tmpl, &f.Active, &f.Order, &f.Preset, &f.ParentID, &f.ParentType, &f.TopicCount, &f.LastTopicID, &f.LastReplyerID) forum.LastReplyer = Users.DirtyGet(forum.LastReplyerID)
mfs.CacheSet(forum)
return forum, err
}
return fint.(*Forum), nil
}
func (mfs *MemoryForumStore) BypassGet(id int) (*Forum, error) {
var forum = &Forum{ID: id}
err := mfs.get.QueryRow(id).Scan(&forum.Name, &forum.Desc, &forum.Active, &forum.Preset, &forum.ParentID, &forum.ParentType, &forum.TopicCount, &forum.LastTopicID, &forum.LastReplyerID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if f.Name == "" {
return nil, ErrNoRows
}
f.Link = BuildForumURL(NameToSlug(f.Name), f.ID)
f.LastTopic = Topics.DirtyGet(f.LastTopicID)
f.LastReplyer = Users.DirtyGet(f.LastReplyerID)
// TODO: Create a specialised function with a bit less overhead for getting the last page for a post count
_, _, lastPage := PageOffset(f.LastTopic.PostCount, 1, Config.ItemsPerPage)
f.LastPage = lastPage
//TopicListThaw.Thaw()
return f, err forum.Link = BuildForumURL(NameToSlug(forum.Name), forum.ID)
forum.LastTopic = Topics.DirtyGet(forum.LastTopicID)
forum.LastReplyer = Users.DirtyGet(forum.LastReplyerID)
return forum, err
} }
// TODO: Optimise this // TODO: Optimise this
func (s *MemoryForumStore) BulkGetCopy(ids []int) (forums []Forum, err error) { func (mfs *MemoryForumStore) BulkGetCopy(ids []int) (forums []Forum, err error) {
forums = make([]Forum, len(ids)) forums = make([]Forum, len(ids))
for i, id := range ids { for i, id := range ids {
f, err := s.Get(id) forum, err := mfs.Get(id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
forums[i] = f.Copy() forums[i] = forum.Copy()
} }
return forums, nil return forums, nil
} }
func (s *MemoryForumStore) Reload(id int) error { func (mfs *MemoryForumStore) Reload(id int) error {
forum, err := s.BypassGet(id) var forum = &Forum{ID: id}
err := mfs.get.QueryRow(id).Scan(&forum.Name, &forum.Desc, &forum.Active, &forum.Preset, &forum.ParentID, &forum.ParentType, &forum.TopicCount, &forum.LastTopicID, &forum.LastReplyerID)
if err != nil { if err != nil {
return err return err
} }
s.CacheSet(forum) forum.Link = BuildForumURL(NameToSlug(forum.Name), forum.ID)
forum.LastTopic = Topics.DirtyGet(forum.LastTopicID)
forum.LastReplyer = Users.DirtyGet(forum.LastReplyerID)
mfs.CacheSet(forum)
return nil return nil
} }
func (s *MemoryForumStore) CacheSet(f *Forum) error { func (mfs *MemoryForumStore) CacheSet(forum *Forum) error {
s.forums.Store(f.ID, f) mfs.forums.Store(forum.ID, forum)
s.rebuildView() mfs.rebuildView()
return nil return nil
} }
// ! Has a randomised order // ! Has a randomised order
func (s *MemoryForumStore) GetAll() (forumView []*Forum, err error) { func (mfs *MemoryForumStore) GetAll() (forumView []*Forum, err error) {
s.forums.Range(func(_, val interface{}) bool { mfs.forums.Range(func(_ interface{}, value interface{}) bool {
forumView = append(forumView, val.(*Forum)) forumView = append(forumView, value.(*Forum))
return true return true
}) })
sort.Sort(SortForum(forumView)) sort.Sort(SortForum(forumView))
@ -264,23 +233,23 @@ func (s *MemoryForumStore) GetAll() (forumView []*Forum, err error) {
} }
// ? - Can we optimise the sorting? // ? - Can we optimise the sorting?
func (s *MemoryForumStore) GetAllIDs() (ids []int, err error) { func (mfs *MemoryForumStore) GetAllIDs() (ids []int, err error) {
s.forums.Range(func(_, val interface{}) bool { mfs.forums.Range(func(_ interface{}, value interface{}) bool {
ids = append(ids, val.(*Forum).ID) ids = append(ids, value.(*Forum).ID)
return true return true
}) })
sort.Ints(ids) sort.Ints(ids)
return ids, nil return ids, nil
} }
func (s *MemoryForumStore) GetAllVisible() (forumView []*Forum, err error) { func (mfs *MemoryForumStore) GetAllVisible() (forumView []*Forum, err error) {
forumView = s.forumView.Load().([]*Forum) forumView = mfs.forumView.Load().([]*Forum)
return forumView, nil return forumView, nil
} }
func (s *MemoryForumStore) GetAllVisibleIDs() ([]int, error) { func (mfs *MemoryForumStore) GetAllVisibleIDs() ([]int, error) {
forumView := s.forumView.Load().([]*Forum) forumView := mfs.forumView.Load().([]*Forum)
ids := make([]int, len(forumView)) var ids = make([]int, len(forumView))
for i := 0; i < len(forumView); i++ { for i := 0; i < len(forumView); i++ {
ids[i] = forumView[i].ID ids[i] = forumView[i].ID
} }
@ -288,117 +257,73 @@ func (s *MemoryForumStore) GetAllVisibleIDs() ([]int, error) {
} }
// TODO: Implement sub-forums. // TODO: Implement sub-forums.
/*func (s *MemoryForumStore) GetChildren(parentID int, parentType string) ([]*Forum,error) { /*func (mfs *MemoryForumStore) GetChildren(parentID int, parentType string) ([]*Forum,error) {
return nil, nil return nil, nil
} }
func (s *MemoryForumStore) GetFirstChild(parentID int, parentType string) (*Forum,error) { func (mfs *MemoryForumStore) GetFirstChild(parentID int, parentType string) (*Forum,error) {
return nil, nil return nil, nil
}*/ }*/
// TODO: Add a query for this rather than hitting cache // TODO: Add a query for this rather than hitting cache
func (s *MemoryForumStore) Exists(id int) bool { func (mfs *MemoryForumStore) Exists(id int) bool {
forum, ok := s.forums.Load(id) forum, ok := mfs.forums.Load(id)
if !ok { return ok && forum.(*Forum).Name != ""
return false
}
return forum.(*Forum).Name != ""
} }
// TODO: Batch deletions with name blanking? Is this necessary? // TODO: Batch deletions with name blanking? Is this necessary?
func (s *MemoryForumStore) CacheDelete(id int) { func (mfs *MemoryForumStore) CacheDelete(id int) {
s.forums.Delete(id) mfs.forums.Delete(id)
s.rebuildView() mfs.rebuildView()
} }
// TODO: Add a hook to allow plugin_guilds to detect when one of it's forums has just been deleted? // TODO: Add a hook to allow plugin_guilds to detect when one of it's forums has just been deleted?
func (s *MemoryForumStore) Delete(id int) error { func (mfs *MemoryForumStore) Delete(id int) error {
if id == ReportForumID { if id == ReportForumID {
return ErrNoDeleteReports return errors.New("You cannot delete the Reports forum")
} }
_, err := s.delete.Exec(id) _, err := mfs.delete.Exec(id)
s.CacheDelete(id) mfs.CacheDelete(id)
return err return err
} }
func (s *MemoryForumStore) AddTopic(tid, uid, fid int) error { func (mfs *MemoryForumStore) AddTopic(tid int, uid int, fid int) error {
_, err := s.updateCache.Exec(tid, uid, fid) _, err := mfs.updateCache.Exec(tid, uid, fid)
if err != nil { if err != nil {
return err return err
} }
_, err = s.addTopics.Exec(1, fid) _, err = mfs.addTopics.Exec(1, fid)
if err != nil { if err != nil {
return err return err
} }
// TODO: Bypass the database and update this with a lock or an unsafe atomic swap // TODO: Bypass the database and update this with a lock or an unsafe atomic swap
return s.Reload(fid) return mfs.Reload(fid)
} }
func (s *MemoryForumStore) RefreshTopic(fid int) (err error) { // TODO: Update the forum cache with the latest topic
var tid int func (mfs *MemoryForumStore) RemoveTopic(fid int) error {
err = s.lastTopic.QueryRow(fid).Scan(&tid) _, err := mfs.removeTopics.Exec(1, fid)
if err == sql.ErrNoRows {
f, err := s.CacheGet(fid)
if err != nil || f.LastTopicID != 0 {
_, err = s.updateCache.Exec(0, 0, fid)
if err != nil {
return err
}
s.Reload(fid)
}
return nil
}
if err != nil {
return err
}
topic, err := Topics.Get(tid)
if err != nil {
return err
}
_, err = s.updateCache.Exec(tid, topic.CreatedBy, fid)
if err != nil { if err != nil {
return err return err
} }
// TODO: Bypass the database and update this with a lock or an unsafe atomic swap // TODO: Bypass the database and update this with a lock or an unsafe atomic swap
s.Reload(fid) mfs.Reload(fid)
return nil return nil
} }
// TODO: Make this update more atomic
func (s *MemoryForumStore) RemoveTopic(fid int) error {
_, err := s.removeTopics.Exec(1, fid)
if err != nil {
return err
}
return s.RefreshTopic(fid)
}
func (s *MemoryForumStore) RemoveTopics(fid, count int) error {
_, err := s.removeTopics.Exec(count, fid)
if err != nil {
return err
}
return s.RefreshTopic(fid)
}
// DEPRECATED. forum.Update() will be the way to do this in the future, once it's completed // DEPRECATED. forum.Update() will be the way to do this in the future, once it's completed
// TODO: Have a pointer to the last topic rather than storing it on the forum itself // TODO: Have a pointer to the last topic rather than storing it on the forum itself
func (s *MemoryForumStore) UpdateLastTopic(tid, uid, fid int) error { func (mfs *MemoryForumStore) UpdateLastTopic(tid int, uid int, fid int) error {
_, err := s.updateCache.Exec(tid, uid, fid) _, err := mfs.updateCache.Exec(tid, uid, fid)
if err != nil { if err != nil {
return err return err
} }
// TODO: Bypass the database and update this with a lock or an unsafe atomic swap // TODO: Bypass the database and update this with a lock or an unsafe atomic swap
return s.Reload(fid) return mfs.Reload(fid)
} }
func (s *MemoryForumStore) Create(name, desc string, active bool, preset string) (int, error) { func (mfs *MemoryForumStore) Create(forumName string, forumDesc string, active bool, preset string) (int, error) {
if name == "" {
return 0, ErrBlankName
}
forumCreateMutex.Lock() forumCreateMutex.Lock()
defer forumCreateMutex.Unlock() res, err := mfs.create.Exec(forumName, forumDesc, active, preset)
res, err := s.create.Exec(name, desc, active, preset)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -409,44 +334,34 @@ func (s *MemoryForumStore) Create(name, desc string, active bool, preset string)
} }
fid := int(fid64) fid := int(fid64)
err = s.Reload(fid) err = mfs.Reload(fid)
if err != nil { if err != nil {
return 0, err return 0, err
} }
PermmapToQuery(PresetToPermmap(preset), fid) PermmapToQuery(PresetToPermmap(preset), fid)
forumCreateMutex.Unlock()
return fid, nil return fid, nil
} }
// TODO: Make this atomic, maybe with a transaction?
func (s *MemoryForumStore) UpdateOrder(updateMap map[int]int) error {
for fid, order := range updateMap {
_, err := s.updateOrder.Exec(order, fid)
if err != nil {
return err
}
}
return s.LoadForums()
}
// ! Might be slightly inaccurate, if the sync.Map is constantly shifting and churning, but it'll stabilise eventually. Also, slow. Don't use this on every request x.x // ! Might be slightly inaccurate, if the sync.Map is constantly shifting and churning, but it'll stabilise eventually. Also, slow. Don't use this on every request x.x
// Length returns the number of forums in the memory cache // Length returns the number of forums in the memory cache
func (s *MemoryForumStore) Length() (len int) { func (mfs *MemoryForumStore) Length() (length int) {
s.forums.Range(func(_, _ interface{}) bool { mfs.forums.Range(func(_ interface{}, value interface{}) bool {
len++ length++
return true return true
}) })
return len return length
} }
// TODO: Get the total count of forums in the forum store rather than doing a heavy query for this? // TODO: Get the total count of forums in the forum store rather than doing a heavy query for this?
// Count returns the total number of forums // GlobalCount returns the total number of forums
func (s *MemoryForumStore) Count() (count int) { func (mfs *MemoryForumStore) GlobalCount() (fcount int) {
err := s.count.QueryRow().Scan(&count) err := mfs.count.QueryRow().Scan(&fcount)
if err != nil { if err != nil {
LogError(err) LogError(err)
} }
return count return fcount
} }
// TODO: Work on SqlForumStore // TODO: Work on SqlForumStore

View File

@ -1,11 +1,7 @@
package common package common
import ( import "database/sql"
"database/sql" import "../query_gen/lib"
"encoding/json"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var blankGroup = Group{ID: 0, Name: ""} var blankGroup = Group{ID: 0, Name: ""}
@ -35,67 +31,33 @@ type Group struct {
} }
type GroupStmts struct { type GroupStmts struct {
updateGroup *sql.Stmt
updateGroupRank *sql.Stmt updateGroupRank *sql.Stmt
updateGroupPerms *sql.Stmt
} }
var groupStmts GroupStmts var groupStmts GroupStmts
func init() { func init() {
DbInits.Add(func(acc *qgen.Accumulator) error { DbInits.Add(func(acc *qgen.Accumulator) error {
set := func(s string) *sql.Stmt {
return acc.Update("users_groups").Set(s).Where("gid=?").Prepare()
}
groupStmts = GroupStmts{ groupStmts = GroupStmts{
updateGroup: set("name=?,tag=?"), updateGroupRank: acc.Update("users_groups").Set("is_admin = ?, is_mod = ?, is_banned = ?").Where("gid = ?").Prepare(),
updateGroupRank: set("is_admin=?,is_mod=?,is_banned=?"),
updateGroupPerms: set("permissions=?"),
} }
return acc.FirstError() return acc.FirstError()
}) })
} }
func (g *Group) ChangeRank(isAdmin, isMod, isBanned bool) (err error) { func (group *Group) ChangeRank(isAdmin bool, isMod bool, isBanned bool) (err error) {
_, err = groupStmts.updateGroupRank.Exec(isAdmin, isMod, isBanned, g.ID) _, err = groupStmts.updateGroupRank.Exec(isAdmin, isMod, isBanned, group.ID)
if err != nil { if err != nil {
return err return err
} }
_ = Groups.Reload(g.ID)
return nil
}
func (g *Group) Update(name, tag string) (err error) { Groups.Reload(group.ID)
_, err = groupStmts.updateGroup.Exec(name, tag, g.ID)
if err != nil {
return err
}
_ = Groups.Reload(g.ID)
return nil return nil
} }
// Please don't pass arbitrary inputs to this method
func (g *Group) UpdatePerms(perms map[string]bool) (err error) {
pjson, err := json.Marshal(perms)
if err != nil {
return err
}
_, err = groupStmts.updateGroupPerms.Exec(pjson, g.ID)
if err != nil {
return err
}
return Groups.Reload(g.ID)
}
// Copy gives you a non-pointer concurrency safe copy of the group // Copy gives you a non-pointer concurrency safe copy of the group
func (g *Group) Copy() Group { func (group *Group) Copy() Group {
return *g return *group
}
func (g *Group) CopyPtr() (co *Group) {
co = new(Group)
*co = *g
return co
} }
// TODO: Replace this sorting mechanism with something a lot more efficient // TODO: Replace this sorting mechanism with something a lot more efficient

View File

@ -7,10 +7,9 @@ import (
"errors" "errors"
"log" "log"
"sort" "sort"
"strconv"
"sync" "sync"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var Groups GroupStore var Groups GroupStore
@ -22,17 +21,15 @@ type GroupStore interface {
Get(id int) (*Group, error) Get(id int) (*Group, error)
GetCopy(id int) (Group, error) GetCopy(id int) (Group, error)
Exists(id int) bool Exists(id int) bool
Create(name, tag string, isAdmin, isMod, isBanned bool) (id int, err error) Create(name string, tag string, isAdmin bool, isMod bool, isBanned bool) (int, error)
GetAll() ([]*Group, error) GetAll() ([]*Group, error)
GetRange(lower, higher int) ([]*Group, error) GetRange(lower int, higher int) ([]*Group, error)
Reload(id int) error // ? - Should we move this to GroupCache? It might require us to do some unnecessary casting though Reload(id int) error // ? - Should we move this to GroupCache? It might require us to do some unnecessary casting though
Count() int GlobalCount() int
} }
type GroupCache interface { type GroupCache interface {
CacheSet(g *Group) error CacheSet(group *Group) error
SetCanSee(gid int, canSee []int) error
CacheAdd(g *Group) error
Length() int Length() int
} }
@ -49,24 +46,23 @@ type MemoryGroupStore struct {
func NewMemoryGroupStore() (*MemoryGroupStore, error) { func NewMemoryGroupStore() (*MemoryGroupStore, error) {
acc := qgen.NewAcc() acc := qgen.NewAcc()
ug := "users_groups"
return &MemoryGroupStore{ return &MemoryGroupStore{
groups: make(map[int]*Group), groups: make(map[int]*Group),
groupCount: 0, groupCount: 0,
getAll: acc.Select(ug).Columns("gid,name,permissions,plugin_perms,is_mod,is_admin,is_banned,tag").Prepare(), getAll: acc.Select("users_groups").Columns("gid, name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag").Prepare(),
get: acc.Select(ug).Columns("name,permissions,plugin_perms,is_mod,is_admin,is_banned,tag").Where("gid=?").Prepare(), get: acc.Select("users_groups").Columns("name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag").Where("gid = ?").Prepare(),
count: acc.Count(ug).Prepare(), count: acc.Count("users_groups").Prepare(),
userCount: acc.Count("users").Where("group = ?").Prepare(), userCount: acc.Count("users").Where("group = ?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
// TODO: Move this query from the global stmt store into this store // TODO: Move this query from the global stmt store into this store
func (s *MemoryGroupStore) LoadGroups() error { func (mgs *MemoryGroupStore) LoadGroups() error {
s.Lock() mgs.Lock()
defer s.Unlock() defer mgs.Unlock()
s.groups[0] = &Group{ID: 0, Name: "Unknown"} mgs.groups[0] = &Group{ID: 0, Name: "Unknown"}
rows, err := s.getAll.Query() rows, err := mgs.getAll.Query()
if err != nil { if err != nil {
return err return err
} }
@ -74,32 +70,32 @@ func (s *MemoryGroupStore) LoadGroups() error {
i := 1 i := 1
for ; rows.Next(); i++ { for ; rows.Next(); i++ {
g := &Group{ID: 0} group := &Group{ID: 0}
err := rows.Scan(&g.ID, &g.Name, &g.PermissionsText, &g.PluginPermsText, &g.IsMod, &g.IsAdmin, &g.IsBanned, &g.Tag) err := rows.Scan(&group.ID, &group.Name, &group.PermissionsText, &group.PluginPermsText, &group.IsMod, &group.IsAdmin, &group.IsBanned, &group.Tag)
if err != nil { if err != nil {
return err return err
} }
err = s.initGroup(g) err = mgs.initGroup(group)
if err != nil { if err != nil {
return err return err
} }
s.groups[g.ID] = g mgs.groups[group.ID] = group
} }
if err = rows.Err(); err != nil { err = rows.Err()
if err != nil {
return err return err
} }
s.groupCount = i mgs.groupCount = i
DebugLog("Binding the Not Loggedin Group") DebugLog("Binding the Not Loggedin Group")
GuestPerms = s.dirtyGetUnsafe(6).Perms // ! Race? GuestPerms = mgs.dirtyGetUnsafe(6).Perms
TopicListThaw.Thaw()
return nil return nil
} }
// TODO: Hit the database when the item isn't in memory // TODO: Hit the database when the item isn't in memory
func (s *MemoryGroupStore) dirtyGetUnsafe(id int) *Group { func (mgs *MemoryGroupStore) dirtyGetUnsafe(gid int) *Group {
group, ok := s.groups[id] group, ok := mgs.groups[gid]
if !ok { if !ok {
return &blankGroup return &blankGroup
} }
@ -107,10 +103,10 @@ func (s *MemoryGroupStore) dirtyGetUnsafe(id int) *Group {
} }
// TODO: Hit the database when the item isn't in memory // TODO: Hit the database when the item isn't in memory
func (s *MemoryGroupStore) DirtyGet(id int) *Group { func (mgs *MemoryGroupStore) DirtyGet(gid int) *Group {
s.RLock() mgs.RLock()
group, ok := s.groups[id] group, ok := mgs.groups[gid]
s.RUnlock() mgs.RUnlock()
if !ok { if !ok {
return &blankGroup return &blankGroup
} }
@ -118,10 +114,10 @@ func (s *MemoryGroupStore) DirtyGet(id int) *Group {
} }
// TODO: Hit the database when the item isn't in memory // TODO: Hit the database when the item isn't in memory
func (s *MemoryGroupStore) Get(id int) (*Group, error) { func (mgs *MemoryGroupStore) Get(gid int) (*Group, error) {
s.RLock() mgs.RLock()
group, ok := s.groups[id] group, ok := mgs.groups[gid]
s.RUnlock() mgs.RUnlock()
if !ok { if !ok {
return nil, ErrNoRows return nil, ErrNoRows
} }
@ -129,104 +125,85 @@ func (s *MemoryGroupStore) Get(id int) (*Group, error) {
} }
// TODO: Hit the database when the item isn't in memory // TODO: Hit the database when the item isn't in memory
func (s *MemoryGroupStore) GetCopy(id int) (Group, error) { func (mgs *MemoryGroupStore) GetCopy(gid int) (Group, error) {
s.RLock() mgs.RLock()
group, ok := s.groups[id] group, ok := mgs.groups[gid]
s.RUnlock() mgs.RUnlock()
if !ok { if !ok {
return blankGroup, ErrNoRows return blankGroup, ErrNoRows
} }
return *group, nil return *group, nil
} }
func (s *MemoryGroupStore) Reload(id int) error { func (mgs *MemoryGroupStore) Reload(id int) error {
// TODO: Reload this data too var group = &Group{ID: id}
g, e := s.Get(id) err := mgs.get.QueryRow(id).Scan(&group.Name, &group.PermissionsText, &group.PluginPermsText, &group.IsMod, &group.IsAdmin, &group.IsBanned, &group.Tag)
if e != nil { if err != nil {
LogError(errors.New("can't get cansee data for group #" + strconv.Itoa(id))) return err
return nil
} }
canSee := g.CanSee
g = &Group{ID: id, CanSee: canSee} err = mgs.initGroup(group)
e = s.get.QueryRow(id).Scan(&g.Name, &g.PermissionsText, &g.PluginPermsText, &g.IsMod, &g.IsAdmin, &g.IsBanned, &g.Tag) if err != nil {
if e != nil { LogError(err)
return e }
mgs.CacheSet(group)
err = RebuildGroupPermissions(id)
if err != nil {
LogError(err)
} }
if e = s.initGroup(g); e != nil {
LogError(e)
return nil return nil
} }
s.CacheSet(g) func (mgs *MemoryGroupStore) initGroup(group *Group) error {
TopicListThaw.Thaw() err := json.Unmarshal(group.PermissionsText, &group.Perms)
return nil if err != nil {
log.Printf("group: %+v\n", group)
log.Print("bad group perms: ", group.PermissionsText)
return err
} }
DebugLogf(group.Name+": %+v\n", group.Perms)
func (s *MemoryGroupStore) initGroup(g *Group) error { err = json.Unmarshal(group.PluginPermsText, &group.PluginPerms)
e := json.Unmarshal(g.PermissionsText, &g.Perms) if err != nil {
if e != nil { log.Printf("group: %+v\n", group)
log.Printf("g: %+v\n", g) log.Print("bad group plugin perms: ", group.PluginPermsText)
log.Print("bad group perms: ", g.PermissionsText) return err
return e
} }
DebugLogf(g.Name+": %+v\n", g.Perms) DebugLogf(group.Name+": %+v\n", group.PluginPerms)
e = json.Unmarshal(g.PluginPermsText, &g.PluginPerms)
if e != nil {
log.Printf("g: %+v\n", g)
log.Print("bad group plugin perms: ", g.PluginPermsText)
return e
}
DebugLogf(g.Name+": %+v\n", g.PluginPerms)
//group.Perms.ExtData = make(map[string]bool) //group.Perms.ExtData = make(map[string]bool)
// TODO: Can we optimise the bit where this cascades down to the user now? // TODO: Can we optimise the bit where this cascades down to the user now?
if g.IsAdmin || g.IsMod { if group.IsAdmin || group.IsMod {
g.IsBanned = false group.IsBanned = false
} }
e = s.userCount.QueryRow(g.ID).Scan(&g.UserCount) err = mgs.userCount.QueryRow(group.ID).Scan(&group.UserCount)
if e != sql.ErrNoRows { if err != sql.ErrNoRows {
return e return err
} }
return nil return nil
} }
func (s *MemoryGroupStore) SetCanSee(gid int, canSee []int) error { func (mgs *MemoryGroupStore) CacheSet(group *Group) error {
s.Lock() mgs.Lock()
group, ok := s.groups[gid] mgs.groups[group.ID] = group
if !ok { mgs.Unlock()
s.Unlock()
return nil
}
ngroup := &Group{}
*ngroup = *group
ngroup.CanSee = canSee
s.groups[group.ID] = ngroup
s.Unlock()
return nil
}
func (s *MemoryGroupStore) CacheSet(g *Group) error {
s.Lock()
s.groups[g.ID] = g
s.Unlock()
return nil return nil
} }
// TODO: Hit the database when the item isn't in memory // TODO: Hit the database when the item isn't in memory
func (s *MemoryGroupStore) Exists(id int) bool { func (mgs *MemoryGroupStore) Exists(gid int) bool {
s.RLock() mgs.RLock()
group, ok := s.groups[id] group, ok := mgs.groups[gid]
s.RUnlock() mgs.RUnlock()
return ok && group.Name != "" return ok && group.Name != ""
} }
// ? Allow two groups with the same name? // ? Allow two groups with the same name?
// TODO: Refactor this // TODO: Refactor this
func (s *MemoryGroupStore) Create(name, tag string, isAdmin, isMod, isBanned bool) (gid int, err error) { func (mgs *MemoryGroupStore) Create(name string, tag string, isAdmin bool, isMod bool, isBanned bool) (gid int, err error) {
permstr := "{}" var permstr = "{}"
tx, err := qgen.Builder.Begin() tx, err := qgen.Builder.Begin()
if err != nil { if err != nil {
return 0, err return 0, err
@ -241,27 +218,28 @@ func (s *MemoryGroupStore) Create(name, tag string, isAdmin, isMod, isBanned boo
if err != nil { if err != nil {
return 0, err return 0, err
} }
gid64, err := res.LastInsertId() gid64, err := res.LastInsertId()
if err != nil { if err != nil {
return 0, err return 0, err
} }
gid = int(gid64) gid = int(gid64)
perms := BlankPerms var perms = BlankPerms
blankIntList := []int{} var blankIntList []int
pluginPerms := make(map[string]bool) var pluginPerms = make(map[string]bool)
pluginPermsBytes := []byte("{}") var pluginPermsBytes = []byte("{}")
GetHookTable().Vhook("create_group_preappend", &pluginPerms, &pluginPermsBytes) RunVhook("create_group_preappend", &pluginPerms, &pluginPermsBytes)
// Generate the forum permissions based on the presets... // Generate the forum permissions based on the presets...
forums, err := Forums.GetAll() fdata, err := Forums.GetAll()
if err != nil { if err != nil {
return 0, err return 0, err
} }
presetSet := make(map[int]string) var presetSet = make(map[int]string)
permSet := make(map[int]*ForumPerms) var permSet = make(map[int]*ForumPerms)
for _, f := range forums { for _, forum := range fdata {
var thePreset string var thePreset string
switch { switch {
case isAdmin: case isAdmin:
@ -274,18 +252,19 @@ func (s *MemoryGroupStore) Create(name, tag string, isAdmin, isMod, isBanned boo
thePreset = "members" thePreset = "members"
} }
permmap := PresetToPermmap(f.Preset) permmap := PresetToPermmap(forum.Preset)
permItem := permmap[thePreset] permItem := permmap[thePreset]
permItem.Overrides = true permItem.Overrides = true
permSet[f.ID] = permItem permSet[forum.ID] = permItem
presetSet[f.ID] = f.Preset presetSet[forum.ID] = forum.Preset
} }
err = ReplaceForumPermsForGroupTx(tx, gid, presetSet, permSet) err = ReplaceForumPermsForGroupTx(tx, gid, presetSet, permSet)
if err != nil { if err != nil {
return 0, err return 0, err
} }
err = tx.Commit() err = tx.Commit()
if err != nil { if err != nil {
return 0, err return 0, err
@ -296,45 +275,47 @@ func (s *MemoryGroupStore) Create(name, tag string, isAdmin, isMod, isBanned boo
isBanned = false isBanned = false
} }
s.CacheAdd(&Group{gid, name, isMod, isAdmin, isBanned, tag, perms, []byte(permstr), pluginPerms, pluginPermsBytes, blankIntList, 0}) mgs.Lock()
mgs.groups[gid] = &Group{gid, name, isMod, isAdmin, isBanned, tag, perms, []byte(permstr), pluginPerms, pluginPermsBytes, blankIntList, 0}
mgs.groupCount++
mgs.Unlock()
TopicListThaw.Thaw() err = FPStore.ReloadAll()
return gid, FPStore.ReloadAll() if err != nil {
//return gid, TopicList.RebuildPermTree() return gid, err
}
err = TopicList.RebuildPermTree()
if err != nil {
return gid, err
} }
func (s *MemoryGroupStore) CacheAdd(g *Group) error { return gid, nil
s.Lock()
s.groups[g.ID] = g
s.groupCount++
s.Unlock()
return nil
} }
func (s *MemoryGroupStore) GetAll() (results []*Group, err error) { func (mgs *MemoryGroupStore) GetAll() (results []*Group, err error) {
var i int var i int
s.RLock() mgs.RLock()
results = make([]*Group, len(s.groups)) results = make([]*Group, len(mgs.groups))
for _, group := range s.groups { for _, group := range mgs.groups {
results[i] = group results[i] = group
i++ i++
} }
s.RUnlock() mgs.RUnlock()
sort.Sort(SortGroup(results)) sort.Sort(SortGroup(results))
return results, nil return results, nil
} }
func (s *MemoryGroupStore) GetAllMap() (map[int]*Group, error) { func (mgs *MemoryGroupStore) GetAllMap() (map[int]*Group, error) {
s.RLock() mgs.RLock()
defer s.RUnlock() defer mgs.RUnlock()
return s.groups, nil return mgs.groups, nil
} }
// ? - Set the lower and higher numbers to 0 to remove the bounds // ? - Set the lower and higher numbers to 0 to remove the bounds
// TODO: Might be a little slow right now, maybe we can cache the groups in a slice or break the map up into chunks // TODO: Might be a little slow right now, maybe we can cache the groups in a slice or break the map up into chunks
func (s *MemoryGroupStore) GetRange(lower, higher int) (groups []*Group, err error) { func (mgs *MemoryGroupStore) GetRange(lower int, higher int) (groups []*Group, err error) {
if lower == 0 && higher == 0 { if lower == 0 && higher == 0 {
return s.GetAll() return mgs.GetAll()
} }
// TODO: Simplify these four conditionals into two // TODO: Simplify these four conditionals into two
@ -348,26 +329,26 @@ func (s *MemoryGroupStore) GetRange(lower, higher int) (groups []*Group, err err
} }
} }
s.RLock() mgs.RLock()
for gid, group := range s.groups { for gid, group := range mgs.groups {
if gid >= lower && (gid <= higher || higher == 0) { if gid >= lower && (gid <= higher || higher == 0) {
groups = append(groups, group) groups = append(groups, group)
} }
} }
s.RUnlock() mgs.RUnlock()
sort.Sort(SortGroup(groups)) sort.Sort(SortGroup(groups))
return groups, nil return groups, nil
} }
func (s *MemoryGroupStore) Length() int { func (mgs *MemoryGroupStore) Length() int {
s.RLock() mgs.RLock()
defer s.RUnlock() defer mgs.RUnlock()
return s.groupCount return mgs.groupCount
} }
func (s *MemoryGroupStore) Count() (count int) { func (mgs *MemoryGroupStore) GlobalCount() (count int) {
err := s.count.QueryRow().Scan(&count) err := mgs.count.QueryRow().Scan(&count)
if err != nil { if err != nil {
LogError(err) LogError(err)
} }

View File

@ -3,13 +3,13 @@ package common
import ( import (
"database/sql" "database/sql"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var IPSearch IPSearcher var IPSearch IPSearcher
type IPSearcher interface { type IPSearcher interface {
Lookup(ip string) (uids []int, e error) Lookup(ip string) (uids []int, err error)
} }
type DefaultIPSearcher struct { type DefaultIPSearcher struct {
@ -22,54 +22,50 @@ type DefaultIPSearcher struct {
// NewDefaultIPSearcher gives you a new instance of DefaultIPSearcher // NewDefaultIPSearcher gives you a new instance of DefaultIPSearcher
func NewDefaultIPSearcher() (*DefaultIPSearcher, error) { func NewDefaultIPSearcher() (*DefaultIPSearcher, error) {
acc := qgen.NewAcc() acc := qgen.NewAcc()
uu := "users"
q := func(tbl string) *sql.Stmt {
return acc.Select(uu).Columns("uid").InQ("uid", acc.Select(tbl).Columns("createdBy").Where("ip=?")).Prepare()
}
return &DefaultIPSearcher{ return &DefaultIPSearcher{
searchUsers: acc.Select(uu).Columns("uid").Where("last_ip=? OR last_ip LIKE CONCAT('%-',?)").Prepare(), searchUsers: acc.Select("users").Columns("uid").Where("last_ip = ?").Prepare(),
searchTopics: q("topics"), searchTopics: acc.Select("users").Columns("uid").InQ("uid", acc.Select("topics").Columns("createdBy").Where("ipaddress = ?")).Prepare(),
searchReplies: q("replies"), searchReplies: acc.Select("users").Columns("uid").InQ("uid", acc.Select("replies").Columns("createdBy").Where("ipaddress = ?")).Prepare(),
searchUsersReplies: q("users_replies"), searchUsersReplies: acc.Select("users").Columns("uid").InQ("uid", acc.Select("users_replies").Columns("createdBy").Where("ipaddress = ?")).Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
func (s *DefaultIPSearcher) Lookup(ip string) (uids []int, e error) { func (searcher *DefaultIPSearcher) Lookup(ip string) (uids []int, err error) {
var uid int var uid int
reqUserList := make(map[int]bool) var reqUserList = make(map[int]bool)
runQuery2 := func(rows *sql.Rows, e error) error {
if e != nil { var runQuery = func(stmt *sql.Stmt) error {
return e rows, err := stmt.Query(ip)
if err != nil {
return err
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
if e := rows.Scan(&uid); e != nil { err := rows.Scan(&uid)
return e if err != nil {
return err
} }
reqUserList[uid] = true reqUserList[uid] = true
} }
return rows.Err() return rows.Err()
} }
runQuery := func(stmt *sql.Stmt) error {
return runQuery2(stmt.Query(ip))
}
e = runQuery2(s.searchUsers.Query(ip, ip)) err = runQuery(searcher.searchUsers)
if e != nil { if err != nil {
return uids, e return uids, err
} }
e = runQuery(s.searchTopics) err = runQuery(searcher.searchTopics)
if e != nil { if err != nil {
return uids, e return uids, err
} }
e = runQuery(s.searchReplies) err = runQuery(searcher.searchReplies)
if e != nil { if err != nil {
return uids, e return uids, err
} }
e = runQuery(s.searchUsersReplies) err = runQuery(searcher.searchUsersReplies)
if e != nil { if err != nil {
return uids, e return uids, err
} }
// Convert the user ID map to a slice, then bulk load the users // Convert the user ID map to a slice, then bulk load the users

View File

@ -1,103 +0,0 @@
package common
import (
"database/sql"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var Likes LikeStore
type LikeStore interface {
BulkExists(ids []int, sentBy int, targetType string) ([]int, error)
BulkExistsFunc(ids []int, sentBy int, targetType string, f func(int) error) error
Delete(targetID int, targetType string) error
Count() (count int)
}
type DefaultLikeStore struct {
count *sql.Stmt
delete *sql.Stmt
singleExists *sql.Stmt
}
func NewDefaultLikeStore(acc *qgen.Accumulator) (*DefaultLikeStore, error) {
return &DefaultLikeStore{
count: acc.Count("likes").Prepare(),
delete: acc.Delete("likes").Where("targetItem=? AND targetType=?").Prepare(),
singleExists: acc.Select("likes").Columns("targetItem").Where("sentBy=? AND targetType=? AND targetItem=?").Prepare(),
}, acc.FirstError()
}
// TODO: Write a test for this
func (s *DefaultLikeStore) BulkExists(ids []int, sentBy int, targetType string) (eids []int, e error) {
if len(ids) == 0 {
return nil, nil
}
var rows *sql.Rows
if len(ids) == 1 {
rows, e = s.singleExists.Query(sentBy, targetType, ids[0])
} else {
rows, e = qgen.NewAcc().Select("likes").Columns("targetItem").Where("sentBy=? AND targetType=?").In("targetItem", ids).Query(sentBy, targetType)
}
if e == sql.ErrNoRows {
return nil, nil
} else if e != nil {
return nil, e
}
defer rows.Close()
var id int
for rows.Next() {
if e := rows.Scan(&id); e != nil {
return nil, e
}
eids = append(eids, id)
}
return eids, rows.Err()
}
// TODO: Write a test for this
func (s *DefaultLikeStore) BulkExistsFunc(ids []int, sentBy int, targetType string, f func(id int) error) (e error) {
if len(ids) == 0 {
return nil
}
var rows *sql.Rows
if len(ids) == 1 {
rows, e = s.singleExists.Query(sentBy, targetType, ids[0])
} else {
rows, e = qgen.NewAcc().Select("likes").Columns("targetItem").Where("sentBy=? AND targetType=?").In("targetItem", ids).Query(sentBy, targetType)
}
if e == sql.ErrNoRows {
return nil
} else if e != nil {
return e
}
defer rows.Close()
var id int
for rows.Next() {
if e := rows.Scan(&id); e != nil {
return e
}
if e := f(id); e != nil {
return e
}
}
return rows.Err()
}
func (s *DefaultLikeStore) Delete(targetID int, targetType string) error {
_, err := s.delete.Exec(targetID, targetType)
return err
}
// TODO: Write a test for this
// Count returns the total number of likes globally
func (s *DefaultLikeStore) Count() (count int) {
e := s.count.QueryRow().Scan(&count)
if e != nil {
LogError(e)
}
return count
}

View File

@ -4,7 +4,7 @@ import "sync"
type DefaultMenuItemStore struct { type DefaultMenuItemStore struct {
items map[int]MenuItem items map[int]MenuItem
lock sync.RWMutex itemLock sync.RWMutex
} }
func NewDefaultMenuItemStore() *DefaultMenuItemStore { func NewDefaultMenuItemStore() *DefaultMenuItemStore {
@ -13,16 +13,16 @@ func NewDefaultMenuItemStore() *DefaultMenuItemStore {
} }
} }
func (s *DefaultMenuItemStore) Add(i MenuItem) { func (store *DefaultMenuItemStore) Add(item MenuItem) {
s.lock.Lock() store.itemLock.Lock()
defer s.lock.Unlock() defer store.itemLock.Unlock()
s.items[i.ID] = i store.items[item.ID] = item
} }
func (s *DefaultMenuItemStore) Get(id int) (MenuItem, error) { func (store *DefaultMenuItemStore) Get(id int) (MenuItem, error) {
s.lock.RLock() store.itemLock.RLock()
item, ok := s.items[id] item, ok := store.items[id]
s.lock.RUnlock() store.itemLock.RUnlock()
if ok { if ok {
return item, nil return item, nil
} }

View File

@ -5,7 +5,7 @@ import (
"strconv" "strconv"
"sync/atomic" "sync/atomic"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var Menus *DefaultMenuStore var Menus *DefaultMenuStore
@ -23,38 +23,38 @@ func NewDefaultMenuStore() *DefaultMenuStore {
} }
// TODO: Add actual support for multiple menus // TODO: Add actual support for multiple menus
func (s *DefaultMenuStore) GetAllMap() (out map[int]*MenuListHolder) { func (store *DefaultMenuStore) GetAllMap() (out map[int]*MenuListHolder) {
out = make(map[int]*MenuListHolder) out = make(map[int]*MenuListHolder)
for mid, atom := range s.menus { for mid, atom := range store.menus {
out[mid] = atom.Load().(*MenuListHolder) out[mid] = atom.Load().(*MenuListHolder)
} }
return out return out
} }
func (s *DefaultMenuStore) Get(mid int) (*MenuListHolder, error) { func (store *DefaultMenuStore) Get(mid int) (*MenuListHolder, error) {
aStore, ok := s.menus[mid] aStore, ok := store.menus[mid]
if ok { if ok {
return aStore.Load().(*MenuListHolder), nil return aStore.Load().(*MenuListHolder), nil
} }
return nil, ErrNoRows return nil, ErrNoRows
} }
func (s *DefaultMenuStore) Items(mid int) (mlist MenuItemList, err error) { func (store *DefaultMenuStore) Items(mid int) (mlist MenuItemList, err error) {
err = qgen.NewAcc().Select("menu_items").Columns("miid, name, htmlID, cssClass, position, path, aria, tooltip, order, tmplName, guestOnly, memberOnly, staffOnly, adminOnly").Where("mid = " + strconv.Itoa(mid)).Orderby("order ASC").Each(func(rows *sql.Rows) error { err = qgen.NewAcc().Select("menu_items").Columns("miid, name, htmlID, cssClass, position, path, aria, tooltip, order, tmplName, guestOnly, memberOnly, staffOnly, adminOnly").Where("mid = " + strconv.Itoa(mid)).Orderby("order ASC").Each(func(rows *sql.Rows) error {
i := MenuItem{MenuID: mid} var mitem = MenuItem{MenuID: mid}
err := rows.Scan(&i.ID, &i.Name, &i.HTMLID, &i.CSSClass, &i.Position, &i.Path, &i.Aria, &i.Tooltip, &i.Order, &i.TmplName, &i.GuestOnly, &i.MemberOnly, &i.SuperModOnly, &i.AdminOnly) err := rows.Scan(&mitem.ID, &mitem.Name, &mitem.HTMLID, &mitem.CSSClass, &mitem.Position, &mitem.Path, &mitem.Aria, &mitem.Tooltip, &mitem.Order, &mitem.TmplName, &mitem.GuestOnly, &mitem.MemberOnly, &mitem.SuperModOnly, &mitem.AdminOnly)
if err != nil { if err != nil {
return err return err
} }
s.itemStore.Add(i) store.itemStore.Add(mitem)
mlist = append(mlist, i) mlist = append(mlist, mitem)
return nil return nil
}) })
return mlist, err return mlist, err
} }
func (s *DefaultMenuStore) Load(mid int) error { func (store *DefaultMenuStore) Load(mid int) error {
mlist, err := s.Items(mid) mlist, err := store.Items(mid)
if err != nil { if err != nil {
return err return err
} }
@ -64,12 +64,12 @@ func (s *DefaultMenuStore) Load(mid int) error {
return err return err
} }
aStore := &atomic.Value{} var aStore = &atomic.Value{}
aStore.Store(hold) aStore.Store(hold)
s.menus[mid] = aStore store.menus[mid] = aStore
return nil return nil
} }
func (s *DefaultMenuStore) ItemStore() *DefaultMenuItemStore { func (store *DefaultMenuStore) ItemStore() *DefaultMenuItemStore {
return s.itemStore return store.itemStore
} }

View File

@ -7,11 +7,8 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"strings"
"git.tuxpa.in/a/gosora/common/phrases" "../query_gen/lib"
tmpl "git.tuxpa.in/a/gosora/common/templates"
qgen "git.tuxpa.in/a/gosora/query_gen"
) )
type MenuItemList []MenuItem type MenuItemList []MenuItem
@ -22,15 +19,9 @@ type MenuListHolder struct {
Variations map[int]menuTmpl // 0 = Guest Menu, 1 = Member Menu, 2 = Super Mod Menu, 3 = Admin Menu Variations map[int]menuTmpl // 0 = Guest Menu, 1 = Member Menu, 2 = Super Mod Menu, 3 = Admin Menu
} }
type menuPath struct {
Path string
Index int
}
type menuTmpl struct { type menuTmpl struct {
RenderBuffer [][]byte RenderBuffer [][]byte
VariableIndices []int VariableIndices []int
PathMappings []menuPath
} }
type MenuItem struct { type MenuItem struct {
@ -65,105 +56,107 @@ var menuItemStmts MenuItemStmts
func init() { func init() {
DbInits.Add(func(acc *qgen.Accumulator) error { DbInits.Add(func(acc *qgen.Accumulator) error {
mi := "menu_items"
menuItemStmts = MenuItemStmts{ menuItemStmts = MenuItemStmts{
update: acc.Update(mi).Set("name=?,htmlID=?,cssClass=?,position=?,path=?,aria=?,tooltip=?,tmplName=?,guestOnly=?,memberOnly=?,staffOnly=?,adminOnly=?").Where("miid=?").Prepare(), update: acc.Update("menu_items").Set("name = ?, htmlID = ?, cssClass = ?, position = ?, path = ?, aria = ?, tooltip = ?, tmplName = ?, guestOnly = ?, memberOnly = ?, staffOnly = ?, adminOnly = ?").Where("miid = ?").Prepare(),
insert: acc.Insert(mi).Columns("mid, name, htmlID, cssClass, position, path, aria, tooltip, tmplName, guestOnly, memberOnly, staffOnly, adminOnly").Fields("?,?,?,?,?,?,?,?,?,?,?,?,?").Prepare(), insert: acc.Insert("menu_items").Columns("mid, name, htmlID, cssClass, position, path, aria, tooltip, tmplName, guestOnly, memberOnly, staffOnly, adminOnly").Fields("?,?,?,?,?,?,?,?,?,?,?,?,?").Prepare(),
delete: acc.Delete(mi).Where("miid=?").Prepare(), delete: acc.Delete("menu_items").Where("miid = ?").Prepare(),
updateOrder: acc.Update(mi).Set("order=?").Where("miid=?").Prepare(), updateOrder: acc.Update("menu_items").Set("order = ?").Where("miid = ?").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })
} }
func (i MenuItem) Commit() error { func (item MenuItem) Commit() error {
_, e := menuItemStmts.update.Exec(i.Name, i.HTMLID, i.CSSClass, i.Position, i.Path, i.Aria, i.Tooltip, i.TmplName, i.GuestOnly, i.MemberOnly, i.SuperModOnly, i.AdminOnly, i.ID) _, err := menuItemStmts.update.Exec(item.Name, item.HTMLID, item.CSSClass, item.Position, item.Path, item.Aria, item.Tooltip, item.TmplName, item.GuestOnly, item.MemberOnly, item.SuperModOnly, item.AdminOnly, item.ID)
Menus.Load(i.MenuID) Menus.Load(item.MenuID)
return e return err
} }
func (i MenuItem) Create() (int, error) { func (item MenuItem) Create() (int, error) {
res, e := menuItemStmts.insert.Exec(i.MenuID, i.Name, i.HTMLID, i.CSSClass, i.Position, i.Path, i.Aria, i.Tooltip, i.TmplName, i.GuestOnly, i.MemberOnly, i.SuperModOnly, i.AdminOnly) res, err := menuItemStmts.insert.Exec(item.MenuID, item.Name, item.HTMLID, item.CSSClass, item.Position, item.Path, item.Aria, item.Tooltip, item.TmplName, item.GuestOnly, item.MemberOnly, item.SuperModOnly, item.AdminOnly)
if e != nil { if err != nil {
return 0, e return 0, err
} }
Menus.Load(i.MenuID) Menus.Load(item.MenuID)
miid64, e := res.LastInsertId() miid64, err := res.LastInsertId()
return int(miid64), e return int(miid64), err
} }
func (i MenuItem) Delete() error { func (item MenuItem) Delete() error {
_, e := menuItemStmts.delete.Exec(i.ID) _, err := menuItemStmts.delete.Exec(item.ID)
Menus.Load(i.MenuID) Menus.Load(item.MenuID)
return e return err
} }
func (h *MenuListHolder) LoadTmpl(name string) (t MenuTmpl, e error) { func (hold *MenuListHolder) LoadTmpl(name string) (menuTmpl MenuTmpl, err error) {
data, e := ioutil.ReadFile("./templates/" + name + ".html") data, err := ioutil.ReadFile("./templates/" + name + ".html")
if e != nil { if err != nil {
return t, e return menuTmpl, err
} }
return h.Parse(name, []byte(tmpl.Minify(string(data)))), nil return hold.Parse(name, data), nil
} }
// TODO: Make this atomic, maybe with a transaction or store the order on the menu itself? // TODO: Make this atomic, maybe with a transaction or store the order on the menu itself?
func (h *MenuListHolder) UpdateOrder(updateMap map[int]int) error { func (hold *MenuListHolder) UpdateOrder(updateMap map[int]int) error {
for miid, order := range updateMap { for miid, order := range updateMap {
_, e := menuItemStmts.updateOrder.Exec(order, miid) _, err := menuItemStmts.updateOrder.Exec(order, miid)
if e != nil { if err != nil {
return e return err
} }
} }
Menus.Load(h.MenuID) Menus.Load(hold.MenuID)
return nil return nil
} }
func (h *MenuListHolder) LoadTmpls() (tmpls map[string]MenuTmpl, e error) { func (hold *MenuListHolder) LoadTmpls() (tmpls map[string]MenuTmpl, err error) {
tmpls = make(map[string]MenuTmpl) tmpls = make(map[string]MenuTmpl)
load := func(name string) error { var loadTmpl = func(name string) error {
menuTmpl, e := h.LoadTmpl(name) menuTmpl, err := hold.LoadTmpl(name)
if e != nil { if err != nil {
return e return err
} }
tmpls[name] = menuTmpl tmpls[name] = menuTmpl
return nil return nil
} }
e = load("menu_item") err = loadTmpl("menu_item")
if e != nil { if err != nil {
return tmpls, e return tmpls, err
} }
e = load("menu_alerts") err = loadTmpl("menu_alerts")
return tmpls, e return tmpls, err
} }
// TODO: Run this in main, sync ticks, when the phrase file changes (need to implement the sync for that first), and when the settings are changed // TODO: Run this in main, sync ticks, when the phrase file changes (need to implement the sync for that first), and when the settings are changed
func (h *MenuListHolder) Preparse() error { func (hold *MenuListHolder) Preparse() error {
tmpls, err := h.LoadTmpls() tmpls, err := hold.LoadTmpls()
if err != nil { if err != nil {
return err return err
} }
addVariation := func(index int, callback func(i MenuItem) bool) { var addVariation = func(index int, callback func(mitem MenuItem) bool) {
renderBuffer, variableIndices, pathList := h.Scan(tmpls, callback) renderBuffer, variableIndices := hold.Scan(tmpls, callback)
h.Variations[index] = menuTmpl{renderBuffer, variableIndices, pathList} hold.Variations[index] = menuTmpl{renderBuffer, variableIndices}
//fmt.Print("renderBuffer: ")
//menuDumpSlice(renderBuffer)
//fmt.Printf("\nvariableIndices: %+v\n", variableIndices)
} }
// Guest Menu // Guest Menu
addVariation(0, func(i MenuItem) bool { addVariation(0, func(mitem MenuItem) bool {
return !i.MemberOnly return !mitem.MemberOnly
}) })
// Member Menu // Member Menu
addVariation(1, func(i MenuItem) bool { addVariation(1, func(mitem MenuItem) bool {
return !i.SuperModOnly && !i.GuestOnly return !mitem.SuperModOnly && !mitem.GuestOnly
}) })
// Super Mod Menu // Super Mod Menu
addVariation(2, func(i MenuItem) bool { addVariation(2, func(mitem MenuItem) bool {
return !i.AdminOnly && !i.GuestOnly return !mitem.AdminOnly && !mitem.GuestOnly
}) })
// Admin Menu // Admin Menu
addVariation(3, func(i MenuItem) bool { addVariation(3, func(mitem MenuItem) bool {
return !i.GuestOnly return !mitem.GuestOnly
}) })
return nil return nil
} }
@ -192,18 +185,6 @@ func skipUntilIfExists(tmplData []byte, i int, expects byte) (newI int, hasIt bo
return j, false return j, false
} }
func skipUntilIfExistsOrLine(tmplData []byte, i int, expects byte) (newI int, hasIt bool) {
j := i
for ; j < len(tmplData); j++ {
if tmplData[j] == 10 {
return j, false
} else if tmplData[j] == expects {
return j, true
}
}
return j, false
}
func skipUntilCharsExist(tmplData []byte, i int, expects []byte) (newI int, hasIt bool) { func skipUntilCharsExist(tmplData []byte, i int, expects []byte) (newI int, hasIt bool) {
j := i j := i
expectIndex := 0 expectIndex := 0
@ -221,11 +202,15 @@ func skipUntilCharsExist(tmplData []byte, i int, expects []byte) (newI int, hasI
func skipAllUntilCharsExist(tmplData []byte, i int, expects []byte) (newI int, hasIt bool) { func skipAllUntilCharsExist(tmplData []byte, i int, expects []byte) (newI int, hasIt bool) {
j := i j := i
expectIndex := 0 expectIndex := 0
//fmt.Printf("tmplData: %+v\n", string(tmplData))
for ; j < len(tmplData) && expectIndex < len(expects); j++ { for ; j < len(tmplData) && expectIndex < len(expects); j++ {
//fmt.Println("j: ", j)
//fmt.Println("tmplData[j]: ", string(tmplData[j])+" ")
if tmplData[j] == expects[expectIndex] { if tmplData[j] == expects[expectIndex] {
//fmt.Printf("expects[expectIndex]: %+v - %d\n", string(expects[expectIndex]), expectIndex) //fmt.Printf("expects[expectIndex]: %+v - %d\n", string(expects[expectIndex]), expectIndex)
expectIndex++ expectIndex++
if len(expects) <= expectIndex { if len(expects) <= expectIndex {
//fmt.Println("breaking")
break break
} }
} else { } else {
@ -241,6 +226,8 @@ func skipAllUntilCharsExist(tmplData []byte, i int, expects []byte) (newI int, h
expectIndex = 0 expectIndex = 0
} }
} }
//fmt.Println("len(expects): ", len(expects))
//fmt.Println("expectIndex: ", expectIndex)
return j, len(expects) == expectIndex return j, len(expects) == expectIndex
} }
@ -259,24 +246,27 @@ type MenuTmpl struct {
func menuDumpSlice(outerSlice [][]byte) { func menuDumpSlice(outerSlice [][]byte) {
for sliceID, slice := range outerSlice { for sliceID, slice := range outerSlice {
fmt.Print(strconv.Itoa(sliceID) + ":[") fmt.Print(strconv.Itoa(sliceID) + ":[")
for _, ch := range slice { for _, char := range slice {
fmt.Print(string(ch)) fmt.Print(string(char))
} }
fmt.Print("] ") fmt.Print("] ")
} }
} }
func (h *MenuListHolder) Parse(name string, tmplData []byte) (menuTmpl MenuTmpl) { func (hold *MenuListHolder) Parse(name string, tmplData []byte) (menuTmpl MenuTmpl) {
//fmt.Println("tmplData: ", string(tmplData))
var textBuffer, variableBuffer [][]byte var textBuffer, variableBuffer [][]byte
var renderList []menuRenderItem var renderList []menuRenderItem
var subBuffer []byte var subBuffer []byte
// ? We only support simple properties on MenuItem right now // ? We only support simple properties on MenuItem right now
addVariable := func(name []byte) { var addVariable = func(name []byte) {
//fmt.Println("appending subBuffer: ", string(subBuffer))
// TODO: Check if the subBuffer has any items or is empty // TODO: Check if the subBuffer has any items or is empty
textBuffer = append(textBuffer, subBuffer) textBuffer = append(textBuffer, subBuffer)
subBuffer = nil subBuffer = nil
//fmt.Println("adding variable: ", string(name))
variableBuffer = append(variableBuffer, name) variableBuffer = append(variableBuffer, name)
renderList = append(renderList, menuRenderItem{0, len(textBuffer) - 1}) renderList = append(renderList, menuRenderItem{0, len(textBuffer) - 1})
renderList = append(renderList, menuRenderItem{1, len(variableBuffer) - 1}) renderList = append(renderList, menuRenderItem{1, len(variableBuffer) - 1})
@ -287,8 +277,10 @@ func (h *MenuListHolder) Parse(name string, tmplData []byte) (menuTmpl MenuTmpl)
for i := 0; i < len(tmplData); i++ { for i := 0; i < len(tmplData); i++ {
char := tmplData[i] char := tmplData[i]
if char == '{' { if char == '{' {
//fmt.Println("found open fence")
dotIndex, hasDot := skipUntilIfExists(tmplData, i, '.') dotIndex, hasDot := skipUntilIfExists(tmplData, i, '.')
if !hasDot { if !hasDot {
//fmt.Println("no dot, assumed template function style")
// Template function style // Template function style
langIndex, hasChars := skipUntilCharsExist(tmplData, i+1, []byte("lang")) langIndex, hasChars := skipUntilCharsExist(tmplData, i+1, []byte("lang"))
if hasChars { if hasChars {
@ -310,6 +302,7 @@ func (h *MenuListHolder) Parse(name string, tmplData []byte) (menuTmpl MenuTmpl)
} }
fenceIndex, hasFence := skipUntilIfExists(tmplData, dotIndex, '}') fenceIndex, hasFence := skipUntilIfExists(tmplData, dotIndex, '}')
if !hasFence { if !hasFence {
//fmt.Println("no end fence")
break break
} }
addVariable(tmplData[dotIndex:fenceIndex]) addVariable(tmplData[dotIndex:fenceIndex])
@ -324,30 +317,35 @@ func (h *MenuListHolder) Parse(name string, tmplData []byte) (menuTmpl MenuTmpl)
renderList = append(renderList, menuRenderItem{0, len(textBuffer) - 1}) renderList = append(renderList, menuRenderItem{0, len(textBuffer) - 1})
} }
//fmt.Println("name: ", name)
//fmt.Print("textBuffer: ")
//menuDumpSlice(textBuffer)
//fmt.Print("\nvariableBuffer: ")
//menuDumpSlice(variableBuffer)
//fmt.Printf("\nrenderList: %+v\n", renderList)
return MenuTmpl{name, textBuffer, variableBuffer, renderList} return MenuTmpl{name, textBuffer, variableBuffer, renderList}
} }
func (h *MenuListHolder) Scan(tmpls map[string]MenuTmpl, showItem func(i MenuItem) bool) (renderBuffer [][]byte, variableIndices []int, pathList []menuPath) { func (hold *MenuListHolder) Scan(menuTmpls map[string]MenuTmpl, showItem func(mitem MenuItem) bool) (renderBuffer [][]byte, variableIndices []int) {
for _, mitem := range h.List { for _, mitem := range hold.List {
// Do we want this item in this variation of the menu? // Do we want this item in this variation of the menu?
if !showItem(mitem) { if !showItem(mitem) {
continue continue
} }
renderBuffer, variableIndices = h.ScanItem(tmpls, mitem, renderBuffer, variableIndices) renderBuffer, variableIndices = hold.ScanItem(menuTmpls, mitem, renderBuffer, variableIndices)
pathList = append(pathList, menuPath{mitem.Path, len(renderBuffer) - 1})
} }
// TODO: Need more coalescing in the renderBuffer // TODO: Need more coalescing in the renderBuffer
return renderBuffer, variableIndices, pathList return renderBuffer, variableIndices
} }
// Note: This doesn't do a visibility check like hold.Scan() does // Note: This doesn't do a visibility check like hold.Scan() does
func (h *MenuListHolder) ScanItem(tmpls map[string]MenuTmpl, mitem MenuItem, renderBuffer [][]byte, variableIndices []int) ([][]byte, []int) { func (hold *MenuListHolder) ScanItem(menuTmpls map[string]MenuTmpl, mitem MenuItem, renderBuffer [][]byte, variableIndices []int) ([][]byte, []int) {
menuTmpl, ok := tmpls[mitem.TmplName] menuTmpl, ok := menuTmpls[mitem.TmplName]
if !ok { if !ok {
menuTmpl = tmpls["menu_item"] menuTmpl = menuTmpls["menu_item"]
} }
//fmt.Println("menuTmpl: ", menuTmpl)
for _, renderItem := range menuTmpl.RenderList { for _, renderItem := range menuTmpl.RenderList {
if renderItem.Type == 0 { if renderItem.Type == 0 {
renderBuffer = append(renderBuffer, menuTmpl.TextBuffer[renderItem.Index]) renderBuffer = append(renderBuffer, menuTmpl.TextBuffer[renderItem.Index])
@ -355,16 +353,17 @@ func (h *MenuListHolder) ScanItem(tmpls map[string]MenuTmpl, mitem MenuItem, ren
} }
variable := menuTmpl.VariableBuffer[renderItem.Index] variable := menuTmpl.VariableBuffer[renderItem.Index]
//fmt.Println("initial variable: ", string(variable))
dotAt, hasDot := skipUntilIfExists(variable, 0, '.') dotAt, hasDot := skipUntilIfExists(variable, 0, '.')
if !hasDot { if !hasDot {
//fmt.Println("no dot")
continue continue
} }
if bytes.Equal(variable[:dotAt], []byte("lang")) { if bytes.Equal(variable[:dotAt], []byte("lang")) {
renderBuffer = append(renderBuffer, []byte(phrases.GetTmplPhrase(string(bytes.TrimPrefix(variable[dotAt:], []byte(".")))))) //fmt.Println("lang: ", string(bytes.TrimPrefix(variable[dotAt:], []byte("."))))
continue renderBuffer = append(renderBuffer, []byte(GetTmplPhrase(string(bytes.TrimPrefix(variable[dotAt:], []byte("."))))))
} } else {
var renderItem []byte var renderItem []byte
switch string(variable) { switch string(variable) {
case ".ID": case ".ID":
@ -383,13 +382,11 @@ func (h *MenuListHolder) ScanItem(tmpls map[string]MenuTmpl, mitem MenuItem, ren
renderItem = []byte(mitem.Aria) renderItem = []byte(mitem.Aria)
case ".Tooltip": case ".Tooltip":
renderItem = []byte(mitem.Tooltip) renderItem = []byte(mitem.Tooltip)
case ".CSSActive":
renderItem = []byte("{dyn.active}")
} }
_, hasInnerVar := skipUntilIfExists(renderItem, 0, '{') _, hasInnerVar := skipUntilIfExists(renderItem, 0, '{')
if hasInnerVar { if hasInnerVar {
DebugLog("inner var: ", string(renderItem)) //fmt.Println("inner var: ", string(renderItem))
dotAt, hasDot := skipUntilIfExists(renderItem, 0, '.') dotAt, hasDot := skipUntilIfExists(renderItem, 0, '.')
endFence, hasEndFence := skipUntilIfExists(renderItem, dotAt, '}') endFence, hasEndFence := skipUntilIfExists(renderItem, dotAt, '}')
if !hasDot || !hasEndFence || (endFence-dotAt) <= 1 { if !hasDot || !hasEndFence || (endFence-dotAt) <= 1 {
@ -400,9 +397,9 @@ func (h *MenuListHolder) ScanItem(tmpls map[string]MenuTmpl, mitem MenuItem, ren
if bytes.Equal(renderItem[1:dotAt], []byte("lang")) { if bytes.Equal(renderItem[1:dotAt], []byte("lang")) {
//fmt.Println("lang var: ", string(renderItem[dotAt+1:endFence])) //fmt.Println("lang var: ", string(renderItem[dotAt+1:endFence]))
renderBuffer = append(renderBuffer, []byte(phrases.GetTmplPhrase(string(renderItem[dotAt+1:endFence])))) renderBuffer = append(renderBuffer, []byte(GetTmplPhrase(string(renderItem[dotAt+1:endFence]))))
} else { } else {
fmt.Println("other var: ", string(variable[:dotAt])) //fmt.Println("other var: ", string(variable[:dotAt]))
if len(renderItem) > 0 { if len(renderItem) > 0 {
renderBuffer = append(renderBuffer, renderItem) renderBuffer = append(renderBuffer, renderItem)
variableIndices = append(variableIndices, len(renderBuffer)-1) variableIndices = append(variableIndices, len(renderBuffer)-1)
@ -410,48 +407,52 @@ func (h *MenuListHolder) ScanItem(tmpls map[string]MenuTmpl, mitem MenuItem, ren
} }
continue continue
} }
//fmt.Println("normal var: ", string(variable[:dotAt]))
if len(renderItem) > 0 { if len(renderItem) > 0 {
renderBuffer = append(renderBuffer, renderItem) renderBuffer = append(renderBuffer, renderItem)
} }
} }
}
return renderBuffer, variableIndices return renderBuffer, variableIndices
} }
// TODO: Pre-render the lang stuff // TODO: Pre-render the lang stuff
func (h *MenuListHolder) Build(w io.Writer, user *User, pathPrefix string) error { func (hold *MenuListHolder) Build(w io.Writer, user *User) error {
var mTmpl menuTmpl var mTmpl menuTmpl
if !user.Loggedin { if !user.Loggedin {
mTmpl = h.Variations[0] mTmpl = hold.Variations[0]
} else if user.IsAdmin { } else if user.IsAdmin {
mTmpl = h.Variations[3] mTmpl = hold.Variations[3]
} else if user.IsSuperMod { } else if user.IsSuperMod {
mTmpl = h.Variations[2] mTmpl = hold.Variations[2]
} else { } else {
mTmpl = h.Variations[1] mTmpl = hold.Variations[1]
}
if pathPrefix == "" {
pathPrefix = Config.DefaultPath
} }
if len(mTmpl.VariableIndices) == 0 { if len(mTmpl.VariableIndices) == 0 {
//fmt.Println("no variable indices")
for _, renderItem := range mTmpl.RenderBuffer { for _, renderItem := range mTmpl.RenderBuffer {
//fmt.Printf("renderItem: %+v\n", renderItem)
w.Write(renderItem) w.Write(renderItem)
} }
return nil return nil
} }
nearIndex := 0 var nearIndex = 0
for index, renderItem := range mTmpl.RenderBuffer { for index, renderItem := range mTmpl.RenderBuffer {
if index != mTmpl.VariableIndices[nearIndex] { if index != mTmpl.VariableIndices[nearIndex] {
//fmt.Println("wrote text: ", string(renderItem))
w.Write(renderItem) w.Write(renderItem)
continue continue
} }
//fmt.Println("variable: ", string(renderItem))
variable := renderItem variable := renderItem
// ? - I can probably remove this check now that I've kicked it upstream, or we could keep it here for safety's sake? // ? - I can probably remove this check now that I've kicked it upstream, or we could keep it here for safety's sake?
if len(variable) == 0 { if len(variable) == 0 {
continue continue
} }
prevIndex := 0 prevIndex := 0
for i := 0; i < len(renderItem); i++ { for i := 0; i < len(renderItem); i++ {
fenceStart, hasFence := skipUntilIfExists(variable, i, '{') fenceStart, hasFence := skipUntilIfExists(variable, i, '{')
@ -468,9 +469,9 @@ func (h *MenuListHolder) Build(w io.Writer, user *User, pathPrefix string) error
if !hasDot { if !hasDot {
continue continue
} }
//fmt.Println("checking me: ", string(variable[fenceStart+1:dotAt]))
switch string(variable[fenceStart+1 : dotAt]) { if bytes.Equal(variable[fenceStart+1:dotAt], []byte("me")) {
case "me": //fmt.Println("maybe me variable")
w.Write(variable[prevIndex:fenceStart]) w.Write(variable[prevIndex:fenceStart])
switch string(variable[dotAt+1 : fenceEnd]) { switch string(variable[dotAt+1 : fenceEnd]) {
case "Link": case "Link":
@ -479,31 +480,10 @@ func (h *MenuListHolder) Build(w io.Writer, user *User, pathPrefix string) error
w.Write([]byte(user.Session)) w.Write([]byte(user.Session))
} }
prevIndex = fenceEnd prevIndex = fenceEnd
// TODO: Optimise this
case "dyn":
w.Write(variable[prevIndex:fenceStart])
var pmi int
for ii, pathItem := range mTmpl.PathMappings {
pmi = ii
if pathItem.Index > index {
break
} }
} }
//fmt.Println("prevIndex: ", prevIndex)
if len(mTmpl.PathMappings) != 0 { //fmt.Println("len(variable)-1: ", len(variable)-1)
path := mTmpl.PathMappings[pmi].Path
if path == "" || path == "/" {
path = Config.DefaultPath
}
if strings.HasPrefix(path, pathPrefix) {
w.Write([]byte(" menu_active"))
}
}
prevIndex = fenceEnd
}
}
w.Write(variable[prevIndex : len(variable)-1]) w.Write(variable[prevIndex : len(variable)-1])
if len(mTmpl.VariableIndices) > (nearIndex + 1) { if len(mTmpl.VariableIndices) > (nearIndex + 1) {
nearIndex++ nearIndex++

View File

@ -1,61 +0,0 @@
package common
import (
"database/sql"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
// MetaStore is a simple key-value store for the system to stash things in when needed
type MetaStore interface {
Get(name string) (val string, err error)
Set(name, val string) error
SetInt(name string, val int) error
SetInt64(name string, val int64) error
}
type DefaultMetaStore struct {
get *sql.Stmt
set *sql.Stmt
add *sql.Stmt
}
func NewDefaultMetaStore(acc *qgen.Accumulator) (*DefaultMetaStore, error) {
t := "meta"
m := &DefaultMetaStore{
get: acc.Select(t).Columns("value").Where("name=?").Prepare(),
set: acc.Update(t).Set("value=?").Where("name=?").Prepare(),
add: acc.Insert(t).Columns("name,value").Fields("?,''").Prepare(),
}
return m, acc.FirstError()
}
func (s *DefaultMetaStore) Get(name string) (val string, e error) {
e = s.get.QueryRow(name).Scan(&val)
return val, e
}
// TODO: Use timestamped rows as a more robust method of ensuring data integrity
func (s *DefaultMetaStore) setVal(name string, val interface{}) error {
_, e := s.Get(name)
if e == sql.ErrNoRows {
_, e := s.add.Exec(name)
if e != nil {
return e
}
}
_, e = s.set.Exec(val, name)
return e
}
func (s *DefaultMetaStore) Set(name, val string) error {
return s.setVal(name, val)
}
func (s *DefaultMetaStore) SetInt(name string, val int) error {
return s.setVal(name, val)
}
func (s *DefaultMetaStore) SetInt64(name string, val int64) error {
return s.setVal(name, val)
}

View File

@ -5,7 +5,7 @@ import (
"errors" "errors"
"strings" "strings"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var MFAstore MFAStore var MFAstore MFAStore
@ -21,7 +21,7 @@ var mfaItemStmts MFAItemStmts
func init() { func init() {
DbInits.Add(func(acc *qgen.Accumulator) error { DbInits.Add(func(acc *qgen.Accumulator) error {
mfaItemStmts = MFAItemStmts{ mfaItemStmts = MFAItemStmts{
update: acc.Update("users_2fa_keys").Set("scratch1=?,scratch2=?,scratch3=?,scratch4=?,scratch5=?,scratch6=?,scratch7=?,scratch8=?").Where("uid=?").Prepare(), update: acc.Update("users_2fa_keys").Set("scratch1 = ?, scratch2, scratch3 = ?, scratch3 = ?, scratch4 = ?, scratch5 = ?, scratch6 = ?, scratch7 = ?, scratch8 = ?").Where("uid = ?").Prepare(),
delete: acc.Delete("users_2fa_keys").Where("uid = ?").Prepare(), delete: acc.Delete("users_2fa_keys").Where("uid = ?").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
@ -34,22 +34,22 @@ type MFAItem struct {
Scratch []string Scratch []string
} }
func (i *MFAItem) BurnScratch(index int) error { func (item *MFAItem) BurnScratch(index int) error {
if index < 0 || len(i.Scratch) <= index { if index < 0 || len(item.Scratch) <= index {
return ErrMFAScratchIndexOutOfBounds return ErrMFAScratchIndexOutOfBounds
} }
newScratch, err := mfaCreateScratch() newScratch, err := mfaCreateScratch()
if err != nil { if err != nil {
return err return err
} }
i.Scratch[index] = newScratch item.Scratch[index] = newScratch
_, err = mfaItemStmts.update.Exec(i.Scratch[0], i.Scratch[1], i.Scratch[2], i.Scratch[3], i.Scratch[4], i.Scratch[5], i.Scratch[6], i.Scratch[7], i.UID) _, err = mfaItemStmts.update.Exec(item.Scratch[0], item.Scratch[1], item.Scratch[2], item.Scratch[3], item.Scratch[4], item.Scratch[5], item.Scratch[6], item.Scratch[7], item.UID)
return err return err
} }
func (i *MFAItem) Delete() error { func (item *MFAItem) Delete() error {
_, err := mfaItemStmts.delete.Exec(i.UID) _, err := mfaItemStmts.delete.Exec(item.UID)
return err return err
} }
@ -76,16 +76,16 @@ func NewSQLMFAStore(acc *qgen.Accumulator) (*SQLMFAStore, error) {
} }
// TODO: Write a test for this // TODO: Write a test for this
func (s *SQLMFAStore) Get(id int) (*MFAItem, error) { func (store *SQLMFAStore) Get(id int) (*MFAItem, error) {
i := MFAItem{UID: id, Scratch: make([]string, 8)} item := MFAItem{UID: id, Scratch: make([]string, 8)}
err := s.get.QueryRow(id).Scan(&i.Secret, &i.Scratch[0], &i.Scratch[1], &i.Scratch[2], &i.Scratch[3], &i.Scratch[4], &i.Scratch[5], &i.Scratch[6], &i.Scratch[7]) err := store.get.QueryRow(id).Scan(&item.Secret, &item.Scratch[0], &item.Scratch[1], &item.Scratch[2], &item.Scratch[3], &item.Scratch[4], &item.Scratch[5], &item.Scratch[6], &item.Scratch[7])
return &i, err return &item, err
} }
// TODO: Write a test for this // TODO: Write a test for this
func (s *SQLMFAStore) Create(secret string, uid int) (err error) { func (store *SQLMFAStore) Create(secret string, uid int) (err error) {
params := make([]interface{}, 10) var params = make([]interface{}, 10)
params[0] = uid params[0] = uid
params[1] = secret params[1] = secret
for i := 2; i < len(params); i++ { for i := 2; i < len(params); i++ {
@ -96,6 +96,6 @@ func (s *SQLMFAStore) Create(secret string, uid int) (err error) {
params[i] = code params[i] = code
} }
_, err = s.create.Exec(params...) _, err = store.create.Exec(params...)
return err return err
} }

View File

@ -1,14 +1,9 @@
package common package common
import ( import "database/sql"
"database/sql" import "../query_gen/lib"
"time"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var RegLogs RegLogStore var RegLogs RegLogStore
var LoginLogs LoginLogStore
type RegLogItem struct { type RegLogItem struct {
ID int ID int
@ -16,7 +11,7 @@ type RegLogItem struct {
Email string Email string
FailureReason string FailureReason string
Success bool Success bool
IP string IPAddress string
DoneAt string DoneAt string
} }
@ -29,10 +24,9 @@ var regLogStmts RegLogStmts
func init() { func init() {
DbInits.Add(func(acc *qgen.Accumulator) error { DbInits.Add(func(acc *qgen.Accumulator) error {
rl := "registration_logs"
regLogStmts = RegLogStmts{ regLogStmts = RegLogStmts{
update: acc.Update(rl).Set("username=?,email=?,failureReason=?,success=?,doneAt=?").Where("rlid=?").Prepare(), update: acc.Update("registration_logs").Set("username = ?, email = ?, failureReason = ?, success = ?").Where("rlid = ?").Prepare(),
create: acc.Insert(rl).Columns("username,email,failureReason,success,ipaddress,doneAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(), create: acc.Insert("registration_logs").Columns("username, email, failureReason, success, ipaddress, doneAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })
@ -40,188 +34,60 @@ func init() {
// TODO: Reload this item in the store, probably doesn't matter right now, but it might when we start caching this stuff in memory // TODO: Reload this item in the store, probably doesn't matter right now, but it might when we start caching this stuff in memory
// ! Retroactive updates of date are not permitted for integrity reasons // ! Retroactive updates of date are not permitted for integrity reasons
// TODO: Do we even use this anymore or can we just make the logs immutable (except for deletes) for simplicity sake? func (log *RegLogItem) Commit() error {
func (l *RegLogItem) Commit() error { _, err := regLogStmts.update.Exec(log.Username, log.Email, log.FailureReason, log.Success, log.ID)
_, e := regLogStmts.update.Exec(l.Username, l.Email, l.FailureReason, l.Success, l.DoneAt, l.ID) return err
return e
} }
func (l *RegLogItem) Create() (id int, e error) { func (log *RegLogItem) Create() (id int, err error) {
id, e = Createf(regLogStmts.create, l.Username, l.Email, l.FailureReason, l.Success, l.IP) res, err := regLogStmts.create.Exec(log.Username, log.Email, log.FailureReason, log.Success, log.IPAddress)
l.ID = id if err != nil {
return l.ID, e return 0, err
}
id64, err := res.LastInsertId()
log.ID = int(id64)
return log.ID, err
} }
type RegLogStore interface { type RegLogStore interface {
Count() (count int) GlobalCount() (logCount int)
GetOffset(offset, perPage int) (logs []RegLogItem, err error) GetOffset(offset int, perPage int) (logs []RegLogItem, err error)
Purge() error
DeleteOlderThanDays(days int) error
} }
type SQLRegLogStore struct { type SQLRegLogStore struct {
count *sql.Stmt count *sql.Stmt
getOffset *sql.Stmt getOffset *sql.Stmt
purge *sql.Stmt
deleteOlderThanDays *sql.Stmt
} }
func NewRegLogStore(acc *qgen.Accumulator) (*SQLRegLogStore, error) { func NewRegLogStore(acc *qgen.Accumulator) (*SQLRegLogStore, error) {
rl := "registration_logs"
return &SQLRegLogStore{ return &SQLRegLogStore{
count: acc.Count(rl).Prepare(), count: acc.Count("registration_logs").Prepare(),
getOffset: acc.Select(rl).Columns("rlid,username,email,failureReason,success,ipaddress,doneAt").Orderby("doneAt DESC").Limit("?,?").Prepare(), getOffset: acc.Select("registration_logs").Columns("rlid, username, email, failureReason, success, ipaddress, doneAt").Orderby("doneAt DESC").Limit("?,?").Prepare(),
purge: acc.Purge(rl),
deleteOlderThanDays: acc.Delete(rl).DateOlderThanQ("doneAt", "day").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
func (s *SQLRegLogStore) Count() (count int) { func (store *SQLRegLogStore) GlobalCount() (logCount int) {
return Count(s.count) err := store.count.QueryRow().Scan(&logCount)
if err != nil {
LogError(err)
}
return logCount
} }
func (s *SQLRegLogStore) GetOffset(offset, perPage int) (logs []RegLogItem, e error) { func (store *SQLRegLogStore) GetOffset(offset int, perPage int) (logs []RegLogItem, err error) {
rows, e := s.getOffset.Query(offset, perPage) rows, err := store.getOffset.Query(offset, perPage)
if e != nil { if err != nil {
return logs, e return logs, err
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var l RegLogItem var log RegLogItem
var doneAt time.Time err := rows.Scan(&log.ID, &log.Username, &log.Email, &log.FailureReason, &log.Success, &log.IPAddress, &log.DoneAt)
e := rows.Scan(&l.ID, &l.Username, &l.Email, &l.FailureReason, &l.Success, &l.IP, &doneAt) if err != nil {
if e != nil { return logs, err
return logs, e
} }
l.DoneAt = doneAt.Format("2006-01-02 15:04:05") logs = append(logs, log)
logs = append(logs, l)
} }
return logs, rows.Err() return logs, rows.Err()
} }
func (s *SQLRegLogStore) DeleteOlderThanDays(days int) error {
_, e := s.deleteOlderThanDays.Exec(days)
return e
}
// Delete all registration logs
func (s *SQLRegLogStore) Purge() error {
_, e := s.purge.Exec()
return e
}
type LoginLogItem struct {
ID int
UID int
Success bool
IP string
DoneAt string
}
type LoginLogStmts struct {
update *sql.Stmt
create *sql.Stmt
}
var loginLogStmts LoginLogStmts
func init() {
DbInits.Add(func(acc *qgen.Accumulator) error {
ll := "login_logs"
loginLogStmts = LoginLogStmts{
update: acc.Update(ll).Set("uid=?,success=?,doneAt=?").Where("lid=?").Prepare(),
create: acc.Insert(ll).Columns("uid,success,ipaddress,doneAt").Fields("?,?,?,UTC_TIMESTAMP()").Prepare(),
}
return acc.FirstError()
})
}
// TODO: Reload this item in the store, probably doesn't matter right now, but it might when we start caching this stuff in memory
// ! Retroactive updates of date are not permitted for integrity reasons
func (l *LoginLogItem) Commit() error {
_, e := loginLogStmts.update.Exec(l.UID, l.Success, l.DoneAt, l.ID)
return e
}
func (l *LoginLogItem) Create() (id int, e error) {
res, e := loginLogStmts.create.Exec(l.UID, l.Success, l.IP)
if e != nil {
return 0, e
}
id64, e := res.LastInsertId()
l.ID = int(id64)
return l.ID, e
}
type LoginLogStore interface {
Count() (count int)
CountUser(uid int) (count int)
GetOffset(uid, offset, perPage int) (logs []LoginLogItem, err error)
Purge() error
DeleteOlderThanDays(days int) error
}
type SQLLoginLogStore struct {
count *sql.Stmt
countForUser *sql.Stmt
getOffsetByUser *sql.Stmt
purge *sql.Stmt
deleteOlderThanDays *sql.Stmt
}
func NewLoginLogStore(acc *qgen.Accumulator) (*SQLLoginLogStore, error) {
ll := "login_logs"
return &SQLLoginLogStore{
count: acc.Count(ll).Prepare(),
countForUser: acc.Count(ll).Where("uid=?").Prepare(),
getOffsetByUser: acc.Select(ll).Columns("lid,success,ipaddress,doneAt").Where("uid=?").Orderby("doneAt DESC").Limit("?,?").Prepare(),
purge: acc.Purge(ll),
deleteOlderThanDays: acc.Delete(ll).DateOlderThanQ("doneAt", "day").Prepare(),
}, acc.FirstError()
}
func (s *SQLLoginLogStore) Count() (count int) {
return Count(s.count)
}
func (s *SQLLoginLogStore) CountUser(uid int) (count int) {
return Countf(s.countForUser, uid)
}
func (s *SQLLoginLogStore) GetOffset(uid, offset, perPage int) (logs []LoginLogItem, e error) {
rows, e := s.getOffsetByUser.Query(uid, offset, perPage)
if e != nil {
return logs, e
}
defer rows.Close()
for rows.Next() {
l := LoginLogItem{UID: uid}
var doneAt time.Time
e := rows.Scan(&l.ID, &l.Success, &l.IP, &doneAt)
if e != nil {
return logs, e
}
l.DoneAt = doneAt.Format("2006-01-02 15:04:05")
logs = append(logs, l)
}
return logs, rows.Err()
}
func (s *SQLLoginLogStore) DeleteOlderThanDays(days int) error {
_, e := s.deleteOlderThanDays.Exec(days)
return e
}
// Delete all login logs
func (s *SQLLoginLogStore) Purge() error {
_, e := s.purge.Exec()
return e
}

View File

@ -1,7 +1,7 @@
/* /*
* *
* OttoJS Plugin Module * OttoJS Plugin Module
* Copyright Azareal 2016 - 2019 * Copyright Azareal 2016 - 2018
* *
*/ */
package common package common
@ -45,7 +45,7 @@ func (js *OttoPluginLang) AddPlugin(meta PluginMeta) (plugin *Plugin, err error)
return nil, err return nil, err
} }
var pluginInit = func(plugin *Plugin) error { var pluginInit = func() error {
retValue, err := js.vm.Run(script) retValue, err := js.vm.Run(script)
if err != nil { if err != nil {
return err return err

View File

@ -1,46 +0,0 @@
package common
// NullReplyCache is a reply cache to be used when you don't want a cache and just want queries to passthrough to the database
type NullReplyCache struct {
}
// NewNullReplyCache gives you a new instance of NullReplyCache
func NewNullReplyCache() *NullReplyCache {
return &NullReplyCache{}
}
// nolint
func (c *NullReplyCache) Get(id int) (*Reply, error) {
return nil, ErrNoRows
}
func (c *NullReplyCache) GetUnsafe(id int) (*Reply, error) {
return nil, ErrNoRows
}
func (c *NullReplyCache) BulkGet(ids []int) (list []*Reply) {
return make([]*Reply, len(ids))
}
func (c *NullReplyCache) Set(_ *Reply) error {
return nil
}
func (c *NullReplyCache) Add(_ *Reply) error {
return nil
}
func (c *NullReplyCache) AddUnsafe(_ *Reply) error {
return nil
}
func (c *NullReplyCache) Remove(id int) error {
return nil
}
func (c *NullReplyCache) RemoveUnsafe(id int) error {
return nil
}
func (c *NullReplyCache) Flush() {
}
func (c *NullReplyCache) Length() int {
return 0
}
func (c *NullReplyCache) SetCapacity(_ int) {
}
func (c *NullReplyCache) GetCapacity() int {
return 0
}

View File

@ -10,40 +10,34 @@ func NewNullTopicCache() *NullTopicCache {
} }
// nolint // nolint
func (c *NullTopicCache) Get(id int) (*Topic, error) { func (mts *NullTopicCache) Get(id int) (*Topic, error) {
return nil, ErrNoRows return nil, ErrNoRows
} }
func (c *NullTopicCache) GetUnsafe(id int) (*Topic, error) { func (mts *NullTopicCache) GetUnsafe(id int) (*Topic, error) {
return nil, ErrNoRows return nil, ErrNoRows
} }
func (c *NullTopicCache) BulkGet(ids []int) (list []*Topic) { func (mts *NullTopicCache) Set(_ *Topic) error {
return make([]*Topic, len(ids))
}
func (c *NullTopicCache) Set(_ *Topic) error {
return nil return nil
} }
func (c *NullTopicCache) Add(_ *Topic) error { func (mts *NullTopicCache) Add(_ *Topic) error {
return nil return nil
} }
func (c *NullTopicCache) AddUnsafe(_ *Topic) error { func (mts *NullTopicCache) AddUnsafe(_ *Topic) error {
return nil return nil
} }
func (c *NullTopicCache) Remove(id int) error { func (mts *NullTopicCache) Remove(id int) error {
return nil return nil
} }
func (c *NullTopicCache) RemoveMany(ids []int) error { func (mts *NullTopicCache) RemoveUnsafe(id int) error {
return nil return nil
} }
func (c *NullTopicCache) RemoveUnsafe(id int) error { func (mts *NullTopicCache) Flush() {
return nil
} }
func (c *NullTopicCache) Flush() { func (mts *NullTopicCache) Length() int {
}
func (c *NullTopicCache) Length() int {
return 0 return 0
} }
func (c *NullTopicCache) SetCapacity(_ int) { func (mts *NullTopicCache) SetCapacity(_ int) {
} }
func (c *NullTopicCache) GetCapacity() int { func (mts *NullTopicCache) GetCapacity() int {
return 0 return 0
} }

View File

@ -10,45 +10,41 @@ func NewNullUserCache() *NullUserCache {
} }
// nolint // nolint
func (c *NullUserCache) DeallocOverflow(evictPriority bool) (evicted int) { func (mus *NullUserCache) DeallocOverflow(evictPriority bool) (evicted int) {
return 0 return 0
} }
func (c *NullUserCache) Get(id int) (*User, error) { func (mus *NullUserCache) Get(id int) (*User, error) {
return nil, ErrNoRows return nil, ErrNoRows
} }
func (mus *NullUserCache) BulkGet(ids []int) (list []*User) {
func (c *NullUserCache) Getn(id int) *User {
return nil
}
func (c *NullUserCache) BulkGet(ids []int) (list []*User) {
return make([]*User, len(ids)) return make([]*User, len(ids))
} }
func (c *NullUserCache) GetUnsafe(id int) (*User, error) { func (mus *NullUserCache) GetUnsafe(id int) (*User, error) {
return nil, ErrNoRows return nil, ErrNoRows
} }
func (c *NullUserCache) Set(_ *User) error { func (mus *NullUserCache) Set(_ *User) error {
return nil return nil
} }
func (c *NullUserCache) Add(_ *User) error { func (mus *NullUserCache) Add(_ *User) error {
return nil return nil
} }
func (c *NullUserCache) AddUnsafe(_ *User) error { func (mus *NullUserCache) AddUnsafe(_ *User) error {
return nil return nil
} }
func (c *NullUserCache) Remove(id int) error { func (mus *NullUserCache) Remove(id int) error {
return nil return nil
} }
func (c *NullUserCache) RemoveUnsafe(id int) error { func (mus *NullUserCache) RemoveUnsafe(id int) error {
return nil return nil
} }
func (c *NullUserCache) BulkRemove(ids []int) {} func (mus *NullUserCache) BulkRemove(ids []int) {}
func (c *NullUserCache) Flush() { func (mus *NullUserCache) Flush() {
} }
func (c *NullUserCache) Length() int { func (mus *NullUserCache) Length() int {
return 0 return 0
} }
func (c *NullUserCache) SetCapacity(_ int) { func (mus *NullUserCache) SetCapacity(_ int) {
} }
func (c *NullUserCache) GetCapacity() int { func (mus *NullUserCache) GetCapacity() int {
return 0 return 0
} }

View File

@ -5,7 +5,7 @@ import (
"strconv" "strconv"
"strings" "strings"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
type CustomPageStmts struct { type CustomPageStmts struct {
@ -38,12 +38,12 @@ func BlankCustomPage() *CustomPage {
return new(CustomPage) return new(CustomPage)
} }
func (p *CustomPage) AddAllowedGroup(gid int) { func (page *CustomPage) AddAllowedGroup(gid int) {
p.AllowedGroups = append(p.AllowedGroups, gid) page.AllowedGroups = append(page.AllowedGroups, gid)
} }
func (p *CustomPage) getRawAllowedGroups() (rawAllowedGroups string) { func (page *CustomPage) getRawAllowedGroups() (rawAllowedGroups string) {
for _, group := range p.AllowedGroups { for _, group := range page.AllowedGroups {
rawAllowedGroups += strconv.Itoa(group) + "," rawAllowedGroups += strconv.Itoa(group) + ","
} }
if len(rawAllowedGroups) > 0 { if len(rawAllowedGroups) > 0 {
@ -52,17 +52,18 @@ func (p *CustomPage) getRawAllowedGroups() (rawAllowedGroups string) {
return rawAllowedGroups return rawAllowedGroups
} }
func (p *CustomPage) Commit() error { func (page *CustomPage) Commit() error {
_, err := customPageStmts.update.Exec(p.Name, p.Title, p.Body, p.getRawAllowedGroups(), p.MenuID, p.ID) _, err := customPageStmts.update.Exec(page.Name, page.Title, page.Body, page.getRawAllowedGroups(), page.MenuID, page.ID)
Pages.Reload(p.ID) Pages.Reload(page.ID)
return err return err
} }
func (p *CustomPage) Create() (int, error) { func (page *CustomPage) Create() (int, error) {
res, err := customPageStmts.create.Exec(p.Name, p.Title, p.Body, p.getRawAllowedGroups(), p.MenuID) res, err := customPageStmts.create.Exec(page.Name, page.Title, page.Body, page.getRawAllowedGroups(), page.MenuID)
if err != nil { if err != nil {
return 0, err return 0, err
} }
pid64, err := res.LastInsertId() pid64, err := res.LastInsertId()
return int(pid64), err return int(pid64), err
} }
@ -71,10 +72,10 @@ 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 // 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 { type PageStore interface {
Count() (count int) GlobalCount() (pageCount int)
Get(id int) (*CustomPage, error) Get(id int) (*CustomPage, error)
GetByName(name string) (*CustomPage, error) GetByName(name string) (*CustomPage, error)
GetOffset(offset, perPage int) (pages []*CustomPage, err error) GetOffset(offset int, perPage int) (pages []*CustomPage, err error)
Reload(id int) error Reload(id int) error
Delete(id int) error Delete(id int) error
} }
@ -89,26 +90,24 @@ type DefaultPageStore struct {
} }
func NewDefaultPageStore(acc *qgen.Accumulator) (*DefaultPageStore, error) { func NewDefaultPageStore(acc *qgen.Accumulator) (*DefaultPageStore, error) {
pa := "pages"
allCols := "pid, name, title, body, allowedGroups, menuID"
return &DefaultPageStore{ return &DefaultPageStore{
get: acc.Select(pa).Columns("name, title, body, allowedGroups, menuID").Where("pid=?").Prepare(), get: acc.Select("pages").Columns("name, title, body, allowedGroups, menuID").Where("pid = ?").Prepare(),
getByName: acc.Select(pa).Columns(allCols).Where("name=?").Prepare(), getByName: acc.Select("pages").Columns("pid, name, title, body, allowedGroups, menuID").Where("name = ?").Prepare(),
getOffset: acc.Select(pa).Columns(allCols).Orderby("pid DESC").Limit("?,?").Prepare(), getOffset: acc.Select("pages").Columns("pid, name, title, body, allowedGroups, menuID").Orderby("pid DESC").Limit("?,?").Prepare(),
count: acc.Count(pa).Prepare(), count: acc.Count("pages").Prepare(),
delete: acc.Delete(pa).Where("pid=?").Prepare(), delete: acc.Delete("pages").Where("pid = ?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
func (s *DefaultPageStore) Count() (count int) { func (store *DefaultPageStore) GlobalCount() (pageCount int) {
err := s.count.QueryRow().Scan(&count) err := store.count.QueryRow().Scan(&pageCount)
if err != nil { if err != nil {
LogError(err) LogError(err)
} }
return count return pageCount
} }
func (s *DefaultPageStore) parseAllowedGroups(raw string, page *CustomPage) error { func (store *DefaultPageStore) parseAllowedGroups(raw string, page *CustomPage) error {
if raw == "" { if raw == "" {
return nil return nil
} }
@ -122,55 +121,55 @@ func (s *DefaultPageStore) parseAllowedGroups(raw string, page *CustomPage) erro
return nil return nil
} }
func (s *DefaultPageStore) Get(id int) (*CustomPage, error) { func (store *DefaultPageStore) Get(id int) (*CustomPage, error) {
p := &CustomPage{ID: id} page := &CustomPage{ID: id}
rawAllowedGroups := "" rawAllowedGroups := ""
err := s.get.QueryRow(id).Scan(&p.Name, &p.Title, &p.Body, &rawAllowedGroups, &p.MenuID) err := store.get.QueryRow(id).Scan(&page.Name, &page.Title, &page.Body, &rawAllowedGroups, &page.MenuID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return p, s.parseAllowedGroups(rawAllowedGroups, p) return page, store.parseAllowedGroups(rawAllowedGroups, page)
} }
func (s *DefaultPageStore) GetByName(name string) (*CustomPage, error) { func (store *DefaultPageStore) GetByName(name string) (*CustomPage, error) {
p := BlankCustomPage() page := BlankCustomPage()
rawAllowedGroups := "" rawAllowedGroups := ""
err := s.getByName.QueryRow(name).Scan(&p.ID, &p.Name, &p.Title, &p.Body, &rawAllowedGroups, &p.MenuID) err := store.getByName.QueryRow(name).Scan(&page.ID, &page.Name, &page.Title, &page.Body, &rawAllowedGroups, &page.MenuID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return p, s.parseAllowedGroups(rawAllowedGroups, p) return page, store.parseAllowedGroups(rawAllowedGroups, page)
} }
func (s *DefaultPageStore) GetOffset(offset, perPage int) (pages []*CustomPage, err error) { func (store *DefaultPageStore) GetOffset(offset int, perPage int) (pages []*CustomPage, err error) {
rows, err := s.getOffset.Query(offset, perPage) rows, err := store.getOffset.Query(offset, perPage)
if err != nil { if err != nil {
return pages, err return pages, err
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
p := &CustomPage{ID: 0} page := &CustomPage{ID: 0}
rawAllowedGroups := "" rawAllowedGroups := ""
err := rows.Scan(&p.ID, &p.Name, &p.Title, &p.Body, &rawAllowedGroups, &p.MenuID) err := rows.Scan(&page.ID, &page.Name, &page.Title, &page.Body, &rawAllowedGroups, &page.MenuID)
if err != nil { if err != nil {
return pages, err return pages, err
} }
err = s.parseAllowedGroups(rawAllowedGroups, p) err = store.parseAllowedGroups(rawAllowedGroups, page)
if err != nil { if err != nil {
return pages, err return pages, err
} }
pages = append(pages, p) pages = append(pages, page)
} }
return pages, rows.Err() return pages, rows.Err()
} }
// Always returns nil as there's currently no cache // Always returns nil as there's currently no cache
func (s *DefaultPageStore) Reload(id int) error { func (store *DefaultPageStore) Reload(id int) error {
return nil return nil
} }
func (s *DefaultPageStore) Delete(id int) error { func (store *DefaultPageStore) Delete(id int) error {
_, err := s.delete.Exec(id) _, err := store.delete.Exec(id)
return err return err
} }

View File

@ -6,119 +6,49 @@ import (
"runtime" "runtime"
"sync" "sync"
"time" "time"
p "git.tuxpa.in/a/gosora/common/phrases"
) )
/*type HResource struct { // TODO: Allow resources in spots other than /static/ and possibly even external domains (e.g. CDNs)
Name string
Hash string
}*/
// TODO: Allow resources in spots other than /s/ and possibly even external domains (e.g. CDNs)
// TODO: Preload Trumboyg on Cosora on the forum list // TODO: Preload Trumboyg on Cosora on the forum list
type Header struct { type Header struct {
Title string Title string
//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
NoticeList []string NoticeList []string
Scripts []HScript Scripts []string
PreScriptsAsync []HScript
ScriptsAsync []HScript
//Preload []string //Preload []string
Stylesheets []HScript Stylesheets []string
Widgets PageWidgets Widgets PageWidgets
Site *site Site *site
Settings SettingMap Settings SettingMap
//Themes map[string]*Theme // TODO: Use a slice containing every theme instead of the main map for speed? Themes map[string]*Theme // TODO: Use a slice containing every theme instead of the main map for speed?
ThemesSlice []*Theme
Theme *Theme Theme *Theme
//TemplateName string // TODO: Use this to move template calls to the router rather than duplicating them over and over and over? //TemplateName string // TODO: Use this to move template calls to the router rather than duplicating them over and over and over?
CurrentUser *User // TODO: Deprecate CurrentUser on the page structs and use a pointer here CurrentUser User // TODO: Deprecate CurrentUser on the page structs and use a pointer here
Hooks *HookTable
Zone string Zone string
ZoneID int
ZoneData interface{}
Path string
MetaDesc string MetaDesc string
//OGImage string
OGDesc string
GoogSiteVerify string
IsoCode string
LooseCSP bool
ExternalMedia bool
//StartedAt time.Time
StartedAt int64
Elapsed1 string
Writer http.ResponseWriter Writer http.ResponseWriter
ExtData ExtData ExtData ExtData
} }
type HScript struct { func (header *Header) AddScript(name string) {
Name string header.Scripts = append(header.Scripts, name)
Hash string
} }
func (h *Header) getScript(name string) HScript { /*func (header *Header) Preload(name string) {
if name[0] == '/' && name[1] == '/' { header.Preload = append(header.Preload, name)
} else {
file, ok := StaticFiles.GetShort(name)
if ok {
return HScript{file.OName, file.Sha256I}
}
}
return HScript{name, ""}
}
func (h *Header) AddScript(name string) {
//log.Print("name:", name)
h.Scripts = append(h.Scripts, h.getScript(name))
}
func (h *Header) AddPreScriptAsync(name string) {
h.PreScriptsAsync = append(h.PreScriptsAsync, h.getScript(name))
}
func (h *Header) AddScriptAsync(name string) {
h.ScriptsAsync = append(h.ScriptsAsync, h.getScript(name))
}
/*func (h *Header) Preload(name string) {
h.Preload = append(h.Preload, name)
}*/ }*/
func (h *Header) AddSheet(name string) { func (header *Header) AddSheet(name string) {
h.Stylesheets = append(h.Stylesheets, h.getScript(name)) header.Stylesheets = append(header.Stylesheets, name)
} }
// ! Experimental func (header *Header) AddNotice(name string) {
func (h *Header) AddXRes(names ...string) { header.NoticeList = append(header.NoticeList, GetNoticePhrase(name))
var o string
for i, name := range names {
if name[0] == '/' && name[1] == '/' {
} else {
file, ok := StaticFiles.GetShort(name)
if ok {
name = file.OName
}
}
if i != 0 {
o += "," + name
} else {
o += name
}
}
h.Writer.Header().Set("X-Res", o)
}
func (h *Header) AddNotice(name string) {
h.NoticeList = append(h.NoticeList, p.GetNoticePhrase(name))
} }
// TODO: Add this to routes which don't use templates. E.g. Json APIs. // TODO: Add this to routes which don't use templates. E.g. Json APIs.
type HeaderLite struct { type HeaderLite struct {
Site *site Site *site
Settings SettingMap Settings SettingMap
Hooks *HookTable
ExtData ExtData ExtData ExtData
} }
@ -155,41 +85,19 @@ type Paginator struct {
LastPage int LastPage int
} }
type PaginatorMod struct {
Params template.URL
PageList []int
Page int
LastPage int
}
type CustomPagePage struct { type CustomPagePage struct {
*Header *Header
Page *CustomPage Page *CustomPage
} }
type TopicCEditPost struct {
ID int
Source string
Ref string
}
type TopicCAttachItem struct {
ID int
ImgSrc string
Path string
FullPath string
}
type TopicCPollInput struct {
Index int
Place string
}
type TopicPage struct { type TopicPage struct {
*Header *Header
ItemList []*ReplyUser ItemList []ReplyUser
Topic TopicUser Topic TopicUser
Forum *Forum Forum *Forum
Poll *Poll Poll Poll
Paginator Page int
LastPage int
} }
type TopicListSort struct { type TopicListSort struct {
@ -197,29 +105,19 @@ type TopicListSort struct {
Ascending bool Ascending bool
} }
type QuickTools struct {
CanDelete bool
CanLock bool
CanMove bool
}
type TopicListPage struct { type TopicListPage struct {
*Header *Header
TopicList []TopicsRowMut TopicList []*TopicsRow
ForumList []Forum ForumList []Forum
DefaultForum int DefaultForum int
Sort TopicListSort Sort TopicListSort
SelectedFids []int
QuickTools
Paginator Paginator
} }
type ForumPage struct { type ForumPage struct {
*Header *Header
ItemList []TopicsRowMut ItemList []*TopicsRow
Forum *Forum Forum *Forum
CanLock bool
CanMove bool
Paginator Paginator
} }
@ -230,14 +128,8 @@ type ForumsPage struct {
type ProfilePage struct { type ProfilePage struct {
*Header *Header
ItemList []*ReplyUser ItemList []ReplyUser
ProfileOwner User ProfileOwner User
CurrentScore int
NextScore int
Blocked bool
CanMessage bool
CanComment bool
ShowComments bool
} }
type CreateTopicPage struct { type CreateTopicPage struct {
@ -252,164 +144,15 @@ type IPSearchPage struct {
IP string IP string
} }
// WIP: Optional anti-bot methods
type RegisterVerifyImageGridImage struct {
Src string
}
type RegisterVerifyImageGrid struct {
Question string
Items []RegisterVerifyImageGridImage
}
type RegisterVerify struct {
NoScript bool
Image *RegisterVerifyImageGrid
}
type RegisterPage struct {
*Header
RequireEmail bool
Token string
Verify []RegisterVerify
}
type Account struct {
*Header
HTMLID string
TmplName string
Inner nobreak
}
type EmailListPage struct { type EmailListPage struct {
*Header *Header
ItemList []Email ItemList []Email
} Something interface{}
type AccountLoginsPage struct {
*Header
ItemList []LoginLogItem
Paginator
}
type AccountBlocksPage struct {
*Header
Users []*User
Paginator
}
type AccountPrivacyPage struct {
*Header
ProfileComments int
ReceiveConvos int
EnableEmbeds bool
} }
type AccountDashPage struct { type AccountDashPage struct {
*Header *Header
MFASetup bool MFASetup bool
CurrentScore int
NextScore int
NextLevel int
Percentage int
}
type LevelListItem struct {
Level int
Score int
Status string
Percentage int // 0 to 200 to fit with the CSS logic
}
type LevelListPage struct {
*Header
Levels []LevelListItem
}
type ResetPage struct {
*Header
UID int
Token string
MFA bool
}
type ConvoListRow struct {
*ConversationExtra
ShortUsers []*User
OneOnOne bool
}
type ConvoListPage struct {
*Header
Convos []ConvoListRow
Paginator
}
type ConvoViewRow struct {
*ConversationPost
User *User
ClassName string
ContentLines int
CanModify bool
}
type ConvoViewPage struct {
*Header
Convo *Conversation
Posts []ConvoViewRow
Users []*User
CanReply bool
Paginator
}
type ConvoCreatePage struct {
*Header
RecpName string
}
/* WIP for dyntmpl */
type Panel struct {
*BasePanelPage
HTMLID string
ClassNames string
TmplName string
Inner nobreak
}
type PanelAnalytics struct {
*BasePanelPage
FormAction string
TmplName string
Inner nobreak
}
type PanelAnalyticsStd struct {
Graph PanelTimeGraph
ViewItems []PanelAnalyticsItem
TimeRange string
Unit string
TimeType string
}
type PanelAnalyticsStdUnit struct {
Graph PanelTimeGraph
ViewItems []PanelAnalyticsItemUnit
TimeRange string
Unit string
TimeType string
}
type PanelAnalyticsActiveMemory struct {
Graph PanelTimeGraph
ViewItems []PanelAnalyticsItemUnit
TimeRange string
Unit string
TimeType string
MemType int
}
type PanelAnalyticsPerf struct {
Graph PanelTimeGraph
ViewItems []PanelAnalyticsItemUnit
TimeRange string
Unit string
TimeType string
PerfType int
} }
type PanelStats struct { type PanelStats struct {
@ -422,13 +165,14 @@ type PanelStats struct {
Themes int Themes int
Reports int Reports int
} }
type BasePanelPage struct { type BasePanelPage struct {
*Header *Header
Stats PanelStats Stats PanelStats
Zone string Zone string
ReportForumID int ReportForumID int
DebugAdmin bool
} }
type PanelPage struct { type PanelPage struct {
*BasePanelPage *BasePanelPage
ItemList []interface{} ItemList []interface{}
@ -437,7 +181,6 @@ type PanelPage struct {
type GridElement struct { type GridElement struct {
ID string ID string
Href string
Body string Body string
Order int // For future use Order int // For future use
Class string Class string
@ -445,69 +188,49 @@ type GridElement struct {
TextColour string TextColour string
Note string Note string
} }
type DashGrids struct {
Grid1 []GridElement
Grid2 []GridElement
}
type PanelDashboardPage struct { type PanelDashboardPage struct {
*BasePanelPage *BasePanelPage
Grids DashGrids GridItems []GridElement
} }
type PanelSetting struct { type PanelSetting struct {
*Setting *Setting
FriendlyName string FriendlyName string
} }
type PanelSettingPage struct { type PanelSettingPage struct {
*BasePanelPage *BasePanelPage
ItemList []OptionLabel ItemList []OptionLabel
Setting *PanelSetting Setting *PanelSetting
} }
type PanelUserEditPage struct {
*BasePanelPage
Groups []*Group
User *User
ShowEmail bool
}
type PanelCustomPagesPage struct { type PanelCustomPagesPage struct {
*BasePanelPage *BasePanelPage
ItemList []*CustomPage ItemList []*CustomPage
Paginator Paginator
} }
type PanelCustomPageEditPage struct { type PanelCustomPageEditPage struct {
*BasePanelPage *BasePanelPage
Page *CustomPage Page *CustomPage
} }
/*type PanelTimeGraph struct { type PanelTimeGraph struct {
Series []int64 // The counts on the left Series []int64 // The counts on the left
Labels []int64 // unixtimes for the bottom, gets converted into 1:00, 2:00, etc. with JS Labels []int64 // unixtimes for the bottom, gets converted into 1:00, 2:00, etc. with JS
}*/
type PanelTimeGraph struct {
Series [][]int64 // The counts on the left
Labels []int64 // unixtimes for the bottom, gets converted into 1:00, 2:00, etc. with JS
Legends []string
} }
type PanelAnalyticsItem struct { type PanelAnalyticsItem struct {
Time int64 Time int64
Count int64 Count int64
} }
type PanelAnalyticsItemUnit struct {
Time int64
Count int64
Unit string
}
type PanelAnalyticsPage struct { type PanelAnalyticsPage struct {
*BasePanelPage *BasePanelPage
Graph PanelTimeGraph PrimaryGraph PanelTimeGraph
ViewItems []PanelAnalyticsItem ViewItems []PanelAnalyticsItem
TimeRange string TimeRange string
Unit string
TimeType string
} }
type PanelAnalyticsRoutesItem struct { type PanelAnalyticsRoutesItem struct {
@ -518,24 +241,9 @@ type PanelAnalyticsRoutesItem struct {
type PanelAnalyticsRoutesPage struct { type PanelAnalyticsRoutesPage struct {
*BasePanelPage *BasePanelPage
ItemList []PanelAnalyticsRoutesItem ItemList []PanelAnalyticsRoutesItem
Graph PanelTimeGraph
TimeRange string TimeRange string
} }
type PanelAnalyticsRoutesPerfItem struct {
Route string
Count int
Unit string
}
type PanelAnalyticsRoutesPerfPage struct {
*BasePanelPage
ItemList []PanelAnalyticsRoutesPerfItem
Graph PanelTimeGraph
TimeRange string
}
// TODO: Rename the fields as this structure is being used in a generic way now
type PanelAnalyticsAgentsItem struct { type PanelAnalyticsAgentsItem struct {
Agent string Agent string
FriendlyAgent string FriendlyAgent string
@ -548,17 +256,10 @@ type PanelAnalyticsAgentsPage struct {
TimeRange string TimeRange string
} }
type PanelAnalyticsReferrersPage struct {
*BasePanelPage
ItemList []PanelAnalyticsAgentsItem
TimeRange string
ShowSpam bool
}
type PanelAnalyticsRoutePage struct { type PanelAnalyticsRoutePage struct {
*BasePanelPage *BasePanelPage
Route string Route string
Graph PanelTimeGraph PrimaryGraph PanelTimeGraph
ViewItems []PanelAnalyticsItem ViewItems []PanelAnalyticsItem
TimeRange string TimeRange string
} }
@ -567,14 +268,7 @@ type PanelAnalyticsAgentPage struct {
*BasePanelPage *BasePanelPage
Agent string Agent string
FriendlyAgent string FriendlyAgent string
Graph PanelTimeGraph PrimaryGraph PanelTimeGraph
TimeRange string
}
type PanelAnalyticsDuoPage struct {
*BasePanelPage
ItemList []PanelAnalyticsAgentsItem
Graph PanelTimeGraph
TimeRange string TimeRange string
} }
@ -595,12 +289,6 @@ type PanelMenuListPage struct {
ItemList []PanelMenuListItem ItemList []PanelMenuListItem
} }
type PanelWidgetListPage struct {
*BasePanelPage
Docks map[string][]WidgetEdit
BlankWidget WidgetEdit
}
type PanelMenuPage struct { type PanelMenuPage struct {
*BasePanelPage *BasePanelPage
MenuID int MenuID int
@ -612,19 +300,10 @@ type PanelMenuItemPage struct {
Item MenuItem Item MenuItem
} }
type PanelUserPageSearch struct {
Name string
Email string
Group int
Any bool
}
type PanelUserPage struct { type PanelUserPage struct {
*BasePanelPage *BasePanelPage
ItemList []*User ItemList []*User
Groups []*Group Paginator
Search PanelUserPageSearch
PaginatorMod
} }
type PanelGroupPage struct { type PanelGroupPage struct {
@ -645,7 +324,6 @@ type PanelEditGroupPage struct {
type GroupForumPermPreset struct { type GroupForumPermPreset struct {
Group *Group Group *Group
Preset string Preset string
DefaultPreset bool
} }
type PanelEditForumPage struct { type PanelEditForumPage struct {
@ -656,12 +334,6 @@ type PanelEditForumPage struct {
Active bool Active bool
Preset string Preset string
Groups []GroupForumPermPreset Groups []GroupForumPermPreset
Actions []*ForumActionAction
}
type ForumActionAction struct {
*ForumAction
ActionName string
} }
type NameLangToggle struct { type NameLangToggle struct {
@ -687,21 +359,6 @@ type PanelEditGroupPermsPage struct {
Name string Name string
LocalPerms []NameLangToggle LocalPerms []NameLangToggle
GlobalPerms []NameLangToggle GlobalPerms []NameLangToggle
ModPerms []NameLangToggle
}
type GroupPromotionExtend struct {
*GroupPromotion
FromGroup *Group
ToGroup *Group
}
type PanelEditGroupPromotionsPage struct {
*BasePanelPage
ID int
Name string
Promotions []*GroupPromotionExtend
Groups []*Group
} }
type BackupItem struct { type BackupItem struct {
@ -719,7 +376,7 @@ type PanelBackupPage struct {
type PageLogItem struct { type PageLogItem struct {
Action template.HTML Action template.HTML
IP string IPAddress string
DoneAt string DoneAt string
} }
@ -740,93 +397,18 @@ type PanelRegLogsPage struct {
Paginator Paginator
} }
type DebugPageTasks struct {
HalfSecond int
Second int
FifteenMinute int
Hour int
Day int
Shutdown int
}
type DebugPageCache struct {
Topics int
Users int
Replies int
TCap int
UCap int
RCap int
TopicListThaw bool
}
type DebugPageDatabase struct {
Topics int
Users int
Replies int
ProfileReplies int
ActivityStream int
Likes int
Attachments int
Polls int
LoginLogs int
RegLogs int
ModLogs int
AdminLogs int
Views int
ViewsAgents int
ViewsForums int
ViewsLangs int
ViewsReferrers int
ViewsSystems int
PostChunks int
TopicChunks int
}
type DebugPageDisk struct {
Static int
Attachments int
Avatars int
Logs int
Backups int
Git int
}
type PanelDebugPage struct { type PanelDebugPage struct {
*BasePanelPage *BasePanelPage
GoVersion string GoVersion string
DBVersion string DBVersion string
Uptime string Uptime string
DBConns int OpenConns int
DBAdapter string DBAdapter string
Goroutines int Goroutines int
CPUs int CPUs int
HttpConns int
Tasks DebugPageTasks
MemStats runtime.MemStats MemStats runtime.MemStats
Cache DebugPageCache
Database DebugPageDatabase
Disk DebugPageDisk
}
type PanelTaskTask struct {
Name string
Type int // 0 = halfsec, 1 = sec, 2 = fifteenmin, 3 = hour, 4 = shutdown
}
type PanelTaskType struct {
Name string
FAvg string
}
type PanelTaskPage struct {
*BasePanelPage
Tasks []PanelTaskTask
Types []PanelTaskType
} }
type PageSimple struct { type PageSimple struct {
@ -840,9 +422,6 @@ type AreYouSure struct {
} }
// TODO: Write a test for this // TODO: Write a test for this
func DefaultHeader(w http.ResponseWriter, u *User) *Header { func DefaultHeader(w http.ResponseWriter, user User) *Header {
return &Header{Site: Site, Theme: Themes[fallbackTheme], CurrentUser: u, Writer: w} return &Header{Site: Site, Theme: Themes[fallbackTheme], CurrentUser: user, Writer: w}
}
func SimpleDefaultHeader(w http.ResponseWriter) *Header {
return &Header{Site: Site, Theme: Themes[fallbackTheme], CurrentUser: &GuestUser, Writer: w}
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,76 +0,0 @@
package common
import (
"crypto/subtle"
"database/sql"
"errors"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var PasswordResetter *DefaultPasswordResetter
var ErrBadResetToken = errors.New("This reset token has expired.")
type DefaultPasswordResetter struct {
getTokens *sql.Stmt
create *sql.Stmt
delete *sql.Stmt
}
/*
type PasswordReset struct {
Email string `q:"email"`
Uid int `q:"uid"`
Validated bool `q:"validated"`
Token string `q:"token"`
CreatedAt time.Time `q:"createdAt"`
}
*/
func NewDefaultPasswordResetter(acc *qgen.Accumulator) (*DefaultPasswordResetter, error) {
pr := "password_resets"
return &DefaultPasswordResetter{
getTokens: acc.Select(pr).Columns("token").Where("uid=?").Prepare(),
create: acc.Insert(pr).Columns("email,uid,validated,token,createdAt").Fields("?,?,0,?,UTC_TIMESTAMP()").Prepare(),
//create: acc.Insert(pr).Cols("email,uid,validated=0,token,createdAt=UTC_TIMESTAMP()").Prep(),
delete: acc.Delete(pr).Where("uid=?").Prepare(),
//model: acc.Model(w).Cols("email,uid,validated=0,token").Key("uid").CreatedAt("createdAt").Prep(),
}, acc.FirstError()
}
func (r *DefaultPasswordResetter) Create(email string, uid int, token string) error {
_, err := r.create.Exec(email, uid, token)
return err
}
func (r *DefaultPasswordResetter) FlushTokens(uid int) error {
_, err := r.delete.Exec(uid)
return err
}
func (r *DefaultPasswordResetter) ValidateToken(uid int, token string) error {
rows, err := r.getTokens.Query(uid)
if err != nil {
return err
}
defer rows.Close()
success := false
for rows.Next() {
var rtoken string
if err := rows.Scan(&rtoken); err != nil {
return err
}
if subtle.ConstantTimeCompare([]byte(token), []byte(rtoken)) == 1 {
success = true
}
}
if err = rows.Err(); err != nil {
return err
}
if !success {
return ErrBadResetToken
}
return nil
}

View File

@ -4,8 +4,7 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"git.tuxpa.in/a/gosora/common/phrases" "../query_gen/lib"
qgen "git.tuxpa.in/a/gosora/query_gen"
) )
// TODO: Refactor the perms system // TODO: Refactor the perms system
@ -38,65 +37,54 @@ var GlobalPermList = []string{
"ViewAdminLogs", "ViewAdminLogs",
"ViewIPs", "ViewIPs",
"UploadFiles", "UploadFiles",
"UploadAvatars",
"UseConvos",
"UseConvosOnlyWithMod",
"CreateProfileReply",
"AutoEmbed",
"AutoLink",
} }
// Permission Structure: ActionComponent[Subcomponent]Flag // Permission Structure: ActionComponent[Subcomponent]Flag
type Perms struct { type Perms struct {
// Global Permissions // Global Permissions
BanUsers bool `json:",omitempty"` BanUsers bool
ActivateUsers bool `json:",omitempty"` ActivateUsers bool
EditUser bool `json:",omitempty"` EditUser bool
EditUserEmail bool `json:",omitempty"` EditUserEmail bool
EditUserPassword bool `json:",omitempty"` EditUserPassword bool
EditUserGroup bool `json:",omitempty"` EditUserGroup bool
EditUserGroupSuperMod bool `json:",omitempty"` EditUserGroupSuperMod bool
EditUserGroupAdmin bool `json:",omitempty"` EditUserGroupAdmin bool
EditGroup bool `json:",omitempty"` EditGroup bool
EditGroupLocalPerms bool `json:",omitempty"` EditGroupLocalPerms bool
EditGroupGlobalPerms bool `json:",omitempty"` EditGroupGlobalPerms bool
EditGroupSuperMod bool `json:",omitempty"` EditGroupSuperMod bool
EditGroupAdmin bool `json:",omitempty"` EditGroupAdmin bool
ManageForums bool `json:",omitempty"` // This could be local, albeit limited for per-forum managers? ManageForums bool // This could be local, albeit limited for per-forum managers?
EditSettings bool `json:",omitempty"` EditSettings bool
ManageThemes bool `json:",omitempty"` ManageThemes bool
ManagePlugins bool `json:",omitempty"` ManagePlugins bool
ViewAdminLogs bool `json:",omitempty"` ViewAdminLogs bool
ViewIPs bool `json:",omitempty"` ViewIPs bool
// Global non-staff permissions // Global non-staff permissions
UploadFiles bool `json:",omitempty"` UploadFiles bool
UploadAvatars bool `json:",omitempty"` // TODO: Add a permission for enabling avatars
UseConvos bool `json:",omitempty"`
UseConvosOnlyWithMod bool `json:",omitempty"`
CreateProfileReply bool `json:",omitempty"`
AutoEmbed bool `json:",omitempty"`
AutoLink bool `json:",omitempty"`
// Forum permissions // Forum permissions
ViewTopic bool `json:",omitempty"` ViewTopic bool
//ViewOwnTopic bool `json:",omitempty"` //ViewOwnTopic bool
LikeItem bool `json:",omitempty"` LikeItem bool
CreateTopic bool `json:",omitempty"` CreateTopic bool
EditTopic bool `json:",omitempty"` EditTopic bool
DeleteTopic bool `json:",omitempty"` DeleteTopic bool
CreateReply bool `json:",omitempty"` CreateReply bool
//CreateReplyToOwn bool `json:",omitempty"` //CreateReplyToOwn bool
EditReply bool `json:",omitempty"` EditReply bool
//EditOwnReply bool `json:",omitempty"` //EditOwnReply bool
DeleteReply bool `json:",omitempty"` DeleteReply bool
//DeleteOwnReply bool `json:",omitempty"` //DeleteOwnReply bool
PinTopic bool `json:",omitempty"` PinTopic bool
CloseTopic bool `json:",omitempty"` CloseTopic bool
//CloseOwnTopic bool `json:",omitempty"` //CloseOwnTopic bool
MoveTopic bool `json:",omitempty"` MoveTopic bool
//ExtData map[string]bool `json:",omitempty"` //ExtData map[string]bool
} }
func init() { func init() {
@ -131,12 +119,6 @@ func init() {
ViewIPs: true, ViewIPs: true,
UploadFiles: true, UploadFiles: true,
UploadAvatars: true,
UseConvos: true,
UseConvosOnlyWithMod: true,
CreateProfileReply: true,
AutoEmbed: true,
AutoLink: true,
ViewTopic: true, ViewTopic: true,
LikeItem: true, LikeItem: true,
@ -176,7 +158,7 @@ func StripInvalidPreset(preset string) string {
// TODO: Move this into the phrase system? // TODO: Move this into the phrase system?
func PresetToLang(preset string) string { func PresetToLang(preset string) string {
phrases := phrases.GetAllPermPresets() phrases := GetAllPermPresets()
phrase, ok := phrases[preset] phrase, ok := phrases[preset]
if !ok { if !ok {
phrase = phrases["unknown"] phrase = phrases["unknown"]
@ -186,54 +168,59 @@ func PresetToLang(preset string) string {
// TODO: Is this racey? // TODO: Is this racey?
// TODO: Test this along with the rest of the perms system // TODO: Test this along with the rest of the perms system
func RebuildGroupPermissions(g *Group) error { func RebuildGroupPermissions(gid int) error {
var permstr []byte var permstr []byte
log.Print("Reloading a group") log.Print("Reloading a group")
// TODO: Avoid re-initting this all the time // TODO: Avoid re-initting this all the time
getGroupPerms, e := qgen.Builder.SimpleSelect("users_groups", "permissions", "gid=?", "", "") getGroupPerms, err := qgen.Builder.SimpleSelect("users_groups", "permissions", "gid = ?", "", "")
if e != nil { if err != nil {
return e return err
} }
defer getGroupPerms.Close() defer getGroupPerms.Close()
e = getGroupPerms.QueryRow(g.ID).Scan(&permstr) err = getGroupPerms.QueryRow(gid).Scan(&permstr)
if e != nil { if err != nil {
return e return err
} }
tmpPerms := Perms{ tmpPerms := Perms{
//ExtData: make(map[string]bool), //ExtData: make(map[string]bool),
} }
e = json.Unmarshal(permstr, &tmpPerms) err = json.Unmarshal(permstr, &tmpPerms)
if e != nil { if err != nil {
return e return err
} }
g.Perms = tmpPerms
group, err := Groups.Get(gid)
if err != nil {
return err
}
group.Perms = tmpPerms
return nil return nil
} }
func OverridePerms(p *Perms, status bool) { func OverridePerms(perms *Perms, status bool) {
if status { if status {
*p = AllPerms *perms = AllPerms
} else { } else {
*p = BlankPerms *perms = BlankPerms
} }
} }
// TODO: We need a better way of overriding forum perms rather than setting them one by one // TODO: We need a better way of overriding forum perms rather than setting them one by one
func OverrideForumPerms(p *Perms, status bool) { func OverrideForumPerms(perms *Perms, status bool) {
p.ViewTopic = status perms.ViewTopic = status
p.LikeItem = status perms.LikeItem = status
p.CreateTopic = status perms.CreateTopic = status
p.EditTopic = status perms.EditTopic = status
p.DeleteTopic = status perms.DeleteTopic = status
p.CreateReply = status perms.CreateReply = status
p.EditReply = status perms.EditReply = status
p.DeleteReply = status perms.DeleteReply = status
p.PinTopic = status perms.PinTopic = status
p.CloseTopic = status perms.CloseTopic = status
p.MoveTopic = status perms.MoveTopic = status
} }
func RegisterPluginPerm(name string) { func RegisterPluginPerm(name string) {

View File

@ -1,24 +1,21 @@
/* /*
* *
* Gosora Phrase System * Gosora Phrase System
* Copyright Azareal 2017 - 2020 * Copyright Azareal 2017 - 2019
* *
*/ */
package phrases package common
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time"
) )
// TODO: Add a phrase store? // TODO: Add a phrase store?
@ -39,21 +36,17 @@ type LevelPhrases struct {
// ! For the sake of thread safety, you must never modify a *LanguagePack directly, but to create a copy of it and overwrite the entry in the sync.Map // ! For the sake of thread safety, you must never modify a *LanguagePack directly, but to create a copy of it and overwrite the entry in the sync.Map
type LanguagePack struct { type LanguagePack struct {
Name string Name string
IsoCode string
ModTime time.Time
//LastUpdated string
// Should we use a sync map or a struct for these? It would be nice, if we could keep all the phrases consistent. // Should we use a sync map or a struct for these? It would be nice, if we could keep all the phrases consistent.
Levels LevelPhrases Levels LevelPhrases
Perms map[string]string GlobalPerms map[string]string
LocalPerms map[string]string
SettingPhrases map[string]string SettingPhrases map[string]string
PermPresets map[string]string PermPresets map[string]string
Accounts map[string]string // TODO: Apply these phrases in the software proper Accounts map[string]string // TODO: Apply these phrases in the software proper
UserAgents map[string]string UserAgents map[string]string
OperatingSystems map[string]string OperatingSystems map[string]string
HumanLanguages map[string]string HumanLanguages map[string]string
Errors map[string]string // Temp stand-in Errors map[string]map[string]string // map[category]map[name]value
ErrorsBytes map[string][]byte
NoticePhrases map[string]string NoticePhrases map[string]string
PageTitles map[string]string PageTitles map[string]string
TmplPhrases map[string]string TmplPhrases map[string]string
@ -66,58 +59,32 @@ type LanguagePack struct {
var langPacks sync.Map // nolint it is used var langPacks sync.Map // nolint it is used
var langTmplIndicesToNames [][]string // [tmplID][index]phraseName var langTmplIndicesToNames [][]string // [tmplID][index]phraseName
func InitPhrases(lang string) error { func InitPhrases() error {
log.Print("Loading the language packs") log.Print("Loading the language packs")
err := filepath.Walk("./langs", func(path string, f os.FileInfo, err error) error { err := filepath.Walk("./langs", func(path string, f os.FileInfo, err error) error {
if f.IsDir() { if f.IsDir() {
return nil return nil
} }
if err != nil {
return err
}
ext := filepath.Ext("/langs/" + path)
if ext != ".json" {
log.Printf("Found a '%s' in /langs/", ext)
return nil
}
data, err := ioutil.ReadFile(path) data, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return err return err
} }
var ext = filepath.Ext("/langs/" + path)
if ext != ".json" {
log.Printf("Found a '%s' in /langs/", ext)
return nil
}
var langPack LanguagePack var langPack LanguagePack
err = json.Unmarshal(data, &langPack) err = json.Unmarshal(data, &langPack)
if err != nil { if err != nil {
return err return err
} }
langPack.ModTime = f.ModTime()
langPack.ErrorsBytes = make(map[string][]byte)
for name, phrase := range langPack.Errors {
langPack.ErrorsBytes[name] = []byte(phrase)
}
// [prefix][name]phrase // [prefix][name]phrase
langPack.TmplPhrasesPrefixes = make(map[string]map[string]string) langPack.TmplPhrasesPrefixes = make(map[string]map[string]string)
conMap := make(map[string]string) // Cache phrase strings so we can de-dupe items to reduce memory use. There appear to be some minor improvements with this, although we would need a more thorough check to be sure.
for name, phrase := range langPack.TmplPhrases {
_, ok := conMap[phrase]
if !ok {
conMap[phrase] = phrase
}
cItem := conMap[phrase]
prefix := strings.Split(name, ".")[0]
_, ok = langPack.TmplPhrasesPrefixes[prefix]
if !ok {
langPack.TmplPhrasesPrefixes[prefix] = make(map[string]string)
}
langPack.TmplPhrasesPrefixes[prefix][name] = cItem
}
// [prefix][name]phrase
/*langPack.TmplPhrasesPrefixes = make(map[string]map[string]string)
for name, phrase := range langPack.TmplPhrases { for name, phrase := range langPack.TmplPhrases {
prefix := strings.Split(name, ".")[0] prefix := strings.Split(name, ".")[0]
_, ok := langPack.TmplPhrasesPrefixes[prefix] _, ok := langPack.TmplPhrasesPrefixes[prefix]
@ -125,21 +92,19 @@ func InitPhrases(lang string) error {
langPack.TmplPhrasesPrefixes[prefix] = make(map[string]string) langPack.TmplPhrasesPrefixes[prefix] = make(map[string]string)
} }
langPack.TmplPhrasesPrefixes[prefix][name] = phrase langPack.TmplPhrasesPrefixes[prefix][name] = phrase
}*/ }
langPack.TmplIndicesToPhrases = make([][][]byte, len(langTmplIndicesToNames)) langPack.TmplIndicesToPhrases = make([][][]byte, len(langTmplIndicesToNames))
for tmplID, phraseNames := range langTmplIndicesToNames { for tmplID, phraseNames := range langTmplIndicesToNames {
phraseSet := make([][]byte, len(phraseNames)) var phraseSet = make([][]byte, len(phraseNames))
for index, phraseName := range phraseNames { for index, phraseName := range phraseNames {
phrase, ok := langPack.TmplPhrases[phraseName] phrase, ok := langPack.TmplPhrases[phraseName]
if !ok { if !ok {
log.Printf("langPack.TmplPhrases: %+v\n", langPack.TmplPhrases) log.Print("Couldn't find template phrase '" + phraseName + "'")
panic("Couldn't find template phrase '" + phraseName + "'")
} }
phraseSet[index] = []byte(phrase) phraseSet[index] = []byte(phrase)
} }
langPack.TmplIndicesToPhrases[tmplID] = phraseSet langPack.TmplIndicesToPhrases[tmplID] = phraseSet
TmplIndexCallback(tmplID, phraseSet)
} }
log.Print("Adding the '" + langPack.Name + "' language pack") log.Print("Adding the '" + langPack.Name + "' language pack")
@ -155,9 +120,9 @@ func InitPhrases(lang string) error {
return errors.New("You don't have any language packs") return errors.New("You don't have any language packs")
} }
langPack, ok := langPacks.Load(lang) langPack, ok := langPacks.Load(Site.Language)
if !ok { if !ok {
return errors.New("Couldn't find the " + lang + " language pack") return errors.New("Couldn't find the " + Site.Language + " language pack")
} }
currentLangPack.Store(langPack) currentLangPack.Store(langPack)
return nil return nil
@ -175,22 +140,18 @@ func SaveLangPack(langPack *LanguagePack) error {
return nil return nil
} }
func GetLangPack() *LanguagePack { // TODO: Merge these two maps?
return currentLangPack.Load().(*LanguagePack) func GetGlobalPermPhrase(name string) string {
} res, ok := currentLangPack.Load().(*LanguagePack).GlobalPerms[name]
func GetLevelPhrase(level int) string {
levelPhrases := currentLangPack.Load().(*LanguagePack).Levels
if len(levelPhrases.Levels) > 0 && level < len(levelPhrases.Levels) {
return strings.Replace(levelPhrases.Levels[level], "{0}", strconv.Itoa(level), -1)
}
return strings.Replace(levelPhrases.Level, "{0}", strconv.Itoa(level), -1)
}
func GetPermPhrase(name string) string {
res, ok := currentLangPack.Load().(*LanguagePack).Perms[name]
if !ok { if !ok {
return getPlaceholder("perms", name) return getPhrasePlaceholder("perms", name)
}
return res
}
func GetLocalPermPhrase(name string) string {
res, ok := currentLangPack.Load().(*LanguagePack).LocalPerms[name]
if !ok {
return getPhrasePlaceholder("perms", name)
} }
return res return res
} }
@ -198,7 +159,7 @@ func GetPermPhrase(name string) string {
func GetSettingPhrase(name string) string { func GetSettingPhrase(name string) string {
res, ok := currentLangPack.Load().(*LanguagePack).SettingPhrases[name] res, ok := currentLangPack.Load().(*LanguagePack).SettingPhrases[name]
if !ok { if !ok {
return getPlaceholder("settings", name) return getPhrasePlaceholder("settings", name)
} }
return res return res
} }
@ -214,7 +175,7 @@ func GetAllPermPresets() map[string]string {
func GetAccountPhrase(name string) string { func GetAccountPhrase(name string) string {
res, ok := currentLangPack.Load().(*LanguagePack).Accounts[name] res, ok := currentLangPack.Load().(*LanguagePack).Accounts[name]
if !ok { if !ok {
return getPlaceholder("account", name) return getPhrasePlaceholder("account", name)
} }
return res return res
} }
@ -238,23 +199,16 @@ func GetOSPhrase(name string) (string, bool) {
func GetHumanLangPhrase(name string) (string, bool) { func GetHumanLangPhrase(name string) (string, bool) {
res, ok := currentLangPack.Load().(*LanguagePack).HumanLanguages[name] res, ok := currentLangPack.Load().(*LanguagePack).HumanLanguages[name]
if !ok { if !ok {
return getPlaceholder("humanlang", name), false return getPhrasePlaceholder("humanlang", name), false
} }
return res, true return res, true
} }
// TODO: Does comma ok work with multi-dimensional maps? // TODO: Does comma ok work with multi-dimensional maps?
func GetErrorPhrase(name string) string { func GetErrorPhrase(category string, name string) string {
res, ok := currentLangPack.Load().(*LanguagePack).Errors[name] res, ok := currentLangPack.Load().(*LanguagePack).Errors[category][name]
if !ok { if !ok {
return getPlaceholder("error", name) return getPhrasePlaceholder("error", name)
}
return res
}
func GetErrorPhraseBytes(name string) []byte {
res, ok := currentLangPack.Load().(*LanguagePack).ErrorsBytes[name]
if !ok {
return getPlaceholderBytes("error", name)
} }
return res return res
} }
@ -262,7 +216,7 @@ func GetErrorPhraseBytes(name string) []byte {
func GetNoticePhrase(name string) string { func GetNoticePhrase(name string) string {
res, ok := currentLangPack.Load().(*LanguagePack).NoticePhrases[name] res, ok := currentLangPack.Load().(*LanguagePack).NoticePhrases[name]
if !ok { if !ok {
return getPlaceholder("notices", name) return getPhrasePlaceholder("notices", name)
} }
return res return res
} }
@ -270,35 +224,19 @@ func GetNoticePhrase(name string) string {
func GetTitlePhrase(name string) string { func GetTitlePhrase(name string) string {
res, ok := currentLangPack.Load().(*LanguagePack).PageTitles[name] res, ok := currentLangPack.Load().(*LanguagePack).PageTitles[name]
if !ok { if !ok {
return getPlaceholder("title", name) return getPhrasePlaceholder("title", name)
} }
return res return res
} }
func GetTitlePhrasef(name string, params ...interface{}) string {
res, ok := currentLangPack.Load().(*LanguagePack).PageTitles[name]
if !ok {
return getPlaceholder("title", name)
}
return fmt.Sprintf(res, params...)
}
func GetTmplPhrase(name string) string { func GetTmplPhrase(name string) string {
res, ok := currentLangPack.Load().(*LanguagePack).TmplPhrases[name] res, ok := currentLangPack.Load().(*LanguagePack).TmplPhrases[name]
if !ok { if !ok {
return getPlaceholder("tmpl", name) return getPhrasePlaceholder("tmpl", name)
} }
return res return res
} }
func GetTmplPhrasef(name string, params ...interface{}) string {
res, ok := currentLangPack.Load().(*LanguagePack).TmplPhrases[name]
if !ok {
return getPlaceholder("tmpl", name)
}
return fmt.Sprintf(res, params...)
}
func GetTmplPhrases() map[string]string { func GetTmplPhrases() map[string]string {
return currentLangPack.Load().(*LanguagePack).TmplPhrases return currentLangPack.Load().(*LanguagePack).TmplPhrases
} }
@ -308,17 +246,9 @@ func GetTmplPhrasesByPrefix(prefix string) (phrases map[string]string, ok bool)
return res, ok return res, ok
} }
func getPlaceholder(prefix, suffix string) string { func getPhrasePlaceholder(prefix string, suffix string) string {
return "{lang." + prefix + "[" + suffix + "]}" return "{lang." + prefix + "[" + suffix + "]}"
} }
func getPlaceholderBytes(prefix, suffix string) []byte {
return []byte("{lang." + prefix + "[" + suffix + "]}")
}
// ! Please don't mutate *LanguagePack
func GetCurrentLangPack() *LanguagePack {
return currentLangPack.Load().(*LanguagePack)
}
// ? - Use runtime reflection for updating phrases? // ? - Use runtime reflection for updating phrases?
// TODO: Implement these // TODO: Implement these
@ -365,15 +295,3 @@ func RegisterTmplPhraseNames(phraseNames []string) (tmplID int) {
func GetTmplPhrasesBytes(tmplID int) [][]byte { func GetTmplPhrasesBytes(tmplID int) [][]byte {
return currentLangPack.Load().(*LanguagePack).TmplIndicesToPhrases[tmplID] return currentLangPack.Load().(*LanguagePack).TmplIndicesToPhrases[tmplID]
} }
// New
var indexCallbacks []func([][]byte)
func TmplIndexCallback(tmplID int, phraseSet [][]byte) {
indexCallbacks[tmplID](phraseSet)
}
func AddTmplIndexCallback(h func([][]byte)) {
indexCallbacks = append(indexCallbacks, h)
}

View File

@ -46,6 +46,7 @@ func InitPluginLangs() error {
for _, pluginLang := range pluginLangs { for _, pluginLang := range pluginLangs {
pluginLang.Init() pluginLang.Init()
} }
pluginList, err := GetPluginFiles() pluginList, err := GetPluginFiles()
if err != nil { if err != nil {
return err return err
@ -66,23 +67,20 @@ func InitPluginLangs() error {
continue continue
} }
e := func(field, name string) error {
return errors.New("The " + field + " field must not be blank on plugin '" + name + "'")
}
if plugin.UName == "" { if plugin.UName == "" {
return e("UName", pluginItem) return errors.New("The UName field must not be blank on plugin '" + pluginItem + "'")
} }
if plugin.Name == "" { if plugin.Name == "" {
return e("Name", pluginItem) return errors.New("The Name field must not be blank on plugin '" + pluginItem + "'")
} }
if plugin.Author == "" { if plugin.Author == "" {
return e("Author", pluginItem) return errors.New("The Author field must not be blank on plugin '" + pluginItem + "'")
} }
if plugin.Main == "" { if plugin.Main == "" {
return errors.New("Couldn't find a main file for plugin '" + pluginItem + "'") return errors.New("Couldn't find a main file for plugin '" + pluginItem + "'")
} }
ext := filepath.Ext(plugin.Main) var ext = filepath.Ext(plugin.Main)
pluginLang, err := ExtToPluginLang(ext) pluginLang, err := ExtToPluginLang(ext)
if err != nil { if err != nil {
return err return err

View File

@ -1,109 +0,0 @@
package common
import (
"database/sql"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var pollStmts PollStmts
type Poll struct {
ID int
ParentID int
ParentTable string
Type int // 0: Single choice, 1: Multiple choice, 2: Multiple choice w/ points
AntiCheat bool // Apply various mitigations for cheating
// GroupPower map[gid]points // The number of points a group can spend in this poll, defaults to 1
Options map[int]string
Results map[int]int // map[optionIndex]points
QuickOptions []PollOption // TODO: Fix up the template transpiler so we don't need to use this hack anymore
VoteCount int
}
// TODO: Use a transaction for this?
// TODO: Add a voters table with castAt / IP data and only populate it when poll anti-cheat is on
func (p *Poll) CastVote(optionIndex, uid int, ip string) error {
if Config.DisablePollIP || !p.AntiCheat {
ip = ""
}
_, e := pollStmts.addVote.Exec(p.ID, uid, optionIndex, ip)
if e != nil {
return e
}
_, e = pollStmts.incVoteCount.Exec(p.ID)
if e != nil {
return e
}
_, e = pollStmts.incVoteCountForOption.Exec(optionIndex, p.ID)
return e
}
func (p *Poll) Delete() error {
_, e := pollStmts.deletePollVotes.Exec(p.ID)
if e != nil {
return e
}
_, e = pollStmts.deletePollOptions.Exec(p.ID)
if e != nil {
return e
}
_, e = pollStmts.deletePoll.Exec(p.ID)
_ = Polls.GetCache().Remove(p.ID)
return e
}
func (p *Poll) Resultsf(f func(votes int) error) error {
rows, e := pollStmts.getResults.Query(p.ID)
if e != nil {
return e
}
defer rows.Close()
var votes int
for rows.Next() {
if e := rows.Scan(&votes); e != nil {
return e
}
if e := f(votes); e != nil {
return e
}
}
return rows.Err()
}
func (p *Poll) Copy() Poll {
return *p
}
type PollStmts struct {
getResults *sql.Stmt
addVote *sql.Stmt
incVoteCount *sql.Stmt
incVoteCountForOption *sql.Stmt
deletePoll *sql.Stmt
deletePollOptions *sql.Stmt
deletePollVotes *sql.Stmt
}
func init() {
DbInits.Add(func(acc *qgen.Accumulator) error {
p := "polls"
wh := "pollID=?"
pollStmts = PollStmts{
getResults: acc.Select("polls_options").Columns("votes").Where("pollID=?").Orderby("option ASC").Prepare(),
addVote: acc.Insert("polls_votes").Columns("pollID,uid,option,castAt,ip").Fields("?,?,?,UTC_TIMESTAMP(),?").Prepare(),
incVoteCount: acc.Update(p).Set("votes=votes+1").Where(wh).Prepare(),
incVoteCountForOption: acc.Update("polls_options").Set("votes=votes+1").Where("option=? AND pollID=?").Prepare(),
deletePoll: acc.Delete(p).Where(wh).Prepare(),
deletePollOptions: acc.Delete("polls_options").Where(wh).Prepare(),
deletePollVotes: acc.Delete("polls_votes").Where(wh).Prepare(),
}
return acc.FirstError()
})
}

View File

@ -39,10 +39,10 @@ func NewMemoryPollCache(capacity int) *MemoryPollCache {
} }
// Get fetches a poll by ID. Returns ErrNoRows if not present. // Get fetches a poll by ID. Returns ErrNoRows if not present.
func (s *MemoryPollCache) Get(id int) (*Poll, error) { func (mus *MemoryPollCache) Get(id int) (*Poll, error) {
s.RLock() mus.RLock()
item, ok := s.items[id] item, ok := mus.items[id]
s.RUnlock() mus.RUnlock()
if ok { if ok {
return item, nil return item, nil
} }
@ -50,19 +50,19 @@ func (s *MemoryPollCache) Get(id int) (*Poll, error) {
} }
// BulkGet fetches multiple polls by their IDs. Indices without polls will be set to nil, so make sure you check for those, we might want to change this behaviour to make it less confusing. // BulkGet fetches multiple polls by their IDs. Indices without polls will be set to nil, so make sure you check for those, we might want to change this behaviour to make it less confusing.
func (s *MemoryPollCache) BulkGet(ids []int) (list []*Poll) { func (mus *MemoryPollCache) BulkGet(ids []int) (list []*Poll) {
list = make([]*Poll, len(ids)) list = make([]*Poll, len(ids))
s.RLock() mus.RLock()
for i, id := range ids { for i, id := range ids {
list[i] = s.items[id] list[i] = mus.items[id]
} }
s.RUnlock() mus.RUnlock()
return list return list
} }
// GetUnsafe fetches a poll by ID. Returns ErrNoRows if not present. THIS METHOD IS NOT THREAD-SAFE. // GetUnsafe fetches a poll by ID. Returns ErrNoRows if not present. THIS METHOD IS NOT THREAD-SAFE.
func (s *MemoryPollCache) GetUnsafe(id int) (*Poll, error) { func (mus *MemoryPollCache) GetUnsafe(id int) (*Poll, error) {
item, ok := s.items[id] item, ok := mus.items[id]
if ok { if ok {
return item, nil return item, nil
} }
@ -70,96 +70,95 @@ func (s *MemoryPollCache) GetUnsafe(id int) (*Poll, error) {
} }
// Set overwrites the value of a poll in the cache, whether it's present or not. May return a capacity overflow error. // Set overwrites the value of a poll in the cache, whether it's present or not. May return a capacity overflow error.
func (s *MemoryPollCache) Set(item *Poll) error { func (mus *MemoryPollCache) Set(item *Poll) error {
s.Lock() mus.Lock()
user, ok := s.items[item.ID] user, ok := mus.items[item.ID]
if ok { if ok {
s.Unlock() mus.Unlock()
*user = *item *user = *item
} else if int(s.length) >= s.capacity { } else if int(mus.length) >= mus.capacity {
s.Unlock() mus.Unlock()
return ErrStoreCapacityOverflow return ErrStoreCapacityOverflow
} else { } else {
s.items[item.ID] = item mus.items[item.ID] = item
s.Unlock() mus.Unlock()
atomic.AddInt64(&s.length, 1) atomic.AddInt64(&mus.length, 1)
} }
return nil return nil
} }
// Add adds a poll to the cache, similar to Set, but it's only intended for new items. This method might be deprecated in the near future, use Set. May return a capacity overflow error. // Add adds a poll to the cache, similar to Set, but it's only intended for new items. This method might be deprecated in the near future, use Set. May return a capacity overflow error.
// ? Is this redundant if we have Set? Are the efficiency wins worth this? Is this even used? // ? Is this redundant if we have Set? Are the efficiency wins worth this? Is this even used?
func (s *MemoryPollCache) Add(item *Poll) error { func (mus *MemoryPollCache) Add(item *Poll) error {
s.Lock() mus.Lock()
if int(s.length) >= s.capacity { if int(mus.length) >= mus.capacity {
s.Unlock() mus.Unlock()
return ErrStoreCapacityOverflow return ErrStoreCapacityOverflow
} }
s.items[item.ID] = item mus.items[item.ID] = item
s.length = int64(len(s.items)) mus.length = int64(len(mus.items))
s.Unlock() mus.Unlock()
return nil return nil
} }
// AddUnsafe is the unsafe version of Add. May return a capacity overflow error. THIS METHOD IS NOT THREAD-SAFE. // AddUnsafe is the unsafe version of Add. May return a capacity overflow error. THIS METHOD IS NOT THREAD-SAFE.
func (s *MemoryPollCache) AddUnsafe(item *Poll) error { func (mus *MemoryPollCache) AddUnsafe(item *Poll) error {
if int(s.length) >= s.capacity { if int(mus.length) >= mus.capacity {
return ErrStoreCapacityOverflow return ErrStoreCapacityOverflow
} }
s.items[item.ID] = item mus.items[item.ID] = item
s.length = int64(len(s.items)) mus.length = int64(len(mus.items))
return nil return nil
} }
// Remove removes a poll from the cache by ID, if they exist. Returns ErrNoRows if no items exist. // Remove removes a poll from the cache by ID, if they exist. Returns ErrNoRows if no items exist.
func (s *MemoryPollCache) Remove(id int) error { func (mus *MemoryPollCache) Remove(id int) error {
s.Lock() mus.Lock()
_, ok := s.items[id] _, ok := mus.items[id]
if !ok { if !ok {
s.Unlock() mus.Unlock()
return ErrNoRows return ErrNoRows
} }
delete(s.items, id) delete(mus.items, id)
s.Unlock() mus.Unlock()
atomic.AddInt64(&s.length, -1) atomic.AddInt64(&mus.length, -1)
return nil return nil
} }
// RemoveUnsafe is the unsafe version of Remove. THIS METHOD IS NOT THREAD-SAFE. // RemoveUnsafe is the unsafe version of Remove. THIS METHOD IS NOT THREAD-SAFE.
func (s *MemoryPollCache) RemoveUnsafe(id int) error { func (mus *MemoryPollCache) RemoveUnsafe(id int) error {
_, ok := s.items[id] _, ok := mus.items[id]
if !ok { if !ok {
return ErrNoRows return ErrNoRows
} }
delete(s.items, id) delete(mus.items, id)
atomic.AddInt64(&s.length, -1) atomic.AddInt64(&mus.length, -1)
return nil return nil
} }
// Flush removes all the polls from the cache, useful for tests. // Flush removes all the polls from the cache, useful for tests.
func (s *MemoryPollCache) Flush() { func (mus *MemoryPollCache) Flush() {
m := make(map[int]*Poll) mus.Lock()
s.Lock() mus.items = make(map[int]*Poll)
s.items = m mus.length = 0
s.length = 0 mus.Unlock()
s.Unlock()
} }
// ! Is this concurrent? // ! Is this concurrent?
// Length returns the number of polls in the memory cache // Length returns the number of polls in the memory cache
func (s *MemoryPollCache) Length() int { func (mus *MemoryPollCache) Length() int {
return int(s.length) return int(mus.length)
} }
// SetCapacity sets the maximum number of polls which this cache can hold // SetCapacity sets the maximum number of polls which this cache can hold
func (s *MemoryPollCache) SetCapacity(capacity int) { func (mus *MemoryPollCache) SetCapacity(capacity int) {
// Ints are moved in a single instruction, so this should be thread-safe // Ints are moved in a single instruction, so this should be thread-safe
s.capacity = capacity mus.capacity = capacity
} }
// GetCapacity returns the maximum number of polls this cache can hold // GetCapacity returns the maximum number of polls this cache can hold
func (s *MemoryPollCache) GetCapacity() int { func (mus *MemoryPollCache) GetCapacity() int {
return s.capacity return mus.capacity
} }
// NullPollCache is a poll cache to be used when you don't want a cache and just want queries to passthrough to the database // NullPollCache is a poll cache to be used when you don't want a cache and just want queries to passthrough to the database
@ -172,37 +171,37 @@ func NewNullPollCache() *NullPollCache {
} }
// nolint // nolint
func (s *NullPollCache) Get(id int) (*Poll, error) { func (mus *NullPollCache) Get(id int) (*Poll, error) {
return nil, ErrNoRows return nil, ErrNoRows
} }
func (s *NullPollCache) BulkGet(ids []int) (list []*Poll) { func (mus *NullPollCache) BulkGet(ids []int) (list []*Poll) {
return make([]*Poll, len(ids)) return make([]*Poll, len(ids))
} }
func (s *NullPollCache) GetUnsafe(id int) (*Poll, error) { func (mus *NullPollCache) GetUnsafe(id int) (*Poll, error) {
return nil, ErrNoRows return nil, ErrNoRows
} }
func (s *NullPollCache) Set(_ *Poll) error { func (mus *NullPollCache) Set(_ *Poll) error {
return nil return nil
} }
func (s *NullPollCache) Add(_ *Poll) error { func (mus *NullPollCache) Add(_ *Poll) error {
return nil return nil
} }
func (s *NullPollCache) AddUnsafe(_ *Poll) error { func (mus *NullPollCache) AddUnsafe(_ *Poll) error {
return nil return nil
} }
func (s *NullPollCache) Remove(id int) error { func (mus *NullPollCache) Remove(id int) error {
return nil return nil
} }
func (s *NullPollCache) RemoveUnsafe(id int) error { func (mus *NullPollCache) RemoveUnsafe(id int) error {
return nil return nil
} }
func (s *NullPollCache) Flush() { func (mus *NullPollCache) Flush() {
} }
func (s *NullPollCache) Length() int { func (mus *NullPollCache) Length() int {
return 0 return 0
} }
func (s *NullPollCache) SetCapacity(_ int) { func (mus *NullPollCache) SetCapacity(_ int) {
} }
func (s *NullPollCache) GetCapacity() int { func (mus *NullPollCache) GetCapacity() int {
return 0 return 0
} }

View File

@ -7,11 +7,33 @@ import (
"log" "log"
"strconv" "strconv"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var Polls PollStore var Polls PollStore
type Poll struct {
ID int
ParentID int
ParentTable string
Type int // 0: Single choice, 1: Multiple choice, 2: Multiple choice w/ points
//AntiCheat bool // Apply various mitigations for cheating
// GroupPower map[gid]points // The number of points a group can spend in this poll, defaults to 1
Options map[int]string
Results map[int]int // map[optionIndex]points
QuickOptions []PollOption // TODO: Fix up the template transpiler so we don't need to use this hack anymore
VoteCount int
}
func (poll *Poll) CastVote(optionIndex int, uid int, ipaddress string) error {
return Polls.CastVote(optionIndex, poll.ID, uid, ipaddress) // TODO: Move the query into a pollStmts rather than having it in the store
}
func (poll *Poll) Copy() Poll {
return *poll
}
type PollOption struct { type PollOption struct {
ID int ID int
Value string Value string
@ -26,10 +48,10 @@ type Pollable interface {
type PollStore interface { type PollStore interface {
Get(id int) (*Poll, error) Get(id int) (*Poll, error)
Exists(id int) bool Exists(id int) bool
ClearIPs() error
Create(parent Pollable, pollType int, pollOptions map[int]string) (int, error) Create(parent Pollable, pollType int, pollOptions map[int]string) (int, error)
CastVote(optionIndex int, pollID int, uid int, ipaddress string) error
Reload(id int) error Reload(id int) error
Count() int //GlobalCount() int
SetCache(cache PollCache) SetCache(cache PollCache)
GetCache() PollCache GetCache() PollCache
@ -42,10 +64,11 @@ type DefaultPollStore struct {
exists *sql.Stmt exists *sql.Stmt
createPoll *sql.Stmt createPoll *sql.Stmt
createPollOption *sql.Stmt createPollOption *sql.Stmt
addVote *sql.Stmt
incrementVoteCount *sql.Stmt
incrementVoteCountForOption *sql.Stmt
delete *sql.Stmt delete *sql.Stmt
count *sql.Stmt //pollCount *sql.Stmt
clearIPs *sql.Stmt
} }
func NewDefaultPollStore(cache PollCache) (*DefaultPollStore, error) { func NewDefaultPollStore(cache PollCache) (*DefaultPollStore, error) {
@ -54,59 +77,59 @@ func NewDefaultPollStore(cache PollCache) (*DefaultPollStore, error) {
cache = NewNullPollCache() cache = NewNullPollCache()
} }
// TODO: Add an admin version of registerStmt with more flexibility? // TODO: Add an admin version of registerStmt with more flexibility?
p := "polls"
return &DefaultPollStore{ return &DefaultPollStore{
cache: cache, cache: cache,
get: acc.Select(p).Columns("parentID,parentTable,type,options,votes").Where("pollID=?").Stmt(), get: acc.Select("polls").Columns("parentID, parentTable, type, options, votes").Where("pollID = ?").Prepare(),
exists: acc.Select(p).Columns("pollID").Where("pollID=?").Stmt(), exists: acc.Select("polls").Columns("pollID").Where("pollID = ?").Prepare(),
createPoll: acc.Insert(p).Columns("parentID,parentTable,type,options").Fields("?,?,?,?").Prepare(), createPoll: acc.Insert("polls").Columns("parentID, parentTable, type, options").Fields("?,?,?,?").Prepare(),
createPollOption: acc.Insert("polls_options").Columns("pollID, option, votes").Fields("?,?,0").Prepare(), createPollOption: acc.Insert("polls_options").Columns("pollID, option, votes").Fields("?,?,0").Prepare(),
count: acc.Count(p).Prepare(), addVote: acc.Insert("polls_votes").Columns("pollID, uid, option, castAt, ipaddress").Fields("?,?,?,UTC_TIMESTAMP(),?").Prepare(),
incrementVoteCount: acc.Update("polls").Set("votes = votes + 1").Where("pollID = ?").Prepare(),
clearIPs: acc.Update("polls_votes").Set("ip=''").Where("ip!=''").Stmt(), incrementVoteCountForOption: acc.Update("polls_options").Set("votes = votes + 1").Where("option = ? AND pollID = ?").Prepare(),
//pollCount: acc.SimpleCount("polls", "", ""),
}, acc.FirstError() }, acc.FirstError()
} }
func (s *DefaultPollStore) Exists(id int) bool { func (store *DefaultPollStore) Exists(id int) bool {
e := s.exists.QueryRow(id).Scan(&id) err := store.exists.QueryRow(id).Scan(&id)
if e != nil && e != ErrNoRows { if err != nil && err != ErrNoRows {
LogError(e) LogError(err)
} }
return e != ErrNoRows return err != ErrNoRows
} }
func (s *DefaultPollStore) Get(id int) (*Poll, error) { func (store *DefaultPollStore) Get(id int) (*Poll, error) {
p, err := s.cache.Get(id) poll, err := store.cache.Get(id)
if err == nil { if err == nil {
return p, nil return poll, nil
} }
p = &Poll{ID: id} poll = &Poll{ID: id}
var optionTxt []byte var optionTxt []byte
err = s.get.QueryRow(id).Scan(&p.ParentID, &p.ParentTable, &p.Type, &optionTxt, &p.VoteCount) err = store.get.QueryRow(id).Scan(&poll.ParentID, &poll.ParentTable, &poll.Type, &optionTxt, &poll.VoteCount)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = json.Unmarshal(optionTxt, &p.Options) err = json.Unmarshal(optionTxt, &poll.Options)
if err == nil { if err == nil {
p.QuickOptions = s.unpackOptionsMap(p.Options) poll.QuickOptions = store.unpackOptionsMap(poll.Options)
s.cache.Set(p) store.cache.Set(poll)
} }
return p, err return poll, err
} }
// TODO: Optimise the query to avoid preparing it on the spot? Maybe, use knowledge of the most common IN() parameter counts? // TODO: Optimise the query to avoid preparing it on the spot? Maybe, use knowledge of the most common IN() parameter counts?
// TODO: ID of 0 should always error? // TODO: ID of 0 should always error?
func (s *DefaultPollStore) BulkGetMap(ids []int) (list map[int]*Poll, err error) { func (store *DefaultPollStore) BulkGetMap(ids []int) (list map[int]*Poll, err error) {
idCount := len(ids) var idCount = len(ids)
list = make(map[int]*Poll) list = make(map[int]*Poll)
if idCount == 0 { if idCount == 0 {
return list, nil return list, nil
} }
var stillHere []int var stillHere []int
sliceList := s.cache.BulkGet(ids) sliceList := store.cache.BulkGet(ids)
for i, sliceItem := range sliceList { for i, sliceItem := range sliceList {
if sliceItem != nil { if sliceItem != nil {
list[sliceItem.ID] = sliceItem list[sliceItem.ID] = sliceItem
@ -121,35 +144,44 @@ func (s *DefaultPollStore) BulkGetMap(ids []int) (list map[int]*Poll, err error)
return list, nil return list, nil
} }
idList, q := inqbuild(ids) // TODO: Add a function for the qlist stuff
rows, err := qgen.NewAcc().Select("polls").Columns("pollID,parentID,parentTable,type,options,votes").Where("pollID IN(" + q + ")").Query(idList...) var qlist string
var pollIDList []interface{}
for _, id := range ids {
pollIDList = append(pollIDList, strconv.Itoa(id))
qlist += "?,"
}
qlist = qlist[0 : len(qlist)-1]
rows, err := qgen.NewAcc().Select("polls").Columns("pollID, parentID, parentTable, type, options, votes").Where("pollID IN(" + qlist + ")").Query(pollIDList...)
if err != nil { if err != nil {
return list, err return list, err
} }
for rows.Next() { for rows.Next() {
p := &Poll{ID: 0} poll := &Poll{ID: 0}
var optionTxt []byte var optionTxt []byte
err := rows.Scan(&p.ID, &p.ParentID, &p.ParentTable, &p.Type, &optionTxt, &p.VoteCount) err := rows.Scan(&poll.ID, &poll.ParentID, &poll.ParentTable, &poll.Type, &optionTxt, &poll.VoteCount)
if err != nil { if err != nil {
return list, err return list, err
} }
err = json.Unmarshal(optionTxt, &p.Options) err = json.Unmarshal(optionTxt, &poll.Options)
if err != nil { if err != nil {
return list, err return list, err
} }
p.QuickOptions = s.unpackOptionsMap(p.Options) poll.QuickOptions = store.unpackOptionsMap(poll.Options)
s.cache.Set(p) store.cache.Set(poll)
list[p.ID] = p list[poll.ID] = poll
} }
// Did we miss any polls? // Did we miss any polls?
if idCount > len(list) { if idCount > len(list) {
var sidList string var sidList string
for _, id := range ids { for _, id := range ids {
if _, ok := list[id]; !ok { _, ok := list[id]
if !ok {
sidList += strconv.Itoa(id) + "," sidList += strconv.Itoa(id) + ","
} }
} }
@ -173,77 +205,83 @@ func (s *DefaultPollStore) BulkGetMap(ids []int) (list map[int]*Poll, err error)
return list, err return list, err
} }
func (s *DefaultPollStore) Reload(id int) error { func (store *DefaultPollStore) Reload(id int) error {
p := &Poll{ID: id} poll := &Poll{ID: id}
var optionTxt []byte var optionTxt []byte
e := s.get.QueryRow(id).Scan(&p.ParentID, &p.ParentTable, &p.Type, &optionTxt, &p.VoteCount) err := store.get.QueryRow(id).Scan(&poll.ParentID, &poll.ParentTable, &poll.Type, &optionTxt, &poll.VoteCount)
if e != nil { if err != nil {
_ = s.cache.Remove(id) store.cache.Remove(id)
return e return err
} }
e = json.Unmarshal(optionTxt, &p.Options)
if e != nil { err = json.Unmarshal(optionTxt, &poll.Options)
_ = s.cache.Remove(id) if err != nil {
return e store.cache.Remove(id)
return err
} }
p.QuickOptions = s.unpackOptionsMap(p.Options)
_ = s.cache.Set(p) poll.QuickOptions = store.unpackOptionsMap(poll.Options)
_ = store.cache.Set(poll)
return nil return nil
} }
func (s *DefaultPollStore) unpackOptionsMap(rawOptions map[int]string) []PollOption { func (store *DefaultPollStore) unpackOptionsMap(rawOptions map[int]string) []PollOption {
opts := make([]PollOption, len(rawOptions)) options := make([]PollOption, len(rawOptions))
for id, opt := range rawOptions { for id, option := range rawOptions {
opts[id] = PollOption{id, opt} options[id] = PollOption{id, option}
} }
return opts return options
} }
func (s *DefaultPollStore) ClearIPs() error { // TODO: Use a transaction for this?
_, e := s.clearIPs.Exec() func (store *DefaultPollStore) CastVote(optionIndex int, pollID int, uid int, ipaddress string) error {
return e _, err := store.addVote.Exec(pollID, uid, optionIndex, ipaddress)
if err != nil {
return err
}
_, err = store.incrementVoteCount.Exec(pollID)
if err != nil {
return err
}
_, err = store.incrementVoteCountForOption.Exec(optionIndex, pollID)
return err
} }
// TODO: Use a transaction for this // TODO: Use a transaction for this
func (s *DefaultPollStore) Create(parent Pollable, pollType int, pollOptions map[int]string) (id int, e error) { func (store *DefaultPollStore) Create(parent Pollable, pollType int, pollOptions map[int]string) (id int, err error) {
// TODO: Move the option names into the polls_options table and get rid of this json sludge? pollOptionsTxt, err := json.Marshal(pollOptions)
pollOptionsTxt, e := json.Marshal(pollOptions) if err != nil {
if e != nil { return 0, err
return 0, e
} }
res, e := s.createPoll.Exec(parent.GetID(), parent.GetTable(), pollType, pollOptionsTxt)
if e != nil { res, err := store.createPoll.Exec(parent.GetID(), parent.GetTable(), pollType, pollOptionsTxt)
return 0, e if err != nil {
return 0, err
} }
lastID, e := res.LastInsertId()
if e != nil { lastID, err := res.LastInsertId()
return 0, e if err != nil {
return 0, err
} }
for i := 0; i < len(pollOptions); i++ { for i := 0; i < len(pollOptions); i++ {
_, e := s.createPollOption.Exec(lastID, i) _, err := store.createPollOption.Exec(lastID, i)
if e != nil { if err != nil {
return 0, e return 0, err
} }
} }
return int(lastID), parent.SetPoll(int(lastID)) // TODO: Delete the poll (and options) if SetPoll fails
}
id = int(lastID) func (store *DefaultPollStore) SetCache(cache PollCache) {
return id, parent.SetPoll(id) // TODO: Delete the poll (and options) if SetPoll fails store.cache = cache
}
func (s *DefaultPollStore) Count() int {
return Count(s.count)
}
func (s *DefaultPollStore) SetCache(cache PollCache) {
s.cache = cache
} }
// TODO: We're temporarily doing this so that you can do ucache != nil in getTopicUser. Refactor it. // TODO: We're temporarily doing this so that you can do ucache != nil in getTopicUser. Refactor it.
func (s *DefaultPollStore) GetCache() PollCache { func (store *DefaultPollStore) GetCache() PollCache {
_, ok := s.cache.(*NullPollCache) _, ok := store.cache.(*NullPollCache)
if ok { if ok {
return nil return nil
} }
return s.cache return store.cache
} }

View File

@ -3,10 +3,9 @@ package common
import ( import (
"database/sql" "database/sql"
"html" "html"
"strconv"
"time" "time"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var profileReplyStmts ProfileReplyStmts var profileReplyStmts ProfileReplyStmts
@ -18,10 +17,11 @@ type ProfileReply struct {
CreatedBy int CreatedBy int
Group int Group int
CreatedAt time.Time CreatedAt time.Time
RelativeCreatedAt string
LastEdit int LastEdit int
LastEditBy int LastEditBy int
ContentLines int ContentLines int
IP string IPAddress string
} }
type ProfileReplyStmts struct { type ProfileReplyStmts struct {
@ -31,10 +31,9 @@ type ProfileReplyStmts struct {
func init() { func init() {
DbInits.Add(func(acc *qgen.Accumulator) error { DbInits.Add(func(acc *qgen.Accumulator) error {
ur := "users_replies"
profileReplyStmts = ProfileReplyStmts{ profileReplyStmts = ProfileReplyStmts{
edit: acc.Update(ur).Set("content=?,parsed_content=?").Where("rid=?").Prepare(), edit: acc.Update("users_replies").Set("content = ?, parsed_content = ?").Where("rid = ?").Prepare(),
delete: acc.Delete(ur).Where("rid=?").Prepare(), delete: acc.Delete("users_replies").Where("rid = ?").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })
@ -46,30 +45,19 @@ func BlankProfileReply(id int) *ProfileReply {
} }
// TODO: Write tests for this // TODO: Write tests for this
func (r *ProfileReply) Delete() error { func (reply *ProfileReply) Delete() error {
_, err := profileReplyStmts.delete.Exec(r.ID) _, err := profileReplyStmts.delete.Exec(reply.ID)
if err != nil {
return err
}
// TODO: Better coupling between the two paramsextra queries
aids, err := Activity.AidsByParamsExtra("reply", r.ParentID, "user", strconv.Itoa(r.ID))
if err != nil {
return err
}
for _, aid := range aids {
DismissAlert(r.ParentID, aid)
}
err = Activity.DeleteByParamsExtra("reply", r.ParentID, "user", strconv.Itoa(r.ID))
return err return err
} }
func (r *ProfileReply) SetBody(content string) error { func (reply *ProfileReply) SetBody(content string) error {
content = PreparseMessage(html.UnescapeString(content)) content = PreparseMessage(html.UnescapeString(content))
_, err := profileReplyStmts.edit.Exec(content, ParseMessage(content, 0, "", nil, nil), r.ID) parsedContent := ParseMessage(content, 0, "")
_, err := profileReplyStmts.edit.Exec(content, parsedContent, reply.ID)
return err return err
} }
// TODO: We can get this from the topic store instead of a query which will always miss the cache... // TODO: We can get this from the topic store instead of a query which will always miss the cache...
func (r *ProfileReply) Creator() (*User, error) { func (reply *ProfileReply) Creator() (*User, error) {
return Users.Get(r.CreatedBy) return Users.Get(reply.CreatedBy)
} }

View File

@ -3,79 +3,46 @@ package common
import ( import (
"database/sql" "database/sql"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var Prstore ProfileReplyStore var Prstore ProfileReplyStore
type ProfileReplyStore interface { type ProfileReplyStore interface {
Get(id int) (*ProfileReply, error) Get(id int) (*ProfileReply, error)
Exists(id int) bool Create(profileID int, content string, createdBy int, ipaddress string) (id int, err error)
ClearIPs() error
Create(profileID int, content string, createdBy int, ip string) (id int, err error)
Count() (count int)
} }
// TODO: Refactor this to stop using the global stmt store // TODO: Refactor this to stop using the global stmt store
// TODO: Add more methods to this like Create() // TODO: Add more methods to this like Create()
type SQLProfileReplyStore struct { type SQLProfileReplyStore struct {
get *sql.Stmt get *sql.Stmt
exists *sql.Stmt
create *sql.Stmt create *sql.Stmt
count *sql.Stmt
clearIPs *sql.Stmt
} }
func NewSQLProfileReplyStore(acc *qgen.Accumulator) (*SQLProfileReplyStore, error) { func NewSQLProfileReplyStore(acc *qgen.Accumulator) (*SQLProfileReplyStore, error) {
ur := "users_replies"
return &SQLProfileReplyStore{ return &SQLProfileReplyStore{
get: acc.Select(ur).Columns("uid,content,createdBy,createdAt,lastEdit,lastEditBy,ip").Where("rid=?").Stmt(), get: acc.Select("users_replies").Columns("uid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress").Where("rid = ?").Prepare(),
exists: acc.Exists(ur, "rid").Prepare(), create: acc.Insert("users_replies").Columns("uid, content, parsed_content, createdAt, createdBy, ipaddress").Fields("?,?,?,UTC_TIMESTAMP(),?,?").Prepare(),
create: acc.Insert(ur).Columns("uid,content,parsed_content,createdAt,createdBy,ip").Fields("?,?,?,UTC_TIMESTAMP(),?,?").Prepare(),
count: acc.Count(ur).Stmt(),
clearIPs: acc.Update(ur).Set("ip=''").Where("ip!=''").Stmt(),
}, acc.FirstError() }, acc.FirstError()
} }
func (s *SQLProfileReplyStore) Get(id int) (*ProfileReply, error) { func (store *SQLProfileReplyStore) Get(id int) (*ProfileReply, error) {
r := ProfileReply{ID: id} reply := ProfileReply{ID: id}
e := s.get.QueryRow(id).Scan(&r.ParentID, &r.Content, &r.CreatedBy, &r.CreatedAt, &r.LastEdit, &r.LastEditBy, &r.IP) err := store.get.QueryRow(id).Scan(&reply.ParentID, &reply.Content, &reply.CreatedBy, &reply.CreatedAt, &reply.LastEdit, &reply.LastEditBy, &reply.IPAddress)
return &r, e return &reply, err
} }
func (s *SQLProfileReplyStore) Exists(id int) bool { func (store *SQLProfileReplyStore) Create(profileID int, content string, createdBy int, ipaddress string) (id int, err error) {
e := s.exists.QueryRow(id).Scan(&id) res, err := store.create.Exec(profileID, content, ParseMessage(content, 0, ""), createdBy, ipaddress)
if e != nil && e != ErrNoRows { if err != nil {
LogError(e) return 0, err
} }
return e != ErrNoRows lastID, err := res.LastInsertId()
if err != nil {
return 0, err
} }
func (s *SQLProfileReplyStore) ClearIPs() error {
_, e := s.clearIPs.Exec()
return e
}
func (s *SQLProfileReplyStore) Create(profileID int, content string, createdBy int, ip string) (id int, e error) {
if Config.DisablePostIP {
ip = ""
}
res, e := s.create.Exec(profileID, content, ParseMessage(content, 0, "", nil, nil), createdBy, ip)
if e != nil {
return 0, e
}
lastID, e := res.LastInsertId()
if e != nil {
return 0, e
}
// Should we reload the user? // Should we reload the user?
return int(lastID), e return int(lastID), err
}
// TODO: Write a test for this
// Count returns the total number of topic replies on these forums
func (s *SQLProfileReplyStore) Count() (count int) {
return Count(s.count)
} }

View File

@ -1,132 +0,0 @@
package common
import (
"database/sql"
//"log"
"time"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var GroupPromotions GroupPromotionStore
type GroupPromotion struct {
ID int
From int
To int
TwoWay bool
Level int
Posts int
MinTime int
RegisteredFor int
}
type GroupPromotionStore interface {
GetByGroup(gid int) (gps []*GroupPromotion, err error)
Get(id int) (*GroupPromotion, error)
PromoteIfEligible(u *User, level, posts int, registeredAt time.Time) error
Delete(id int) error
Create(from, to int, twoWay bool, level, posts, registeredFor int) (int, error)
}
type DefaultGroupPromotionStore struct {
getByGroup *sql.Stmt
get *sql.Stmt
delete *sql.Stmt
create *sql.Stmt
getByUser *sql.Stmt
getByUserMins *sql.Stmt
updateUser *sql.Stmt
updateGeneric *sql.Stmt
}
func NewDefaultGroupPromotionStore(acc *qgen.Accumulator) (*DefaultGroupPromotionStore, error) {
ugp := "users_groups_promotions"
prs := &DefaultGroupPromotionStore{
getByGroup: acc.Select(ugp).Columns("pid, from_gid, to_gid, two_way, level, posts, minTime, registeredFor").Where("from_gid=? OR to_gid=?").Prepare(),
get: acc.Select(ugp).Columns("from_gid, to_gid, two_way, level, posts, minTime, registeredFor").Where("pid=?").Prepare(),
delete: acc.Delete(ugp).Where("pid=?").Prepare(),
create: acc.Insert(ugp).Columns("from_gid, to_gid, two_way, level, posts, minTime, registeredFor").Fields("?,?,?,?,?,?,?").Prepare(),
getByUserMins: acc.Select(ugp).Columns("pid, to_gid, two_way, level, posts, minTime, registeredFor").Where("from_gid=? AND level<=? AND posts<=? AND registeredFor<=?").Orderby("level DESC").Limit("1").Prepare(),
getByUser: acc.Select(ugp).Columns("pid, to_gid, two_way, level, posts, minTime, registeredFor").Where("from_gid=? AND level<=? AND posts<=?").Orderby("level DESC").Limit("1").Prepare(),
updateUser: acc.Update("users").Set("group=?").Where("group=? AND uid=?").Prepare(),
updateGeneric: acc.Update("users").Set("group=?").Where("group=? AND level>=? AND posts>=?").Prepare(),
}
Tasks.FifteenMin.Add(prs.Tick)
return prs, acc.FirstError()
}
func (s *DefaultGroupPromotionStore) Tick() error {
return nil
}
func (s *DefaultGroupPromotionStore) GetByGroup(gid int) (gps []*GroupPromotion, err error) {
rows, err := s.getByGroup.Query(gid, gid)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
g := &GroupPromotion{}
err := rows.Scan(&g.ID, &g.From, &g.To, &g.TwoWay, &g.Level, &g.Posts, &g.MinTime, &g.RegisteredFor)
if err != nil {
return nil, err
}
gps = append(gps, g)
}
return gps, rows.Err()
}
// TODO: Cache the group promotions to avoid hitting the database as much
func (s *DefaultGroupPromotionStore) Get(id int) (*GroupPromotion, error) {
/*g, err := s.cache.Get(id)
if err == nil {
return u, nil
}*/
g := &GroupPromotion{ID: id}
err := s.get.QueryRow(id).Scan(&g.From, &g.To, &g.TwoWay, &g.Level, &g.Posts, &g.MinTime, &g.RegisteredFor)
if err == nil {
//s.cache.Set(u)
}
return g, err
}
// TODO: Optimise this to avoid the query
func (s *DefaultGroupPromotionStore) PromoteIfEligible(u *User, level, posts int, registeredAt time.Time) error {
mins := time.Since(registeredAt).Minutes()
g := &GroupPromotion{From: u.Group}
//log.Printf("pre getByUserMins: %+v\n", u)
err := s.getByUserMins.QueryRow(u.Group, level, posts, mins).Scan(&g.ID, &g.To, &g.TwoWay, &g.Level, &g.Posts, &g.MinTime, &g.RegisteredFor)
if err == sql.ErrNoRows {
//log.Print("no matches found")
return nil
} else if err != nil {
return err
}
//log.Printf("g: %+v\n", g)
if g.RegisteredFor == 0 {
_, err = s.updateGeneric.Exec(g.To, g.From, g.Level, g.Posts)
} else {
_, err = s.updateUser.Exec(g.To, g.From, u.ID)
}
return err
}
func (s *DefaultGroupPromotionStore) Delete(id int) error {
_, err := s.delete.Exec(id)
return err
}
func (s *DefaultGroupPromotionStore) Create(from, to int, twoWay bool, level, posts, registeredFor int) (int, error) {
res, err := s.create.Exec(from, to, twoWay, level, posts, 0, registeredFor)
if err != nil {
return 0, err
}
lastID, err := res.LastInsertId()
return int(lastID), err
}

View File

@ -1,100 +0,0 @@
package common
import (
"errors"
"strconv"
"sync"
"time"
)
var ErrBadRateLimiter = errors.New("That rate limiter doesn't exist")
var ErrExceededRateLimit = errors.New("You're exceeding a rate limit. Please wait a while before trying again.")
// TODO: Persist rate limits to disk
type RateLimiter interface {
LimitIP(limit, ip string) error
LimitUser(limit string, user int) error
}
type RateData struct {
value int
floorTime int
}
type RateFence struct {
duration int
max int
}
// TODO: Optimise this by using something other than a string when possible
type RateLimit struct {
data map[string][]RateData
fences []RateFence
sync.RWMutex
}
func NewRateLimit(fences []RateFence) *RateLimit {
for i, fence := range fences {
fences[i].duration = fence.duration * 1000 * 1000 * 1000
}
return &RateLimit{data: make(map[string][]RateData), fences: fences}
}
func (l *RateLimit) Limit(name string, ltype int) error {
l.Lock()
defer l.Unlock()
data, ok := l.data[name]
if !ok {
data = make([]RateData, len(l.fences))
for i, _ := range data {
data[i] = RateData{0, int(time.Now().Unix())}
}
}
for i, field := range data {
fence := l.fences[i]
diff := int(time.Now().Unix()) - field.floorTime
if diff >= fence.duration {
field = RateData{0, int(time.Now().Unix())}
data[i] = field
}
if field.value > fence.max {
return ErrExceededRateLimit
}
field.value++
data[i] = field
}
return nil
}
type DefaultRateLimiter struct {
limits map[string]*RateLimit
}
func NewDefaultRateLimiter() *DefaultRateLimiter {
return &DefaultRateLimiter{map[string]*RateLimit{
"register": NewRateLimit([]RateFence{{int(time.Hour / 2), 1}}),
}}
}
func (l *DefaultRateLimiter) LimitIP(limit, ip string) error {
limiter, ok := l.limits[limit]
if !ok {
return ErrBadRateLimiter
}
return limiter.Limit(ip, 0)
}
func (l *DefaultRateLimiter) LimitUser(limit string, user int) error {
limiter, ok := l.limits[limit]
if !ok {
return ErrBadRateLimiter
}
return limiter.Limit(strconv.Itoa(user), 1)
}

View File

@ -1,196 +0,0 @@
package common
import (
"database/sql"
//"log"
"strconv"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var Recalc RecalcInt
type RecalcInt interface {
Replies() (count int, err error)
Forums() (count int, err error)
Subscriptions() (count int, err error)
ActivityStream() (count int, err error)
Users() error
Attachments() (count int, err error)
}
type DefaultRecalc struct {
getActivitySubscriptions *sql.Stmt
getActivityStream *sql.Stmt
getAttachments *sql.Stmt
getTopicCount *sql.Stmt
resetTopicCount *sql.Stmt
}
func NewDefaultRecalc(acc *qgen.Accumulator) (*DefaultRecalc, error) {
return &DefaultRecalc{
getActivitySubscriptions: acc.Select("activity_subscriptions").Columns("targetID,targetType").Prepare(),
getActivityStream: acc.Select("activity_stream").Columns("asid,event,elementID,elementType,extra").Prepare(),
getAttachments: acc.Select("attachments").Columns("attachID,originID,originTable").Prepare(),
getTopicCount: acc.Count("topics").Where("parentID=?").Prepare(),
//resetTopicCount: acc.SimpleUpdateSelect("forums", "topicCount = tc", "topics", "count(*) as tc", "parentID=?", "", ""),
// TODO: Avoid using RawPrepare
resetTopicCount: acc.RawPrepare("UPDATE forums, (SELECT COUNT(*) as tc FROM topics WHERE parentID=?) AS src SET forums.topicCount=src.tc WHERE forums.fid=?"),
}, acc.FirstError()
}
func (s *DefaultRecalc) Replies() (count int, err error) {
var ltid int
err = Rstore.Each(func(r *Reply) error {
if ltid == r.ParentID && r.ParentID > 0 {
//return nil
}
if !Topics.Exists(r.ParentID) {
// TODO: Delete in chunks not one at a time?
if err := r.Delete(); err != nil {
return err
}
count++
}
return nil
})
return count, err
}
func (s *DefaultRecalc) Forums() (count int, err error) {
err = Forums.Each(func(f *Forum) error {
_, err := s.resetTopicCount.Exec(f.ID, f.ID)
if err != nil {
return err
}
count++
return nil
})
return count, err
}
func (s *DefaultRecalc) Subscriptions() (count int, err error) {
err = eachall(s.getActivitySubscriptions, func(r *sql.Rows) error {
var targetID int
var targetType string
err := r.Scan(&targetID, &targetType)
if err != nil {
return err
}
if targetType == "topic" {
if !Topics.Exists(targetID) {
// TODO: Delete in chunks not one at a time?
err := Subscriptions.DeleteResource(targetID, targetType)
if err != nil {
return err
}
count++
}
}
return nil
})
return count, err
}
type Existable interface {
Exists(id int) bool
}
func (s *DefaultRecalc) ActivityStream() (count int, err error) {
err = eachall(s.getActivityStream, func(r *sql.Rows) error {
var asid, elementID int
var event, elementType, extra string
err := r.Scan(&asid, &event, &elementID, &elementType, &extra)
if err != nil {
return err
}
//log.Print("asid:",asid)
var s Existable
switch elementType {
case "user":
if event == "reply" {
extraI, _ := strconv.Atoi(extra)
if extraI > 0 {
s = Prstore
elementID = extraI
} else {
return nil
}
} else {
return nil
}
case "topic":
s = Topics
// TODO: Delete reply events with an empty extra field
if event == "reply" {
extraI, _ := strconv.Atoi(extra)
if extraI > 0 {
s = Rstore
elementID = extraI
}
}
case "post":
s = Rstore
// TODO: Add a TopicExistsByReplyID for efficiency
/*_, err = TopicByReplyID(elementID)
if err == sql.ErrNoRows {
// TODO: Delete in chunks not one at a time?
err := Activity.Delete(asid)
if err != nil {
return err
}
count++
} else if err != nil {
return err
}*/
default:
return nil
}
if !s.Exists(elementID) {
// TODO: Delete in chunks not one at a time?
err := Activity.Delete(asid)
if err != nil {
return err
}
count++
}
return nil
})
return count, err
}
func (s *DefaultRecalc) Users() error {
return Users.Each(func(u *User) error {
return u.RecalcPostStats()
})
}
func (s *DefaultRecalc) Attachments() (count int, err error) {
err = eachall(s.getAttachments, func(r *sql.Rows) error {
var aid, originID int
var originType string
err := r.Scan(&aid, &originID, &originType)
if err != nil {
return err
}
var s Existable
switch originType {
case "topics":
s = Topics
case "replies":
s = Rstore
default:
return nil
}
if !s.Exists(originID) {
// TODO: Delete in chunks not one at a time?
err := Attachments.Delete(aid)
if err != nil {
return err
}
count++
}
return nil
})
return count, err
}

View File

@ -1,143 +0,0 @@
package common
import (
"database/sql"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var UserBlocks BlockStore
//var UserFriends FriendStore
type BlockStore interface {
IsBlockedBy(blocker, blockee int) (bool, error)
BulkIsBlockedBy(blockers []int, blockee int) (bool, error)
Add(blocker, blockee int) error
Remove(blocker, blockee int) error
BlockedByOffset(blocker, offset, perPage int) ([]int, error)
BlockedByCount(blocker int) int
}
type DefaultBlockStore struct {
isBlocked *sql.Stmt
add *sql.Stmt
remove *sql.Stmt
blockedBy *sql.Stmt
blockedByCount *sql.Stmt
}
func NewDefaultBlockStore(acc *qgen.Accumulator) (*DefaultBlockStore, error) {
ub := "users_blocks"
return &DefaultBlockStore{
isBlocked: acc.Select(ub).Cols("blocker").Where("blocker=? AND blockedUser=?").Prepare(),
add: acc.Insert(ub).Columns("blocker,blockedUser").Fields("?,?").Prepare(),
remove: acc.Delete(ub).Where("blocker=? AND blockedUser=?").Prepare(),
blockedBy: acc.Select(ub).Columns("blockedUser").Where("blocker=?").Limit("?,?").Prepare(),
blockedByCount: acc.Count(ub).Where("blocker=?").Prepare(),
}, acc.FirstError()
}
func (s *DefaultBlockStore) IsBlockedBy(blocker, blockee int) (bool, error) {
e := s.isBlocked.QueryRow(blocker, blockee).Scan(&blocker)
if e == ErrNoRows {
return false, nil
}
return e == nil, e
}
// TODO: Optimise the query to avoid preparing it on the spot? Maybe, use knowledge of the most common IN() parameter counts?
func (s *DefaultBlockStore) BulkIsBlockedBy(blockers []int, blockee int) (bool, error) {
if len(blockers) == 0 {
return false, nil
}
if len(blockers) == 1 {
return s.IsBlockedBy(blockers[0], blockee)
}
idList, q := inqbuild(blockers)
count, e := qgen.NewAcc().Count("users_blocks").Where("blocker IN(" + q + ") AND blockedUser=?").TotalP(idList...)
if e == ErrNoRows {
return false, nil
}
return count == 0, e
}
func (s *DefaultBlockStore) Add(blocker, blockee int) error {
_, e := s.add.Exec(blocker, blockee)
return e
}
func (s *DefaultBlockStore) Remove(blocker, blockee int) error {
_, e := s.remove.Exec(blocker, blockee)
return e
}
func (s *DefaultBlockStore) BlockedByOffset(blocker, offset, perPage int) (uids []int, err error) {
rows, e := s.blockedBy.Query(blocker, offset, perPage)
if e != nil {
return nil, e
}
defer rows.Close()
for rows.Next() {
var uid int
if e := rows.Scan(&uid); e != nil {
return nil, e
}
uids = append(uids, uid)
}
return uids, rows.Err()
}
func (s *DefaultBlockStore) BlockedByCount(blocker int) (count int) {
e := s.blockedByCount.QueryRow(blocker).Scan(&count)
if e != nil {
LogError(e)
}
return count
}
type FriendInvite struct {
Requester int
Target int
}
type FriendStore interface {
AddInvite(requester, target int) error
Confirm(requester, target int) error
GetOwSentInvites(uid int) ([]FriendInvite, error)
GetOwnRecvInvites(uid int) ([]FriendInvite, error)
}
type DefaultFriendStore struct {
addInvite *sql.Stmt
confirm *sql.Stmt
getOwnSentInvites *sql.Stmt
getOwnRecvInvites *sql.Stmt
}
func NewDefaultFriendStore(acc *qgen.Accumulator) (*DefaultFriendStore, error) {
ufi := "users_friends_invites"
return &DefaultFriendStore{
addInvite: acc.Insert(ufi).Columns("requester,target").Fields("?,?").Prepare(),
confirm: acc.Insert("users_friends").Columns("uid,uid2").Fields("?,?").Prepare(),
getOwnSentInvites: acc.Select(ufi).Cols("requester,target").Where("requester=?").Prepare(),
getOwnRecvInvites: acc.Select(ufi).Cols("requester,target").Where("target=?").Prepare(),
}, acc.FirstError()
}
func (s *DefaultFriendStore) AddInvite(requester, target int) error {
_, e := s.addInvite.Exec(requester, target)
return e
}
func (s *DefaultFriendStore) Confirm(requester, target int) error {
_, e := s.confirm.Exec(requester, target)
return e
}
func (s *DefaultFriendStore) GetOwnSentInvites(uid int) ([]FriendInvite, error) {
return nil, nil
}
func (s *DefaultFriendStore) GetOwnRecvInvites(uid int) ([]FriendInvite, error) {
return nil, nil
}

View File

@ -1,7 +1,7 @@
/* /*
* *
* Reply Resources File * Reply Resources File
* Copyright Azareal 2016 - 2020 * Copyright Azareal 2016 - 2018
* *
*/ */
package common package common
@ -10,31 +10,38 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"html" "html"
"strconv"
"time" "time"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
type ReplyUser struct { type ReplyUser struct {
Reply ID int
ParentID int
Content string
ContentHtml string ContentHtml string
CreatedBy int
UserLink string UserLink string
CreatedByName string CreatedByName string
Group int
CreatedAt time.Time
RelativeCreatedAt string
LastEdit int
LastEditBy int
Avatar string Avatar string
MicroAvatar string MicroAvatar string
ClassName string ClassName string
ContentLines int
Tag string Tag string
URL string URL string
//URLPrefix string URLPrefix string
//URLName string URLName string
Group int
Level int Level int
IPAddress string
Liked bool
LikeCount int
ActionType string
ActionIcon string ActionIcon string
Attachments []*MiniAttachment
Deletable bool
} }
type Reply struct { type Reply struct {
@ -42,16 +49,15 @@ type Reply struct {
ParentID int ParentID int
Content string Content string
CreatedBy int CreatedBy int
//Group int Group int
CreatedAt time.Time CreatedAt time.Time
RelativeCreatedAt string
LastEdit int LastEdit int
LastEditBy int LastEditBy int
ContentLines int ContentLines int
IP string IPAddress string
Liked bool Liked bool
LikeCount int LikeCount int
AttachCount uint16
ActionType string
} }
var ErrAlreadyLiked = errors.New("You already liked this!") var ErrAlreadyLiked = errors.New("You already liked this!")
@ -65,36 +71,18 @@ type ReplyStmts struct {
delete *sql.Stmt delete *sql.Stmt
addLikesToReply *sql.Stmt addLikesToReply *sql.Stmt
removeRepliesFromTopic *sql.Stmt removeRepliesFromTopic *sql.Stmt
deleteLikesForReply *sql.Stmt
deleteActivity *sql.Stmt
deleteActivitySubs *sql.Stmt
updateTopicReplies *sql.Stmt
updateTopicReplies2 *sql.Stmt
getAidsOfReply *sql.Stmt
} }
func init() { func init() {
DbInits.Add(func(acc *qgen.Accumulator) error { DbInits.Add(func(acc *qgen.Accumulator) error {
re := "replies"
replyStmts = ReplyStmts{ replyStmts = ReplyStmts{
isLiked: acc.Select("likes").Columns("targetItem").Where("sentBy = ? and targetItem = ? and targetType = 'replies'").Prepare(), isLiked: acc.Select("likes").Columns("targetItem").Where("sentBy = ? and targetItem = ? and targetType = 'replies'").Prepare(),
createLike: acc.Insert("likes").Columns("weight,targetItem,targetType,sentBy,createdAt").Fields("?,?,?,?,UTC_TIMESTAMP()").Prepare(), createLike: acc.Insert("likes").Columns("weight, targetItem, targetType, sentBy").Fields("?,?,?,?").Prepare(),
edit: acc.Update(re).Set("content=?,parsed_content=?").Where("rid=? AND poll=0").Prepare(), edit: acc.Update("replies").Set("content = ?, parsed_content = ?").Where("rid = ? AND poll = 0").Prepare(),
setPoll: acc.Update(re).Set("poll=?").Where("rid=? AND poll=0").Prepare(), setPoll: acc.Update("replies").Set("poll = ?").Where("rid = ? AND poll = 0").Prepare(),
delete: acc.Delete(re).Where("rid=?").Prepare(), delete: acc.Delete("replies").Where("rid = ?").Prepare(),
addLikesToReply: acc.Update(re).Set("likeCount=likeCount+?").Where("rid=?").Prepare(), addLikesToReply: acc.Update("replies").Set("likeCount = likeCount + ?").Where("rid = ?").Prepare(),
removeRepliesFromTopic: acc.Update("topics").Set("postCount = postCount - ?").Where("tid = ?").Prepare(), removeRepliesFromTopic: acc.Update("topics").Set("postCount = postCount - ?").Where("tid = ?").Prepare(),
deleteLikesForReply: acc.Delete("likes").Where("targetItem=? AND targetType='replies'").Prepare(),
deleteActivity: acc.Delete("activity_stream").Where("elementID=? AND elementType='post'").Prepare(),
deleteActivitySubs: acc.Delete("activity_subscriptions").Where("targetID=? AND targetType='post'").Prepare(),
// TODO: Optimise this to avoid firing an update if it's not the last reply in a topic. We will need to set lastReplyID properly in other places and in the patcher first so we can use it here.
updateTopicReplies: acc.RawPrepare("UPDATE topics t INNER JOIN replies r ON t.tid=r.tid SET t.lastReplyBy=r.createdBy, t.lastReplyAt=r.createdAt, t.lastReplyID=r.rid WHERE t.tid=? ORDER BY r.rid DESC"),
updateTopicReplies2: acc.Update("topics").Set("lastReplyAt=createdAt,lastReplyBy=createdBy,lastReplyID=0").Where("postCount=1 AND tid=?").Prepare(),
getAidsOfReply: acc.Select("attachments").Columns("attachID").Where("originID=? AND originTable='replies'").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })
@ -102,9 +90,9 @@ func init() {
// TODO: Write tests for this // TODO: Write tests for this
// TODO: Wrap these queries in a transaction to make sure the state is consistent // TODO: Wrap these queries in a transaction to make sure the state is consistent
func (r *Reply) Like(uid int) (err error) { func (reply *Reply) Like(uid int) (err error) {
var rid int // unused, just here to avoid mutating reply.ID var rid int // unused, just here to avoid mutating reply.ID
err = replyStmts.isLiked.QueryRow(uid, r.ID).Scan(&rid) err = replyStmts.isLiked.QueryRow(uid, reply.ID).Scan(&rid)
if err != nil && err != ErrNoRows { if err != nil && err != ErrNoRows {
return err return err
} else if err != ErrNoRows { } else if err != ErrNoRows {
@ -112,120 +100,62 @@ func (r *Reply) Like(uid int) (err error) {
} }
score := 1 score := 1
_, err = replyStmts.createLike.Exec(score, r.ID, "replies", uid) _, err = replyStmts.createLike.Exec(score, reply.ID, "replies", uid)
if err != nil { if err != nil {
return err return err
} }
_, err = replyStmts.addLikesToReply.Exec(1, r.ID) _, err = replyStmts.addLikesToReply.Exec(1, reply.ID)
if err != nil { if err != nil {
return err return err
} }
_, err = userStmts.incLiked.Exec(1, uid) _, err = userStmts.incrementLiked.Exec(1, uid)
_ = Rstore.GetCache().Remove(r.ID)
return err
}
// TODO: Use a transaction
func (r *Reply) Unlike(uid int) error {
err := Likes.Delete(r.ID, "replies")
if err != nil {
return err
}
_, err = replyStmts.addLikesToReply.Exec(-1, r.ID)
if err != nil {
return err
}
_, err = userStmts.decLiked.Exec(1, uid)
_ = Rstore.GetCache().Remove(r.ID)
return err
}
// TODO: Refresh topic list?
func (r *Reply) Delete() error {
creator, err := Users.Get(r.CreatedBy)
if err == nil {
err = creator.DecreasePostStats(WordCount(r.Content), false)
if err != nil {
return err
}
} else if err != ErrNoRows {
return err
}
_, err = replyStmts.delete.Exec(r.ID)
if err != nil {
return err
}
// TODO: Move this bit to *Topic
_, err = replyStmts.removeRepliesFromTopic.Exec(1, r.ParentID)
if err != nil {
return err
}
_, err = replyStmts.updateTopicReplies.Exec(r.ParentID)
if err != nil {
return err
}
_, err = replyStmts.updateTopicReplies2.Exec(r.ParentID)
tc := Topics.GetCache()
if tc != nil {
tc.Remove(r.ParentID)
}
_ = Rstore.GetCache().Remove(r.ID)
if err != nil {
return err
}
_, err = replyStmts.deleteLikesForReply.Exec(r.ID)
if err != nil {
return err
}
err = handleReplyAttachments(r.ID)
if err != nil {
return err
}
err = Activity.DeleteByParamsExtra("reply", r.ParentID, "topic", strconv.Itoa(r.ID))
if err != nil {
return err
}
_, err = replyStmts.deleteActivitySubs.Exec(r.ID)
if err != nil {
return err
}
_, err = replyStmts.deleteActivity.Exec(r.ID)
return err
}
func (r *Reply) SetPost(content string) error {
topic, err := r.Topic()
if err != nil {
return err
}
content = PreparseMessage(html.UnescapeString(content))
parsedContent := ParseMessage(content, topic.ParentID, "forums", nil, nil)
_, err = replyStmts.edit.Exec(content, parsedContent, r.ID) // TODO: Sniff if this changed anything to see if we hit an existing poll
_ = Rstore.GetCache().Remove(r.ID)
return err return err
} }
// TODO: Write tests for this // TODO: Write tests for this
func (r *Reply) SetPoll(pollID int) error { func (reply *Reply) Delete() error {
_, err := replyStmts.setPoll.Exec(pollID, r.ID) // TODO: Sniff if this changed anything to see if we hit a poll _, err := replyStmts.delete.Exec(reply.ID)
_ = Rstore.GetCache().Remove(r.ID) if err != nil {
return err
}
// TODO: Move this bit to *Topic
_, err = replyStmts.removeRepliesFromTopic.Exec(1, reply.ParentID)
tcache := Topics.GetCache()
if tcache != nil {
tcache.Remove(reply.ParentID)
}
return err return err
} }
func (r *Reply) Topic() (*Topic, error) { func (reply *Reply) SetPost(content string) error {
return Topics.Get(r.ParentID) topic, err := reply.Topic()
if err != nil {
return err
}
content = PreparseMessage(html.UnescapeString(content))
parsedContent := ParseMessage(content, topic.ParentID, "forums")
_, err = replyStmts.edit.Exec(content, parsedContent, reply.ID) // TODO: Sniff if this changed anything to see if we hit an existing poll
return err
} }
func (r *Reply) GetID() int { func (reply *Reply) SetPoll(pollID int) error {
return r.ID _, err := replyStmts.setPoll.Exec(pollID, reply.ID) // TODO: Sniff if this changed anything to see if we hit a poll
return err
} }
func (r *Reply) GetTable() string { func (reply *Reply) Topic() (*Topic, error) {
return Topics.Get(reply.ParentID)
}
func (reply *Reply) GetID() int {
return reply.ID
}
func (reply *Reply) GetTable() string {
return "replies" return "replies"
} }
// Copy gives you a non-pointer concurrency safe copy of the reply // Copy gives you a non-pointer concurrency safe copy of the reply
func (r *Reply) Copy() Reply { func (reply *Reply) Copy() Reply {
return *r return *reply
} }

View File

@ -1,163 +0,0 @@
package common
import (
//"log"
"sync"
"sync/atomic"
)
// ReplyCache is an interface which spits out replies from a fast cache rather than the database, whether from memory or from an application like Redis. Replies may not be present in the cache but may be in the database
type ReplyCache interface {
Get(id int) (*Reply, error)
GetUnsafe(id int) (*Reply, error)
BulkGet(ids []int) (list []*Reply)
Set(item *Reply) error
Add(item *Reply) error
AddUnsafe(item *Reply) error
Remove(id int) error
RemoveUnsafe(id int) error
Flush()
Length() int
SetCapacity(cap int)
GetCapacity() int
}
// MemoryReplyCache stores and pulls replies out of the current process' memory
type MemoryReplyCache struct {
items map[int]*Reply
length int64 // sync/atomic only lets us operate on int32s and int64s
capacity int
sync.RWMutex
}
// NewMemoryReplyCache gives you a new instance of MemoryReplyCache
func NewMemoryReplyCache(cap int) *MemoryReplyCache {
return &MemoryReplyCache{
items: make(map[int]*Reply),
capacity: cap,
}
}
// Get fetches a reply by ID. Returns ErrNoRows if not present.
func (s *MemoryReplyCache) Get(id int) (*Reply, error) {
s.RLock()
item, ok := s.items[id]
s.RUnlock()
if ok {
return item, nil
}
return item, ErrNoRows
}
// GetUnsafe fetches a reply by ID. Returns ErrNoRows if not present. THIS METHOD IS NOT THREAD-SAFE.
func (s *MemoryReplyCache) GetUnsafe(id int) (*Reply, error) {
item, ok := s.items[id]
if ok {
return item, nil
}
return item, ErrNoRows
}
// BulkGet fetches multiple replies by their IDs. Indices without replies will be set to nil, so make sure you check for those, we might want to change this behaviour to make it less confusing.
func (s *MemoryReplyCache) BulkGet(ids []int) (list []*Reply) {
list = make([]*Reply, len(ids))
s.RLock()
for i, id := range ids {
list[i] = s.items[id]
}
s.RUnlock()
return list
}
// Set overwrites the value of a reply in the cache, whether it's present or not. May return a capacity overflow error.
func (s *MemoryReplyCache) Set(item *Reply) error {
s.Lock()
_, ok := s.items[item.ID]
if ok {
s.items[item.ID] = item
} else if int(s.length) >= s.capacity {
s.Unlock()
return ErrStoreCapacityOverflow
} else {
s.items[item.ID] = item
atomic.AddInt64(&s.length, 1)
}
s.Unlock()
return nil
}
// Add adds a reply to the cache, similar to Set, but it's only intended for new items. This method might be deprecated in the near future, use Set. May return a capacity overflow error.
// ? Is this redundant if we have Set? Are the efficiency wins worth this? Is this even used?
func (s *MemoryReplyCache) Add(item *Reply) error {
//log.Print("MemoryReplyCache.Add")
s.Lock()
if int(s.length) >= s.capacity {
s.Unlock()
return ErrStoreCapacityOverflow
}
s.items[item.ID] = item
s.Unlock()
atomic.AddInt64(&s.length, 1)
return nil
}
// AddUnsafe is the unsafe version of Add. May return a capacity overflow error. THIS METHOD IS NOT THREAD-SAFE.
func (s *MemoryReplyCache) AddUnsafe(item *Reply) error {
if int(s.length) >= s.capacity {
return ErrStoreCapacityOverflow
}
s.items[item.ID] = item
s.length = int64(len(s.items))
return nil
}
// Remove removes a reply from the cache by ID, if they exist. Returns ErrNoRows if no items exist.
func (s *MemoryReplyCache) Remove(id int) error {
s.Lock()
_, ok := s.items[id]
if !ok {
s.Unlock()
return ErrNoRows
}
delete(s.items, id)
s.Unlock()
atomic.AddInt64(&s.length, -1)
return nil
}
// RemoveUnsafe is the unsafe version of Remove. THIS METHOD IS NOT THREAD-SAFE.
func (s *MemoryReplyCache) RemoveUnsafe(id int) error {
_, ok := s.items[id]
if !ok {
return ErrNoRows
}
delete(s.items, id)
atomic.AddInt64(&s.length, -1)
return nil
}
// Flush removes all the replies from the cache, useful for tests.
func (s *MemoryReplyCache) Flush() {
s.Lock()
s.items = make(map[int]*Reply)
s.length = 0
s.Unlock()
}
// ! Is this concurrent?
// Length returns the number of replies in the memory cache
func (s *MemoryReplyCache) Length() int {
return int(s.length)
}
// SetCapacity sets the maximum number of replies which this cache can hold
func (s *MemoryReplyCache) SetCapacity(cap int) {
// Ints are moved in a single instruction, so this should be thread-safe
s.capacity = cap
}
// GetCapacity returns the maximum number of replies this cache can hold
func (s *MemoryReplyCache) GetCapacity() int {
return s.capacity
}

View File

@ -1,147 +1,44 @@
package common package common
//import "log" import "database/sql"
import ( import "../query_gen/lib"
"database/sql"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var Rstore ReplyStore var Rstore ReplyStore
type ReplyStore interface { type ReplyStore interface {
Get(id int) (*Reply, error) Get(id int) (*Reply, error)
Each(f func(*Reply) error) error Create(topic *Topic, content string, ipaddress string, uid int) (id int, err error)
Exists(id int) bool
ClearIPs() error
Create(t *Topic, content, ip string, uid int) (id int, err error)
Count() (count int)
CountUser(uid int) (count int)
CountMegaUser(uid int) (count int)
CountBigUser(uid int) (count int)
SetCache(cache ReplyCache)
GetCache() ReplyCache
} }
type SQLReplyStore struct { type SQLReplyStore struct {
cache ReplyCache
get *sql.Stmt get *sql.Stmt
getAll *sql.Stmt
exists *sql.Stmt
create *sql.Stmt create *sql.Stmt
count *sql.Stmt
countUser *sql.Stmt
countWordUser *sql.Stmt
clearIPs *sql.Stmt
} }
func NewSQLReplyStore(acc *qgen.Accumulator, cache ReplyCache) (*SQLReplyStore, error) { func NewSQLReplyStore(acc *qgen.Accumulator) (*SQLReplyStore, error) {
if cache == nil {
cache = NewNullReplyCache()
}
re := "replies"
return &SQLReplyStore{ return &SQLReplyStore{
cache: cache, get: acc.Select("replies").Columns("tid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount").Where("rid = ?").Prepare(),
get: acc.Select(re).Columns("tid,content,createdBy,createdAt,lastEdit,lastEditBy,ip,likeCount,attachCount,actionType").Where("rid=?").Prepare(), create: acc.Insert("replies").Columns("tid, content, parsed_content, createdAt, lastUpdated, ipaddress, words, createdBy").Fields("?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?").Prepare(),
getAll: acc.Select(re).Columns("rid,tid,content,createdBy,createdAt,lastEdit,lastEditBy,ip,likeCount,attachCount,actionType").Prepare(),
exists: acc.Exists(re, "rid").Prepare(),
create: acc.Insert(re).Columns("tid,content,parsed_content,createdAt,lastUpdated,ip,words,createdBy").Fields("?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?").Prepare(),
count: acc.Count(re).Prepare(),
countUser: acc.Count(re).Where("createdBy=?").Prepare(),
countWordUser: acc.Count(re).Where("createdBy=? AND words>=?").Prepare(),
clearIPs: acc.Update(re).Set("ip=''").Where("ip!=''").Stmt(),
}, acc.FirstError() }, acc.FirstError()
} }
func (s *SQLReplyStore) Get(id int) (*Reply, error) { func (store *SQLReplyStore) Get(id int) (*Reply, error) {
r, err := s.cache.Get(id) reply := Reply{ID: id}
if err == nil { err := store.get.QueryRow(id).Scan(&reply.ParentID, &reply.Content, &reply.CreatedBy, &reply.CreatedAt, &reply.LastEdit, &reply.LastEditBy, &reply.IPAddress, &reply.LikeCount)
return r, nil return &reply, err
}
r = &Reply{ID: id}
err = s.get.QueryRow(id).Scan(&r.ParentID, &r.Content, &r.CreatedBy, &r.CreatedAt, &r.LastEdit, &r.LastEditBy, &r.IP, &r.LikeCount, &r.AttachCount, &r.ActionType)
if err == nil {
_ = s.cache.Set(r)
}
return r, err
}
/*func (s *SQLReplyStore) eachr(f func(*sql.Rows) error) error {
return eachall(s.getAll, f)
}*/
func (s *SQLReplyStore) Each(f func(*Reply) error) error {
rows, err := s.getAll.Query()
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
r := new(Reply)
if err := rows.Scan(&r.ID, &r.ParentID, &r.Content, &r.CreatedBy, &r.CreatedAt, &r.LastEdit, &r.LastEditBy, &r.IP, &r.LikeCount, &r.AttachCount, &r.ActionType); err != nil {
return err
}
if err := f(r); err != nil {
return err
}
}
return rows.Err()
}
func (s *SQLReplyStore) Exists(id int) bool {
err := s.exists.QueryRow(id).Scan(&id)
if err != nil && err != ErrNoRows {
LogError(err)
}
return err != ErrNoRows
}
func (s *SQLReplyStore) ClearIPs() error {
_, e := s.clearIPs.Exec()
return e
} }
// TODO: Write a test for this // TODO: Write a test for this
func (s *SQLReplyStore) Create(t *Topic, content, ip string, uid int) (id int, err error) { func (store *SQLReplyStore) Create(topic *Topic, content string, ipaddress string, uid int) (id int, err error) {
if Config.DisablePostIP { wcount := WordCount(content)
ip = "" res, err := store.create.Exec(topic.ID, content, ParseMessage(content, topic.ParentID, "forums"), ipaddress, wcount, uid)
}
res, err := s.create.Exec(t.ID, content, ParseMessage(content, t.ParentID, "forums", nil, nil), ip, WordCount(content), uid)
if err != nil { if err != nil {
return 0, err return 0, err
} }
lastID, err := res.LastInsertId() lastID, err := res.LastInsertId()
if err != nil { if err != nil {
return 0, err return 0, err
} }
id = int(lastID) return int(lastID), topic.AddReply(uid)
return id, t.AddReply(id, uid)
}
// TODO: Write a test for this
// Count returns the total number of topic replies on these forums
func (s *SQLReplyStore) Count() (count int) {
return Countf(s.count)
}
func (s *SQLReplyStore) CountUser(uid int) (count int) {
return Countf(s.countUser, uid)
}
func (s *SQLReplyStore) CountMegaUser(uid int) (count int) {
return Countf(s.countWordUser, uid, SettingBox.Load().(SettingMap)["megapost_min_words"].(int))
}
func (s *SQLReplyStore) CountBigUser(uid int) (count int) {
return Countf(s.countWordUser, uid, SettingBox.Load().(SettingMap)["bigpost_min_words"].(int))
}
func (s *SQLReplyStore) SetCache(cache ReplyCache) {
s.cache = cache
}
func (s *SQLReplyStore) GetCache() ReplyCache {
return s.cache
} }

View File

@ -5,7 +5,7 @@ import (
"errors" "errors"
"strconv" "strconv"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
// TODO: Make the default report forum ID configurable // TODO: Make the default report forum ID configurable
@ -17,7 +17,7 @@ var ErrAlreadyReported = errors.New("This item has already been reported")
// The report system mostly wraps around the topic system for simplicty // The report system mostly wraps around the topic system for simplicty
type ReportStore interface { type ReportStore interface {
Create(title, content string, u *User, itemType string, itemID int) (int, error) Create(title string, content string, user *User, itemType string, itemID int) (int, error)
} }
type DefaultReportStore struct { type DefaultReportStore struct {
@ -26,17 +26,16 @@ type DefaultReportStore struct {
} }
func NewDefaultReportStore(acc *qgen.Accumulator) (*DefaultReportStore, error) { func NewDefaultReportStore(acc *qgen.Accumulator) (*DefaultReportStore, error) {
t := "topics"
return &DefaultReportStore{ return &DefaultReportStore{
create: acc.Insert(t).Columns("title, content, parsed_content, ip, createdAt, lastReplyAt, createdBy, lastReplyBy, data, parentID, css_class").Fields("?,?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?,?,'report'").Prepare(), create: acc.Insert("topics").Columns("title, content, parsed_content, ipaddress, createdAt, lastReplyAt, createdBy, lastReplyBy, data, parentID, css_class").Fields("?,?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?,?,'report'").Prepare(),
exists: acc.Count(t).Where("data=? AND data!='' AND parentID=?").Prepare(), exists: acc.Count("topics").Where("data = ? AND data != '' AND parentID = ?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
// ! There's a data race in this. If two users report one item at the exact same time, then both reports will go through // ! There's a data race in this. If two users report one item at the exact same time, then both reports will go through
func (s *DefaultReportStore) Create(title, content string, u *User, itemType string, itemID int) (tid int, err error) { func (store *DefaultReportStore) Create(title string, content string, user *User, itemType string, itemID int) (int, error) {
var count int var count int
err = s.exists.QueryRow(itemType+"_"+strconv.Itoa(itemID), ReportForumID).Scan(&count) err := store.exists.QueryRow(itemType + "_" + strconv.Itoa(itemID)).Scan(&count)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
return 0, err return 0, err
} }
@ -44,19 +43,15 @@ func (s *DefaultReportStore) Create(title, content string, u *User, itemType str
return 0, ErrAlreadyReported return 0, ErrAlreadyReported
} }
ip := u.GetIP() res, err := store.create.Exec(title, content, ParseMessage(content, 0, ""), user.LastIP, user.ID, user.ID, itemType+"_"+strconv.Itoa(itemID), ReportForumID)
if Config.DisablePostIP {
ip = ""
}
res, err := s.create.Exec(title, content, ParseMessage(content, 0, "", nil, nil), ip, u.ID, u.ID, itemType+"_"+strconv.Itoa(itemID), ReportForumID)
if err != nil { if err != nil {
return 0, err return 0, err
} }
lastID, err := res.LastInsertId() lastID, err := res.LastInsertId()
if err != nil { if err != nil {
return 0, err return 0, err
} }
tid = int(lastID)
return tid, Forums.AddTopic(tid, u.ID, ReportForumID) return int(lastID), Forums.AddTopic(int(lastID), user.ID, ReportForumID)
} }

View File

@ -1,19 +1,11 @@
package common package common
import ( import (
"crypto/subtle"
"html" "html"
"io"
"net" "net"
"net/http" "net/http"
"os"
"regexp"
"strconv" "strconv"
"strings" "strings"
"time"
"git.tuxpa.in/a/gosora/common/phrases"
"git.tuxpa.in/a/gosora/uutils"
) )
// nolint // nolint
@ -23,88 +15,81 @@ var PreRoute func(http.ResponseWriter, *http.Request) (User, bool) = preRoute
// nolint We need these types so people can tell what they are without scrolling to the bottom of the file // nolint We need these types so people can tell what they are without scrolling to the bottom of the file
var PanelUserCheck func(http.ResponseWriter, *http.Request, *User) (*Header, PanelStats, RouteError) = panelUserCheck var PanelUserCheck func(http.ResponseWriter, *http.Request, *User) (*Header, PanelStats, RouteError) = panelUserCheck
var SimplePanelUserCheck func(http.ResponseWriter, *http.Request, *User) (*HeaderLite, RouteError) = simplePanelUserCheck var SimplePanelUserCheck func(http.ResponseWriter, *http.Request, *User) (*HeaderLite, RouteError) = simplePanelUserCheck
var SimpleForumUserCheck func(w http.ResponseWriter, r *http.Request, u *User, fid int) (headerLite *HeaderLite, err RouteError) = simpleForumUserCheck var SimpleForumUserCheck func(w http.ResponseWriter, r *http.Request, user *User, fid int) (headerLite *HeaderLite, err RouteError) = simpleForumUserCheck
var ForumUserCheck func(h *Header, w http.ResponseWriter, r *http.Request, u *User, fid int) (err RouteError) = forumUserCheck var ForumUserCheck func(w http.ResponseWriter, r *http.Request, user *User, fid int) (header *Header, err RouteError) = forumUserCheck
var SimpleUserCheck func(w http.ResponseWriter, r *http.Request, u *User) (headerLite *HeaderLite, err RouteError) = simpleUserCheck var SimpleUserCheck func(w http.ResponseWriter, r *http.Request, user *User) (headerLite *HeaderLite, err RouteError) = simpleUserCheck
var UserCheck func(w http.ResponseWriter, r *http.Request, u *User) (h *Header, err RouteError) = userCheck var UserCheck func(w http.ResponseWriter, r *http.Request, user *User) (header *Header, err RouteError) = userCheck
var UserCheckNano func(w http.ResponseWriter, r *http.Request, u *User, nano int64) (h *Header, err RouteError) = userCheck2
func simpleForumUserCheck(w http.ResponseWriter, r *http.Request, u *User, fid int) (h *HeaderLite, rerr RouteError) { func simpleForumUserCheck(w http.ResponseWriter, r *http.Request, user *User, fid int) (headerLite *HeaderLite, rerr RouteError) {
h, rerr = SimpleUserCheck(w, r, u)
if rerr != nil {
return h, rerr
}
if !Forums.Exists(fid) { if !Forums.Exists(fid) {
return nil, PreError("The target forum doesn't exist.", w, r) return nil, PreError("The target forum doesn't exist.", w, r)
} }
// Is there a better way of doing the skip AND the success flag on this hook like multiple returns? // Is there a better way of doing the skip AND the success flag on this hook like multiple returns?
/*skip, rerr := h.Hooks.VhookSkippable("simple_forum_check_pre_perms", w, r, u, &fid, h) if VhookSkippable["simple_forum_check_pre_perms"] != nil {
var skip bool
skip, rerr = RunVhookSkippable("simple_forum_check_pre_perms", w, r, user, &fid, &headerLite)
if skip || rerr != nil { if skip || rerr != nil {
return h, rerr return headerLite, rerr
}*/ }
skip, rerr := H_simple_forum_check_pre_perms_hook(h.Hooks, w, r, u, &fid, h)
if skip || rerr != nil {
return h, rerr
} }
fp, err := FPStore.Get(fid, u.Group) fperms, err := FPStore.Get(fid, user.Group)
if err == ErrNoRows { if err == ErrNoRows {
fp = BlankForumPerms() fperms = BlankForumPerms()
} else if err != nil { } else if err != nil {
return h, InternalError(err, w, r) return headerLite, InternalError(err, w, r)
} }
cascadeForumPerms(fp, u) cascadeForumPerms(fperms, user)
return h, nil return headerLite, nil
} }
func forumUserCheck(h *Header, w http.ResponseWriter, r *http.Request, u *User, fid int) (rerr RouteError) { func forumUserCheck(w http.ResponseWriter, r *http.Request, user *User, fid int) (header *Header, rerr RouteError) {
header, rerr = UserCheck(w, r, user)
if rerr != nil {
return header, rerr
}
if !Forums.Exists(fid) { if !Forums.Exists(fid) {
return NotFound(w, r, h) return header, NotFound(w, r, header)
} }
/*skip, rerr := h.Hooks.VhookSkippable("forum_check_pre_perms", w, r, u, &fid, h) if VhookSkippable["forum_check_pre_perms"] != nil {
var skip bool
skip, rerr = RunVhookSkippable("forum_check_pre_perms", w, r, user, &fid, &header)
if skip || rerr != nil { if skip || rerr != nil {
return rerr return header, rerr
}*/ }
/*skip, rerr := VhookSkippableTest(h.Hooks, "forum_check_pre_perms", w, r, u, &fid, h)
if skip || rerr != nil {
return rerr
}*/
skip, rerr := H_forum_check_pre_perms_hook(h.Hooks, w, r, u, &fid, h)
if skip || rerr != nil {
return rerr
} }
fp, err := FPStore.Get(fid, u.Group) fperms, err := FPStore.Get(fid, user.Group)
if err == ErrNoRows { if err == ErrNoRows {
fp = BlankForumPerms() fperms = BlankForumPerms()
} else if err != nil { } else if err != nil {
return InternalError(err, w, r) return header, InternalError(err, w, r)
} }
cascadeForumPerms(fp, u) cascadeForumPerms(fperms, user)
h.CurrentUser = u // TODO: Use a pointer instead for CurrentUser, so we don't have to do this header.CurrentUser = *user // TODO: Use a pointer instead for CurrentUser, so we don't have to do this
return rerr return header, rerr
} }
// TODO: Put this on the user instance? Do we really want forum specific logic in there? Maybe, a method which spits a new pointer with the same contents as user? // TODO: Put this on the user instance? Do we really want forum specific logic in there? Maybe, a method which spits a new pointer with the same contents as user?
func cascadeForumPerms(fp *ForumPerms, u *User) { func cascadeForumPerms(fperms *ForumPerms, user *User) {
if fp.Overrides && !u.IsSuperAdmin { if fperms.Overrides && !user.IsSuperAdmin {
u.Perms.ViewTopic = fp.ViewTopic user.Perms.ViewTopic = fperms.ViewTopic
u.Perms.LikeItem = fp.LikeItem user.Perms.LikeItem = fperms.LikeItem
u.Perms.CreateTopic = fp.CreateTopic user.Perms.CreateTopic = fperms.CreateTopic
u.Perms.EditTopic = fp.EditTopic user.Perms.EditTopic = fperms.EditTopic
u.Perms.DeleteTopic = fp.DeleteTopic user.Perms.DeleteTopic = fperms.DeleteTopic
u.Perms.CreateReply = fp.CreateReply user.Perms.CreateReply = fperms.CreateReply
u.Perms.EditReply = fp.EditReply user.Perms.EditReply = fperms.EditReply
u.Perms.DeleteReply = fp.DeleteReply user.Perms.DeleteReply = fperms.DeleteReply
u.Perms.PinTopic = fp.PinTopic user.Perms.PinTopic = fperms.PinTopic
u.Perms.CloseTopic = fp.CloseTopic user.Perms.CloseTopic = fperms.CloseTopic
u.Perms.MoveTopic = fp.MoveTopic user.Perms.MoveTopic = fperms.MoveTopic
if len(fp.ExtData) != 0 { if len(fperms.ExtData) != 0 {
for name, perm := range fp.ExtData { for name, perm := range fperms.ExtData {
u.PluginPerms[name] = perm user.PluginPerms[name] = perm
} }
} }
} }
@ -112,42 +97,42 @@ func cascadeForumPerms(fp *ForumPerms, u *User) {
// Even if they have the right permissions, the control panel is only open to supermods+. There are many areas without subpermissions which assume that the current user is a supermod+ and admins are extremely unlikely to give these permissions to someone who isn't at-least a supermod to begin with // Even if they have the right permissions, the control panel is only open to supermods+. There are many areas without subpermissions which assume that the current user is a supermod+ and admins are extremely unlikely to give these permissions to someone who isn't at-least a supermod to begin with
// TODO: Do a panel specific theme? // TODO: Do a panel specific theme?
func panelUserCheck(w http.ResponseWriter, r *http.Request, u *User) (h *Header, stats PanelStats, rerr RouteError) { func panelUserCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Header, stats PanelStats, rerr RouteError) {
theme := GetThemeByReq(r) var theme = &Theme{Name: ""}
h = &Header{
cookie, err := r.Cookie("current_theme")
if err == nil {
inTheme, ok := Themes[html.EscapeString(cookie.Value)]
if ok && !theme.HideFromThemes {
theme = inTheme
}
}
if theme.Name == "" {
theme = Themes[DefaultThemeBox.Load().(string)]
}
header = &Header{
Site: Site, Site: Site,
Settings: SettingBox.Load().(SettingMap), Settings: SettingBox.Load().(SettingMap),
//Themes: Themes, Themes: Themes,
ThemesSlice: ThemesSlice,
Theme: theme, Theme: theme,
CurrentUser: u, CurrentUser: *user,
Hooks: GetHookTable(),
Zone: "panel", Zone: "panel",
Writer: w, Writer: w,
IsoCode: phrases.GetLangPack().IsoCode,
//StartedAt: time.Now(),
StartedAt: uutils.Nanotime(),
} }
// TODO: We should probably initialise header.ExtData // TODO: We should probably initialise header.ExtData
// ? - Should we only show this in debug mode? It might be useful for detecting issues in production, if we show it there as-well
//if user.IsAdmin {
//h.StartedAt = time.Now()
//}
h.AddSheet(theme.Name + "/main.css") header.AddSheet(theme.Name + "/panel.css")
h.AddSheet(theme.Name + "/panel.css")
if len(theme.Resources) > 0 { if len(theme.Resources) > 0 {
rlist := theme.Resources rlist := theme.Resources
for _, res := range rlist { for _, resource := range rlist {
if res.LocID == LocGlobal || res.LocID == LocPanel { if resource.Location == "global" || resource.Location == "panel" {
if res.Type == ResTypeSheet { extarr := strings.Split(resource.Name, ".")
h.AddSheet(res.Name) ext := extarr[len(extarr)-1]
} else if res.Type == ResTypeScript { if ext == "css" {
if res.Async { header.AddSheet(resource.Name)
h.AddScriptAsync(res.Name) } else if ext == "js" {
} else { header.AddScript(resource.Name)
h.AddScript(res.Name)
}
} }
} }
} }
@ -157,53 +142,53 @@ func panelUserCheck(w http.ResponseWriter, r *http.Request, u *User) (h *Header,
//h.Set("Content-Security-Policy", "default-src 'self'") //h.Set("Content-Security-Policy", "default-src 'self'")
// TODO: GDPR. Add a global control panel notice warning the admins of staff members who don't have 2FA enabled // TODO: GDPR. Add a global control panel notice warning the admins of staff members who don't have 2FA enabled
stats.Users = Users.Count() stats.Users = Users.GlobalCount()
stats.Groups = Groups.Count() stats.Groups = Groups.GlobalCount()
stats.Forums = Forums.Count() stats.Forums = Forums.GlobalCount()
stats.Pages = Pages.Count() stats.Pages = Pages.GlobalCount()
stats.Settings = len(h.Settings) stats.Settings = len(header.Settings)
stats.WordFilters = WordFilters.EstCount() stats.WordFilters = WordFilters.EstCount()
stats.Themes = len(Themes) stats.Themes = len(Themes)
stats.Reports = 0 // TODO: Do the report count. Only show open threads? stats.Reports = 0 // TODO: Do the report count. Only show open threads?
addPreScript := func(name string, i int) { // TODO: Remove this as it might be counter-productive
// TODO: Optimise this by removing a superfluous string alloc /*pusher, ok := w.(http.Pusher)
if theme.OverridenMap != nil { if ok {
//fmt.Printf("name %+v\n", name) pusher.Push("/static/"+theme.Name+"/main.css", nil)
//fmt.Printf("theme.OverridenMap %+v\n", theme.OverridenMap) pusher.Push("/static/"+theme.Name+"/panel.css", nil)
if _, ok := theme.OverridenMap[name]; ok { pusher.Push("/static/global.js", nil)
tname := "_" + theme.Name pusher.Push("/static/jquery-3.1.1.min.js", nil)
//fmt.Printf("tname %+v\n", tname) // TODO: Test these
h.AddPreScriptAsync("tmpl_" + name + tname + ".js") for _, sheet := range header.Stylesheets {
return pusher.Push("/static/"+sheet, nil)
} }
for _, script := range header.Scripts {
pusher.Push("/static/"+script, nil)
} }
//fmt.Printf("tname %+v\n", tname) // TODO: Push avatars?
h.AddPreScriptAsync(ucstrs[i]) }*/
}
addPreScript("alert", 3)
addPreScript("notice", 4)
return h, stats, nil return header, stats, nil
} }
func simplePanelUserCheck(w http.ResponseWriter, r *http.Request, u *User) (lite *HeaderLite, rerr RouteError) { func simplePanelUserCheck(w http.ResponseWriter, r *http.Request, user *User) (headerLite *HeaderLite, rerr RouteError) {
return SimpleUserCheck(w, r, u) return simpleUserCheck(w, r, user)
} }
// SimpleUserCheck is back from the grave, yay :D // SimpleUserCheck is back from the grave, yay :D
func simpleUserCheck(w http.ResponseWriter, r *http.Request, u *User) (lite *HeaderLite, rerr RouteError) { func simpleUserCheck(w http.ResponseWriter, r *http.Request, user *User) (headerLite *HeaderLite, rerr RouteError) {
return &HeaderLite{ return &HeaderLite{
Site: Site, Site: Site,
Settings: SettingBox.Load().(SettingMap), Settings: SettingBox.Load().(SettingMap),
Hooks: GetHookTable(),
}, nil }, nil
} }
func GetThemeByReq(r *http.Request) *Theme { // TODO: Add the ability for admins to restrict certain themes to certain groups?
theme := &Theme{Name: ""} func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Header, rerr RouteError) {
cookie, e := r.Cookie("current_theme") var theme = &Theme{Name: ""}
if e == nil {
cookie, err := r.Cookie("current_theme")
if err == nil {
inTheme, ok := Themes[html.EscapeString(cookie.Value)] inTheme, ok := Themes[html.EscapeString(cookie.Value)]
if ok && !theme.HideFromThemes { if ok && !theme.HideFromThemes {
theme = inTheme theme = inTheme
@ -212,145 +197,76 @@ func GetThemeByReq(r *http.Request) *Theme {
if theme.Name == "" { if theme.Name == "" {
theme = Themes[DefaultThemeBox.Load().(string)] theme = Themes[DefaultThemeBox.Load().(string)]
} }
return theme
}
func userCheck(w http.ResponseWriter, r *http.Request, u *User) (h *Header, rerr RouteError) { header = &Header{
return userCheck2(w, r, u, uutils.Nanotime())
}
// TODO: Add the ability for admins to restrict certain themes to certain groups?
// ! Be careful about firing errors off here as CustomError uses this
func userCheck2(w http.ResponseWriter, r *http.Request, u *User, nano int64) (h *Header, rerr RouteError) {
theme := GetThemeByReq(r)
h = &Header{
Site: Site, Site: Site,
Settings: SettingBox.Load().(SettingMap), Settings: SettingBox.Load().(SettingMap),
//Themes: Themes, Themes: Themes,
ThemesSlice: ThemesSlice,
Theme: theme, Theme: theme,
CurrentUser: u, // ! Some things rely on this being a pointer downstream from this function CurrentUser: *user, // ! Some things rely on this being a pointer downstream from this function
Hooks: GetHookTable(), Zone: "frontend",
Zone: ucstrs[0],
Writer: w, Writer: w,
IsoCode: phrases.GetLangPack().IsoCode,
StartedAt: nano,
}
// TODO: Optimise this by avoiding accessing a map string index
if !u.Loggedin {
h.GoogSiteVerify = h.Settings["google_site_verify"].(string)
} }
if u.IsBanned { if user.IsBanned {
h.AddNotice("account_banned") header.AddNotice("account_banned")
} }
if u.Loggedin && !u.Active { if user.Loggedin && !user.Active {
h.AddNotice("account_inactive") header.AddNotice("account_inactive")
} }
/*h.Scripts, _ = StrSlicePool.Get().([]string)
if h.Scripts != nil {
h.Scripts = h.Scripts[:0]
}
h.PreScriptsAsync, _ = StrSlicePool.Get().([]string)
if h.PreScriptsAsync != nil {
h.PreScriptsAsync = h.PreScriptsAsync[:0]
}*/
// An optimisation so we don't populate StartedAt for users who shouldn't see the stat anyway
// ? - Should we only show this in debug mode? It might be useful for detecting issues in production, if we show it there as-well
//if u.IsAdmin {
//h.StartedAt = time.Now()
//}
//PrepResources(u,h,theme)
return h, nil
}
func PrepResources(u *User, h *Header, theme *Theme) {
h.AddSheet(theme.Name + "/main.css")
if len(theme.Resources) > 0 { if len(theme.Resources) > 0 {
rlist := theme.Resources rlist := theme.Resources
for _, res := range rlist { for _, resource := range rlist {
if res.Loggedin && !u.Loggedin { if resource.Loggedin && !user.Loggedin {
continue continue
} }
if res.LocID == LocGlobal || res.LocID == LocFront { if resource.Location == "global" || resource.Location == "frontend" {
if res.Type == ResTypeSheet { extarr := strings.Split(resource.Name, ".")
h.AddSheet(res.Name) ext := extarr[len(extarr)-1]
} else if res.Type == ResTypeScript { if ext == "css" {
if res.Async { header.AddSheet(resource.Name)
h.AddScriptAsync(res.Name) } else if ext == "js" {
} else { header.AddScript(resource.Name)
h.AddScript(res.Name)
}
} }
} }
} }
} }
addPreScript := func(name string, i int) { /*pusher, ok := w.(http.Pusher)
// TODO: Optimise this by removing a superfluous string alloc if ok {
if theme.OverridenMap != nil { pusher.Push("/static/"+theme.Name+"/main.css", nil)
//fmt.Printf("name %+v\n", name) pusher.Push("/static/global.js", nil)
//fmt.Printf("theme.OverridenMap %+v\n", theme.OverridenMap) pusher.Push("/static/jquery-3.1.1.min.js", nil)
if _, ok := theme.OverridenMap[name]; ok { // TODO: Test these
tname := "_" + theme.Name for _, sheet := range header.Stylesheets {
//fmt.Printf("tname %+v\n", tname) pusher.Push("/static/"+sheet, nil)
h.AddPreScriptAsync("tmpl_" + name + tname + ".js")
return
}
}
//fmt.Printf("tname %+v\n", tname)
h.AddPreScriptAsync(ucstrs[i])
}
addPreScript("topics_topic", 1)
addPreScript("paginator", 2)
addPreScript("alert", 3)
addPreScript("notice", 4)
if u.Loggedin {
addPreScript("topic_c_edit_post", 5)
addPreScript("topic_c_attach_item", 6)
addPreScript("topic_c_poll_input", 7)
} }
for _, script := range header.Scripts {
pusher.Push("/static/"+script, nil)
} }
// TODO: Push avatars?
}*/
func pstr(name string) string { return header, nil
return "tmpl_" + name + ".js"
}
var ucstrs = [...]string{
"frontend",
pstr("topics_topic"),
pstr("paginator"),
pstr("alert"),
pstr("notice"),
pstr("topic_c_edit_post"),
pstr("topic_c_attach_item"),
pstr("topic_c_poll_input"),
} }
func preRoute(w http.ResponseWriter, r *http.Request) (User, bool) { func preRoute(w http.ResponseWriter, r *http.Request) (User, bool) {
userptr, halt := Auth.SessionCheck(w, r) user, halt := Auth.SessionCheck(w, r)
if halt { if halt {
return *userptr, false return *user, false
} }
var usercpy *User = BlankUser() var usercpy *User = BlankUser()
*usercpy = *userptr *usercpy = *user
usercpy.Init() // TODO: Can we reduce the amount of work we do here?
// TODO: Add a config setting to disable this header
// TODO: Have this header cover more things
if Config.SslSchema {
w.Header().Set("Content-Security-Policy", "upgrade-insecure-requests")
}
// TODO: WIP. Refactor this to eliminate the unnecessary query // TODO: WIP. Refactor this to eliminate the unnecessary query
// TODO: Better take proxies into consideration // TODO: Better take proxies into consideration
if !Config.DisableIP { host, _, err := net.SplitHostPort(r.RemoteAddr)
var host string if err != nil {
PreError("Bad IP", w, r)
return *usercpy, false
}
// TODO: Prefer Cf-Connecting-Ip header, fewer shenanigans // TODO: Prefer Cf-Connecting-Ip header, fewer shenanigans
if Site.HasProxy { if Site.HasProxy {
// TODO: Check the right-most IP, might get tricky with multiple proxies, maybe have a setting for the number of hops we jump through // TODO: Check the right-most IP, might get tricky with multiple proxies, maybe have a setting for the number of hops we jump through
@ -362,178 +278,82 @@ func preRoute(w http.ResponseWriter, r *http.Request) (User, bool) {
} }
} }
if host == "" { // TODO: Add a config setting to disable this header
var e error // TODO: Have this header cover more things
host, _, e = net.SplitHostPort(r.RemoteAddr) w.Header().Set("Content-Security-Policy", "upgrade-insecure-requests")
if e != nil {
_ = PreError("Bad IP", w, r) if user == &GuestUser {
return *usercpy, false usercpy.LastIP = host
} return *usercpy, true
} }
if !Config.DisableLastIP && usercpy.Loggedin && host != usercpy.GetIP() { if host != usercpy.LastIP {
mon := time.Now().Month() err = usercpy.UpdateIP(host)
e := usercpy.UpdateIP(strconv.Itoa(int(mon)) + "-" + host) if err != nil {
if e != nil { InternalError(err, w, r)
_ = InternalError(e, w, r)
return *usercpy, false return *usercpy, false
} }
}
usercpy.LastIP = host usercpy.LastIP = host
} }
return *usercpy, true return *usercpy, true
} }
func UploadAvatar(w http.ResponseWriter, r *http.Request, u *User, tuid int) (ext string, ferr RouteError) {
// We don't want multiple files
// TODO: Are we doing this correctly?
filenameMap := make(map[string]bool)
for _, fheaders := range r.MultipartForm.File {
for _, hdr := range fheaders {
if hdr.Filename == "" {
continue
}
filenameMap[hdr.Filename] = true
}
}
if len(filenameMap) > 1 {
return "", LocalError("You may only upload one avatar", w, r, u)
}
for _, fheaders := range r.MultipartForm.File {
for _, hdr := range fheaders {
if hdr.Filename == "" {
continue
}
inFile, err := hdr.Open()
if err != nil {
return "", LocalError("Upload failed", w, r, u)
}
defer inFile.Close()
if ext == "" {
extarr := strings.Split(hdr.Filename, ".")
if len(extarr) < 2 {
return "", LocalError("Bad file", w, r, u)
}
ext = extarr[len(extarr)-1]
// TODO: Can we do this without a regex?
reg, err := regexp.Compile("[^A-Za-z0-9]+")
if err != nil {
return "", LocalError("Bad file extension", w, r, u)
}
ext = reg.ReplaceAllString(ext, "")
ext = strings.ToLower(ext)
if !ImageFileExts.Contains(ext) {
return "", LocalError("You can only use an image for your avatar", w, r, u)
}
}
// TODO: Centralise this string, so we don't have to change it in two different places when it changes
outFile, err := os.Create("./uploads/avatar_" + strconv.Itoa(tuid) + "." + ext)
if err != nil {
return "", LocalError("Upload failed [File Creation Failed]", w, r, u)
}
defer outFile.Close()
_, err = io.Copy(outFile, inFile)
if err != nil {
return "", LocalError("Upload failed [Copy Failed]", w, r, u)
}
}
}
if ext == "" {
return "", LocalError("No file", w, r, u)
}
return ext, nil
}
func ChangeAvatar(path string, w http.ResponseWriter, r *http.Request, u *User) RouteError {
e := u.ChangeAvatar(path)
if e != nil {
return InternalError(e, w, r)
}
// Clean up the old avatar data, so we don't end up with too many dead files in /uploads/
if len(u.RawAvatar) > 2 {
if u.RawAvatar[0] == '.' && u.RawAvatar[1] == '.' {
e := os.Remove("./uploads/avatar_" + strconv.Itoa(u.ID) + "_tmp" + u.RawAvatar[1:])
if e != nil && !os.IsNotExist(e) {
LogWarning(e)
return LocalError("Something went wrong", w, r, u)
}
e = os.Remove("./uploads/avatar_" + strconv.Itoa(u.ID) + "_w48" + u.RawAvatar[1:])
if e != nil && !os.IsNotExist(e) {
LogWarning(e)
return LocalError("Something went wrong", w, r, u)
}
}
}
return nil
}
// SuperAdminOnly makes sure that only super admin can access certain critical panel routes // SuperAdminOnly makes sure that only super admin can access certain critical panel routes
func SuperAdminOnly(w http.ResponseWriter, r *http.Request, u *User) RouteError { func SuperAdminOnly(w http.ResponseWriter, r *http.Request, user User) RouteError {
if !u.IsSuperAdmin { if !user.IsSuperAdmin {
return NoPermissions(w, r, u) return NoPermissions(w, r, user)
} }
return nil return nil
} }
// AdminOnly makes sure that only admins can access certain panel routes // AdminOnly makes sure that only admins can access certain panel routes
func AdminOnly(w http.ResponseWriter, r *http.Request, u *User) RouteError { func AdminOnly(w http.ResponseWriter, r *http.Request, user User) RouteError {
if !u.IsAdmin { if !user.IsAdmin {
return NoPermissions(w, r, u) return NoPermissions(w, r, user)
} }
return nil return nil
} }
// SuperModeOnly makes sure that only super mods or higher can access the panel routes // SuperModeOnly makes sure that only super mods or higher can access the panel routes
func SuperModOnly(w http.ResponseWriter, r *http.Request, u *User) RouteError { func SuperModOnly(w http.ResponseWriter, r *http.Request, user User) RouteError {
if !u.IsSuperMod { if !user.IsSuperMod {
return NoPermissions(w, r, u) return NoPermissions(w, r, user)
} }
return nil return nil
} }
// MemberOnly makes sure that only logged in users can access this route // MemberOnly makes sure that only logged in users can access this route
func MemberOnly(w http.ResponseWriter, r *http.Request, u *User) RouteError { func MemberOnly(w http.ResponseWriter, r *http.Request, user User) RouteError {
if !u.Loggedin { if !user.Loggedin {
return LoginRequired(w, r, u) return LoginRequired(w, r, user)
} }
return nil return nil
} }
// NoBanned stops any banned users from accessing this route // NoBanned stops any banned users from accessing this route
func NoBanned(w http.ResponseWriter, r *http.Request, u *User) RouteError { func NoBanned(w http.ResponseWriter, r *http.Request, user User) RouteError {
if u.IsBanned { if user.IsBanned {
return Banned(w, r, u) return Banned(w, r, user)
} }
return nil return nil
} }
func ParseForm(w http.ResponseWriter, r *http.Request, u *User) RouteError { func ParseForm(w http.ResponseWriter, r *http.Request, user User) RouteError {
if e := r.ParseForm(); e != nil { err := r.ParseForm()
return LocalError("Bad Form", w, r, u) if err != nil {
return LocalError("Bad Form", w, r, user)
} }
return nil return nil
} }
func NoSessionMismatch(w http.ResponseWriter, r *http.Request, u *User) RouteError { func NoSessionMismatch(w http.ResponseWriter, r *http.Request, user User) RouteError {
if e := r.ParseForm(); e != nil { err := r.ParseForm()
return LocalError("Bad Form", w, r, u) if err != nil {
return LocalError("Bad Form", w, r, user)
} }
if len(u.Session) == 0 { if r.FormValue("session") != user.Session {
return SecurityError(w, r, u) return SecurityError(w, r, user)
}
// TODO: Try to eliminate some of these allocations
sess := []byte(u.Session)
if subtle.ConstantTimeCompare([]byte(r.FormValue("session")), sess) != 1 && subtle.ConstantTimeCompare([]byte(r.FormValue("s")), sess) != 1 {
return SecurityError(w, r, u)
} }
return nil return nil
} }
@ -542,29 +362,24 @@ func ReqIsJson(r *http.Request) bool {
return r.Header.Get("Content-type") == "application/json" return r.Header.Get("Content-type") == "application/json"
} }
func HandleUploadRoute(w http.ResponseWriter, r *http.Request, u *User, maxFileSize int) RouteError { func HandleUploadRoute(w http.ResponseWriter, r *http.Request, user User, maxFileSize int) RouteError {
// TODO: Reuse this code more // TODO: Reuse this code more
if r.ContentLength > int64(maxFileSize) { if r.ContentLength > int64(maxFileSize) {
size, unit := ConvertByteUnit(float64(maxFileSize)) size, unit := ConvertByteUnit(float64(maxFileSize))
return CustomError("Your upload is too big. Your files need to be smaller than "+strconv.Itoa(int(size))+unit+".", http.StatusExpectationFailed, "Error", w, r, nil, u) return CustomError("Your upload is too big. Your files need to be smaller than "+strconv.Itoa(int(size))+unit+".", http.StatusExpectationFailed, "Error", w, r, nil, user)
} }
r.Body = http.MaxBytesReader(w, r.Body, r.ContentLength) r.Body = http.MaxBytesReader(w, r.Body, int64(maxFileSize))
e := r.ParseMultipartForm(int64(Megabyte)) err := r.ParseMultipartForm(int64(Megabyte))
if e != nil { if err != nil {
return LocalError("Bad Form", w, r, u) return LocalError("Bad Form", w, r, user)
} }
return nil return nil
} }
func NoUploadSessionMismatch(w http.ResponseWriter, r *http.Request, u *User) RouteError { func NoUploadSessionMismatch(w http.ResponseWriter, r *http.Request, user User) RouteError {
if len(u.Session) == 0 { if r.FormValue("session") != user.Session {
return SecurityError(w, r, u) return SecurityError(w, r, user)
}
// TODO: Try to eliminate some of these allocations
sess := []byte(u.Session)
if subtle.ConstantTimeCompare([]byte(r.FormValue("session")), sess) != 1 && subtle.ConstantTimeCompare([]byte(r.FormValue("s")), sess) != 1 {
return SecurityError(w, r, u)
} }
return nil return nil
} }

View File

@ -1,155 +0,0 @@
package common
import (
"database/sql"
"errors"
"strconv"
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var RepliesSearch Searcher
type Searcher interface {
Query(q string, zones []int) ([]int, error)
}
// TODO: Implement this
// Note: This is slow compared to something like ElasticSearch and very limited
type SQLSearcher struct {
queryReplies *sql.Stmt
queryTopics *sql.Stmt
queryRepliesZone *sql.Stmt
queryTopicsZone *sql.Stmt
//queryZone *sql.Stmt
fuzzyZone *sql.Stmt
}
// TODO: Support things other than MySQL
// TODO: Use LIMIT?
func NewSQLSearcher(acc *qgen.Accumulator) (*SQLSearcher, error) {
if acc.GetAdapter().GetName() != "mysql" {
return nil, errors.New("SQLSearcher only supports MySQL at this time")
}
return &SQLSearcher{
queryReplies: acc.RawPrepare("SELECT tid FROM replies WHERE MATCH(content) AGAINST (? IN BOOLEAN MODE)"),
queryTopics: acc.RawPrepare("SELECT tid FROM topics WHERE MATCH(title) AGAINST (? IN BOOLEAN MODE) OR MATCH(content) AGAINST (? IN BOOLEAN MODE)"),
queryRepliesZone: acc.RawPrepare("SELECT tid FROM replies WHERE MATCH(content) AGAINST (? IN BOOLEAN MODE) AND tid=?"),
queryTopicsZone: acc.RawPrepare("SELECT tid FROM topics WHERE (MATCH(title) AGAINST (? IN BOOLEAN MODE) OR MATCH(content) AGAINST (? IN BOOLEAN MODE)) AND parentID=?"),
//queryZone: acc.RawPrepare("SELECT topics.tid FROM topics INNER JOIN replies ON topics.tid = replies.tid WHERE (topics.title=? OR (MATCH(topics.title) AGAINST (? IN BOOLEAN MODE) OR MATCH(topics.content) AGAINST (? IN BOOLEAN MODE) OR MATCH(replies.content) AGAINST (? IN BOOLEAN MODE)) OR topics.content=? OR replies.content=?) AND topics.parentID=?"),
fuzzyZone: acc.RawPrepare("SELECT topics.tid FROM topics INNER JOIN replies ON topics.tid = replies.tid WHERE (topics.title LIKE ? OR topics.content LIKE ? OR replies.content LIKE ?) AND topics.parentID=?"),
}, acc.FirstError()
}
func (s *SQLSearcher) queryAll(q string) ([]int, error) {
var ids []int
run := func(stmt *sql.Stmt, q ...interface{}) error {
rows, e := stmt.Query(q...)
if e == sql.ErrNoRows {
return nil
} else if e != nil {
return e
}
defer rows.Close()
for rows.Next() {
var id int
if e := rows.Scan(&id); e != nil {
return e
}
ids = append(ids, id)
}
return rows.Err()
}
err := run(s.queryReplies, q)
if err != nil {
return nil, err
}
err = run(s.queryTopics, q, q)
if err != nil {
return nil, err
}
if len(ids) == 0 {
err = sql.ErrNoRows
}
return ids, err
}
func (s *SQLSearcher) Query(q string, zones []int) (ids []int, err error) {
if len(zones) == 0 {
return nil, nil
}
run := func(rows *sql.Rows, e error) error {
/*if e == sql.ErrNoRows {
return nil
} else */if e != nil {
return e
}
defer rows.Close()
for rows.Next() {
var id int
if e := rows.Scan(&id); e != nil {
return e
}
ids = append(ids, id)
}
return rows.Err()
}
if len(zones) == 1 {
//err = run(s.queryZone.Query(q, q, q, q, q,q, zones[0]))
err = run(s.queryRepliesZone.Query(q, zones[0]))
if err != nil {
return nil, err
}
err = run(s.queryTopicsZone.Query(q, q,zones[0]))
} else {
var zList string
for _, zone := range zones {
zList += strconv.Itoa(zone) + ","
}
zList = zList[:len(zList)-1]
acc := qgen.NewAcc()
/*stmt := acc.RawPrepare("SELECT topics.tid FROM topics INNER JOIN replies ON topics.tid = replies.tid WHERE (MATCH(topics.title) AGAINST (? IN BOOLEAN MODE) OR MATCH(topics.content) AGAINST (? IN BOOLEAN MODE) OR MATCH(replies.content) AGAINST (? IN BOOLEAN MODE) OR topics.title=? OR topics.content=? OR replies.content=?) AND topics.parentID IN(" + zList + ")")
if err = acc.FirstError(); err != nil {
return nil, err
}*/
// TODO: Cache common IN counts
stmt := acc.RawPrepare("SELECT tid FROM topics WHERE (MATCH(topics.title) AGAINST (? IN BOOLEAN MODE) OR MATCH(topics.content) AGAINST (? IN BOOLEAN MODE)) AND parentID IN(" + zList + ")")
if err = acc.FirstError(); err != nil {
return nil, err
}
err = run(stmt.Query(q, q))
if err != nil {
return nil, err
}
stmt = acc.RawPrepare("SELECT tid FROM replies WHERE MATCH(replies.content) AGAINST (? IN BOOLEAN MODE) AND tid IN(" + zList + ")")
if err = acc.FirstError(); err != nil {
return nil, err
}
err = run(stmt.Query(q))
//err = run(stmt.Query(q, q, q, q, q, q))
}
if err != nil {
return nil, err
}
if len(ids) == 0 {
err = sql.ErrNoRows
}
return ids, err
}
// TODO: Implement this
type ElasticSearchSearcher struct {
}
func NewElasticSearchSearcher() (*ElasticSearchSearcher, error) {
return &ElasticSearchSearcher{}, nil
}
func (s *ElasticSearchSearcher) Query(q string, zones []int) ([]int, error) {
return nil, nil
}

View File

@ -7,7 +7,7 @@ import (
"strings" "strings"
"sync/atomic" "sync/atomic"
qgen "git.tuxpa.in/a/gosora/query_gen" "../query_gen/lib"
) )
var SettingBox atomic.Value // An atomic value pointing to a SettingBox var SettingBox atomic.Value // An atomic value pointing to a SettingBox
@ -16,7 +16,7 @@ var SettingBox atomic.Value // An atomic value pointing to a SettingBox
type SettingMap map[string]interface{} type SettingMap map[string]interface{}
type SettingStore interface { type SettingStore interface {
ParseSetting(name, content, typ, constraint string) string ParseSetting(sname string, scontent string, stype string, sconstraint string) string
BypassGet(name string) (*Setting, error) BypassGet(name string) (*Setting, error)
BypassGetAll(name string) ([]*Setting, error) BypassGetAll(name string) ([]*Setting, error)
} }
@ -45,31 +45,30 @@ var settingStmts SettingStmts
func init() { func init() {
SettingBox.Store(SettingMap(make(map[string]interface{}))) SettingBox.Store(SettingMap(make(map[string]interface{})))
DbInits.Add(func(acc *qgen.Accumulator) error { DbInits.Add(func(acc *qgen.Accumulator) error {
s := "settings"
settingStmts = SettingStmts{ settingStmts = SettingStmts{
getAll: acc.Select(s).Columns("name,content,type,constraints").Prepare(), getAll: acc.Select("settings").Columns("name, content, type, constraints").Prepare(),
get: acc.Select(s).Columns("content,type,constraints").Where("name=?").Prepare(), get: acc.Select("settings").Columns("content, type, constraints").Where("name = ?").Prepare(),
update: acc.Update(s).Set("content=?").Where("name=?").Prepare(), update: acc.Update("settings").Set("content = ?").Where("name = ?").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })
} }
func (s *Setting) Copy() (o *Setting) { func (setting *Setting) Copy() (out *Setting) {
o = &Setting{Name: ""} out = &Setting{Name: ""}
*o = *s *out = *setting
return o return out
} }
func LoadSettings() error { func LoadSettings() error {
sBox := SettingMap(make(map[string]interface{})) var sBox = SettingMap(make(map[string]interface{}))
settings, err := sBox.BypassGetAll() settings, err := sBox.BypassGetAll()
if err != nil { if err != nil {
return err return err
} }
for _, s := range settings { for _, setting := range settings {
err = sBox.ParseSetting(s.Name, s.Content, s.Type, s.Constraint) err = sBox.ParseSetting(setting.Name, setting.Content, setting.Type, setting.Constraint)
if err != nil { if err != nil {
return err return err
} }
@ -79,21 +78,33 @@ func LoadSettings() error {
return nil return nil
} }
// nolint
var ErrNotInteger = errors.New("You were supposed to enter an integer x.x")
var ErrSettingNotInteger = errors.New("Only integers are allowed in this setting x.x")
var ErrBadConstraintNotInteger = errors.New("Invalid contraint! The constraint field wasn't an integer!")
var ErrBadSettingRange = errors.New("Only integers between a certain range are allowed in this setting")
// To avoid leaking internal state to the user
// TODO: We need to add some sort of DualError interface
func SafeSettingError(err error) bool {
return err == ErrNotInteger || err == ErrSettingNotInteger || err == ErrBadConstraintNotInteger || err == ErrBadSettingRange || err == ErrNoRows
}
// TODO: Add better support for HTML attributes (html-attribute). E.g. Meta descriptions. // TODO: Add better support for HTML attributes (html-attribute). E.g. Meta descriptions.
func (sBox SettingMap) ParseSetting(name, content, typ, constraint string) (err error) { func (sBox SettingMap) ParseSetting(sname string, scontent string, stype string, constraint string) (err error) {
ssBox := map[string]interface{}(sBox) var ssBox = map[string]interface{}(sBox)
switch typ { switch stype {
case "bool": case "bool":
ssBox[name] = (content == "1") ssBox[sname] = (scontent == "1")
case "int": case "int":
ssBox[name], err = strconv.Atoi(content) ssBox[sname], err = strconv.Atoi(scontent)
if err != nil { if err != nil {
return errors.New("You were supposed to enter an integer x.x") return ErrNotInteger
} }
case "int64": case "int64":
ssBox[name], err = strconv.ParseInt(content, 10, 64) ssBox[sname], err = strconv.ParseInt(scontent, 10, 64)
if err != nil { if err != nil {
return errors.New("You were supposed to enter an integer x.x") return ErrNotInteger
} }
case "list": case "list":
cons := strings.Split(constraint, "-") cons := strings.Split(constraint, "-")
@ -104,28 +115,28 @@ func (sBox SettingMap) ParseSetting(name, content, typ, constraint string) (err
con1, err := strconv.Atoi(cons[0]) con1, err := strconv.Atoi(cons[0])
con2, err2 := strconv.Atoi(cons[1]) con2, err2 := strconv.Atoi(cons[1])
if err != nil || err2 != nil { if err != nil || err2 != nil {
return errors.New("Invalid contraint! The constraint field wasn't an integer!") return ErrBadConstraintNotInteger
} }
val, err := strconv.Atoi(content) value, err := strconv.Atoi(scontent)
if err != nil { if err != nil {
return errors.New("Only integers are allowed in this setting x.x") return ErrSettingNotInteger
} }
if val < con1 || val > con2 { if value < con1 || value > con2 {
return errors.New("Only integers between a certain range are allowed in this setting") return ErrBadSettingRange
} }
ssBox[name] = val ssBox[sname] = value
default: default:
ssBox[name] = content ssBox[sname] = scontent
} }
return nil return nil
} }
func (sBox SettingMap) BypassGet(name string) (*Setting, error) { func (sBox SettingMap) BypassGet(name string) (*Setting, error) {
s := &Setting{Name: name} setting := &Setting{Name: name}
err := settingStmts.get.QueryRow(name).Scan(&s.Content, &s.Type, &s.Constraint) err := settingStmts.get.QueryRow(name).Scan(&setting.Content, &setting.Type, &setting.Constraint)
return s, err return setting, err
} }
func (sBox SettingMap) BypassGetAll() (settingList []*Setting, err error) { func (sBox SettingMap) BypassGetAll() (settingList []*Setting, err error) {
@ -136,26 +147,24 @@ func (sBox SettingMap) BypassGetAll() (settingList []*Setting, err error) {
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
s := &Setting{Name: ""} setting := &Setting{Name: ""}
err := rows.Scan(&s.Name, &s.Content, &s.Type, &s.Constraint) err := rows.Scan(&setting.Name, &setting.Content, &setting.Type, &setting.Constraint)
if err != nil { if err != nil {
return nil, err return nil, err
} }
settingList = append(settingList, s) settingList = append(settingList, setting)
} }
return settingList, rows.Err() return settingList, rows.Err()
} }
func (sBox SettingMap) Update(name, content string) RouteError { func (sBox SettingMap) Update(name string, content string) error {
s, err := sBox.BypassGet(name) setting, err := sBox.BypassGet(name)
if err == ErrNoRows { if err == ErrNoRows {
return FromError(err) return err
} else if err != nil {
return SysError(err.Error())
} }
// TODO: Why is this here and not in a common function? // TODO: Why is this here and not in a common function?
if s.Type == "bool" { if setting.Type == "bool" {
if content == "on" || content == "1" { if content == "on" || content == "1" {
content = "1" content = "1"
} else { } else {
@ -163,20 +172,17 @@ func (sBox SettingMap) Update(name, content string) RouteError {
} }
} }
err = sBox.ParseSetting(name, content, s.Type, s.Constraint)
if err != nil {
return FromError(err)
}
// TODO: Make this a method or function? // TODO: Make this a method or function?
_, err = settingStmts.update.Exec(content, name) _, err = settingStmts.update.Exec(content, name)
if err != nil { if err != nil {
return SysError(err.Error()) return err
} }
err = LoadSettings() err = sBox.ParseSetting(name, content, setting.Type, setting.Constraint)
if err != nil { if err != nil {
return SysError(err.Error()) return err
} }
// TODO: Do a reload instead?
SettingBox.Store(sBox)
return nil return nil
} }

View File

@ -2,13 +2,11 @@ package common
import ( import (
"encoding/json" "encoding/json"
"errors"
"io/ioutil" "io/ioutil"
"log" "log"
"net/url"
"strconv" "strconv"
"strings" "strings"
"github.com/pkg/errors"
) )
// Site holds the basic settings which should be tweaked when setting up a site, we might move them to the settings table at some point // Site holds the basic settings which should be tweaked when setting up a site, we might move them to the settings table at some point
@ -23,17 +21,13 @@ var Config = new(config)
// Dev holds build flags and other things which should only be modified during developers or to gather additional test data // Dev holds build flags and other things which should only be modified during developers or to gather additional test data
var Dev = new(devConfig) var Dev = new(devConfig)
var PluginConfig = map[string]string{}
type site struct { type site struct {
ShortName string ShortName string
Name string Name string
Email string Email string
URL string URL string
Host string Host string
LocalHost bool // Used internally, do not modify as it will be overwritten
Port string Port string
PortInt int // Alias for efficiency, do not modify, will be overwritten
EnableSsl bool EnableSsl bool
EnableEmails bool EnableEmails bool
HasProxy bool HasProxy bool
@ -64,7 +58,6 @@ type config struct {
SslPrivkey string SslPrivkey string
SslFullchain string SslFullchain string
HashAlgo string // Defaults to bcrypt, and in the future, possibly something stronger HashAlgo string // Defaults to bcrypt, and in the future, possibly something stronger
ConvoKey string
MaxRequestSizeStr string MaxRequestSizeStr string
MaxRequestSize int MaxRequestSize int
@ -72,16 +65,12 @@ type config struct {
UserCacheCapacity int UserCacheCapacity int
TopicCache string TopicCache string
TopicCacheCapacity int TopicCacheCapacity int
ReplyCache string
ReplyCacheCapacity int
SMTPServer string SMTPServer string
SMTPUsername string SMTPUsername string
SMTPPassword string SMTPPassword string
SMTPPort string SMTPPort string
SMTPEnableTLS bool //SMTPEnableTLS bool
Search string
DefaultPath string DefaultPath string
DefaultGroup int // Should be a setting in the database DefaultGroup int // Should be a setting in the database
@ -90,58 +79,14 @@ type config struct {
DefaultForum int // The forum posts go in by default, this used to be covered by the Uncategorised Forum, but we want to replace it with a more robust solution. Make this a setting? DefaultForum int // The forum posts go in by default, this used to be covered by the Uncategorised Forum, but we want to replace it with a more robust solution. Make this a setting?
MinifyTemplates bool MinifyTemplates bool
BuildSlugs bool // TODO: Make this a setting? BuildSlugs bool // TODO: Make this a setting?
PrimaryServer bool
ServerCount int ServerCount int
LastIPCutoff int // Currently just -1, non--1, but will accept the number of months a user's last IP should be retained for in the future before being purged. Please note that the other two cutoffs below operate off the numbers of days instead.
PostIPCutoff int
PollIPCutoff int
LogPruneCutoff int
//SelfDeleteTruncCutoff int // Personal data is stripped from the mod action rows only leaving the TID and the action for later investigation.
DisableIP bool
DisableLastIP bool
DisablePostIP bool
DisablePollIP bool
DisableRegLog bool
DisableLoginLog bool
//DisableSelfDeleteLog bool
DisableLiveTopicList bool DisableLiveTopicList bool
DisableJSAntispam bool
//LooseCSP bool
LooseHost bool
LoosePort bool
SslSchema bool // Pretend we're using SSL, might be useful if a reverse-proxy terminates SSL in-front of Gosora
DisableServerPush bool
EnableCDNPush bool
DisableNoavatarRange bool
DisableDefaultNoavatar bool
DisableAnalytics bool
RefNoTrack bool
RefNoRef bool
NoEmbed bool
ExtraCSPOrigins string
StaticResBase string // /s/
//DynStaticResBase string
AvatarResBase string // /uploads/
Noavatar string // ? - Move this into the settings table? Noavatar string // ? - Move this into the settings table?
ItemsPerPage int // ? - Move this into the settings table? ItemsPerPage int // ? - Move this into the settings table?
MaxTopicTitleLength int MaxTopicTitleLength int
MaxUsernameLength int MaxUsernameLength int
ReadTimeout int
WriteTimeout int
IdleTimeout int
LogDir string
DisableSuspLog bool
DisableBadRouteLog bool
DisableStdout bool
DisableStderr bool
} }
type devConfig struct { type devConfig struct {
@ -150,19 +95,6 @@ type devConfig struct {
TemplateDebug bool TemplateDebug bool
Profiling bool Profiling bool
TestDB bool TestDB bool
NoFsnotify bool // Super Experimental!
FullReqLog bool
ExtraTmpls string // Experimental flag for adding compiled templates, we'll likely replace this with a better mechanism
//QuicPort int // Experimental!
//ExpFix1 bool // unlisted setting, experimental fix for http/1.1 conn hangs
LogLongTick bool // unlisted setting
LogNewLongRoute bool // unlisted setting
Log4thLongRoute bool // unlisted setting
HourDBTimeout bool // unlisted setting
} }
// configHolder is purely for having a big struct to unmarshal data into // configHolder is purely for having a big struct to unmarshal data into
@ -171,7 +103,6 @@ type configHolder struct {
Config *config Config *config
Database *dbConfig Database *dbConfig
Dev *devConfig Dev *devConfig
Plugin map[string]string
} }
func LoadConfig() error { func LoadConfig() error {
@ -190,42 +121,19 @@ func LoadConfig() error {
Config = config.Config Config = config.Config
DbConfig = config.Database DbConfig = config.Database
Dev = config.Dev Dev = config.Dev
PluginConfig = config.Plugin
return nil return nil
} }
var noavatarCache200 []string
var noavatarCache48 []string
/*var noavatarCache200Jpg []string
var noavatarCache48Jpg []string
var noavatarCache200Avif []string
var noavatarCache48Avif []string*/
func ProcessConfig() (err error) { func ProcessConfig() (err error) {
// Strip these unnecessary bits, if we find them. Config.Noavatar = strings.Replace(Config.Noavatar, "{site_url}", Site.URL, -1)
Site.URL = strings.TrimPrefix(Site.URL, "http://")
Site.URL = strings.TrimPrefix(Site.URL, "https://")
Site.Host = Site.URL Site.Host = Site.URL
Site.LocalHost = Site.Host == "localhost" || Site.Host == "127.0.0.1" || Site.Host == "::1" if Site.Port != "80" && Site.Port != "443" {
Site.PortInt, err = strconv.Atoi(Site.Port)
if err != nil {
return errors.New("The port must be a valid integer")
}
if Site.PortInt != 80 && Site.PortInt != 443 {
Site.URL = strings.TrimSuffix(Site.URL, "/") Site.URL = strings.TrimSuffix(Site.URL, "/")
Site.URL = strings.TrimSuffix(Site.URL, "\\") Site.URL = strings.TrimSuffix(Site.URL, "\\")
Site.URL = strings.TrimSuffix(Site.URL, ":") Site.URL = strings.TrimSuffix(Site.URL, ":")
Site.URL = Site.URL + ":" + Site.Port Site.URL = Site.URL + ":" + Site.Port
} }
uurl, err := url.Parse(Site.URL)
if err != nil {
return errors.Wrap(err, "failed to parse Site.URL: ")
}
if Site.EnableSsl {
Config.SslSchema = Site.EnableSsl
}
if Config.DefaultPath == "" { if Config.DefaultPath == "" {
Config.DefaultPath = "/topics/" Config.DefaultPath = "/topics/"
} }
@ -258,82 +166,6 @@ func ProcessConfig() (err error) {
} }
Site.MaxRequestSize = Config.MaxRequestSize Site.MaxRequestSize = Config.MaxRequestSize
local := func(h string) bool {
return h == "localhost" || h == "127.0.0.1" || h == "::1" || h == Site.URL
}
uurl, err = url.Parse(Config.StaticResBase)
if err != nil {
return errors.Wrap(err, "failed to parse Config.StaticResBase: ")
}
host := uurl.Hostname()
if !local(host) {
Config.ExtraCSPOrigins += " " + host
Config.RefNoRef = true // Avoid leaking origin data to the CDN
}
if Config.StaticResBase != "" {
StaticFiles.Prefix = Config.StaticResBase
}
uurl, err = url.Parse(Config.AvatarResBase)
if err != nil {
return errors.Wrap(err, "failed to parse Config.AvatarResBase: ")
}
host2 := uurl.Hostname()
if host != host2 && !local(host) {
Config.ExtraCSPOrigins += " " + host
Config.RefNoRef = true // Avoid leaking origin data to the CDN
}
if Config.AvatarResBase == "" {
Config.AvatarResBase = "/uploads/"
}
if !Config.DisableDefaultNoavatar {
cap := 11
noavatarCache200 = make([]string, cap)
noavatarCache48 = make([]string, cap)
/*noavatarCache200Jpg = make([]string, cap)
noavatarCache48Jpg = make([]string, cap)
noavatarCache200Avif = make([]string, cap)
noavatarCache48Avif = make([]string, cap)*/
for i := 0; i < cap; i++ {
noavatarCache200[i] = StaticFiles.Prefix + "n" + strconv.Itoa(i) + "-" + strconv.Itoa(200) + ".png?i=0"
noavatarCache48[i] = StaticFiles.Prefix + "n" + strconv.Itoa(i) + "-" + strconv.Itoa(48) + ".png?i=0"
/*noavatarCache200Jpg[i] = StaticFiles.Prefix + "n" + strconv.Itoa(i) + "-" + strconv.Itoa(200) + ".jpg?i=0"
noavatarCache48Jpg[i] = StaticFiles.Prefix + "n" + strconv.Itoa(i) + "-" + strconv.Itoa(48) + ".jpg?i=0"
noavatarCache200Avif[i] = StaticFiles.Prefix + "n" + strconv.Itoa(i) + "-" + strconv.Itoa(200) + ".avif?i=0"
noavatarCache48Avif[i] = StaticFiles.Prefix + "n" + strconv.Itoa(i) + "-" + strconv.Itoa(48) + ".avif?i=0"*/
}
}
Config.Noavatar = strings.Replace(Config.Noavatar, "{site_url}", Site.URL, -1)
guestAvatar = GuestAvatar{buildNoavatar(0, 200), buildNoavatar(0, 48)}
if Config.DisableIP {
Config.DisableLastIP = true
Config.DisablePostIP = true
Config.DisablePollIP = true
}
if Config.PostIPCutoff == 0 {
Config.PostIPCutoff = 90 // Default cutoff
}
if Config.LogPruneCutoff == 0 {
Config.LogPruneCutoff = 180 // Default cutoff
}
if Config.LastIPCutoff == 0 {
Config.LastIPCutoff = 3 // Default cutoff
}
if Config.LastIPCutoff > 12 {
Config.LastIPCutoff = 12
}
if Config.PollIPCutoff == 0 {
Config.PollIPCutoff = 90 // Default cutoff
}
if Config.NoEmbed {
DefaultParseSettings.NoEmbed = true
}
// ? Find a way of making these unlimited if zero? It might rule out some optimisations, waste memory, and break layouts // ? Find a way of making these unlimited if zero? It might rule out some optimisations, waste memory, and break layouts
if Config.MaxTopicTitleLength == 0 { if Config.MaxTopicTitleLength == 0 {
Config.MaxTopicTitleLength = 100 Config.MaxTopicTitleLength = 100
@ -347,10 +179,6 @@ func ProcessConfig() (err error) {
// TODO: Set the alternate hash algo, e.g. argon2 // TODO: Set the alternate hash algo, e.g. argon2
} }
if Config.LogDir == "" {
Config.LogDir = "./logs/"
}
// We need this in here rather than verifyConfig as switchToTestDB() currently overwrites the values it verifies // We need this in here rather than verifyConfig as switchToTestDB() currently overwrites the values it verifies
if DbConfig.TestDbname == DbConfig.Dbname { if DbConfig.TestDbname == DbConfig.Dbname {
return errors.New("Your test database can't have the same name as your production database") return errors.New("Your test database can't have the same name as your production database")
@ -361,18 +189,20 @@ func ProcessConfig() (err error) {
return nil return nil
} }
func VerifyConfig() (err error) { func VerifyConfig() error {
switch { if !Forums.Exists(Config.DefaultForum) {
case !Forums.Exists(Config.DefaultForum): return errors.New("Invalid default forum")
err = errors.New("Invalid default forum")
case Config.ServerCount < 1:
err = errors.New("You can't have less than one server")
case Config.MaxTopicTitleLength > 100:
err = errors.New("The max topic title length cannot be over 100 as that's unable to fit in the database row")
case Config.MaxUsernameLength > 100:
err = errors.New("The max username length cannot be over 100 as that's unable to fit in the database row")
} }
return err if Config.ServerCount < 1 {
return errors.New("You can't have less than one server")
}
if Config.MaxTopicTitleLength > 100 {
return errors.New("The max topic title length cannot be over 100 as that's unable to fit in the database row")
}
if Config.MaxUsernameLength > 100 {
return errors.New("The max username length cannot be over 100 as that's unable to fit in the database row")
}
return nil
} }
func SwitchToTestDB() { func SwitchToTestDB() {

View File

@ -18,15 +18,15 @@ func NewDefaultStatStore() *DefaultStatStore {
return &DefaultStatStore{} return &DefaultStatStore{}
} }
func (s *DefaultStatStore) LookupInt(name string, duration int, unit string) (int, error) { func (store *DefaultStatStore) LookupInt(name string, duration int, unit string) (int, error) {
switch name { switch name {
case "postCount": case "postCount":
return s.countTable("replies", duration, unit) return store.countTable("replies", duration, unit)
} }
return 0, errors.New("The requested stat doesn't exist") return 0, errors.New("The requested stat doesn't exist")
} }
func (s *DefaultStatStore) countTable(table string, duration int, unit string) (stat int, err error) { func (store *DefaultStatStore) countTable(table string, duration int, unit string) (stat int, err error) {
/*counter := qgen.NewAcc().Count("replies").DateCutoff("createdAt", 1, "day").Prepare() /*counter := qgen.NewAcc().Count("replies").DateCutoff("createdAt", 1, "day").Prepare()
if acc.FirstError() != nil { if acc.FirstError() != nil {
return 0, acc.FirstError() return 0, acc.FirstError()

Some files were not shown because too many files have changed in this diff Show More