shithub: hell

ref: 4fb050ab9bda0c5617cce8f10e380a00307a5630
dir: /notifications.go/

View raw version
package main

import (
	"context"
	"fmt"
	"strings"

	mastodon "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)
	plaintexts := hc.RenderNotificationsArray(notifications)
	fmt.Print(plaintexts[0])
}

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

func (hc *Hellclient) RenderNotificationsArray(notifications []*mastodon.Notification) []string {
	var noticeTexts []string
	formatter := &StatusFormatter{prefs: hc.preferences, postContext: hc.ctxref}
	templater := newStatusTemplateRenderer(formatter)
	for item := range notifications {
		formatter.notif = notifications[item]
		if formatter.notif.Status != nil {
			formatter.status = formatter.notif.Status
			line, _ := templater.render("$status_notif")
			noticeTexts = append(noticeTexts, line)
			justIncrementPostref(hc.ctxref, formatter.status)
			continue
		}
		line, _ := templater.render("$other_notif")
		noticeTexts = append(noticeTexts, line)
	}
	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()
}