shithub: hell

ref: cfc62c33b91c49cced442b070507a701de394fd0
dir: /notifications.go/

View raw version
package main

import (
	"context"
	"fmt"
	"strings"

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

func (hc *Hellclient) GetUnreadNotifications() (notifications []*mastodon.Notification, err error) {
	markers, err := hc.client.GetTimelineMarkers(context.Background(), []string{"notifications"})
	if err != nil {
		return
	}

	LastID := markers["notifications"].LastID
	page := &mastodon.Pagination{SinceID: LastID}

	noticeFunc := func(job *GenericJob) {
		notifications, err = hc.client.GetNotificationsExclude(context.Background(), nil, page)
	}
	noticeJob := hc.dispatchFunc(noticeFunc)
	noticeJob.Wait()
	if err != nil {
		return
	}

	notifications = reverseArray(notifications)

	return
}

func (hc *Hellclient) SetNotificationsRead(ID mastodon.ID) (err error) {

	markers, err := hc.client.GetTimelineMarkers(context.Background(), []string{"notifications"})
	if err != nil {
		return
	}

	marker := markers["notifications"]
	marker.ID = ID
	marker.Timeline = "notifications"

	err = hc.client.SetTimelineMarkers(context.Background(), &[]mastodon.Marker{*marker})
	return
}

func (hc *Hellclient) PrintReceivedNotification(notification *mastodon.Notification) {
	var notifications []*mastodon.Notification
	notifications = append(notifications, notification)

	status := func(Notification *mastodon.Notification, plaintext string) {
		notification := fmt.Sprintf("Notification [%s] from <%s>: %s", Notification.Type, Notification.Account.Acct, plaintext)
		fmt.Println(hyphenate(notification))
	}

	other := func(Notification *mastodon.Notification) {
		notification := fmt.Sprintf("Notification [%s] from <%s>", Notification.Type, Notification.Account.Acct)
		fmt.Println(hyphenate(notification))
	}

	hc.PrintNotificationsCustom(notifications, status, other)

}

func (hc *Hellclient) PrintNotifications(notifications []*mastodon.Notification) {
	fmt.Println(hc.RenderNotifications(notifications))
}

func (hc *Hellclient) RenderNotificationsArray(notifications []*mastodon.Notification) []string {
	var noticeTexts []string
	status := func(Notification *mastodon.Notification, plaintext string) {
		notification := fmt.Sprintf("[%s] from <%s>: %s\n", Notification.Type, Notification.Account.Acct, plaintext)
		noticeTexts = append(noticeTexts, hyphenate(notification))
	}

	other := func(Notification *mastodon.Notification) {
		notification := fmt.Sprintf("[%s] from <%s>\n", Notification.Type, Notification.Account.Acct)
		noticeTexts = append(noticeTexts, hyphenate(notification))
	}

	hc.PrintNotificationsCustom(notifications, status, other)

	return noticeTexts
}

func (hc *Hellclient) RenderNotifications(notifications []*mastodon.Notification) string {
	var sb strings.Builder
	noticeTexts := hc.RenderNotificationsArray(notifications)
	for _, text := range noticeTexts {
		sb.WriteString(text)
	}
	return sb.String()
}

func (hc *Hellclient) PrintNotificationsCustom(notifications []*mastodon.Notification,
	printstatus func(*mastodon.Notification, string),
	printother func(*mastodon.Notification)) {
	for _, Notification := range notifications {
		if Notification.Status == nil {
			printother(Notification)
			continue
		}

		_, plaintext := hc.RenderPostPlaintext(Notification.Status, hc.ctxref.ref, "")
		printstatus(Notification, plaintext)

		saveRef(hc.ctxref.postmap, Notification.Status, hc.ctxref.ref)
		hc.ctxref.ref = IncrementString(hc.ctxref.ref)
		continue
	}
}