diff --git a/appview/db/issues.go b/appview/db/issues.go index 74231b7a..fe5b441b 100644 --- a/appview/db/issues.go +++ b/appview/db/issues.go @@ -5,6 +5,7 @@ import ( "time" "github.com/bluesky-social/indigo/atproto/syntax" + "tangled.sh/tangled.sh/core/appview/pagination" ) type Issue struct { @@ -102,7 +103,7 @@ func GetIssueOwnerDid(e Execer, repoAt syntax.ATURI, issueId int) (string, error return ownerDid, err } -func GetIssues(e Execer, repoAt syntax.ATURI, isOpen bool) ([]Issue, error) { +func GetIssues(e Execer, repoAt syntax.ATURI, isOpen bool, page pagination.Page) ([]Issue, error) { var issues []Issue openValue := 0 if isOpen { @@ -110,25 +111,39 @@ func GetIssues(e Execer, repoAt syntax.ATURI, isOpen bool) ([]Issue, error) { } rows, err := e.Query( - `select - i.owner_did, - i.issue_id, - i.created, - i.title, - i.body, - i.open, - count(c.id) - from - issues i - left join - comments c on i.repo_at = c.repo_at and i.issue_id = c.issue_id - where - i.repo_at = ? and i.open = ? - group by - i.id, i.owner_did, i.issue_id, i.created, i.title, i.body, i.open - order by - i.created desc`, - repoAt, openValue) + ` + with numbered_issue as ( + select + i.owner_did, + i.issue_id, + i.created, + i.title, + i.body, + i.open, + count(c.id) as comment_count, + row_number() over (order by i.created desc) as row_num + from + issues i + left join + comments c on i.repo_at = c.repo_at and i.issue_id = c.issue_id + where + i.repo_at = ? and i.open = ? + group by + i.id, i.owner_did, i.issue_id, i.created, i.title, i.body, i.open + ) + select + owner_did, + issue_id, + created, + title, + body, + open, + comment_count + from + numbered_issue + where + row_num between ? and ?`, + repoAt, openValue, page.Offset+1, page.Offset+page.Limit) if err != nil { return nil, err } diff --git a/appview/middleware/middleware.go b/appview/middleware/middleware.go index 077bee45..51aa75fd 100644 --- a/appview/middleware/middleware.go +++ b/appview/middleware/middleware.go @@ -1,14 +1,17 @@ package middleware import ( + "context" "log" "net/http" + "strconv" "time" comatproto "github.com/bluesky-social/indigo/api/atproto" "github.com/bluesky-social/indigo/xrpc" "tangled.sh/tangled.sh/core/appview" "tangled.sh/tangled.sh/core/appview/auth" + "tangled.sh/tangled.sh/core/appview/pagination" ) type Middleware func(http.Handler) http.Handler @@ -92,3 +95,32 @@ func AuthMiddleware(a *auth.Auth) Middleware { }) } } + +func Paginate(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + page := pagination.FirstPage() + + offsetVal := r.URL.Query().Get("offset") + if offsetVal != "" { + offset, err := strconv.Atoi(offsetVal) + if err != nil { + log.Println("invalid offset") + } else { + page.Offset = offset + } + } + + limitVal := r.URL.Query().Get("limit") + if limitVal != "" { + limit, err := strconv.Atoi(limitVal) + if err != nil { + log.Println("invalid limit") + } else { + page.Limit = limit + } + } + + ctx := context.WithValue(r.Context(), "page", page) + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} diff --git a/appview/pages/pages.go b/appview/pages/pages.go index 190d8cd0..12f3daa8 100644 --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -20,6 +20,7 @@ import ( "tangled.sh/tangled.sh/core/appview/auth" "tangled.sh/tangled.sh/core/appview/db" "tangled.sh/tangled.sh/core/appview/pages/markup" + "tangled.sh/tangled.sh/core/appview/pagination" "tangled.sh/tangled.sh/core/appview/state/userutil" "tangled.sh/tangled.sh/core/patchutil" "tangled.sh/tangled.sh/core/types" @@ -651,12 +652,12 @@ func (p *Pages) RepoSettings(w io.Writer, params RepoSettingsParams) error { } type RepoIssuesParams struct { - LoggedInUser *auth.User - RepoInfo RepoInfo - Active string - Issues []db.Issue - DidHandleMap map[string]string - + LoggedInUser *auth.User + RepoInfo RepoInfo + Active string + Issues []db.Issue + DidHandleMap map[string]string + Page pagination.Page FilteringByOpen bool } diff --git a/appview/pages/templates/repo/issues/issues.html b/appview/pages/templates/repo/issues/issues.html index baf59343..b65d66cf 100644 --- a/appview/pages/templates/repo/issues/issues.html +++ b/appview/pages/templates/repo/issues/issues.html @@ -70,4 +70,42 @@ {{ end }} + +{{ block "pagination" . }} {{ end }} + +{{ end }} + +{{ define "pagination" }} +