Monorepo for Tangled

appview: remove `Get{Issue,Pull}IDs` methods

prepare for PR page pagination

Signed-off-by: Seongmin Lee <git@boltless.me>

authored by

Seongmin Lee and committed by tangled.org 50b88f01 fbe41aeb

+41 -144
-56
appview/db/issues.go
··· 295 295 return GetIssuesPaginated(e, pagination.Page{}, filters...) 296 296 } 297 297 298 - // GetIssueIDs gets list of all existing issue's IDs 299 - func GetIssueIDs(e Execer, opts models.IssueSearchOptions) ([]int64, error) { 300 - var ids []int64 301 - 302 - var filters []orm.Filter 303 - openValue := 0 304 - if opts.IsOpen { 305 - openValue = 1 306 - } 307 - filters = append(filters, orm.FilterEq("open", openValue)) 308 - if opts.RepoAt != "" { 309 - filters = append(filters, orm.FilterEq("repo_at", opts.RepoAt)) 310 - } 311 - 312 - var conditions []string 313 - var args []any 314 - 315 - for _, filter := range filters { 316 - conditions = append(conditions, filter.Condition()) 317 - args = append(args, filter.Arg()...) 318 - } 319 - 320 - whereClause := "" 321 - if conditions != nil { 322 - whereClause = " where " + strings.Join(conditions, " and ") 323 - } 324 - query := fmt.Sprintf( 325 - ` 326 - select 327 - id 328 - from 329 - issues 330 - %s 331 - limit ? offset ?`, 332 - whereClause, 333 - ) 334 - args = append(args, opts.Page.Limit, opts.Page.Offset) 335 - rows, err := e.Query(query, args...) 336 - if err != nil { 337 - return nil, err 338 - } 339 - defer rows.Close() 340 - 341 - for rows.Next() { 342 - var id int64 343 - err := rows.Scan(&id) 344 - if err != nil { 345 - return nil, err 346 - } 347 - 348 - ids = append(ids, id) 349 - } 350 - 351 - return ids, nil 352 - } 353 - 354 298 func AddIssueComment(tx *sql.Tx, c models.IssueComment) (int64, error) { 355 299 result, err := tx.Exec( 356 300 `insert into issue_comments (
+12 -68
appview/db/pulls.go
··· 13 13 14 14 "github.com/bluesky-social/indigo/atproto/syntax" 15 15 "tangled.org/core/appview/models" 16 + "tangled.org/core/appview/pagination" 16 17 "tangled.org/core/orm" 17 18 ) 18 19 ··· 119 120 return pullId - 1, err 120 121 } 121 122 122 - func GetPullsWithLimit(e Execer, limit int, filters ...orm.Filter) ([]*models.Pull, error) { 123 + func GetPullsPaginated(e Execer, page pagination.Page, filters ...orm.Filter) ([]*models.Pull, error) { 123 124 pulls := make(map[syntax.ATURI]*models.Pull) 124 125 125 126 var conditions []string ··· 133 134 if conditions != nil { 134 135 whereClause = " where " + strings.Join(conditions, " and ") 135 136 } 136 - limitClause := "" 137 - if limit != 0 { 138 - limitClause = fmt.Sprintf(" limit %d ", limit) 137 + pageClause := "" 138 + if page.Limit != 0 { 139 + pageClause = fmt.Sprintf( 140 + " limit %d offset %d ", 141 + page.Limit, 142 + page.Offset, 143 + ) 139 144 } 140 145 141 146 query := fmt.Sprintf(` ··· 161 166 order by 162 167 created desc 163 168 %s 164 - `, whereClause, limitClause) 169 + `, whereClause, pageClause) 165 170 166 171 rows, err := e.Query(query, args...) 167 172 if err != nil { ··· 297 302 } 298 303 299 304 func GetPulls(e Execer, filters ...orm.Filter) ([]*models.Pull, error) { 300 - return GetPullsWithLimit(e, 0, filters...) 301 - } 302 - 303 - func GetPullIDs(e Execer, opts models.PullSearchOptions) ([]int64, error) { 304 - var ids []int64 305 - 306 - var filters []orm.Filter 307 - filters = append(filters, orm.FilterEq("state", opts.State)) 308 - if opts.RepoAt != "" { 309 - filters = append(filters, orm.FilterEq("repo_at", opts.RepoAt)) 310 - } 311 - 312 - var conditions []string 313 - var args []any 314 - 315 - for _, filter := range filters { 316 - conditions = append(conditions, filter.Condition()) 317 - args = append(args, filter.Arg()...) 318 - } 319 - 320 - whereClause := "" 321 - if conditions != nil { 322 - whereClause = " where " + strings.Join(conditions, " and ") 323 - } 324 - pageClause := "" 325 - if opts.Page.Limit != 0 { 326 - pageClause = fmt.Sprintf( 327 - " limit %d offset %d ", 328 - opts.Page.Limit, 329 - opts.Page.Offset, 330 - ) 331 - } 332 - 333 - query := fmt.Sprintf( 334 - ` 335 - select 336 - id 337 - from 338 - pulls 339 - %s 340 - %s`, 341 - whereClause, 342 - pageClause, 343 - ) 344 - args = append(args, opts.Page.Limit, opts.Page.Offset) 345 - rows, err := e.Query(query, args...) 346 - if err != nil { 347 - return nil, err 348 - } 349 - defer rows.Close() 350 - 351 - for rows.Next() { 352 - var id int64 353 - err := rows.Scan(&id) 354 - if err != nil { 355 - return nil, err 356 - } 357 - 358 - ids = append(ids, id) 359 - } 360 - 361 - return ids, nil 305 + return GetPullsPaginated(e, pagination.Page{}, filters...) 362 306 } 363 307 364 308 func GetPull(e Execer, repoAt syntax.ATURI, pullId int) (*models.Pull, error) { 365 - pulls, err := GetPullsWithLimit(e, 1, orm.FilterEq("repo_at", repoAt), orm.FilterEq("pull_id", pullId)) 309 + pulls, err := GetPullsPaginated(e, pagination.Page{Limit: 1}, orm.FilterEq("repo_at", repoAt), orm.FilterEq("pull_id", pullId)) 366 310 if err != nil { 367 311 return nil, err 368 312 }
+26 -17
appview/pulls/pulls.go
··· 29 29 "tangled.org/core/appview/pages" 30 30 "tangled.org/core/appview/pages/markup" 31 31 "tangled.org/core/appview/pages/repoinfo" 32 + "tangled.org/core/appview/pagination" 32 33 "tangled.org/core/appview/reporesolver" 33 34 "tangled.org/core/appview/validator" 34 35 "tangled.org/core/appview/xrpcclient" ··· 574 575 575 576 keyword := params.Get("q") 576 577 577 - var ids []int64 578 + page := pagination.Page{ 579 + Limit: 99999, 580 + } 581 + 582 + var pulls []*models.Pull 578 583 searchOpts := models.PullSearchOptions{ 579 584 Keyword: keyword, 580 585 RepoAt: f.RepoAt().String(), 581 586 State: state, 582 - // Page: page, 587 + Page: page, 583 588 } 584 589 l.Debug("searching with", "searchOpts", searchOpts) 585 590 if keyword != "" { ··· 588 593 l.Error("failed to search for pulls", "err", err) 589 594 return 590 595 } 591 - ids = res.Hits 592 - l.Debug("searched pulls with indexer", "count", len(ids)) 596 + l.Debug("searched pulls with indexer", "count", len(res.Hits)) 597 + 598 + pulls, err = db.GetPulls( 599 + s.db, 600 + orm.FilterIn("id", res.Hits), 601 + ) 602 + if err != nil { 603 + log.Println("failed to get pulls", err) 604 + s.pages.Notice(w, "pulls", "Failed to load pulls. Try again later.") 605 + return 606 + } 593 607 } else { 594 - ids, err = db.GetPullIDs(s.db, searchOpts) 608 + pulls, err = db.GetPullsPaginated( 609 + s.db, 610 + page, 611 + orm.FilterEq("repo_at", f.RepoAt()), 612 + orm.FilterEq("state", searchOpts.State), 613 + ) 595 614 if err != nil { 596 - l.Error("failed to get all pull ids", "err", err) 615 + log.Println("failed to get pulls", err) 616 + s.pages.Notice(w, "pulls", "Failed to load pulls. Try again later.") 597 617 return 598 618 } 599 - l.Debug("indexed all pulls from the db", "count", len(ids)) 600 - } 601 - 602 - pulls, err := db.GetPulls( 603 - s.db, 604 - orm.FilterIn("id", ids), 605 - ) 606 - if err != nil { 607 - log.Println("failed to get pulls", err) 608 - s.pages.Notice(w, "pulls", "Failed to load pulls. Try again later.") 609 - return 610 619 } 611 620 612 621 for _, p := range pulls {
+3 -3
appview/repo/feed.go
··· 19 19 ) 20 20 21 21 func (rp *Repo) getRepoFeed(ctx context.Context, repo *models.Repo, ownerSlashRepo string) (*feeds.Feed, error) { 22 - const feedLimitPerType = 100 22 + feedPagePerType := pagination.Page{Limit: 100} 23 23 24 - pulls, err := db.GetPullsWithLimit(rp.db, feedLimitPerType, orm.FilterEq("repo_at", repo.RepoAt())) 24 + pulls, err := db.GetPullsPaginated(rp.db, feedPagePerType, orm.FilterEq("repo_at", repo.RepoAt())) 25 25 if err != nil { 26 26 return nil, err 27 27 } 28 28 29 29 issues, err := db.GetIssuesPaginated( 30 30 rp.db, 31 - pagination.Page{Limit: feedLimitPerType}, 31 + feedPagePerType, 32 32 orm.FilterEq("repo_at", repo.RepoAt()), 33 33 ) 34 34 if err != nil {