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

     1  package common
     2  
     3  import (
     4  	"bytes"
     5  	//"log"
     6  	"net/http/httptest"
     7  
     8  	p "github.com/Azareal/Gosora/common/phrases"
     9  	min "github.com/Azareal/Gosora/common/templates"
    10  )
    11  
    12  type wolUsers struct {
    13  	*Header
    14  	Name      string
    15  	Users     []*User
    16  	UserCount int
    17  }
    18  
    19  func wolInit(w *Widget, sched *WidgetScheduler) error {
    20  	sched.Add(w)
    21  	return nil
    22  }
    23  
    24  func wolGetUsers() ([]*User, int) {
    25  	ucount := WsHub.UserCount()
    26  	// We don't want a ridiculously long list, so we'll show the number if it's too high and only show staff individually
    27  	var users []*User
    28  	if ucount < 30 {
    29  		users = WsHub.AllUsers()
    30  		if len(users) >= 30 {
    31  			users = nil
    32  		}
    33  	}
    34  	return users, ucount
    35  }
    36  
    37  func wolBuild(w *Widget, hvars interface{}) (string, error) {
    38  	users, ucount := wolGetUsers()
    39  	wol := &wolUsers{hvars.(*Header), p.GetTmplPhrase("widget.online_name"), users, ucount}
    40  	err := wol.Header.Theme.RunTmpl("widget_online", wol, wol.Header.Writer)
    41  	return "", err
    42  }
    43  
    44  func wolRender(w *Widget, hvars interface{}) (string, error) {
    45  	iTickMask := w.TickMask.Load()
    46  	if iTickMask != nil {
    47  		tickMask := iTickMask.(*Widget)
    48  		if tickMask != nil {
    49  			return tickMask.Body, nil
    50  		}
    51  	}
    52  	return wolBuild(w, hvars)
    53  }
    54  
    55  var wolLastUsers []*User
    56  
    57  func wolTick(widget *Widget) error {
    58  	w := httptest.NewRecorder()
    59  	users, ucount := wolGetUsers()
    60  	inOld := func(id int) bool {
    61  		for _, user := range wolLastUsers {
    62  			if id == user.ID {
    63  				return true
    64  			}
    65  		}
    66  		return false
    67  	}
    68  
    69  	// Avoid rebuilding the widget, if the users are exactly the same as on the last tick
    70  	if len(users) == len(wolLastUsers) {
    71  		diff := false
    72  		for _, user := range users {
    73  			if !inOld(user.ID) {
    74  				diff = true
    75  				break
    76  			}
    77  		}
    78  		if !diff {
    79  			iTickMask := widget.TickMask.Load()
    80  			if iTickMask != nil {
    81  				tickMask := iTickMask.(*Widget)
    82  				if tickMask != nil {
    83  					return nil
    84  				}
    85  			}
    86  		}
    87  	}
    88  	//log.Printf("users: %+v\n", users)
    89  	//log.Printf("wolLastUsers: %+v\n", wolLastUsers)
    90  
    91  	wol := &wolUsers{SimpleDefaultHeader(w), p.GetTmplPhrase("widget.online_name"), users, ucount}
    92  	err := wol.Header.Theme.RunTmpl("widget_online", wol, wol.Header.Writer)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	buf := new(bytes.Buffer)
    98  	buf.ReadFrom(w.Result().Body)
    99  	bs := buf.String()
   100  	if Config.MinifyTemplates {
   101  		bs = min.Minify(bs)
   102  	}
   103  
   104  	twidget := &Widget{}
   105  	*twidget = *widget
   106  	twidget.Body = bs
   107  	widget.TickMask.Store(twidget)
   108  	wolLastUsers = users
   109  
   110  	hTbl := GetHookTable()
   111  	_, _ = hTbl.VhookSkippable("tasks_tick_widget_wol", widget, bs)
   112  
   113  	return nil
   114  }