ref: 046bf407f6bc4ea1d4b35ebbcd449fd493d292c4
dir: /notifications.go/
package main
import (
"context"
"fmt"
"github.com/mattn/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}
notificationbuffer, err := hc.client.GetNotificationsExclude(context.Background(), nil, page)
//Reverse to print from oldest to newest
for i := len(notificationbuffer) - 1; i >= 0; i-- {
notifications = append(notifications, notificationbuffer[i])
}
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 [%v] from <%v>: %v\n\n", Notification.Type, Notification.Account.Acct, plaintext)
fmt.Printf(hyphenate(notification))
}
other := func(Notification *mastodon.Notification) {
notification := fmt.Sprintf("Notification [%v] from <%v>\n\n", Notification.Type, Notification.Account.Acct)
fmt.Printf(hyphenate(notification))
}
hc.PrintNotificationsCustom(notifications, status, other)
}
func (hc *Hellclient) PrintNotifications(notifications []*mastodon.Notification) {
status := func(Notification *mastodon.Notification, plaintext string) {
notification := fmt.Sprintf("[%v] from <%v>: %v\n\n", Notification.Type, Notification.Account.Acct, plaintext)
fmt.Printf(hyphenate(notification))
}
other := func(Notification *mastodon.Notification) {
notification := fmt.Sprintf("[%v] from <%v>\n\n", Notification.Type, Notification.Account.Acct)
fmt.Printf(hyphenate(notification))
}
hc.PrintNotificationsCustom(notifications, status, other)
}
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
}
}