diff --git a/internal/backend/sqlite/sqlite.go b/internal/backend/sqlite/sqlite.go index f15a61a..d430f4c 100644 --- a/internal/backend/sqlite/sqlite.go +++ b/internal/backend/sqlite/sqlite.go @@ -175,24 +175,24 @@ func (sb *SQLiteBackend) GetArticleNumbers(g *models.Group, low, high int64) ([] func (sb *SQLiteBackend) GetLastArticleByNum(g *models.Group, a *models.Article) (models.Article, error) { var lastArticle models.Article - if err := sb.db.Get(&lastArticle, "SELECT articles.* FROM articles INNER JOIN articles_to_groups atg on atg.article_id = articles.id WHERE atg.article_number < ? AND atg.group_id = ? LIMIT 1", a.ArticleNumber, g.ID); err != nil { + if err := sb.db.Get(&lastArticle, "SELECT articles.* FROM articles INNER JOIN articles_to_groups atg on atg.article_id = articles.id WHERE atg.article_number < ? AND atg.group_id = ? ORDER BY atg.article_number DESC LIMIT 1", a.ArticleNumber, g.ID); err != nil { return lastArticle, err } - if err := sb.db.Get(&a.ArticleNumber, "SELECT article_number FROM articles_to_groups WHERE article_id = ?", a.ID); err != nil { + if err := sb.db.Get(&lastArticle.ArticleNumber, "SELECT article_number FROM articles_to_groups WHERE article_id = ?", lastArticle.ID); err != nil { return lastArticle, err } - return lastArticle, json.Unmarshal([]byte(a.HeaderRaw), &a.Header) + return lastArticle, json.Unmarshal([]byte(lastArticle.HeaderRaw), &lastArticle.Header) } func (sb *SQLiteBackend) GetNextArticleByNum(g *models.Group, a *models.Article) (models.Article, error) { - var lastArticle models.Article - if err := sb.db.Get(&lastArticle, "SELECT articles.* FROM articles INNER JOIN articles_to_groups atg on atg.article_id = articles.id WHERE atg.article_number > ? AND atg.group_id = ? LIMIT 1", a.ArticleNumber, g.ID); err != nil { - return lastArticle, err + var nextArticle models.Article + if err := sb.db.Get(&nextArticle, "SELECT articles.* FROM articles INNER JOIN articles_to_groups atg on atg.article_id = articles.id WHERE atg.article_number > ? AND atg.group_id = ? ORDER BY atg.article_number LIMIT 1", a.ArticleNumber, g.ID); err != nil { + return nextArticle, err } - if err := sb.db.Get(&a.ArticleNumber, "SELECT article_number FROM articles_to_groups WHERE article_id = ?", a.ID); err != nil { - return lastArticle, err + if err := sb.db.Get(&nextArticle.ArticleNumber, "SELECT article_number FROM articles_to_groups WHERE article_id = ?", nextArticle.ID); err != nil { + return nextArticle, err } - return lastArticle, json.Unmarshal([]byte(a.HeaderRaw), &a.Header) + return nextArticle, json.Unmarshal([]byte(nextArticle.HeaderRaw), &nextArticle.Header) } func (sb *SQLiteBackend) GetNewArticlesSince(timestamp int64) ([]string, error) { diff --git a/internal/server/handler.go b/internal/server/handler.go index 7203c4c..95972ee 100644 --- a/internal/server/handler.go +++ b/internal/server/handler.go @@ -664,6 +664,8 @@ func (h *Handler) handleLast(s *Session, command string, arguments []string, id return err } + s.currentArticle = &a + return s.tconn.PrintfLine(protocol.NNTPResponse{Code: 223, Message: fmt.Sprintf("%d %s retrieved", a.ArticleNumber, a.Header.Get("Message-ID"))}.String()) } @@ -700,7 +702,9 @@ func (h *Handler) handleNext(s *Session, command string, arguments []string, id return err } - return s.tconn.PrintfLine(protocol.NNTPResponse{Code: 223, Message: fmt.Sprintf("%d %s retrieved", a.ArticleNumber, a.Header.Get("Message-ID"))}.String()) + s.currentArticle = &a + + return s.tconn.PrintfLine(protocol.NNTPResponse{Code: 223, Message: fmt.Sprintf("%d %s retrieved", a.ArticleNumber, a.Header.Get("Message-Id"))}.String()) } func (h *Handler) Handle(s *Session, message string, id uint) error {