shithub: hell

ref: 9d38f0a5cb1e494c072de5e4e1d33954f143faa2
dir: /dispatch.go/

View raw version
package main

import (
	"fmt"
	"time"

	"github.com/mattn/go-mastodon"
)

func (hc *Hellclient) queueManager() {

}

func (hc *Hellclient) clientDispatch() {
	var tootQueue []*mastodon.Toot
	//Last time we sent an API call
	var lastfire time.Time
	//Last time the user sent us a line
	var lastreceive time.Time
	var comingfast bool
	for {
		select {
		case statustoot := <-hc.dispatch:
			//Got multiple lines within a second
			if time.Since(lastreceive) < time.Second {
				comingfast = true
			}
			lastreceive = time.Now()
			tootQueue = append(tootQueue, statustoot)
		//API delay needs to be tracked without being reset by new inputs
		case <-time.After(hc.preferences.apidelay - time.Since(lastfire)):
			if 1 > len(tootQueue) {
				statustoot := <-hc.dispatch
				if time.Since(lastreceive) < time.Second {
					comingfast = true
				}
				lastreceive = time.Now()
				tootQueue = append(tootQueue, statustoot)
				break
			}
			//User is sending lines faster than one a second, flood control
			if comingfast {
				if time.Since(lastreceive) < time.Second {
					//looks like they're still coming
					break
				}
				fmt.Printf("Got %v lines fast!\n", len(tootQueue))
				tootQueue = nil
				comingfast = false
				break
			}

			toot := tootQueue[0]
			lastfire = time.Now()
			var err error
			status, err := postStatusDetailed(*hc.client, *toot)
			if err != nil {
				fmt.Println(err)
			}
			hc.lock()
			hc.recentpost = status
			hc.unlock()
			tootQueue = tootQueue[1:]
		}
	}
}

func (hc *Hellclient) dispatchStatus(status string, visibility string) {
	hc.dispatch <- postStatus(status, visibility)
}

func (hc *Hellclient) dispatchReply(posttext string, replyto mastodon.ID, postItem *mastodon.Status) {
	hc.dispatch <- postReply(posttext, replyto, hc.currentuser.ID, postItem)
}