this repo has no description

feat: cleanup boundaries between feedweb and feedgens

Changed files
+37 -23
cmd
feedweb
pkg
feeds
mostliked
+2 -2
Makefile
··· 1 1 all: bin/mostliked bin/feedweb 2 2 3 - bin/mostliked: cmd/mostliked/main.go pkg/mostliked/handler.go db/mostliked/*.go 3 + bin/mostliked: cmd/mostliked/main.go pkg/mostliked/*.go db/mostliked/*.go pkg/feeds/*.go 4 4 go build -o $@ ./cmd/mostliked 5 5 6 - bin/feedweb: cmd/feedweb/main.go pkg/*/view.go db/*/*.go 6 + bin/feedweb: cmd/feedweb/main.go pkg/*/*.go db/*/*.go pkg/feeds/*.go 7 7 go build -o $@ ./cmd/feedweb 8 8 9 9 .PHONY: clean
+14 -9
cmd/feedweb/main.go
··· 4 4 "net/http" 5 5 6 6 appbsky "github.com/bluesky-social/indigo/api/bsky" 7 + "github.com/edavis/bsky-feeds/pkg/feeds" 7 8 "github.com/edavis/bsky-feeds/pkg/mostliked" 8 9 "github.com/labstack/echo/v4" 9 10 "github.com/labstack/echo/v4/middleware" ··· 19 20 Langs []string `header:"Accept-Language"` 20 21 } 21 22 23 + type FeedLookup map[string]func(feeds.FeedgenParams) appbsky.FeedGetFeedSkeleton_Output 24 + 22 25 func getFeedSkeleton(c echo.Context) error { 23 26 var req SkeletonRequest 24 27 if err := c.Bind(&req); err != nil { ··· 29 32 return c.String(http.StatusBadRequest, "bad request") 30 33 } 31 34 32 - var posts []*appbsky.FeedDefs_SkeletonFeedPost 33 - uris := mostliked.Feed(mostliked.FeedViewParams{ 35 + generators := FeedLookup{ 36 + "at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/most-liked": mostliked.Feed, 37 + } 38 + params := feeds.FeedgenParams{ 39 + Feed: req.Feed, 34 40 Limit: req.Limit, 35 41 Offset: req.Offset, 36 42 Langs: hdr.Langs, 37 - }) 38 - for _, uri := range uris { 39 - posts = append(posts, &appbsky.FeedDefs_SkeletonFeedPost{Post: uri}) 43 + } 44 + feedFunc, ok := generators[req.Feed] 45 + if !ok { 46 + return c.String(http.StatusNotFound, "feed not found") 40 47 } 41 - 42 - return c.JSON(http.StatusOK, appbsky.FeedGetFeedSkeleton_Output{ 43 - Feed: posts, 44 - }) 48 + feed := feedFunc(params) 49 + return c.JSON(http.StatusOK, feed) 45 50 } 46 51 47 52 func main() {
+8
pkg/feeds/feeds.go
··· 1 + package feeds 2 + 3 + type FeedgenParams struct { 4 + Feed string 5 + Limit int64 6 + Offset string 7 + Langs []string 8 + }
+13 -12
pkg/mostliked/view.go
··· 6 6 "log" 7 7 "strconv" 8 8 9 + appbsky "github.com/bluesky-social/indigo/api/bsky" 9 10 db "github.com/edavis/bsky-feeds/db/mostliked" 11 + "github.com/edavis/bsky-feeds/pkg/feeds" 10 12 _ "github.com/mattn/go-sqlite3" 11 13 ) 12 14 13 - type FeedViewParams struct { 14 - Limit int64 15 - Offset string 16 - Langs []string 17 - } 18 - 19 - func Feed(args FeedViewParams) []string { 15 + func Feed(params feeds.FeedgenParams) appbsky.FeedGetFeedSkeleton_Output { 20 16 ctx := context.Background() 21 17 dbCnx, err := sql.Open("sqlite3", "data/mostliked.db?_journal=WAL&_fk=on&mode=ro") 22 18 if err != nil { ··· 24 20 } 25 21 defer dbCnx.Close() 26 22 27 - offset, err := strconv.Atoi(args.Offset) 23 + offset, err := strconv.Atoi(params.Offset) 28 24 if err != nil { 29 25 log.Println("error converting offset to integer") 30 26 } 31 27 32 28 queries := db.New(dbCnx) 33 29 rows, err := queries.ViewFeed(ctx, db.ViewFeedParams{ 34 - Limit: args.Limit, 30 + Limit: params.Limit, 35 31 Offset: int64(offset), 36 32 }) 37 33 if err != nil { 38 34 log.Println("error fetching rows") 39 35 } 40 - var uris []string 36 + var cursor string 37 + var posts []*appbsky.FeedDefs_SkeletonFeedPost 41 38 for _, row := range rows { 42 - uris = append(uris, row.Uri) 39 + posts = append(posts, &appbsky.FeedDefs_SkeletonFeedPost{Post: row.Uri}) 40 + cursor = row.Uri 43 41 } 44 - return uris 42 + return appbsky.FeedGetFeedSkeleton_Output{ 43 + Cursor: &cursor, 44 + Feed: posts, 45 + } 45 46 }