shithub: hell

ref: b0fd01dbd673c7637f22db70ca9039f820333a6b
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
	var lastfire time.Time
	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)
		default:
			if 1 > len(tootQueue) {
				statustoot := <-hc.dispatch
				if time.Since(lastreceive) < time.Second {
					comingfast = true
				}
				lastreceive = time.Now()
				tootQueue = append(tootQueue, statustoot)
				break
			}
			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
			}
			if time.Since(lastfire) < hc.preferences.apidelay {
				time.Sleep(hc.preferences.apidelay - time.Since(lastfire))
				break
			}

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

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)
}