66 "fmt"
77 "net/http"
88 "net/url"
9+ "regexp"
910 "sort"
1011 "strings"
1112 "time"
@@ -217,6 +218,17 @@ func (s *StatusGetter) ActualMention(n Notification) (string, error) {
217218// These are split up by endpoint since it is along that boundary we parallelize
218219// work
219220
221+ var linkRE = regexp .MustCompile (`<([^>]+)>;\s*rel="([^"]+)"` )
222+
223+ func findNextPage (resp * http.Response ) (string , bool ) {
224+ for _ , m := range linkRE .FindAllStringSubmatch (resp .Header .Get ("Link" ), - 1 ) {
225+ if len (m ) > 2 && m [2 ] == "next" {
226+ return m [1 ], true
227+ }
228+ }
229+ return "" , false
230+ }
231+
220232// Populate .Mentions
221233func (s * StatusGetter ) LoadNotifications () error {
222234 perPage := 100
@@ -233,21 +245,24 @@ func (s *StatusGetter) LoadNotifications() error {
233245 // not work with PATs right now.
234246 var ns []Notification
235247 var resp []Notification
236- pages := 3
237- for page := 1 ; page <= pages ; page ++ {
238- query .Add ("page" , fmt .Sprintf ("%d" , page ))
239- p := fmt .Sprintf ("notifications?%s" , query .Encode ())
240- err := c .REST (ghinstance .Default (), "GET" , p , nil , & resp )
248+ pages := 0
249+ p := fmt .Sprintf ("notifications?%s" , query .Encode ())
250+ for pages < 3 {
251+ next , err := c .RESTWithNext (ghinstance .Default (), "GET" , p , nil , & resp )
241252 if err != nil {
242253 var httpErr api.HTTPError
243254 if ! errors .As (err , & httpErr ) || httpErr .StatusCode != 404 {
244255 return fmt .Errorf ("could not get notifications: %w" , err )
245256 }
246257 }
247258 ns = append (ns , resp ... )
248- if len (resp ) == 0 || len (resp ) < perPage {
259+
260+ if next == "" || len (resp ) < perPage {
249261 break
250262 }
263+
264+ pages ++
265+ p = next
251266 }
252267
253268 s .Mentions = []StatusItem {}
@@ -430,21 +445,23 @@ func (s *StatusGetter) LoadEvents() error {
430445
431446 var events []Event
432447 var resp []Event
433- pages := 2
434- for page := 1 ; page <= pages ; page ++ {
435- query .Add ("page" , fmt .Sprintf ("%d" , page ))
436- p := fmt .Sprintf ("users/%s/received_events?%s" , currentUsername , query .Encode ())
437- err := c .REST (ghinstance .Default (), "GET" , p , nil , & resp )
448+ pages := 0
449+ p := fmt .Sprintf ("users/%s/received_events?%s" , currentUsername , query .Encode ())
450+ for pages < 2 {
451+ next , err := c .RESTWithNext (ghinstance .Default (), "GET" , p , nil , & resp )
438452 if err != nil {
439453 var httpErr api.HTTPError
440454 if ! errors .As (err , & httpErr ) || httpErr .StatusCode != 404 {
441455 return fmt .Errorf ("could not get events: %w" , err )
442456 }
443457 }
444458 events = append (events , resp ... )
445- if len ( resp ) == 0 || len (resp ) < perPage {
459+ if next == "" || len (resp ) < perPage {
446460 break
447461 }
462+
463+ pages ++
464+ p = next
448465 }
449466
450467 s .RepoActivity = []StatusItem {}
0 commit comments