go-forge/notifications.go
Snider 57d8af13ad
All checks were successful
Security Scan / security (push) Successful in 14s
Test / test (push) Successful in 2m1s
feat: modernise to Go 1.26 iterators and stdlib helpers
Add ListIter in pagination + generic Resource.Iter for streaming
paginated results as iter.Seq2[T, error]. Add Iter* methods across
all service files (actions, admin, branches, issues, labels, notifs,
orgs, packages, pulls, releases, repos, teams, users, webhooks).
Modernise cmd/forgegen with slices.Sort, maps.Keys, strings.FieldsFuncSeq.

Co-Authored-By: Gemini <noreply@google.com>
Co-Authored-By: Virgil <virgil@lethean.io>
2026-02-23 05:39:13 +00:00

62 lines
2.3 KiB
Go

package forge
import (
"context"
"fmt"
"iter"
"forge.lthn.ai/core/go-forge/types"
)
// NotificationService handles notification operations via the Forgejo API.
// No Resource embedding — varied endpoint shapes.
type NotificationService struct {
client *Client
}
func newNotificationService(c *Client) *NotificationService {
return &NotificationService{client: c}
}
// List returns all notifications for the authenticated user.
func (s *NotificationService) List(ctx context.Context) ([]types.NotificationThread, error) {
return ListAll[types.NotificationThread](ctx, s.client, "/api/v1/notifications", nil)
}
// Iter returns an iterator over all notifications for the authenticated user.
func (s *NotificationService) Iter(ctx context.Context) iter.Seq2[types.NotificationThread, error] {
return ListIter[types.NotificationThread](ctx, s.client, "/api/v1/notifications", nil)
}
// ListRepo returns all notifications for a specific repository.
func (s *NotificationService) ListRepo(ctx context.Context, owner, repo string) ([]types.NotificationThread, error) {
path := fmt.Sprintf("/api/v1/repos/%s/%s/notifications", owner, repo)
return ListAll[types.NotificationThread](ctx, s.client, path, nil)
}
// IterRepo returns an iterator over all notifications for a specific repository.
func (s *NotificationService) IterRepo(ctx context.Context, owner, repo string) iter.Seq2[types.NotificationThread, error] {
path := fmt.Sprintf("/api/v1/repos/%s/%s/notifications", owner, repo)
return ListIter[types.NotificationThread](ctx, s.client, path, nil)
}
// MarkRead marks all notifications as read.
func (s *NotificationService) MarkRead(ctx context.Context) error {
return s.client.Put(ctx, "/api/v1/notifications", nil, nil)
}
// GetThread returns a single notification thread by ID.
func (s *NotificationService) GetThread(ctx context.Context, id int64) (*types.NotificationThread, error) {
path := fmt.Sprintf("/api/v1/notifications/threads/%d", id)
var out types.NotificationThread
if err := s.client.Get(ctx, path, &out); err != nil {
return nil, err
}
return &out, nil
}
// MarkThreadRead marks a single notification thread as read.
func (s *NotificationService) MarkThreadRead(ctx context.Context, id int64) error {
path := fmt.Sprintf("/api/v1/notifications/threads/%d", id)
return s.client.Patch(ctx, path, nil, nil)
}