2019-01-21 12:27:59 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-05-13 05:13:27 +00:00
|
|
|
//"log"
|
2019-01-21 12:27:59 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
|
|
|
|
"github.com/Azareal/Gosora/common/phrases"
|
2019-03-11 08:47:45 +00:00
|
|
|
min "github.com/Azareal/Gosora/common/templates"
|
2019-01-21 12:27:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type wolUsers struct {
|
|
|
|
*Header
|
|
|
|
Name string
|
|
|
|
Users []*User
|
|
|
|
UserCount int
|
|
|
|
}
|
|
|
|
|
|
|
|
func wolInit(widget *Widget, schedule *WidgetScheduler) error {
|
|
|
|
schedule.Add(widget)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-13 05:13:27 +00:00
|
|
|
func wolGetUsers() ([]*User, int) {
|
2019-01-21 12:27:59 +00:00
|
|
|
ucount := WsHub.UserCount()
|
|
|
|
// We don't want a ridiculously long list, so we'll show the number if it's too high and only show staff individually
|
|
|
|
var users []*User
|
|
|
|
if ucount < 30 {
|
|
|
|
users = WsHub.AllUsers()
|
2019-01-22 12:06:55 +00:00
|
|
|
if len(users) >= 30 {
|
|
|
|
users = nil
|
|
|
|
}
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2019-04-20 12:49:48 +00:00
|
|
|
return users, ucount
|
|
|
|
}
|
|
|
|
|
|
|
|
func wolBuild(widget *Widget, hvars interface{}) (string, error) {
|
|
|
|
users, ucount := wolGetUsers()
|
2019-01-21 12:27:59 +00:00
|
|
|
wol := &wolUsers{hvars.(*Header), phrases.GetTmplPhrase("widget.online_name"), users, ucount}
|
|
|
|
err := wol.Header.Theme.RunTmpl("widget_online", wol, wol.Header.Writer)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
func wolRender(widget *Widget, hvars interface{}) (string, error) {
|
|
|
|
iTickMask := widget.TickMask.Load()
|
|
|
|
if iTickMask != nil {
|
|
|
|
tickMask := iTickMask.(*Widget)
|
|
|
|
if tickMask != nil {
|
|
|
|
return tickMask.Body, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wolBuild(widget, hvars)
|
|
|
|
}
|
|
|
|
|
2019-04-20 12:49:48 +00:00
|
|
|
var wolLastUsers []*User
|
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
func wolTick(widget *Widget) error {
|
|
|
|
w := httptest.NewRecorder()
|
2019-04-20 12:49:48 +00:00
|
|
|
users, ucount := wolGetUsers()
|
2019-07-26 23:29:42 +00:00
|
|
|
inOld := func(id int) bool {
|
2019-05-13 05:13:27 +00:00
|
|
|
for _, user := range wolLastUsers {
|
|
|
|
if id == user.ID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-04-20 12:49:48 +00:00
|
|
|
|
|
|
|
// Avoid rebuilding the widget, if the users are exactly the same as on the last tick
|
|
|
|
if len(users) == len(wolLastUsers) {
|
|
|
|
diff := false
|
2019-05-13 05:13:27 +00:00
|
|
|
for _, user := range users {
|
|
|
|
if !inOld(user.ID) {
|
2019-04-20 12:49:48 +00:00
|
|
|
diff = true
|
2019-05-13 05:13:27 +00:00
|
|
|
break
|
2019-04-20 12:49:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !diff {
|
|
|
|
iTickMask := widget.TickMask.Load()
|
|
|
|
if iTickMask != nil {
|
|
|
|
tickMask := iTickMask.(*Widget)
|
|
|
|
if tickMask != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-13 05:13:27 +00:00
|
|
|
//log.Printf("users: %+v\n", users)
|
|
|
|
//log.Printf("wolLastUsers: %+v\n", wolLastUsers)
|
2019-04-20 12:49:48 +00:00
|
|
|
|
|
|
|
wol := &wolUsers{SimpleDefaultHeader(w), phrases.GetTmplPhrase("widget.online_name"), users, ucount}
|
|
|
|
err := wol.Header.Theme.RunTmpl("widget_online", wol, wol.Header.Writer)
|
2019-01-21 12:27:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-20 12:49:48 +00:00
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(w.Result().Body)
|
2019-03-11 08:47:45 +00:00
|
|
|
bs := buf.String()
|
|
|
|
if Config.MinifyTemplates {
|
|
|
|
bs = min.Minify(bs)
|
|
|
|
}
|
2019-04-20 12:49:48 +00:00
|
|
|
|
|
|
|
twidget := &Widget{}
|
|
|
|
*twidget = *widget
|
|
|
|
twidget.Body = bs
|
|
|
|
widget.TickMask.Store(twidget)
|
|
|
|
wolLastUsers = users
|
|
|
|
|
|
|
|
hTbl := GetHookTable()
|
|
|
|
_, _ = hTbl.VhookSkippable("tasks_tick_widget_wol", widget, bs)
|
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
return nil
|
|
|
|
}
|