shithub: hell

ref: 8ee08e84e56e321733b64ba7829ebf8c7ab10ee3
dir: /status.go/

View raw version
package main

import (
	"context"
	"fmt"

	mastodon "codeberg.org/penny64/hellclient-go-mastodon"
)

const maxUnreadStatusesToLoad = 30

func (hc *Hellclient) GetStatusesSince(ID mastodon.ID, GetTimeline func(ctx context.Context, pg *mastodon.Pagination) ([]*mastodon.Status, error)) ([]*mastodon.Status, error) {

	page := &mastodon.Pagination{MinID: ID, Limit: 40}

	var err error
	var statuses []*mastodon.Status

	for {
		var statusbatch []*mastodon.Status

		statusfunc := func(job *GenericJob) { statusbatch, err = GetTimeline(context.Background(), page) }
		statusjob := hc.dispatchFunc(statusfunc)
		statusjob.Wait()
		if err != nil {
			return statuses, err
		}

		if len(statusbatch) == 0 {
			break
		}

		for i := len(statusbatch) - 1; i >= 0; i-- {
			statuses = append(statuses, statusbatch[i])
		}
		//I really don't understand the server's results but erase the max ID and it paginates up I don't know man
		page.MaxID = ""
		fmt.Printf("Loaded %v statuses....", len(statuses))
		if len(statuses) >= maxUnreadStatusesToLoad {
			break
		}
		//We ask for 40 results, if there were less than 40, there's no more to load
		if len(statusbatch) < 40 {
			break
		}
	}

	return statuses, err

}