This repository has no description
0

Configure Feed

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

core / appview / pulls / pulls.go
3.0 kB 121 lines
1package pulls 2 3import ( 4 "bytes" 5 "compress/gzip" 6 "fmt" 7 "io" 8 "log/slog" 9 "strings" 10 "time" 11 12 "tangled.org/core/appview/config" 13 "tangled.org/core/appview/db" 14 pulls_indexer "tangled.org/core/appview/indexer/pulls" 15 "tangled.org/core/appview/knotacl" 16 "tangled.org/core/appview/mentions" 17 "tangled.org/core/appview/models" 18 "tangled.org/core/appview/notify" 19 "tangled.org/core/appview/oauth" 20 "tangled.org/core/appview/pages" 21 "tangled.org/core/appview/reporesolver" 22 knotmirror "tangled.org/core/gitmirror/proto/gen" 23 "tangled.org/core/idresolver" 24 "tangled.org/core/ogre" 25 "tangled.org/core/patchutil" 26 "tangled.org/core/types" 27 28 indigoxrpc "github.com/bluesky-social/indigo/xrpc" 29 "github.com/hashicorp/golang-lru/v2/expirable" 30) 31 32const ApplicationGzip = "application/gzip" 33 34const ( 35 diffCacheSize = 128 36 diffCacheTTL = 15 * time.Minute 37) 38 39type Pulls struct { 40 oauth *oauth.OAuth 41 repoResolver *reporesolver.RepoResolver 42 pages *pages.Pages 43 idResolver *idresolver.Resolver 44 mentionsResolver *mentions.Resolver 45 db *db.DB 46 config *config.Config 47 notifier notify.Notifier 48 acl *knotacl.Service 49 logger *slog.Logger 50 indexer *pulls_indexer.Indexer 51 ogreClient *ogre.Client 52 diffCache *expirable.LRU[string, types.DiffRenderer] 53 gitmirror knotmirror.GitMirrorServiceClient 54} 55 56func New( 57 oauth *oauth.OAuth, 58 repoResolver *reporesolver.RepoResolver, 59 pages *pages.Pages, 60 resolver *idresolver.Resolver, 61 mentionsResolver *mentions.Resolver, 62 db *db.DB, 63 config *config.Config, 64 notifier notify.Notifier, 65 acl *knotacl.Service, 66 indexer *pulls_indexer.Indexer, 67 logger *slog.Logger, 68 gitmirror knotmirror.GitMirrorServiceClient, 69) *Pulls { 70 return &Pulls{ 71 oauth: oauth, 72 repoResolver: repoResolver, 73 pages: pages, 74 idResolver: resolver, 75 mentionsResolver: mentionsResolver, 76 db: db, 77 config: config, 78 notifier: notifier, 79 acl: acl, 80 logger: logger, 81 indexer: indexer, 82 ogreClient: ogre.NewClient(config.Ogre.Host), 83 diffCache: expirable.NewLRU[string, types.DiffRenderer](diffCacheSize, nil, diffCacheTTL), 84 gitmirror: gitmirror, 85 } 86} 87 88func (s *Pulls) knotClient(host string) *indigoxrpc.Client { 89 scheme := "https" 90 if s.config.Core.Dev { 91 scheme = "http" 92 } 93 return &indigoxrpc.Client{Host: fmt.Sprintf("%s://%s", scheme, host)} 94} 95 96func gz(s string) io.Reader { 97 var b bytes.Buffer 98 w := gzip.NewWriter(&b) 99 w.Write([]byte(s)) 100 w.Close() 101 return &b 102} 103 104func ptrPullState(s models.PullState) *models.PullState { return &s } 105 106func validatePatch(patch *string) error { 107 if patch == nil || *patch == "" { 108 return fmt.Errorf("patch is empty") 109 } 110 111 // add newline if not present to diff style patches 112 if !patchutil.IsFormatPatch(*patch) && !strings.HasSuffix(*patch, "\n") { 113 *patch = *patch + "\n" 114 } 115 116 if err := patchutil.IsPatchValid(*patch); err != nil { 117 return err 118 } 119 120 return nil 121}