Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2

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

prepare for PR page pagination

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

authored by boltless.me and committed by tangled.org 50b88f01 fbe41aeb

+42 -145
-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 ··· 120 119 return pullId - 1, err 121 120 } 122 121 123 - func GetPullsWithLimit(e Execer, limit int, filters ...orm.Filter) ([]*models.Pull, error) { 122 + func GetPullsPaginated(e Execer, page pagination.Page, filters ...orm.Filter) ([]*models.Pull, error) { 124 123 pulls := make(map[syntax.ATURI]*models.Pull) 125 124 126 125 var conditions []string ··· 134 133 if conditions != nil { 135 134 whereClause = " where " + strings.Join(conditions, " and ") 136 135 } 137 - limitClause := "" 138 - if limit != 0 { 139 - limitClause = fmt.Sprintf(" limit %d ", limit) 136 + pageClause := "" 137 + if page.Limit != 0 { 138 + pageClause = fmt.Sprintf( 139 + " limit %d offset %d ", 140 + page.Limit, 141 + page.Offset, 142 + ) 140 143 } 141 144 142 145 query := fmt.Sprintf(` ··· 166 161 order by 167 162 created desc 168 163 %s 169 - `, whereClause, limitClause) 164 + `, whereClause, pageClause) 170 165 171 166 rows, err := e.Query(query, args...) 172 167 if err != nil { ··· 302 297 } 303 298 304 299 func GetPulls(e Execer, filters ...orm.Filter) ([]*models.Pull, error) { 305 - return GetPullsWithLimit(e, 0, filters...) 306 - } 307 - 308 - func GetPullIDs(e Execer, opts models.PullSearchOptions) ([]int64, error) { 309 - var ids []int64 310 - 311 - var filters []orm.Filter 312 - filters = append(filters, orm.FilterEq("state", opts.State)) 313 - if opts.RepoAt != "" { 314 - filters = append(filters, orm.FilterEq("repo_at", opts.RepoAt)) 315 - } 316 - 317 - var conditions []string 318 - var args []any 319 - 320 - for _, filter := range filters { 321 - conditions = append(conditions, filter.Condition()) 322 - args = append(args, filter.Arg()...) 323 - } 324 - 325 - whereClause := "" 326 - if conditions != nil { 327 - whereClause = " where " + strings.Join(conditions, " and ") 328 - } 329 - pageClause := "" 330 - if opts.Page.Limit != 0 { 331 - pageClause = fmt.Sprintf( 332 - " limit %d offset %d ", 333 - opts.Page.Limit, 334 - opts.Page.Offset, 335 - ) 336 - } 337 - 338 - query := fmt.Sprintf( 339 - ` 340 - select 341 - id 342 - from 343 - pulls 344 - %s 345 - %s`, 346 - whereClause, 347 - pageClause, 348 - ) 349 - args = append(args, opts.Page.Limit, opts.Page.Offset) 350 - rows, err := e.Query(query, args...) 351 - if err != nil { 352 - return nil, err 353 - } 354 - defer rows.Close() 355 - 356 - for rows.Next() { 357 - var id int64 358 - err := rows.Scan(&id) 359 - if err != nil { 360 - return nil, err 361 - } 362 - 363 - ids = append(ids, id) 364 - } 365 - 366 - return ids, nil 300 + return GetPullsPaginated(e, pagination.Page{}, filters...) 367 301 } 368 302 369 303 func GetPull(e Execer, repoAt syntax.ATURI, pullId int) (*models.Pull, error) { 370 - pulls, err := GetPullsWithLimit(e, 1, orm.FilterEq("repo_at", repoAt), orm.FilterEq("pull_id", pullId)) 304 + pulls, err := GetPullsPaginated(e, pagination.Page{Limit: 1}, orm.FilterEq("repo_at", repoAt), orm.FilterEq("pull_id", pullId)) 371 305 if err != nil { 372 306 return nil, err 373 307 }
+27 -18
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" ··· 575 574 576 575 keyword := params.Get("q") 577 576 578 - var ids []int64 577 + page := pagination.Page{ 578 + Limit: 99999, 579 + } 580 + 581 + var pulls []*models.Pull 579 582 searchOpts := models.PullSearchOptions{ 580 583 Keyword: keyword, 581 584 RepoAt: f.RepoAt().String(), 582 585 State: state, 583 - // Page: page, 586 + Page: page, 584 587 } 585 588 l.Debug("searching with", "searchOpts", searchOpts) 586 589 if keyword != "" { ··· 593 588 l.Error("failed to search for pulls", "err", err) 594 589 return 595 590 } 596 - ids = res.Hits 597 - l.Debug("searched pulls with indexer", "count", len(ids)) 598 - } else { 599 - ids, err = db.GetPullIDs(s.db, searchOpts) 591 + l.Debug("searched pulls with indexer", "count", len(res.Hits)) 592 + 593 + pulls, err = db.GetPulls( 594 + s.db, 595 + orm.FilterIn("id", res.Hits), 596 + ) 600 597 if err != nil { 601 - l.Error("failed to get all pull ids", "err", err) 598 + log.Println("failed to get pulls", err) 599 + s.pages.Notice(w, "pulls", "Failed to load pulls. Try again later.") 602 600 return 603 601 } 604 - l.Debug("indexed all pulls from the db", "count", len(ids)) 605 - } 606 - 607 - pulls, err := db.GetPulls( 608 - s.db, 609 - orm.FilterIn("id", ids), 610 - ) 611 - if err != nil { 612 - log.Println("failed to get pulls", err) 613 - s.pages.Notice(w, "pulls", "Failed to load pulls. Try again later.") 614 - return 602 + } else { 603 + pulls, err = db.GetPullsPaginated( 604 + s.db, 605 + page, 606 + orm.FilterEq("repo_at", f.RepoAt()), 607 + orm.FilterEq("state", searchOpts.State), 608 + ) 609 + if err != nil { 610 + log.Println("failed to get pulls", err) 611 + s.pages.Notice(w, "pulls", "Failed to load pulls. Try again later.") 612 + return 613 + } 615 614 } 616 615 617 616 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 {