this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

appview: refactor with service layer (issues) (wip)

appview/issues/issues.go
-> appview/state/router.go
-> appview/service/issue.go

Signed-off-by: Seongmin Lee <boltlessengineer@proton.me>

boltless.me ab4f3062 f60213c7

verified
+131 -44
+9 -44
appview/issues/issues.go
··· 26 26 "tangled.sh/tangled.sh/core/appview/reporesolver" 27 27 "tangled.sh/tangled.sh/core/idresolver" 28 28 "tangled.sh/tangled.sh/core/tid" 29 + "tangled.sh/tangled.sh/core/appview/service" 29 30 ) 30 31 31 32 type Issues struct { ··· 35 36 idResolver *idresolver.Resolver 36 37 db *db.DB 37 38 config *config.Config 38 - notifier notify.Notifier 39 + 40 + issue *service.IssueService 39 41 } 40 42 41 43 func New( ··· 54 56 idResolver: idResolver, 55 57 db: db, 56 58 config: config, 57 - notifier: notifier, 59 + issue: service.NewIssueService( 60 + db, 61 + oauth, 62 + notifier, 63 + ), 58 64 } 59 65 } 60 66 ··· 654 660 return 655 661 } 656 662 657 - tx, err := rp.db.BeginTx(r.Context(), nil) 658 - if err != nil { 659 - rp.pages.Notice(w, "issues", "Failed to create issue, try again later") 660 - return 661 - } 663 + issue, err := rp.issue.Create(r.Context(), f.RepoAt(), title, body) 662 664 663 - issue := &db.Issue{ 664 - RepoAt: f.RepoAt(), 665 - Rkey: tid.TID(), 666 - Title: title, 667 - Body: body, 668 - OwnerDid: user.Did, 669 - } 670 - err = db.NewIssue(tx, issue) 671 665 if err != nil { 672 666 log.Println("failed to create issue", err) 673 667 rp.pages.Notice(w, "issues", "Failed to create issue.") 674 668 return 675 669 } 676 - 677 - client, err := rp.oauth.AuthorizedClient(r) 678 - if err != nil { 679 - log.Println("failed to get authorized client", err) 680 - rp.pages.Notice(w, "issues", "Failed to create issue.") 681 - return 682 - } 683 - atUri := f.RepoAt().String() 684 - _, err = client.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 685 - Collection: tangled.RepoIssueNSID, 686 - Repo: user.Did, 687 - Rkey: issue.Rkey, 688 - Record: &lexutil.LexiconTypeDecoder{ 689 - Val: &tangled.RepoIssue{ 690 - Repo: atUri, 691 - Title: title, 692 - Body: &body, 693 - Owner: user.Did, 694 - IssueId: int64(issue.IssueId), 695 - }, 696 - }, 697 - }) 698 - if err != nil { 699 - log.Println("failed to create issue", err) 700 - rp.pages.Notice(w, "issues", "Failed to create issue.") 701 - return 702 - } 703 - 704 - rp.notifier.NewIssue(r.Context(), issue) 705 670 706 671 rp.pages.HxLocation(w, fmt.Sprintf("/%s/issues/%d", f.OwnerSlashRepo(), issue.IssueId)) 707 672 return
+94
appview/service/issue.go
··· 1 + package service 2 + 3 + import ( 4 + "context" 5 + "net/http" 6 + 7 + comatproto "github.com/bluesky-social/indigo/api/atproto" 8 + "github.com/bluesky-social/indigo/atproto/syntax" 9 + lexutil "github.com/bluesky-social/indigo/lex/util" 10 + 11 + "tangled.sh/tangled.sh/core/api/tangled" 12 + "tangled.sh/tangled.sh/core/appview/db" 13 + "tangled.sh/tangled.sh/core/appview/notify" 14 + "tangled.sh/tangled.sh/core/appview/oauth" 15 + "tangled.sh/tangled.sh/core/tid" 16 + ) 17 + 18 + type IssueService struct { 19 + db *db.DB 20 + 21 + // don't need oauth client if we pass current session through context 22 + oauth *oauth.OAuth 23 + notifier notify.Notifier 24 + } 25 + 26 + func NewIssueService(db *db.DB, oauth *oauth.OAuth, notifier notify.Notifier) *IssueService { 27 + return &IssueService { db, oauth, notifier } 28 + } 29 + 30 + func (s *IssueService) Create(ctx context.Context, repoAt syntax.ATURI, title string, body string) (*db.Issue, error) { 31 + // TODO: replace this to something else 32 + // ctx -> session -> user|xrpcclient 33 + r := &http.Request{} 34 + 35 + user := s.oauth.GetUser(r) 36 + client, err := s.oauth.AuthorizedClient(r) 37 + if err != nil { 38 + return nil, err 39 + } 40 + 41 + issue := &db.Issue{ 42 + OwnerDid: user.Did, 43 + Rkey: tid.TID(), 44 + RepoAt: repoAt, 45 + Title: title, 46 + Body: body, 47 + Open: true, 48 + } 49 + tx, err := s.db.BeginTx(ctx, nil) 50 + if err != nil { 51 + return nil, err 52 + } 53 + 54 + // add to db first to populate IssueId 55 + err = db.NewIssue(tx, issue) 56 + if err != nil { 57 + return nil, err 58 + } 59 + 60 + _, err = client.RepoPutRecord(ctx, &comatproto.RepoPutRecord_Input{ 61 + Collection: tangled.RepoIssueNSID, 62 + Repo: issue.OwnerDid, 63 + Rkey: issue.Rkey, 64 + Record: &lexutil.LexiconTypeDecoder{ 65 + Val: &tangled.RepoIssue{ 66 + Repo: issue.RepoAt.String(), 67 + Title: issue.Title, 68 + Body: &issue.Body, 69 + Owner: issue.OwnerDid, 70 + IssueId: int64(issue.IssueId), 71 + }, 72 + }, 73 + }) 74 + 75 + s.notifier.NewIssue(ctx, issue) 76 + 77 + return issue, nil 78 + } 79 + 80 + func (s *IssueService) UpdateTitle(issue db.Issue) (db.Issue, error) { 81 + panic("not implemented yet") 82 + } 83 + 84 + func (s *IssueService) UpdateBody(issue db.Issue) (db.Issue, error) { 85 + panic("not implemented yet") 86 + } 87 + 88 + func (s *IssueService) Close(issue db.Issue) (db.Issue, error) { 89 + panic("not implemented yet") 90 + } 91 + 92 + func (s *IssueService) Reopen(issue db.Issue) (db.Issue, error) { 93 + panic("not implemented yet") 94 + }
+28
appview/service/pull.go
··· 1 + package service 2 + 3 + import ( 4 + "context" 5 + 6 + "tangled.sh/tangled.sh/core/appview/db" 7 + "tangled.sh/tangled.sh/core/appview/oauth" 8 + ) 9 + 10 + type PullService struct { 11 + db *db.DB 12 + oauth *oauth.OAuth 13 + } 14 + 15 + func (s *PullService) Merge(ctx context.Context, pull *db.Pull) error { 16 + // side effects 17 + // -> Close related issues 18 + // -> Update dependent sub-stacked PRs 19 + // -> run search indexer 20 + // -> run posthog 21 + panic("not implemented yet") 22 + } 23 + 24 + func (s *PullService) Close(ctx context.Context, pull *db.Pull) error { 25 + // side effects 26 + // -> Close descendent PRs 27 + panic("not implemented yet") 28 + }