diff --git a/internal/commands/commands.go b/internal/commands/commands.go index 4d95716..54af59e 100644 --- a/internal/commands/commands.go +++ b/internal/commands/commands.go @@ -87,7 +87,43 @@ func GetWslHostName() string { return strings.TrimSpace(string(out)) } +func (c Commands) CleanFeeds() error { + urls, err := c.store.GetAllFeedURLs() + if err != nil { + return fmt.Errorf("[commands.go]: %w", err) + } + + var urlsToRemove []string + + for _, u := range urls { + inFeeds := false + for _, f := range c.config.Feeds { + if f.URL == u { + inFeeds = true + } + } + + if !inFeeds { + urlsToRemove = append(urlsToRemove, u) + } + } + + for _, url := range urlsToRemove { + err := c.store.DeleteByFeedURL(url) + if err != nil { + return fmt.Errorf("[commands.go]: %w", err) + } + } + + return nil +} + func (c Commands) TUI() error { + err := c.CleanFeeds() + if err != nil { + return fmt.Errorf("commands List: %w", err) + } + its, err := c.GetAllFeeds() if err != nil { return fmt.Errorf("commands List: %w", err) @@ -117,6 +153,10 @@ func (c Commands) TUI() error { } func (c Commands) List(numResults int) error { + err := c.CleanFeeds() + if err != nil { + return fmt.Errorf("commands List: %w", err) + } its, err := c.GetAllFeeds() if err != nil { return fmt.Errorf("commands List: %w", err) diff --git a/internal/store/store.go b/internal/store/store.go index 2aa3b5a..8dff963 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -32,7 +32,9 @@ func (i Item) Read() bool { type Store interface { UpsertItem(item Item) error GetAllItems() ([]Item, error) + GetAllFeedURLs() ([]string, error) ToggleRead(ID int) error + DeleteByFeedURL(feedurl string) error } type SQLiteStore struct { @@ -217,3 +219,38 @@ func (sls SQLiteStore) ToggleRead(ID int) error { return nil } + +func (sls SQLiteStore) GetAllFeedURLs() ([]string, error) { + var urls []string + + stmt, _ := sls.db.Prepare(`select feedurl from items group by feedurl;`) + + rows, err := stmt.Query() + if err != nil { + return urls, fmt.Errorf("[store.go] GetAllFeedURLs: %w", err) + } + + for rows.Next() { + var feedurl string + + err := rows.Scan(&feedurl) + if err != nil { + return urls, fmt.Errorf("[store.go] GetAllFeedURLs: %w", err) + } + + urls = append(urls, feedurl) + } + + return urls, nil +} + +func (sls SQLiteStore) DeleteByFeedURL(feedurl string) error { + stmt, _ := sls.db.Prepare(`delete from items where feedurl = ?`) + + _, err := stmt.Exec(feedurl) + if err != nil { + return fmt.Errorf("[store.go] DeleteByFeedURL: %w", err) + } + + return nil +}