diff --git a/internal/feeds/refresh.go b/internal/feeds/refresh.go index 727cd32..4865c97 100644 --- a/internal/feeds/refresh.go +++ b/internal/feeds/refresh.go @@ -28,8 +28,9 @@ func Refresh() error { } } -func RefreshFeed(url string) error { +func RefreshFeed(url string) Feed { fmt.Printf("Refreshing %s\n", url) + var feed Feed f, err := fp.ParseURL(url) if err != nil { fmt.Printf("Failed to refresh %s\n", url) @@ -39,7 +40,7 @@ func RefreshFeed(url string) error { imageURL = f.Image.URL } - feed := Feed{ + feed = Feed{ ID: strings.ReplaceAll(base64.StdEncoding.EncodeToString([]byte(url)), "/", ""), Title: f.Title, Description: f.Description, @@ -73,5 +74,5 @@ func RefreshFeed(url string) error { fmt.Printf("Finished refreshing '%s'\n", feed.Title) } - return nil + return feed } diff --git a/internal/feeds/store.go b/internal/feeds/store.go index 5f1090c..36b80e3 100644 --- a/internal/feeds/store.go +++ b/internal/feeds/store.go @@ -32,7 +32,7 @@ func (fs *FeedStore) GetItem(id string) *Item { func (fs *FeedStore) GetFeeds() *[]Feed { feeds := &[]Feed{} - fs.getDB().Preload("Items").Order("title asc").Find(feeds) + fs.getDB().Order("title asc").Find(feeds) return feeds } diff --git a/internal/server/api.go b/internal/server/api.go index f25fbde..9c85ce0 100644 --- a/internal/server/api.go +++ b/internal/server/api.go @@ -17,6 +17,14 @@ func (a *API) GetFeed(c *fiber.Ctx) error { return c.JSON(a.FeedStore.GetFeed(c.Params("id"))) } +func (a *API) PostFeed(c *fiber.Ctx) error { + url := "" + if err := c.BodyParser(&url); err != nil { + return err + } + return c.JSON(feeds.RefreshFeed(url)) +} + func (a *API) GetItem(c *fiber.Ctx) error { return c.JSON(a.FeedStore.GetItem(c.Params("id"))) } @@ -29,3 +37,22 @@ func (a *API) PostRead(c *fiber.Ctx) error { a.FeedStore.MarkAsRead(c.Params("id")) return nil } + +func (a *API) PostReadAll(c *fiber.Ctx) error { + ids := &[]string{} + if err := c.BodyParser(ids); err != nil { + return err + } + for _, id := range *ids { + a.FeedStore.MarkAsRead(id) + } + return c.JSON(a.FeedStore.GetUnread()) +} + +func (a *API) RefreshAll(c *fiber.Ctx) error { + for _, feed := range *a.FeedStore.GetFeeds() { + feeds.RefreshFeed(feed.FeedURL) + } + + return c.JSON(a.FeedStore.GetUnread()) +} diff --git a/internal/server/main.go b/internal/server/main.go index 50ac9a7..02ff01e 100644 --- a/internal/server/main.go +++ b/internal/server/main.go @@ -43,38 +43,32 @@ func Start(port string) error { Views: engine, }) - app.Static("/", "./views/static") - app.Get("/", GetFeeds) + app.Static("/", "./views") app.Post("/opml", PostOPML) // API app.Get("/api/feeds", api.GetFeeds) - // app.Post("/api/feeds", api.PostFeed) + app.Post("/api/feeds", api.PostFeed) app.Get("/api/feed/:id", api.GetFeed) - // app.Get("/api/feed/:id/unread", api.GetFeedUnread) app.Get("/api/item/:id", api.GetItem) app.Get("/api/unread", api.GetUnread) app.Post("/api/read/:id", api.PostRead) + app.Post("/api/read", api.PostReadAll) + app.Get("/api/refresh", api.RefreshAll) return app.Listen(fmt.Sprintf(":%s", port)) } -func GetFeeds(c *fiber.Ctx) error { - return c.Render("index", fiber.Map{ - "Feeds": api.FeedStore.GetFeeds(), - "Unread": api.FeedStore.GetUnread(), - }, "layouts/main") -} - func PostOPML(c *fiber.Ctx) error { opml := &feeds.Opml{} if err := c.BodyParser(opml); err != nil { return err } + f := []feeds.Feed{} for _, outline := range opml.Outlines { - feeds.RefreshFeed(outline.XmlUrl) + f = append(f, feeds.RefreshFeed(outline.XmlUrl)) } - return nil + return c.JSON(f) } diff --git a/views/index.html b/views/index.html index d3c89d4..23a8a1b 100644 --- a/views/index.html +++ b/views/index.html @@ -1,108 +1,301 @@ + + +
+ + +